Code Yarns ‍👨‍💻
Tech BlogPersonal Blog

How to get mouse position in wxWidgets

📅 2014-Jul-22 ⬩ ✍️ Ashwin Nanjappa ⬩ 📚 Archive

To get mouse position relative to top-left corner of display, add this in a mouse event handler:

const wxPoint pt = wxGetMousePosition();
int mouseX = pt.x;
int mouseY = pt.y;

To get mouse position relative to top-left corner of current window or canvas, add this in a mouse event handler:

const wxPoint pt = wxGetMousePosition();
int mouseX = pt.x - this->GetScreenPosition().x;
int mouseY = pt.y - this->GetScreenPosition().y;

Tried with: wxWidgets 3.0 and Ubuntu 14.04