c++ win32 Relative position to desktop - c++

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().

Related

SFML mouse getPosition not working properly

I'm trying to get my cursor sprite to try and appear on top of my mouse cursor but for some reason it is not appearing on top of the mouse cursor.
Code:
cursorSprite.setPosition(sf::Mouse::getPosition().x, sf::Mouse::getPosition().y);
As you can see, I'm simply using setPosition() with my sprite, and setting it at the x and y positions of the mouse. However, this is not working and the cursor sprite is appearing at a different location to my actual mouse cursor location.
Why? Could it be something to do with how I'm setting up my window?
window(sf::VideoMode(800, 600, 32), "SFML Test", sf::Style::Default)
There are two main coordinate systems that should be considered when running SFML app (as well as pretty much every other application) in windowed mode: screen coordinates and windowed coordinates.
You are getting wrong results because sf::Mouse::getPosition() return click position in screen coordinates and you want click in window coordinates.
You can manually transform screen coordinates to window coordinates, but it is much better to use this interface provided by SFML:
sf::Vector2i pos = sf::Mouse::getPosition(window);
The pos will be in window coordinates.

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.

What is the difference between GetClientRect and GetWindowRect in WinApi?

What of these should I use in InvalidateRect to refresh my window? And why?
The window rect includes the non-client area, i.e. the window borders, caption bar etc. The client rect does not.
GetWindowRect returns a rect in screen coordinates whereas GetClientRect returns a rect in client coordinates.
InvalidateRect receives a rect in client coordinates. If you want to invalidate your entire client area, then pass NULL to InvalidateRect. You could pass in the rect returned by GetClientRect, but it is far simpler and clearer to pass NULL.
A very simple explanation is that GetWindowRect() gives you the rectangle that includes the borders of the window. GetClientRect() gives you the rectangle that excludes the borders - the area that is allocated to the window specific drawing.
Please note that GetWindowRect() returns a rectangle in screen coordinates - coordinates that are relative to the screen/monitor. GetClientRect() returns a rectangle that is relative to itself.
GetClientRect gets the coordinates of the window's client area. Specifically this is the area inside the window chrome and excludes the header etc. One of the comments on the MSDN page sums it up quite well:
I would say that this function return size of the area that I can render to.
GetWindowsRect gets the coordinates of the whole window. This includes the header, status bar etc. However according to a comment on the MSDN page
Apps under Vista that are not linked with WINVER=6 will receive a misleading set of values here, that do not account for the extra padding of "glass" pixels Vista Aero applies to the window.
So unless this have been fixed for Windows 7 double check the result you get and make sure you have the correct value of WINVER.
From MSDN:
GetWindowRect
Retrieves the dimensions of the bounding rectangle of the specified window. The dimensions are given in screen coordinates that are relative to the upper-left corner of the screen.
GetClientRect
Retrieves the coordinates of a window's client area. The client coordinates specify the upper-left and lower-right corners of the client area. Because client coordinates are relative to the upper-left corner of a window's client area, the coordinates of the upper-left corner are (0,0).
More: client rect does not include title bar, borders, scroll bars, status bar...

Center Mouse on Window

This is daft, I'm trying to move the mouse to the center of the active window. I cant find a method on MSDN that gives me the coords (RECT) relative to the corner of the screen.
::GetClientRect returns 0,0 for the top left.
Is there a method that returns 100, 134 or some such.
Did you look into GetWindowRect function?