SendMessage无法使用InternetExplorer对象

时间:2022-02-11 07:29:02

I am trying to simulate mouse left click for InternetExplorer object, even when the IE object is a background window. The system function I am using is SendMessage. Below is relevant code.

我试图模拟鼠标左键单击InternetExplorer对象,即使IE对象是一个背景窗口。我正在使用的系统功能是SendMessage。以下是相关代码。

[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]
public static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);

InternetExplorer IE = new InternetExplorer();
IntPtr handle = (IntPtr) IE.HWND;

int x = 50;
int y = 50;
IntPtr lParam = (IntPtr)((y << 16) | x); // X and Y coordinates of the click
IntPtr wParam = IntPtr.Zero;

const uint downCode = 0x0201; 
const uint upCode = 0x202;
SendMessage(handle, downCode, wParam, lParam); // mousedown
SendMessage(handle, upCode, wParam, lParam); // mouseup

I know for sure that the position I specified will generate a new IE window upon left click. However, that doesn't happen using the code above. So, what am I missing here?

我确定我指定的位置会在左键单击时生成一个新的IE窗口。但是,使用上面的代码不会发生这种情况。那么,我在这里错过了什么?

update
The OS is Windows 7 Professional. The IDE is Visual Studio 2013 Pro.

更新操作系统是Windows 7 Professional。 IDE是Visual Studio 2013 Pro。

I also tried adding a manifest and specifying UIAccess="true" per this page. But it didn't work.

我还尝试添加清单并在此页面中指定UIAccess =“true”。但它没有用。

1 个解决方案

#1


You cannot simulate mouse events with postmessage/sendmessage, because the mouse up/down events are always send at the current cursor position. You could first set the mouse position, but that won't work when the window is in the background or minimized.

您无法使用postmessage / sendmessage模拟鼠标事件,因为鼠标向上/向下事件始终在当前光标位置发送。您可以先设置鼠标位置,但是当窗口在后台或最小化时,这将不起作用。

More information: here, here and here.

更多信息:这里,这里和这里。

#1


You cannot simulate mouse events with postmessage/sendmessage, because the mouse up/down events are always send at the current cursor position. You could first set the mouse position, but that won't work when the window is in the background or minimized.

您无法使用postmessage / sendmessage模拟鼠标事件,因为鼠标向上/向下事件始终在当前光标位置发送。您可以先设置鼠标位置,但是当窗口在后台或最小化时,这将不起作用。

More information: here, here and here.

更多信息:这里,这里和这里。