使用 SendMessage() C# 在 IE8 中打开选项卡

发布于 2024-10-30 05:20:14 字数 1661 浏览 5 评论 0原文

在下面的代码中,hWnd 是 Spy++ 中的“URL 句柄”:

'WorkerW -> ReBarWindow32 -> Address Band Root -> Edit' 

URL 是我要打开的内容。

我使用相同的方法在 IE7 中打开选项卡并相应地发送 hWnd 。我发现这适用于 IE7,但不适用于 IE8。在 IE8 中,它只打开 4 个选项卡,然后 IE8 停止接受 SendMessage 请求;但是,我仍然可以按 CTRL+TALT+Enter 在 IE8 中打开新选项卡(因此 IE8 仍然具有响应能力)。

/**
 * Open URL in IE (open new tab when newTab is true)
 * hWnd is found at runtime 
 **/
private void LaunchURLinIE(IntPtr hWnd, String url, bool newTab = false)
{
    StringBuilder ob = new StringBuilder(url);
    // Type text in the URL window
    SendMessage(hWnd, WM_SETTEXT, 0, ob);
    if (!newTab)
    {   // Press Enter
        SendMessage(hWnd, WM_KEYDOWN, VK_RETURN, 1);
    }
    else
    {   // Press ALT Enter to open new tab
        SendMessage(hWnd, WM_SYSKEYDOWN, VK_RETURN, 1 << 29);
    }
}

我的环境是:Windows XP Service Pack 3 [32位操作系统],IE8版本8.0.6001.18702

那么,是IE8还是我缺少的东西?

更新 - 1 我更新了代码的注释,以便清楚地了解代码的作用。上面的代码在 IE7 上工作得很好(测试了最多 15 个选项卡),但在 IE8 上它最多只能打开 4 个选项卡。

