Hi,
Im trying to move mouse and click inside another window to automate a mundane task. Im testing it on a paint window.

I can send keyboard entires fine but cant seem to get the mouse working.

What am i doing wrong?

#include <windows.h>
#include <stdio.h>
int main()
{
	POINT pt;
	pt.x = 20;
	pt.y = 20;
	DWORD dw = MAKEWORD(pt.x, pt.y);


	HWND PaintWindow = FindWindow(NULL, "Untitled - Paint");
	if (PaintWindow != NULL)
	{
		ShowWindow(PaintWindow, SW_SHOWMAXIMIZED);
		SetForegroundWindow(PaintWindow);
		SendMessage(PaintWindow, WM_MOUSEMOVE, 0, dw);
		SendMessage(PaintWindow, WM_LBUTTONDOWN, MK_LBUTTON, dw);
		SendMessage(PaintWindow, WM_LBUTTONUP, MK_LBUTTON, dw);
	}	
	return 0;
}

Not entirely sure, but you're trying to communicate with a program that you don't fully understand, or know how it was made. For all you know, mspaint.exe may use a mouse hook to retreive clicks and movement, in which case this may not work.

So if possible, try using an alternative method such as mouse_event or SetCursorPos.

Here's a snippet I made which uses both of these. [link]

The problem could also be that you're trying to send messages to the main window itself, not the child windows, meaning that they never recieve the messages.

The problem could also be that you're trying to send messages to the main window itself, not the child windows, meaning that they never recieve the messages.

I thought this as well as with notepad you need to send to the edit control part of the window.

Also the reason for not using SetCursor/ mouse_event was that the window could be fullscreen or windowed and placed anywhere on the screen. I was trying to make it so that the mouse input was relative to the position inside the window instead of the screen and do it as cleanly as possible.

Im guessing the GetWindowRect(paint, &r); & GetClientRect(paint, &cr); might help me somewhere here? i'll google it some more.
Thanks for the reply :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.