Let us solve it step-by-step by performing the following steps:
wxWidgets
project and set the project name to MyPaint
. Choose wxSmith as the Preferred GUI Builder.Title
property to MyPaint
.1
.MyPaintWindow
640
and Min Height to 480
Select file
*.bmp;*.jpg;*.png
MyPaintFileDialog
StatusBarMain
2
5
10
.
Menu item |
ID |
Label |
Accelerator |
Help item |
---|---|---|---|---|
&File | &Open Image |
|
|
|
|
&View | Zoom &In |
|
|
|
|
&View | Zoom &Out |
|
|
|
|
The final menu bar editor window shall look similar to the following screenshot:
Label |
Options |
Bitmap |
Tooltip/Help text |
---|---|---|---|
Open an image… |
Normal |
Image from wxArtProvider—wxART_FILE_OPEN |
|
– |
Separator |
– |
– |
Zoom in |
Normal |
|
|
Zoom out |
Normal |
|
|
The final ToolBar editor window will look similar to the following screenshot.
We have completed addition of most GUI elements. We need to write code to complete our app. Before doing that please note that wxSmith generated code remains inside the matching //(*
and //*)
block. Do not write your code inside this block as wxSmith will delete any custom code within this block while regenerating code.
MyPaintFrame
class declaration in the MyPaintFrame.h
file as a private member variable and function.wxImage* m_Image; // To store loaded image double m_zoomFactor; // To store current zoom factor void RefreshPaintWindow(void); // To paint image
MyPaintFrame()
constructor function. We will be creating a new image class and assign it to m_Image
variable. We'll use SetScrollbars()
and ShowScrollbars()
function to assign scroll bar related properties. We'll assign initial zoom factor of 100 percent and use the wxInitAllImageHandlers()
function to initialize image processing engine for our app. Finally we'll use the SetStatusText()
function to set status bar text.m_Image = new wxImage(640, 480); MyPaintWindow->SetScrollbars(10, 10, 10, 10); MyPaintWindow->ShowScrollbars(wxSHOW_SB_ALWAYS, wxSHOW_SB_ALWAYS); m_zoomFactor = 1.0; wxInitAllImageHandlers(); StatusBarMain->SetStatusText(_T("Ready!"), 0); wxString msg; msg.Printf(_T("%d %%"), static_cast<int>(m_zoomFactor*100)); StatusBarMain->SetStatusText(msg, 1);
OnFileOpen
as the name of the event handler. Then enter the following code inside the MyPaintFrame::OnFileOpen()
function:int result; result = MyPaintFileDialog->ShowModal(); if (result == wxID_OK) { m_Image->LoadFile(MyPaintFileDialog->GetPath()); m_zoomFactor = 1.0; RefreshPaintWindow(); }
OnViewZoomIn
and OnViewZoomOut
event handler function to Zoom &In and Zoom &Out by navigating to &View | Zoom &In and &View | Zoom &Out menu options respectively. Refer completed exercise for the code to be added to each handler.OnMyPaintWindowPaint
event handler to EVT_PAINT and paste the following code. This code paints loaded image on wxScrolledWindow
:wxPaintDC paintDC(MyPaintWindow); wxRect rect; const wxBitmap bitmap(m_Image->Scale(m_Image->GetWidth() * m_zoomFactor, m_Image->GetHeight()* m_zoomFactor)); rect.SetSize(m_Image->GetSize() * m_zoomFactor); MyPaintWindow->SetVirtualSize(m_Image->GetSize() * m_zoomFactor); if ( (rect.GetWidth() < MyPaintWindow->GetVirtualSize().GetWidth()) || (rect.GetHeight() < MyPaintWindow->GetVirtualSize().GetHeight()) ) { rect = rect.CenterIn(MyPaintWindow->GetVirtualSize()); } MyPaintWindow->DoPrepareDC(paintDC); paintDC.DrawBitmap(bitmap, rect.GetTopLeft());
OnResize
event handler to MyPaintWindow and add the following line of code:RefreshPaintWindow();
RefreshPaintWindow()
function to the MyPaintFrame
class and add the following code inside that function:wxString msg; MyPaintWindow->ClearBackground(); MyPaintWindow->Refresh(); msg.Printf(_T("%d %%"), static_cast<int>(m_zoomFactor*100)); StatusBarMain->SetStatusText(msg, 1);
OnFileOpen
event handler to EVT_TOOL. This will connect the existing OnFileOpen()
function to this toolbar button. So clicking on this toolbar button will emulate navigating to the File | Open menu options.OnViewZoomIn
and OnViewZoomOut
event handlers respectively.3.128.173.53