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);
}
Related
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.
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
}
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().
My application's main window is starting to have lots of stuff so I need a vertical scrollbar to fit everything inside client area. I coded a scrollbar control, WM_VSCROLL messages like SB_LINEDOWN are being processed and scrollbar moves nicely. The last part is to make the content of a main window to move along with the thumb of a scrollbar and it seems a bit hard task for me. This is my best try:
int dy = -( CurrPos - si.nPos );
RECT rctMainWindowArea = { 0, 0, 1000, main_window.bottom };
ScrollWindowEx( hwndMainWindow, 0, dy,( CONST RECT * ) &rctMainWindowArea,( CONST RECT * ) NULL,( HRGN ) NULL,( LPRECT ) NULL, SW_SCROLLCHILDREN | SW_INVALIDATE | SW_ERASE );
UpdateWindow( hwndMainWindow );
It works as long I'm scrolling down. When I scroll back up again everything gets messed up. I've been googling about this issue for a while and it seems that I have to redraw the lost client area of main window. However I have no idea how to do it. I've found on the web only examples where text is being scrolled inside edit control. I need to scroll whole main window which has couple of different basic controls, some bmp graphic, some other graphic elements like TextOut(), RoundRect() and so on.
I need some code examples how to solve my issue or at least some simple explanation (I'm amateur programmer). Thanks a lot !
Windows doesn't keep track of how much the window has scrolled, so when it asks you to repaint part of the window, you need to change what you paint based on how much scrolling you've done.
The easiest way to do this is to adjust the window origin to match the amount of scrolling you've done. Your WM_PAINT handler might look something like this. offsetX and offsetY are the distances you've scrolled in the X and Y directions respectively.
// Adjust coordinates to automatically scroll
POINT origin;
GetWindowOrgEx(hdc, &origin);
SetWindowOrgEx(hdc, origin.x + offsetX, origin.y + offsetY, 0);
// Move the paint rectangle into the new coordinate system
OffsetRect(&ps.rcPaint, offsetX, offsetY);
// Do the painting
// Change this to call your painting function
CWindow::DoPaint(hdc, ps);
// Restore coordinates
SetWindowOrgEx(hdc, origin.x, origin.y, 0);
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.