稍微碰了一下wxWidget,感覺它在設計menu或一些系統內建元件上比較方便,譬如說menu可以直接以文字定義快捷鍵,而無須另寫event;狀態列也可以直接對應元件操作而做出個別不同的輔助說明。
wxWidget編譯出來的是原生(Native)程式,不知道是不是因為這個緣故,使得它在繪圖上的設定幾乎與Windows API一樣難搞。雖然圖片已經更換,但畫面仍未改變,除非縮下再放大,或拖曳一個視窗在它前面晃過,才能顯示出來。實在不夠直覺,而我目前也還未想到方法可以徹底解決這個問題。麻煩,還是再回頭玩GTK+好了。
wxWidgetsApp.cpp:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
#include "wxWidgetsApp.h" #include "wxWidgetsFrame.h" IMPLEMENT_APP(wxWidgetsApp) // ============================================================================ // implementation // ============================================================================ wxWidgetsApp::wxWidgetsApp() { } wxWidgetsApp::~wxWidgetsApp() { } bool wxWidgetsApp::OnInit() { wxInitAllImageHandlers() ; // 初始化所有的image handlers. wxImage image; wxWidgetsFrame *frame = new wxWidgetsFrame( wxBitmap(image), _("圖片顯示測試程式"), wxPoint(50, 50), wxSize(400, 300)); frame->Show(TRUE); SetTopWindow(frame); return TRUE; }
wxWidgetsApp.h:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#ifndef __WXWIDGETSAPP_H #define __WXWIDGETSAPP_H #include <wx/wx.h> class wxWidgetsApp : public wxApp { public: wxWidgetsApp(); virtual ~wxWidgetsApp(); virtual bool OnInit(); }; DECLARE_APP(wxWidgetsApp) #endif //__WXWIDGETSAPP_H
wxWidgetsFrame.cpp:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
#include "wxWidgetsApp.h" #include "wxWidgetsFrame.h" // ---------------------------------------------------------------------------- // constants // ---------------------------------------------------------------------------- enum { ID_MENU_QUIT = 1, ID_MENU_ABOUT = 2, ID_MENU_OPEN = 3 }; IMPLEMENT_CLASS(wxWidgetsFrame, wxFrame) BEGIN_EVENT_TABLE(wxWidgetsFrame, wxFrame) EVT_MENU(wxID_EXIT, wxWidgetsFrame::OnQuit) EVT_MENU(wxID_ABOUT, wxWidgetsFrame::OnAbout) EVT_MENU(wxID_OPEN, wxWidgetsFrame::OnNewFrame) EVT_PAINT(wxWidgetsFrame::OnPaint) END_EVENT_TABLE() BEGIN_EVENT_TABLE(MyImageFrame, wxFrame) EVT_ERASE_BACKGROUND(MyImageFrame::OnEraseBackground) EVT_PAINT(MyImageFrame::OnPaint) EVT_LEFT_DCLICK(MyImageFrame::OnSave) END_EVENT_TABLE() // ---------------------------------------------------------------------------- // main frame // ---------------------------------------------------------------------------- wxWidgetsFrame::wxWidgetsFrame(const wxBitmap& bitmap, const wxString& title, const wxPoint& pos, const wxSize& size) : wxFrame((wxFrame *)NULL, wxID_ANY, title, pos, size ) { wxMenu *fileMenu = new wxMenu(_(""), wxMENU_TEAROFF); wxMenu *helpMenu = new wxMenu(_(""), wxMENU_TEAROFF); fileMenu->Append(wxID_OPEN, _("&開啟\tAlt-K"), _("開啟圖片")); fileMenu->Append(wxID_EXIT, _("&離開\tAlt-Y"), _("離開本程式")); helpMenu->Append(wxID_ABOUT, _("&關於\tAlt-Z"), _("關於本程式")); wxMenuBar *menuBar = new wxMenuBar(); menuBar->Append(fileMenu, _("&檔案")); menuBar->Append(helpMenu, _("&幫助")); SetMenuBar(menuBar); CreateStatusBar(2); SetStatusText(_("狀態列")); wxImage image; } wxWidgetsFrame::~wxWidgetsFrame() { } void wxWidgetsFrame::OnQuit(wxCommandEvent& event) { Close(TRUE); } void wxWidgetsFrame::OnAbout(wxCommandEvent& event) { wxString msg; msg.Printf(_( "Hello and welcome to %s" ), wxVERSION_STRING); wxMessageBox(msg, _( "About" ), wxOK | wxICON_INFORMATION, this); } void wxWidgetsFrame::OnNewFrame( wxCommandEvent &WXUNUSED(event) ) { wxString filename = wxFileSelector(_("Select image file")); if ( !filename ) return; wxImage image; if ( !image.LoadFile(filename) ) { wxLogError(_T("Couldn't load image from '%s'."), filename.c_str()); return; } (new MyImageFrame(this, wxBitmap(image), filename))->Show(); // 另開圖片顯示視窗 m_bitmap = wxBitmap(image); // 儲存讀入的圖片資料 SetClientSize(m_bitmap.GetWidth(), m_bitmap.GetHeight()); // 隨圖片大小改變視窗大小 } void wxWidgetsFrame::OnPaint(wxPaintEvent& WXUNUSED(event)) { wxPaintDC dc( this ); //dc.SetBackground(*wxGREY_BRUSH); //dc.Clear() ; dc.DrawBitmap( m_bitmap, 0, 0, false /* use mask */ ); // 繪出圖片 } // ---------------------------------------------------------------------------- // image frame // ---------------------------------------------------------------------------- MyImageFrame::MyImageFrame(wxFrame *parent, const wxBitmap& bitmap, wxString filename) : wxFrame(parent, wxID_ANY, _(filename), wxDefaultPosition, wxDefaultSize, wxCAPTION | wxSYSTEM_MENU | wxCLOSE_BOX), m_bitmap(bitmap) { SetClientSize(bitmap.GetWidth(), bitmap.GetHeight()); // 隨圖片大小變更視窗大小 } void MyImageFrame::OnEraseBackground(wxEraseEvent& WXUNUSED(event)) { // do nothing here to be able to see how transparent images are shown } void MyImageFrame::OnPaint(wxPaintEvent& WXUNUSED(event)) { wxPaintDC dc( this ); dc.DrawBitmap( m_bitmap, 0, 0, true /* use mask */ ); } void MyImageFrame::OnSave(wxMouseEvent& WXUNUSED(event)) { }
wxWidgetsFrame.h:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
#ifndef __WXWIDGETSFRAME_H #define __WXWIDGETSFRAME_H #include <wx/wx.h> #include <wx/wxprec.h> #include <wx/brush.h> #include <wx/image.h> #include <wx/file.h> class wxWidgetsFrame : public wxFrame { public: wxWidgetsFrame(const wxBitmap& bitmap, const wxString& title, const wxPoint& pos, const wxSize& size); virtual ~wxWidgetsFrame(); void OnQuit(wxCommandEvent& event); void OnAbout(wxCommandEvent& event); void OnNewFrame(wxCommandEvent& event); void OnPaint(wxPaintEvent& WXUNUSED(event)); wxBitmap m_bitmap ; private: DECLARE_CLASS(wxWidgetsFrame) DECLARE_EVENT_TABLE() }; class MyImageFrame : public wxFrame { public: MyImageFrame(wxFrame *parent, const wxBitmap& bitmap, wxString filename); void OnEraseBackground(wxEraseEvent& WXUNUSED(event)); void OnPaint(wxPaintEvent& WXUNUSED(event)); void OnSave(wxMouseEvent& WXUNUSED(event)); private: wxBitmap m_bitmap; DECLARE_EVENT_TABLE() }; #endif //__WXWIDGETSFRAME_H
No response to “wxWidget實作之圖片顯示” ;
張貼留言