Click Coordinates after Window Movement - c++

Not being entirely familiar with programming GUIs in C++ and whatnot, I'm running into a problem where my program will not respond to a left-mouse click after moving/dragging the application window. What should I do to make it work properly? Here is how I am handling the left-click message in the callback function:
case WM_LBUTTONDOWN:
{
POINT point;
GetCursorPos(&point);
break;
}

The mouse location is included in the message as the LPARAM.
POINT point;
point.x = GET_X_LPARAM(lParam);
point.y = GET_Y_LPARAM(lParam);
As documented at MSDN, the point is relative to the upper left corner of the client area. Therefore if you move the window, point will still be relative to your window.
GetCursorPos gets the position of the cursor in screen coordinates. So you would have to compensate for the position of your window on the screen to get a usable position.

Related

MFC getting mouse pointer coordinate problem

I'm trying to get coordinate of the Rect (Picture control) but It's a bit glitchy.
So here're the process that I've done.
1st. made a picture control
2nd. I've earned WindowRect through GetWindowRect
// myDialogDlg.cpp
CRect m_rcDisp // (is acually in myDialogDlg.h)
BOOL myDialogDlg::OnInitDialog()
{
// IDC_PIC1 == ID of the (static) picture control
GetDlgItem(IDC_PIC1)->GetWindowRect(m_rcDisp);
...
}
3rd. I've made OnMouseMove event, and used PtInRect to make some action while mouse pointer is inside the picture control.
// myDialogDlg.cpp
void myDialogDlg::OnMouseMove(UINT nFlags, CPoint point)
{
CString debug;
{
if (m_rcDisp.PtInRect(point))
{
// my event starts
OutputDebugString(_T("here"));
if (m_CamTrig == CAMERA_TRIG_SW)
{
m_CurSor.x = point.x;
m_CurSor.y = point.y;
InvalidateRect(m_rcDisp, NULL);
}
// my event ends
}
}
CDialogEx::OnMouseMove(nFlags, point);
}
and... where actually PtInRect works is about here inside the red box
:( Hope I get the best answer.
thx!
GetWindowRect returns the window dimensions in screen coordinates. WM_MOUSEMOVE reports the mouse position in client coordinates. ScreenToClient1 can be used to translate from screen coordinates to client coordinates, making the picture control's window rectangle coordinates and hit testing function agree on a common origin.
This needs to be done whenever the picture control is moved relative to its parent dialog. If the picture control is never moved you only need to adjust the rectangle once.
This answer links to the Windows API documentation, since it's generally more informative and better maintained than the respective MFC entries. You can call either one from MFC code, though it's usually more convenient to just call into the CWnd members.
1 Use MapWindowPoints instead if you plan on supporting RTL and LTR layouts. See Window Layout and Mirroring for guidance.

How to check mouse click position is on required application?

I know the mouse click position using API GetCursorPos and the handle of application (HWND).
How can I check mouse click position is on this application?
My view:
Get the bounding box of application from its handle. (GetWindowRect(hWnd, &rect);)
Check cursor position lies in this bounding box. (PtInRect(&rect, p))
This is not working if windows are overlapping.
As we know the handle of targeted screen handle and click cursor position:
// hWnd : Already known windows handle
GetCursorPos(&p);
HWND hWndFromPoint = WindowFromPoint(p);
// If the handle got from click point is child of the desire window means it is clicked on the desire window itself.
if (IsChild(hWnd, hWndFromPoint))
{
// Do something on Mouse click
}

c++ win32 Relative position to desktop

How will i get a application x y position relative to the client screen?
I tried but all unsucessful so can anyone help.
RECT pta;
GetWindowRect(hWnd,&pta);
POINT Rpt = { pta.left, pta.top };
ScreenToClient(hWnd, &Rpt);
But this doesn't work.
I want to set my cursor position to middle in the window of my app
If I understand right, you want to call the SetCursorPos() windows API call to center the mouse cursor to your window. That function takes screen coordinates.
GetWindowRect() returns the window top and left coordinates already in the screen coordinates, so no transform is necessary.
To get to your window's center coordinates, you just need to add half of your window's width and height to the top-left point's coordinates. Then you can call SetCursorPos().

C++ get off set position window

Iam newbie in c++ programming and i was developing a application but am stuck at somewhere i want to get position a element in the window to simulate a mouse click.
I tried this but nowhere
POINT p;
if (GetCursorPos(&p))
{
//cursor position now in p.x and p.y
}
This doesn't give the exact thing i want like if i move window to another position in windows it display wrong position.
But i want to get position of a button in the window when the user presses the Enter button.
Can someone help me.
GetCursorPos() returns you the position of the cursor in screen coordinates.
If you want coordinates relative to the client area of your window, you can use ScreenToClient function.
If you are using MFC, try this.
CPoint pos;
if( GetCursorPos(&pos) )
{
ScreenToClient( &pos );
TRACE("client pos: %d,%d\n", pos.x, pos.y);
}

Convert screen mouse coordinate to window coordinate

This is continue of my previous question -> Draw mouse pointer icon?
I want to draw mouse in specific window on the desktop, i already know how to draw the mouse and how to track the movement of the real mouse.
But i fail to convert screen coordinates, here is example what i want to do:
When the REAL mouse is in the upper left corner 0,0 the DRAWN mouse to be in 0,0 of my specific window, and when the REAL mouse is in the down right corner 1600,900 the DRAWN mouse to be in 700,400 of my specific window.
I will re-explain if someone is not understanding exactly the problem.
Thanks in advance.
You need to scale the mouse position w.r.t your window dimensions.
Let DX and DY be the desktop size. Let WX and WY be your window size. Let (Dmx,Dmy) be the coordinate of the original mouse position w.r.t the desktop. Then the position of your mouse within your window according to your requirement would be (Dmx/DX * WX, Dmy/DY * WY). When coding, please remember to make sure the division happens with floating point numbers.