I need to perform mouse click on a particular element in a window. I have calculated the coordinates of that element in my system and written a code to perform mouse click on that particular coordinate.
The code works fine for my machine. However, if I run it on a different machine the click operation is performed in a different position. It's because the screen size is different. How do I perform the mouse click on the same element in any machine regardless of window size. Is there any other way to locate an element on the screen? like percentage instead of pixels? Will it work?
RECT rect;
GetWindowRect(hwnd, &rect);
int x = rect.left;
int y = rect.top;
SetCursorPos(x + 570, y + 450);
mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
In the above code I have set the cursor position with respect to my system. The element's XY coordinate is different for other machines with bigger screens. Any hint would be of much help.
Related
In the assignment, I'm working on, I have to create a huge box in an OpenGL window and be able to rotate it.
A window is being created by this code:
window = glfwCreateWindow(1000, 1000, "Window for Box", NULL, NULL);
This first 1000 in the above line corresponds to the width of the window where the second 1000 window corresponds to the height of the window.
Now, when I open the lid of the box I've created and rotated it, it looks like this:
The yellow coloured square is the lid and it is opened. You can see that it doesn't look like a box as the depth of the window is less and the box is cut with a white surface. How can I increase the window size to display box perfectly?
The window is 2D, it has no depth. What you're looking for is a projection matrix. If you have used glOrtho(), the last parameter defines how far from the camera a fragment can be, to still be visible. If you have defined your own projection matrix set the zFar value to be greater than the z coordinates of all your vertices.
Alternatively you can use depth clamping: https://www.khronos.org/opengl/wiki/Vertex_Post-Processing#Depth_clamping
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().
I want to set the default size in pixels of dialog, say it is 640 pixel width and 384 pixel height. what I mean by the default is that when the first time the CXXXDlg::OnSize(UINT nType, int cx, int cy) is called, the value of cx is 640 and the value of cy is 384. scene the default size of the dialog is in dialog units, and I can use the MapDialogRect() to convert the dialog units to the pixels, How can I do the reverse? the MoveWindow() and the SetWindowPos() can set eh size of the dialog but not the default size. I also have tried the GetDialogBaseUnits() like this:
DWORD dw = GetDialogBaseUnits();
WORD m_duXx4 = LOWORD(dw);
WORD m_duYx8 = HIWORD(dw);
int dialogUnitX = MulDiv(640, 4, m_duXx4);
int dialogUnitY = MulDiv(384, 8, m_duYx8);
it turned out that the dialogUnitX is 320 and the dialogUnitY is 192, but when I set the dialog unit to 320 * 192, what I got in CXXXDlg::OnSize(UINT nType, int cx, int cy) is not 640 * 384 but 560 * 336. Any ideas?
A window consists of a Client Area and a Nonclient Area.
The client area is the part of a window where the application displays output, such as text or graphics.
The title bar, menu bar, window menu, minimize and maximize buttons, sizing border, and scroll bars are referred to collectively as the window's nonclient area.
The Window Rect designates the area that encompasses the entire window. It includes the client area as well as the nonclient area. It can be retrieved by calling GetWindowRect (or its MFC-equivalent). It is also used as the input to functions like MoveWindow or SetWindowPos.
The Client Rect is the area of a window that is not occupied by the nonclient area. It can be queried by calling GetClientRect. The client rect dimensions are passed to the WM_SIZE message handler.
If an application requires a specific size for its client area it can calculate the respective window rect by calling AdjustWindowRect or AdjustWindowRectEx.
The window rect is usually expressed in Screen Coordinates while the client rect uses Client Coordinates. Both coordinate systems represent device pixels. The origin is in the top left corner of the primary display for screen coordinates and the top left corner of the client area for client coordinates. To translate between the coordinate systems an application uses ClientToScreen or ScreenToClient.
Dialog templates specify dimensions and positions in Dialog Template Units. Dialog template units are directly related to a dialog's font. To convert between dialog template units and device pixels an application calls MapDialogRect. There is no API call to calculate the reverse. An application has to perform the calculations manually:
width = MulDiv(width, 4, baseunitX);
height = MulDiv(height, 8, baseunitY);
If an application wants to confine the window size dynamically it can handle the WM_GETMINMAXINFO message and populate a MINMAXINFO structure with the desired dimensions. This message is sent to a window when the size or position of the window is about to change.
Someone can explain me how to convert dialog box units to screen coordinates values ?
I saw that there is MapDialogRect function, but its converting RECT, i want to convert the x,y and cx,cy values to screen coordinate values and i dont really understand how to achieve this.
Thanks in advance.
If you already have a window handle, then just use the MapDialogRect function. As others have noted, MapDialogRect takes a RECT, so if you don't have a RECT, you can create one.
RECT rc;
rc.left = x;
rc.top = y;
rc.right = x + cx;
rc.bottom = y + cy;
MapDialogRect(hdlg, &rc);
If your problem is that you don't have a dialog box handle in the first place, then the documentation for the MapDialogRect function tells you how to perform the calculations: Determine the the average character dimensions for the dialog box (which the documentation calls baseunitX and baseunitY) and then plug it into the formulas.
Note that this calculates the client rectangle of the dialog box. You still have to add non-client space. It's not clear what you're trying to do, so I don't know whether adding non-client space is appropriate or not.
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);