简体   繁体   中英

Open/Change IE9 Tabs Using SendMessage/PostMessage

I'm trying basically SendKey's to IE9 to change tabs. I have 3 tabs so I'd need to Send keys Ctrl+1, Ctrl+2, Ctrl+3 and also Ctrl+T to open a new tab.

I start by adding the import dlls and constants

[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;

I get the instance of Internet Explorer by opening a new process.

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

Use the process handle to PostMessage to IE9 instance

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);

No response. I've also tried using SendMessage to no avail as well.

Am I doing anything obviously wrong?

How about SendKeys("^1");

as seen here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM