void MyFrame::OnLeftClick(wxMouseEvent& event) { wxPoint pos; event.GetLogicalPosition(&pos.x, &pos.y); wxMessageBox("Left click at position (" + wxString::Format("%d", pos.x) + ", " + wxString::Format("%d", pos.y) + ")."); }
void MyCanvas::OnMouseDown(wxMouseEvent& event) { if (event.LeftIsDown()) { wxPoint startPos; event.GetLogicalPosition(&startPos.x, &startPos.y); m_previousPos = startPos; Connect(wxEVT_MOTION, wxMouseEventHandler(MyCanvas::OnMouseMotion)); } } void MyCanvas::OnMouseMotion(wxMouseEvent& event) { if (event.Dragging() && event.LeftIsDown()) { wxPoint currentPos; event.GetLogicalPosition(¤tPos.x, ¤tPos.y); wxClientDC dc(this); dc.SetPen(wxPen(wxColor(0, 0, 255), 5)); dc.DrawLine(m_previousPos, currentPos); m_previousPos = currentPos; } } void MyCanvas::OnMouseUp(wxMouseEvent& event) { Disconnect(wxEVT_MOTION, wxMouseEventHandler(MyCanvas::OnMouseMotion)); }In this example, a line is drawn on a canvas between the previous and current mouse positions when the left button is held down and dragged. The logical positions of the mouse events are obtained using GetLogicalPosition method and stored in the objects startPos and currentPos of wxPoint class. Then, a line is drawn between the two positions using wxClientDC class methods. Finally, the mouse motion event is disconnected when the left button is released. Package library: wxWidgets