1.wx的核心原理和MFC差不多,其中的一部分是menu,event,thread,dc,control.
2.以下是使用和wx这些功能的例子,在MinGW(g++),msys(make)下编译.g++ 4.4,我用的eclipse CDT建的工程,单是编译的话直接用make就行了,不需要eclipse.
3.用的wxWidgets-2.9.2,核心的dll只需要3个,由于是编译了基本上全部功能和控件,所以这些库还是比较大的,不过有很大的压缩余地。也和gcc编译有关。
wxbase292u_gcc_custom.dll (6,461 kb) wxmsw292u_adv_gcc_custom.dll (5,498 kb) wxmsw292u_core_gcc_custom.dll (12,237 kb)
my_app.h
/* * my_app.h * * Created on: 2013-5-30 * Author: Sai */ #ifndef MY_APP_H_ #define MY_APP_H_ #include "wx/wx.h" class MyApp: public wxApp { public: MyApp(); virtual ~MyApp(); bool OnInit(); }; #endif /* MY_APP_H_ */
my_app.cpp
/* * my_app.cpp * * Created on: 2013-5-30 * Author: Sai */ #include "my_app.h" #include "my_frame.h" IMPLEMENT_APP(MyApp) MyApp::MyApp() { } MyApp::~MyApp() { } bool MyApp::OnInit() { MyFrame* frame = new MyFrame(); frame->Create(wxID_ANY,wxT("infoworld")); SetTopWindow(frame); frame->Show(true); return true; }
my_frame.h
/* * my_frame.h * * Created on: 2013-5-30 * Author: Sai */ #ifndef MY_FRAME_H_ #define MY_FRAME_H_ #include "wx/wx.h" /** * Show Example * * 1.menu * 2.event * 3.thread * 4.dc * 5.control */ class MyFrame : public wxFrame { DECLARE_EVENT_TABLE() DECLARE_DYNAMIC_CLASS(MyFrame) public: MyFrame(); virtual ~MyFrame(); virtual bool Create(wxWindowID id, const wxString& title); protected: void OnQuit(wxCommandEvent& event); void OnThread(wxCommandEvent& event); void OnThreadUpdate(wxThreadEvent& event); void OnPaintPanel(wxPaintEvent& event); private: wxGauge* gauge_; wxPanel *panel_; }; #endif /* MY_FRAME_H_ */
/* * my_frame.cpp * * Created on: 2013-5-30 * Author: Sai */ #include "my_frame.h" namespace { enum { kGauge = 1, kButton, kThreadUpdateId }; } class MyThread: public wxThread { public: MyThread(MyFrame *handler) : wxThread(wxTHREAD_DETACHED) { m_pHandler = handler; } ~MyThread() { } void Execute() { Create(); Run(); } protected: virtual ExitCode Entry() { for (int i = 1; i < 11; ++i) { //1.asynchronous update,communicate with main thread. wxThreadEvent* event = new wxThreadEvent(wxEVT_THREAD, kThreadUpdateId); event->SetInt(i * 10); wxQueueEvent(m_pHandler->GetEventHandler(), event); } return (wxThread::ExitCode) 0; } MyFrame *m_pHandler; }; BEGIN_EVENT_TABLE(MyFrame, wxFrame) EVT_MENU(wxID_EXIT, MyFrame::OnQuit) EVT_BUTTON(kButton, MyFrame::OnThread) EVT_THREAD(kThreadUpdateId, MyFrame::OnThreadUpdate) END_EVENT_TABLE() IMPLEMENT_DYNAMIC_CLASS(MyFrame, wxFrame) MyFrame::MyFrame() { } MyFrame::~MyFrame() { } bool MyFrame::Create(wxWindowID id, const wxString& title) { if (!wxFrame::Create(NULL, id, title, wxDefaultPosition, wxSize(400, 400))) { return false; } panel_ = new wxPanel(this, wxID_ANY, wxPoint(0, 0)); panel_->SetBackgroundColour(*wxBLUE); panel_->Connect(wxEVT_PAINT, wxPaintEventHandler(MyFrame::OnPaintPanel), NULL, this); //1.menubar and statusbar wxMenu *fileMenu = new wxMenu(); (*fileMenu).Append(wxID_EXIT, wxT("&Exit\tAlt-X"), wxT("Quit this program")); wxMenuBar *bar = new wxMenuBar(); bar->Append(fileMenu, wxT("&File")); SetMenuBar(bar); CreateStatusBar(); SetStatusText(wxT("welcome to WxWidget")); //2.button, wxButton* button = new wxButton(panel_, kButton, wxT("wxThread"), wxPoint(20, 20)); gauge_ = new wxGauge(panel_, kGauge, 100, wxPoint(20, 60), wxSize(200, 40), wxGA_HORIZONTAL); gauge_->SetValue(0); return true; } void MyFrame::OnPaintPanel(wxPaintEvent& event) { wxPaintDC dc(panel_); dc.SetPen(wxPen(*wxRED)); wxSize size = panel_->GetSize(); dc.DrawRectangle(wxRect(20, 20, size.x - 40, size.y - 40)); } void MyFrame::OnThreadUpdate(wxThreadEvent& event) { gauge_->SetValue(event.GetInt()); } void MyFrame::OnThread(wxCommandEvent& event) { wxMessageBox(wxT("Start a wxThread"), wxT("Good evening.")); MyThread* thread = new MyThread(this); thread->Execute(); } void MyFrame::OnQuit(wxCommandEvent& event) { Close(); }
6.值的一提的是如果要用vista以上的好看的控件样式,需要资源文件定义,再把它链接到exe里。
windres --input-format=rc -O coff -i resources/style.rc -o build/style.o --define __WXMSW__ --include-dir E:/software/Lib/gui/wxWidgets-2.9.2/include
#include "winresrc.h" // set this to 1 if you don't want to use manifest resource (manifest resource // is needed to enable visual styles on Windows XP - see docs/msw/winxp.txt // for more information) #define wxUSE_NO_MANIFEST 0 // this is not always needed but doesn't hurt (except making the executable // very slightly larger): this file contains the standard icons, cursors, ... #include "wx/msw/wx.rc" LANGUAGE LANG_ENGLISH, SUBLANG_ENGLISH_US //wx.rc contain .manifest,so override it. 1 RT_MANIFEST "uac.manifest"
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0"> <assemblyIdentity version="0.64.1.0" processorArchitecture="x86" name="Controls" type="win32"></assemblyIdentity> <description></description> <trustInfo xmlns="urn:schemas-microsoft-com:asm.v3"> <security> <requestedPrivileges> <requestedExecutionLevel level="asInvoker" uiAccess="false"></requestedExecutionLevel> </requestedPrivileges> </security> </trustInfo> <dependency> <dependentAssembly> <assemblyIdentity type="win32" name="Microsoft.Windows.Common-Controls" version="6.0.0.0" processorArchitecture="X86" publicKeyToken="6595b64144ccf1df" language="*"></assemblyIdentity> </dependentAssembly> </dependency> </assembly>
http://download.csdn.net/download/infoworld/5487427