how to set cursor position out off the application window? (c++) - c++

I'm creating an application using C++ to set the cursor position even when the mouse is out off the application window... the Win32 library has the SetCursorPos function which according to the documentation
A window should move the cursor only when the cursor is in the window's client area
however I want to move the cursor outside the window : EVEN when the window IS HIDDEN...
I want it to be more like a mouse driver : windows reads the cursor position from it...

it seems that although the documentation says :
A window should move the cursor only when the cursor is in the window's client area
it can control the mouse even when the window is HIDDEN

Related

Get control under mouse on GUI with WS_EX_TRANSPARENT style

I have a LayeredWindow GUI that contains some child's and all of them contains the WS_EX_TRANSPARENT style.
The style is used to be able to remove their background.
When i move the mouse over the GUI only the LayeredWindow receives the message WM_MOUSEMOVE.
I tried calling ChildWindowFromPointEx using the XY pos got from the WM_MOUSEMOVE lParam
to detect the control being hovered, but the API didn't recognize any of the controls belonging to the child GUI's.
Docs says:
The search is restricted to immediate child windows. Grandchildren and deeper descendants are not searched.
The other option i tried was EnumChildWindow and compare each control rect to the XY position of the message, this method is using around 1% of CPU only from moving the mouse.
I wonder if there's any 'better' option?
According to the Doc:Layered Windows
Hit testing of a layered window is based on the shape and transparency
of the window. This means that the areas of the window that are
color-keyed or whose alpha value is zero will let the mouse messages
through. However, if the layered window has the WS_EX_TRANSPARENT
extended window style, the shape of the layered window will be ignored
and the mouse events will be passed to other windows underneath the
layered window.
You could try to use GetCursorPos function to get the position of the mouse cursor, in screen coordinates.

How to check mouse click position is on required application?

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
}

Mouse click on a background window without moving mouse c++

I need to know if there is a code in c++ that allows to simulate a click, on a background window (if that's impossible, a foreground window will do fine), without moving the mouse.
I also need to click on specific coordinates, and drag an item to some other position (always without moving the mouse).
Example:
my mouse pointer is at (500,700),
but i need to left click at (100,150),
and drag to (700,300).
I need to be able to move my mouse pointer without affecting the program,
and the program must run correctly without moving, or locking, the mouse pointer.
If this action is impossible in c++, a VB code will be apreciated.
From a C++ Windows application, you can call Windows API directly by first finding the window using FindWindow that will give you the window handle you want, then finding the area within this window that you want to click. You can use API like GetWindowRect for this.
Finally, you can send this window, or an area within it, mouse messages using mouse_event function to cause the mouse to move, click, drag and let go at a new location.

Weird cursor behavioir in Qt

In Qt (C++, MinGW, Windows 7), QCursor::setPos(...) seems to change the position of the cursor at the application level but not at the system level. For example, if you intercept the mouseMoveEvent and cancel out the movement of the cursor (with setMouseTracking(false) such that you do this cancellation only when a mouse button is pressed), then you'll see no cursor move as long as you maintain the mouse button pressed. But then, when you release the button and move a little, the cursor will go to the place it would have been if you haven't blocked its movement. Is this behavior normal? How to get around it such that the mouse cursor is changed at the system level?
If it is not clear enough, I can give code snippet.
Moving the cursor of a QWidget always uses the widget's coordinates. For global mouse positioning, you should use the desktop widget (QDesktopWidget) instead. You can get it through the QApplication::desktop() static function. Example:
QApplication::desktop()->cursor().setPos(0,0);
This should move the mouse cursor to the top-left corner of the desktop.

MFC Unclickable Button (Running away from cursor on MouseMove)

How would i make a button that will change it's position on MouseMove Event if the cursor is close enough to the center of the button in MFC ?
WM_MOUSEMOVE is not delivered to the button if the cursor is not over it (and is not captured, but you don't want that). So you have to process WM_MOUSEMOVE in the parent dialog. If you want your button to be a self-contained control, you have to subclass the parent window upon button creation.
Subclassing, in this context, means:
- you retrieve and store the parent's window proc address with GetParent()->GetWindowLong(GWL_WNDPROC)
- you set it to your procedure with SetWindowLong()
- in the procedure, you call the parent's previous window proc, after handling WM_MOUSEMOVE the way you want.
The WM_MOUSEMOVE coordinates will be relative to the screen, but you'll probably want to track the button position relative to the window that contains it. Use the ScreenToClient method on the parent window to convert, then you can compare the coordinates to see if it's close. Then use MoveWindow to move the button.
If you track the mouse cursor position you can determine when the cursor gets close to or enters the button window rect. You can then use the SetWindowPos() function to reposition the button window in the parent window client area.