使用SendMessage / PostMessage打开/更改IE9选项卡

时间:2012-04-23 19:29:46

标签: c# tabs internet-explorer-9 sendmessage postmessage

我正在尝试将SendKey发送到IE9以更改标签。我有3个标签,所以我需要发送按键Ctrl + 1,Ctrl + 2,Ctrl + 3以及Ctrl + T来打开新标签。

首先添加导入dll和常量

[DllImport("user32.dll")]
public static extern IntPtr SendMessage(IntPtr hWnd, uint Msg,
 IntPtr wParam, IntPtr lParam);
[DllImport("user32.dll")]
public static extern IntPtr PostMessage(IntPtr hWnd, uint Msg,
 IntPtr wParam, IntPtr lParam);
const int WM_KEYDOWN = 0x100;
const int WM_KEYUP = 0x101;

我通过打开一个新进程来获取Internet Explorer的实例。

Process p = Process.Start("iexplorer.exe");

使用PostMessage到IE9实例的进程句柄

IntPtr handle = p.MainWindowHandle; //p.Handle (doesn't work either)
//Change to Tab2 using PostMessage

PostMessage(handle, WM_KEYDOWN, ((IntPtr)Keys.LControlKey), (IntPtr)0);
PostMessage(handle, WM_KEYDOWN, ((IntPtr)Keys.D2), (IntPtr)0);
PostMessage(handle, WM_KEYUP, ((IntPtr)Keys.D2), (IntPtr)0);
PostMessage(handle, WM_KEYUP, ((IntPtr)Keys.LControlKey), (IntPtr)0);

没有回应。我也试过使用SendMessage也无济于事。

我做了什么明显的错误吗?

1 个答案:

答案 0 :(得分:0)

SendKeys("^1");

怎么样?

here