简体   繁体   中英

How to draw a rectangle in wxwidgets?

#include "wx/wx.h"

class MyFrame : public wxFrame{
   public:
       MyFrame();
       ~MyFrame();
    private:
       //DECLARE_EVENT_TABLE()
};

class MyWindow : public wxWindow{
   public:
    void OnPaint(wxPaintEvent& event);
    private:
      DECLARE_EVENT_TABLE()
};

class MyApp : public wxApp
{
    public:
       MyApp();
       ~MyApp();
       virtual bool OnInit();
        void DrawSimpleShapes(wxDC& dc);
    private:
       MyFrame* m_frame = NULL;
       //MyWindow* w = NULL;
};

MyFrame::MyFrame() : wxFrame(nullptr,wxID_ANY,"Rectangle",wxPoint(30,30),wxSize(800,600))
{

}

bool MyApp :: OnInit()
{
   m_frame = new MyFrame();
   m_frame->Show();
   //w = new MyWindow();
   //w->Show();
   return true;
}
wxIMPLEMENT_APP(MyApp);
wxBEGIN_EVENT_TABLE(MyWindow,wxWindow)
   EVT_PAINT(MyWindow::OnPaint)
wxEND_EVENT_TABLE()
MyFrame::~MyFrame()
{

}

MyApp::MyApp()
{

}

MyApp::~MyApp()
{

}

 void MyWindow :: OnPaint(wxPaintEvent& event)
 {
    wxPaintDC dc(this);
    dc.SetPen(*wxBLACK_PEN);
    dc.SetBrush(*wxRED_BRUSH);
    wxSize sz = GetClientSize();
    wxCoord w = 100, h = 50;
    int x = wxMax(0,(sz.x-w)/2);
    int y = wxMax(0,(sz.y - h)/2);
    wxRect recToDraw(x,y,w,h);
    dc.DrawRectangle(recToDraw);
 }

I need some guidance learning wxWidgets. What's the problem with my code? When I run this code it does not print any rectangle. Instead it just print window only. I am new to wxWidgets library so it is difficult for me to find any errors. I cannot do any error handling in the wxWidgets.

There are multiple issues with the code posted, but I'll restrict this answer to the question that was asked. If you want to draw a rectangle on the applications frame, you need to

  1. declare the OnPaint method in the frame class, and
  2. alter the event table macro to set the OnPaint method to handle the paint event.

Here is a corrected example with these 2 changes:

#include "wx/wx.h"

class MyFrame : public wxFrame{
   public:
       MyFrame();
       ~MyFrame();
    private:
       void OnPaint(wxPaintEvent& event);
       DECLARE_EVENT_TABLE()
};

class MyApp : public wxApp
{
    public:
       MyApp();
       ~MyApp();
       virtual bool OnInit();
    private:
       MyFrame* m_frame = NULL;
};

MyFrame::MyFrame() : wxFrame(nullptr,wxID_ANY,"Rectangle",wxPoint(30,30),wxSize(800,600))
{

}

bool MyApp :: OnInit()
{
   m_frame = new MyFrame();
   m_frame->Show();
   return true;
}
wxIMPLEMENT_APP(MyApp);

wxBEGIN_EVENT_TABLE(MyFrame,wxFrame)
   EVT_PAINT(MyFrame::OnPaint)
wxEND_EVENT_TABLE()

MyFrame::~MyFrame()
{

}

MyApp::MyApp()
{

}

MyApp::~MyApp()
{

}

void MyFrame :: OnPaint(wxPaintEvent& event)
{
    wxPaintDC dc(this);
    dc.SetPen(*wxBLACK_PEN);
    dc.SetBrush(*wxRED_BRUSH);
    wxSize sz = GetClientSize();
    wxCoord w = 100, h = 50;
    int x = wxMax(0,(sz.x-w)/2);
    int y = wxMax(0,(sz.y - h)/2);
    wxRect recToDraw(x,y,w,h);
    dc.DrawRectangle(recToDraw);
}

In the code you posted, you had an extra MyWindow class, but that class was never used anywhere.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM