#includeclass MyFrame : public wxFrame { public: MyFrame() : wxFrame(NULL, wxID_ANY, "Line Drawing") {} void OnPaint(wxPaintEvent& event) { wxPaintDC pdc(this); pdc.DrawLine(20, 20, 200, 200); } DECLARE_EVENT_TABLE() }; BEGIN_EVENT_TABLE(MyFrame, wxFrame) EVT_PAINT(MyFrame::OnPaint) END_EVENT_TABLE() class MyApp : public wxApp { public: bool OnInit() { MyFrame* frame = new MyFrame(); frame->Show(); return true; } }; IMPLEMENT_APP(MyApp)
#includeIn this example, a memory bitmap is created using wxBitmap, and wxMemoryDC is used to draw multiple lines on it using the DrawLine method. The bitmap is later painted on a window using wxPaintDC's DrawBitmap method. This example is useful when drawing complex graphics that can be cached in memory and used repeatedly without redrawing on a device.class MyFrame : public wxFrame { public: MyFrame() : wxFrame(NULL, wxID_ANY, "Line Drawing") { bitmap = new wxBitmap(400, 400); wxMemoryDC mdc(*bitmap); mdc.SetPen(wxPen(wxColor(0, 0, 0))); mdc.DrawLine(20, 20, 200, 200); mdc.DrawLine(50, 50, 300, 100); mdc.DrawLine(100, 300, 200, 100); } void OnPaint(wxPaintEvent& event) { wxPaintDC pdc(this); pdc.DrawBitmap(*bitmap, 0, 0); } DECLARE_EVENT_TABLE() private: wxBitmap* bitmap; }; BEGIN_EVENT_TABLE(MyFrame, wxFrame) EVT_PAINT(MyFrame::OnPaint) END_EVENT_TABLE() class MyApp : public wxApp { public: bool OnInit() { MyFrame* frame = new MyFrame(); frame->Show(); return true; } }; IMPLEMENT_APP(MyApp)