Return mouse coords. relative to window Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
parad0x13
Experienced Solver
Experienced Solver
Posts: 79
Joined: Tue Jan 06, 2009 3:09 am

Return mouse coords. relative to window

Post by parad0x13 » Thu Feb 12, 2009 3:42 pm

I have a window (Render window) which has inside a GLCanvas of a certain size (SizeX, SizeY)

The window is initialized to have a client size of SizeX, and SizeY so the whole render window has a size of (SizeX+WindowsBorderX, SizeY+WindowsBorderY).

I'm trying to return the Mouse(x, y) coords. that are generated within the GLCanvas window, not the render window (It's parent) because if it returns the coordinates of the render window it will be like (-4, -30) off due to the size of the borders on the basic Windows windows.

I hope this isnt too confusing, I've tried the ScreenToClient() function and havn't quite understood how I'm going to return the CORRECT position

I wish there would be a function like

Code: Select all

wxPoint *mouse = new MousePosInWindow(this);
but I havn't found one yet...

If anyone knows a solution I would be very happy

- Thank You!

User avatar
Disch
Experienced Solver
Experienced Solver
Posts: 99
Joined: Wed Oct 17, 2007 2:01 am

Re: Return mouse coords. relative to window

Post by Disch » Thu Feb 12, 2009 4:15 pm

parad0x13 wrote: I hope this isnt too confusing, I've tried the ScreenToClient() function and havn't quite understood how I'm going to return the CORRECT position
ScreenToClient is what you want. Just run the mouse's screen coords through it:

Code: Select all

wxPoint MyWindow::MousePosInWindow()
{
  return ScreenToClient(::wxGetMousePosition());
}
Note that this will return coords which may be negative or greater than the window size if the mouse was outside the window at the time of the function call. An alternative approach would be to catch mouse events in your window and track the mouse position as it's moved:

Code: Select all

BEGIN_EVENT_TABLE(MyWindow,wxGLCanvas)
  ...
  EVT_MOTION(MyWindow::OnMouseMotion);
  EVT_ENTER_WINDOW(MyWindow::OnMouseEnterLeave)
  EVT_LEAVE_WINDOW(MyWindow::OnMouseEnterLeave)
END_EVENT_TABLE()

...

void MyWindow::OnMouseMotion(wxMouseEvent& event)
{
  ptMousePos = event.GetPosition();
}

void MyWindow::OnMouseEnterLeave(wxMouseEvent& event)
{
  bMouseInWindow = event.Entering();
}

parad0x13
Experienced Solver
Experienced Solver
Posts: 79
Joined: Tue Jan 06, 2009 3:09 am

Post by parad0x13 » Thu Feb 12, 2009 4:55 pm

Yesh! Your proper use of the function is exactly what I needed. Thank you very much!

Post Reply