Center Mouse on Window - c++

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?

Related

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

Cocos2d-x - Understanding positioning sprites on screen

Can anyone provide some basic pointers on placing CCSprites on screen?
Example:
CCSize s = CCDirector::sharedDirector()->getWinSize();
With s, say I wanted to position a sprite on the very bottom of the screen starting at 0, think something like grass.
if I am running at 1024 x 768, middle is:
setPosition( ccp(s.width/2, s.height/2) );
so starting all the way left and middle would be:
setPosition( ccp(0, s.height/2) );
So how do I get farther down?
setPosition( 0, s.height) );
This puts me starting at the top left and staying along the top of the screen.
Any help would be appreciated.
Position is relative to the sprite's parent, as well as its anchorPoint.
anchorPoint generally ranges from 0 to 1 for each coordinate, with a default of 0.5. I say "generally" because it can really be any value, but ranges outside of 0-1 place you outside of the bounds of the sprite.
For example, an anchorPoint of (0,0) makes positions relative to the bottom left. (1,0) is the bottom right, (0,1) is the top left and (1,1) is the top right. (0.5,0.5) is the very center of the sprite, which is the default.
Basically you just multiple the value by the width to get the relative position.
If you want to place a sprite at the very bottom of the screen (the bottom left corner aligned with the bottom left corner of the screen), you can do it multiple ways, based on the anchorPoint alone.
With the default anchorPoint of (0.5,0.5), the position would be (sprite.contentSize.width/2, sprite.contentSize.height/2).
If you set the anchorPoint to (0,0), the same position is obtained by simply (0,0).
If you wanted to move that sprite to the very center of the screen (the center of the sprite right in the middle), with an anchorpoint of (0.5, 0.5), the position would be (s.width/2, s.height/2).
This is all assuming you are adding a sprite to a parent the size of the screen, which is where the 2nd part of positioning comes in.
Position is also relative to the sprite's parent - which could be any other CCNode (CCLayer, another CCSprite, etc).
The way to think of that is not much different than adding a full screen node - except think in terms of the parent's size and position, not the screen.
Also Just to add something, all buttons start out in the middle of the screen then you can move them from there. if you wanted to button at (0,0):
CCLabelTTF *label1 = [CCLabelTTF labelWithString:#"Press Me!" fontName:#"Marker Felt" fontSize:20];
CCMenuItemLabel *button1 = [CCMenuItemLabel itemWithLabel:label1 block:^(id sender) { NSLog(#"button1 pressed"); }];
button1.position = ccp(-(s.width/2) , -(s.height/2)); // <---

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

Get relative position of mouse in a window in x11

I need to get the relative position of mouse pointer in the window in which it is clicked, i've coded to capture the click event, but when i use this code
result = XQueryPointer(disp, XRootWindow(disp,0),&win, &win, &root_x, &root_y, &win_x, &win_y, &mask_return);
it shows the global position of mouse when printing win_x and win_y
only window i have is "win" the display is "disp"
Found out evt.xbutton.x and evt.xbutton.y returns window relative mouse co ordinates