#include#include class MyFrame : public wxFrame { public: MyFrame() : wxFrame(nullptr, wxID_ANY, "DrawPoint Example") {} void OnPaint(wxPaintEvent& event) override { wxPaintDC dc(this); dc.SetPen(wxPen(wxColour(255,0,0), 2)); // set red pen with 2-pixel width wxSize size = GetClientSize(); wxPoint center(size.GetWidth() / 2, size.GetHeight() / 2); // get the center point dc.DrawPoint(center); // draw the point at the center } wxDECLARE_EVENT_TABLE(); }; wxBEGIN_EVENT_TABLE(MyFrame, wxFrame) EVT_PAINT(MyFrame::OnPaint) wxEND_EVENT_TABLE() int main(int argc, char** argv) { wxApp::SetInstance(wxCREATE_APP(MyApp)); MyApp::GetInstance()->OnInit(); MyFrame frame; frame.Show(); return MyApp::GetInstance()->OnRun(); }
#includeThis example creates a frame, sets its pen color and width, and allows the user to add points to the frame by clicking on the left mouse button. Each point is stored in a vector and redrawn on the window using the wxClientDC device context. This example also demonstrates how to handle mouse events using EVT_LEFT_DOWN and the OnMouse method. The library package required for using the wxDC class and its methods is wxWidgets.#include #include #include class MyFrame : public wxFrame { public: MyFrame() : wxFrame(nullptr, wxID_ANY, "DrawPoint Example") {} void OnPaint(wxPaintEvent& event) override { wxClientDC dc(this); dc.SetPen(wxPen(*wxBLUE, 2)); for (int i = 0; i < points.size(); ++i) { wxPoint& p = points[i]; dc.DrawPoint(p); // draw each point in the vector } } void OnMouse(wxMouseEvent& event) override { if (event.LeftDown()) { wxPoint pos = event.GetPosition(); points.push_back(wxPoint(pos.x, pos.y)); // add a new point to the vector Refresh(); // update the window to repaint the new point } } std::vector points; wxDECLARE_EVENT_TABLE(); }; wxBEGIN_EVENT_TABLE(MyFrame, wxFrame) EVT_PAINT(MyFrame::OnPaint) EVT_LEFT_DOWN(MyFrame::OnMouse) wxEND_EVENT_TABLE() int main(int argc, char** argv) { wxApp::SetInstance(wxCREATE_APP(MyApp)); MyApp::GetInstance()->OnInit(); MyFrame frame; frame.Show(); return MyApp::GetInstance()->OnRun(); }