更新 - 2 我可以通过使用 PostMessage 而不是 SendMessage 来解决这个问题。

   private void LaunchURLinIE(IntPtr hWnd, String url, bool newTab = false)
    {
        StringBuilder ob = new StringBuilder(url);
        // Type text in the URL window
        SendMessage(hWnd, WM_SETTEXT, 0, ob);
        if (!newTab)
        {   // Press Enter
            PostMessage(hWnd, WM_KEYDOWN, VK_RETURN, 1);
        }
        else
        {   // Press ALT Enter to open new tab
            PostMessage(hWnd, WM_SYSKEYDOWN, VK_RETURN, 1 

In this below code, hWnd is the "URL Handle" in Spy++:

'WorkerW -> ReBarWindow32 -> Address Band Root -> Edit' 

The URL is what I want to open.

I use the same method to open tabs in IE7 and send hWnd appropriately. I see that this works fine for IE7 and not for IE8. In IE8, it only opens 4 tabs and then IE8 stops honoring the SendMessage request; however, I can still press CTRL+T OR ALT+Enter to open new tabs in IE8 (so IE8 is still responsive).

/**
 * Open URL in IE (open new tab when newTab is true)
 * hWnd is found at runtime 
 **/
private void LaunchURLinIE(IntPtr hWnd, String url, bool newTab = false)
{
    StringBuilder ob = new StringBuilder(url);
    // Type text in the URL window
    SendMessage(hWnd, WM_SETTEXT, 0, ob);
    if (!newTab)
    {   // Press Enter
        SendMessage(hWnd, WM_KEYDOWN, VK_RETURN, 1);
    }
    else
    {   // Press ALT Enter to open new tab
        SendMessage(hWnd, WM_SYSKEYDOWN, VK_RETURN, 1 << 29);
    }
}

My environment is: Windows XP Service Pack 3 [32-bit OS] , IE8 version 8.0.6001.18702

So, is it IE8 or something I am missing?

UPDATE - 1
I have updated comments on the code so that its clear what code does. The above code works perfectly fine for IE7 (tested upto 15 tabs) but with IE8 it only opens upto 4 tabs.

Update - 2
I was able to sove this by using PostMessage instead of SendMessage.

   private void LaunchURLinIE(IntPtr hWnd, String url, bool newTab = false)
    {
        StringBuilder ob = new StringBuilder(url);
        // Type text in the URL window
        SendMessage(hWnd, WM_SETTEXT, 0, ob);
        if (!newTab)
        {   // Press Enter
            PostMessage(hWnd, WM_KEYDOWN, VK_RETURN, 1);
        }
        else
        {   // Press ALT Enter to open new tab
            PostMessage(hWnd, WM_SYSKEYDOWN, VK_RETURN, 1 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不羁少年 2024-11-06 05:20:14

您可能想尝试使用 ShDocVw 对象公开的 COM,该对象可以作为名为 Interop.ShDocVw.dll 的 .dll 找到,它包含一个 InternetExplorerClass 接口,允许您可靠地执行大多数 IE 自动化。

我是 SWAT 的贡献者 http://sourceforge.net/projects/ulti-swat/ ,如果您想要一些好的示例,请随意使用我们位于 SWAT.Core/InternetExplorer.cs 中的 IE 自动化源。

实际上,您的代码存在很多问题,窗口句柄是硬编码的(为什么?)而且您只发送一个键按下时,应用程序通常需要一个 keydown/keyup 组合,有时需要一个 keydown/keypress/keyup 序列才能使其有效。您的应用程序看起来好像您按住了要发送的按键。您可能需要使用 FindWindow 窗口 API 调用,它可以帮助您在运行时查找要向其发送消息的窗口的窗口句柄。

You might want to try using the COM exposed by the ShDocVw object which can be found as a .dll named Interop.ShDocVw.dll this contains an InternetExplorerClass interface which allows you to do most IE automation reliably.

I am a contributor to SWAT http://sourceforge.net/projects/ulti-swat/, if you would like some good examples feel free to use our source for IE automation located in SWAT.Core/InternetExplorer.cs

There are alot of problems with your code actually, window handles are hardcoded (why?) also you are only sending a key down, the application usually expects a keydown/keyup combo or sometimes a keydown/keypress/keyup sequence in order for it to be valid. Your application makes it seem as if you are holding down the keys you are sending. You may want to use the FindWindow windows API call which can help you find the window handle at runtime for the window you want to send the messages to.

居里长安 2024-11-06 05:20:14

您不应该也发送 WM_KEYUP 吗?

VK_返回?不知道它有什么作用。发送 CTRL+T 击键会更好吗?

  • KEYDOWN CTRL
  • KEYDOWN T
  • KEYUP T
  • KEYUP CTRL

您可以使用 KeyInterop.VirtualKeyFromKey 查找按键代码。

我还认为你需要使用 PostMessage:

[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("user32.dll", SetLastError = true)]
private static extern bool PostMessage(IntPtr hWnd, uint Msg, int wParam, int lParam);

private const UInt32 WM_KEYDOWN = 0x0100;
private const UInt32 WM_KEYUP = 0x0101;

public static void IENewTab(IntPtr HWnd)
{
    PostMessage(HWnd, WM_KEYDOWN, KeyInterop.VirtualKeyFromKey(System.Windows.Input.Key.LeftCtrl), 0);
    PostMessage(HWnd, WM_KEYDOWN, KeyInterop.VirtualKeyFromKey(System.Windows.Input.Key.T), 0);
    PostMessage(HWnd, WM_KEYUP, KeyInterop.VirtualKeyFromKey(System.Windows.Input.Key.T), 0);
    PostMessage(HWnd, WM_KEYUP, KeyInterop.VirtualKeyFromKey(System.Windows.Input.Key.LeftCtrl), 0);
}

Should you not send WM_KEYUP as well?

VK_RETURN? Not sure what it does. Would it be better to send CTRL+T keystrokes instead?

  • KEYDOWN CTRL
  • KEYDOWN T
  • KEYUP T
  • KEYUP CTRL

You can find key codes with KeyInterop.VirtualKeyFromKey.

I also think you need to use PostMessage:

[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("user32.dll", SetLastError = true)]
private static extern bool PostMessage(IntPtr hWnd, uint Msg, int wParam, int lParam);

private const UInt32 WM_KEYDOWN = 0x0100;
private const UInt32 WM_KEYUP = 0x0101;

public static void IENewTab(IntPtr HWnd)
{
    PostMessage(HWnd, WM_KEYDOWN, KeyInterop.VirtualKeyFromKey(System.Windows.Input.Key.LeftCtrl), 0);
    PostMessage(HWnd, WM_KEYDOWN, KeyInterop.VirtualKeyFromKey(System.Windows.Input.Key.T), 0);
    PostMessage(HWnd, WM_KEYUP, KeyInterop.VirtualKeyFromKey(System.Windows.Input.Key.T), 0);
    PostMessage(HWnd, WM_KEYUP, KeyInterop.VirtualKeyFromKey(System.Windows.Input.Key.LeftCtrl), 0);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文