Convert screen mouse coordinate to window coordinate - c++

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.

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.

Get absolute mouse click location on widget (also when zoomed in) Qt

I want to trigger where I click with the mouse on my QGraphicsView.
Problem here: this QGraphicsView is always zoomed in. But I want to get the point relative to the whole widget and not only to the viewport.
Or in other words: when I zoomed in and click on the upper left corner, the location should NOT be 0,0 (the QMousePressEvent just gives me this point). It should be the distance from there to the upper left corner of the whole sceneRectangle (or to the middle, doesn't matter).
Is there any way to do this?
Thanks for answers.
You can do what you want with Qt's mapToScene function.

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

Resize By Mouse

I have a rectangle class that has 2 points, the center axes point and the size of the rectangle. Lets say I want to drag the bottom of the rectangle with the mouse but keep the top of it in the same position. What is the algorithm to find the center position and the new rectangle size based on the mouse? Thanks in advance :)
Move the center in the same direction and half the distance (in either or both X and Y) as the bottom (right-hand corner) was dragged.
I would assume a graphics API is at hand here, what is it? I also assume that you have worked out how to detect that mouse clicking onto the edge of your box, have you decided exactly how that works though? do they just need to click near it, and they then drag the exact corner or what?
I can tell you that you are going to need to log the position of the mouse when they first click and get the differance to where they are now. Half that distance, and then add it to the original centre.
EDIT
oh, for the new size, its the difference of the mouse position added onto the original size. so if the mouse has moved down (increasing y) and left (decreasing x) say 10 units each way, you make the box ten units taller and ten narrower, the centre will be 5 down and 5 left.
It would be easier to tell the difference in the mouse movement, and create a new rectangle that size. Then use the rectangle class to check for the center point. Far easier than offsetting the old center.

Finding current mouse position in QT

This is my first attempt at writing a QT app, and I'm just trying to get a feel for how it works. My goal is to have a 400x400 widget which knows the exact position of the mouse when the mouse is hovering over it. For example, if the mouse was hovering in the top left corner, the position might be 10,10 (or something similar). If the mouse is in the bottom right corner, it might say 390,390.
Eventually, these coordinates will be displayed in a label on the main window, but that should be trivial. I'm stuck at the actual fetching of the coordinates. Any ideas?
For your widget, you must enable mouse tracking.
Then, you can either install an event filter, paying attention to mouse events and looking for the move event, or you can inherit from QWidget and override the mouse event, looking for mouse move events.
http://doc.qt.io/qt-4.8/qwidget.html#mouseTracking-prop
http://doc.qt.io/qt-4.8/eventsandfilters.html
http://doc.qt.io/qt-4.8/qmouseevent.html
If you are ever in a situation when you don't need actual tracking, just position at the moment, you can use QCursor::pos().