分享

Simulate Mouse Click To Non

 quasiceo 2015-07-24

3 Replies - 23507 Views - Last Post: 14 November 2010 - 02:09 PM Rate Topic: -----

#1 MathiasVP  Icon User is offline

  • D.I.C Head

Reputation: 27
  • View blog
  • Posts: 154
  • Joined: 08-August 10

Simulate mouse click to non-foreground window using SendMessage()?

Posted 14 November 2010 - 07:20 AM

Hello people!
I'm trying to simulate a mouseclick to a window that is currently not the foreground window, so that I'm able to interact with it, even though there's another window ontop of it.

I'm using the following hook procedure to intercept the actual mouseclick, and then send number of SendMessage():

LRESULT CALLBACK LowLevelMouseProc(int nCode, WPARAM wParam, LPARAM lParam)
{
	if(wParam == WM_LBUTTONDOWN)
	{
		MSLLHOOKSTRUCT* mouseInfo = (MSLLHOOKSTRUCT*)lParam;
		HWND h = GetForegroundWindow();
		DWORD dw, dw2;
		//Get mouse coords and store them as a DWORD for LPARAM
		SetWord(mouseInfo->pt.x, mouseInfo->pt.y, dw);

		SetCapture(h);
		SendMessage(h, WM_LBUTTONDOWN, MK_LBUTTON, dw);
		SendMessage(h, WM_LBUTTONUP, MK_LBUTTON, dw);
		ReleaseCapture();
		//Prevent the real mouseclick from being processed
		return 1;
	}

	return CallNextHookEx(NULL, nCode, wParam, lParam);
}


And yep I know that I'm actually attempting to send the mouse click to the foreground window in my code here, but this is just a test to keep it as simple as possible while figuring out the best method for solving my problem. I will later use some kind of EnumWindows().

My actual problem is that, even though the messages are being send (which I can see using spy++), the window is not responding to the messages. Am I forgetting some kind of message that I have to send before these?

Thanks in advance!

If it has anything to do with it, I'm installing my hook with the following line:

SetWindowsHookEx(WH_MOUSE_LL, LowLevelMouseProc, NULL, 0)



Edit:

My SetWord function looks like this:

template <typename T>
void SetWord(T Hi, T Low, DWORD &out)
{
	out = (Low & 0x0000FFFF) + (Hi << 16);
}


Which is working as it should, as I am getting the correct x and y coords!

This post has been edited by MathiasVP: 14 November 2010 - 07:25 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Simulate mouse click to non-foreground window using SendMessage()?

#2 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 938
  • View blog
  • Posts: 2,822
  • Joined: 20-March 10

Re: Simulate mouse click to non-foreground window using SendMessage()?

Posted 14 November 2010 - 01:26 PM

I dont think you can use SendMessage in this case.

You have to use PostMessage and you use it as such.

HWND h = (hwnd of window)
WORD mouseX = 10;// x coord of mouse
WORD mouseY = 10;// y coord of mouse
PostMessage(hWnd,WM_LBUTTONDOWN,0,MAKELPARAM(mouseX,mouseY));



Was This Post Helpful? 0
  • +
  • -

#3 MathiasVP  Icon User is offline

  • D.I.C Head

Reputation: 27
  • View blog
  • Posts: 154
  • Joined: 08-August 10

Re: Simulate mouse click to non-foreground window using SendMessage()?

Posted 14 November 2010 - 02:01 PM

Hey Snoopy11!

Your solution worked... ish

It's working for a few windows, like Google Chrome's tabs and the windows taskbar... But those are the only cases where it worked

I'm using the following code:

LRESULT CALLBACK LowLevelMouseProc(int nCode, WPARAM wParam, LPARAM lParam)
{
	if(wParam == WM_LBUTTONDOWN)
	{
		MSLLHOOKSTRUCT* mouseInfo = (MSLLHOOKSTRUCT*)lParam;
		HWND h = GetForegroundWindow();

		SetCapture(h);
		PostMessage(h, WM_LBUTTONDOWN, 0, MAKELPARAM(mouseInfo->pt.x, mouseInfo->pt.y));
		Sleep(5);
		PostMessage(h, WM_LBUTTONUP, 0, MAKELPARAM(mouseInfo->pt.x, mouseInfo->pt.y));
		ReleaseCapture();
		//Prevent the real mouseclick from being processed
		return 1;
	}

	return CallNextHookEx(NULL, nCode, wParam, lParam);
}



I have no idea why it's only working in some windows though :( Any ideas?
Was This Post Helpful? 0
  • +
  • -

#4 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 938
  • View blog
  • Posts: 2,822
  • Joined: 20-March 10

Re: Simulate mouse click to non-foreground window using SendMessage()?

Posted 14 November 2010 - 02:09 PM

Hmm I dont know why its only working for a few windows Mathias
it should work for all windows

The API only does marshalling for system messages (those in the range 0 to (WM_USER-1)). To send other messages (those >= WM_USER) to another process, you must do custom marshalling.

If you send a message in the range below WM_USER to the asynchronous message functions (PostMessage, SendNotifyMessage, and SendMessageCallback), its message parameters cannot include pointers. Otherwise, the operation will fail. The functions will return before the receiving thread has had a chance to process the message and the sender will free the memory before it is used.

try making your mouse coords WORD
see if that helps

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多