最近看了一下chrome UI 学习笔记(http://blog.chinaunix.net/u2/76055/showart.php?id=2093274),觉得chrome的ui架构不错,将很多基本控件进行了封装,这样写出来的ui就可以运行在windows,linux,mac机上,但其比qt和wxwidget轻量化,应该可以抽离出来,单独来作为一个简易的图形库。另外,chrome对多语言和皮肤等处理都很值得参考。在这里面学到了一个技巧是,设置完cygwin的环境后就可以在bat里面运行linux的相关命令了(http://www.oschina.net/code/explore/chromium.r67069/chrome/tools/build/win/version.bat):
:: Put cygwin in the path
call %SolutionDir%/../third_party/cygwin/setup_env.bat
:: Load version digits as environment variables
cat %SolutionDir%/VERSION | sed "s//(.*/)/set /1/" > %VarsBat%
看完了chrome UI 学习笔记,就想自己搭建环境来跑跑相关的例子,但将pdf的例子拷贝下来后,设置好编译环境,编过去了,却发现运行不起来。根据中断点的地方,可以看出是相关环境没有初始化,但该初始化那些环境呢?网上也有人问,作者的回答是:
1.多个线程的启动和绑定
2.PathService初始化
3.资源文件初始化
4.国际化判断和加载
5.如果在Windows 平台下,需要初始化的一些关于wtl和com的模块。
但该怎么样初始化作者没讲?看来只能自己去找了。参考d:/project/chrome/src/src/chrome/app/chrome_dll_main.cc这个文件,同时参考http://xjchilli.blog.163.com/blog/static/453477392010612111245425/,经过几次尝试后总算跑起来了,下面就说一下对应的代码:
1.多个线程的启动和绑定 可以不需要
2.PathService初始化
app::RegisterPathProvider();
3 4.资源文件初始化和国际化判断和加载
bool icu_result = icu_util::Initialize();
std::string app_locale = ResourceBundle::InitSharedInstance("en-US");
5.如果在Windows 平台下,需要初始化的一些关于wtl和com的模块。
#define _WTL_USE_CSTRING
#include // base ATL classes
#include // base WTL classes
extern CAppModule _Module; // WTL version of CComModule
#include // ATL GUI classes
#include // WTL frame window classes
#include // WTL utility classes like CString
#include // WTL enhanced msg map macros
OleInitialize(NULL);
运行的结果如图:
为了方便大家自己搭建,现给出完整的测试代码:
//test.h #ifndef _TEST_CHROME_VIEWS_ #define _TEST_CHROME_VIEWS_ #include "views/view.h" #include "views/window/window_delegate.h" #include "views/controls/button/button.h" class TestWindow : public views::View, public views::WindowDelegate{ public: virtual View* GetContentsView() { return this; } virtual gfx::Size GetPreferredSize(){ return gfx::Size(400,300); } static void CreateTestWindow(); }; namespace views{ class Label; class TextButton; } namespace gfx{ class Canvas; } class ChromeCanvas; class TestWindow_Widget : public views::View, public views::WindowDelegate, public views::ButtonListener{ public: TestWindow_Widget(); virtual View* GetContentsView() { return this; } virtual gfx::Size GetPreferredSize(){ return gfx::Size(400,300); } virtual void Layout(); virtual void Paint(gfx::Canvas* canvas); virtual void ButtonPressed(views::Button* sender,const views::Event &event); static void CreateTestWindow(); private: views::Label * lable_; views::TextButton * button_; }; #endif
#include <windows.h> #include <string> #include <commctrl.h> #include <commdlg.h> #include <objbase.h> #include <shlwapi.h> #include <wininet.h> #include "test.h" #include "resource.h" #include "base/at_exit.h" #include "base/message_loop.h" #include "app/app_paths.h" #include "app/app_switches.h" #include "app/resource_bundle.h" #include "base/at_exit.h" #include "base/command_line.h" #include "base/debug_util.h" #include "base/debug/debugger.h" #include "base/i18n/icu_util.h" #include "base/mac/scoped_nsautorelease_pool.h" #include "base/message_loop.h" #include "base/metrics/stats_counters.h" #include "base/metrics/stats_table.h" #include "base/path_service.h" #include "base/process_util.h" #include "base/string_number_conversions.h" #include "base/string_util.h" #include "base/utf_string_conversions.h" //#include "browser/chrome_thread.h" //#define STRICT //#define WIN32_LEAN_AND_MEAN #define _WTL_USE_CSTRING #include <atlbase.h> // base ATL classes #include <atlapp.h> // base WTL classes extern CAppModule _Module; // WTL version of CComModule #include <atlwin.h> // ATL GUI classes #include <atlframe.h> // WTL frame window classes #include <atlmisc.h> // WTL utility classes like CString #include <atlcrack.h> // WTL enhanced msg map macros #include "views/window/window.h" #include "views/controls/button/text_button.h" #include "views/controls/label.h" #include "gfx/canvas.h" TestWindow_Widget::TestWindow_Widget(){ this->lable_ = new views::Label(L""); this->button_ = new views::TextButton(this,L"点击"); AddChildView(this->lable_); AddChildView(this->button_); } void TestWindow_Widget::Layout(){ this->lable_->SetBounds(10,10,50,30); this->button_->SetBounds(10,50,60,30); } void TestWindow_Widget::Paint(gfx::Canvas* canvas){ canvas->FillRectInt(SK_ColorWHITE, 0, 0, width(), height()); } void TestWindow_Widget::ButtonPressed(views::Button* sender,const views::Event &event){ if(sender == this->button_){ this->lable_->SetText(L"点击了"); } } void TestWindow::CreateTestWindow(){ //views::Window::CreateChromeWindow(NULL,gfx::Rect(),new TestWindow)->Show(); views::Window::CreateChromeWindow(NULL,gfx::Rect(),new TestWindow_Widget)->Show(); } int CALLBACK WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow ) { UNREFERENCED_PARAMETER(hPrevInstance); UNREFERENCED_PARAMETER(lpCmdLine); OleInitialize(NULL); // The exit manager is in charge of calling the dtors of singletons. base::AtExitManager exit_manager; app::RegisterPathProvider(); //chrome::RegisterPathProvider(); bool icu_result = icu_util::Initialize(); std::string app_locale = ResourceBundle::InitSharedInstance("en-US"); //MessageLoopForUI message_loop; //ChromeThread ui_thread(ChromeThread::UI, &message_loop); MessageLoopForUI loop; // //loop.DoRunLoop(); //base::MessagePumpForUI pump; TestWindow::CreateTestWindow(); //pump.Run(NULL); //MessageLoop::current()->Run(); loop.Run(NULL); }