【问题标题】:Give focus to Internet Explorer object created through C#关注通过 C# 创建的 Internet Explorer 对象
【发布时间】:2012-03-25 03:45:39
【问题描述】:

我在通过 C# 控制 IE 时遇到了一些问题。 我几乎把所有事情都搞定了。 但我似乎无法将焦点设置在打开的资源管理器上。 当我在 VS 2010 中时,它可以工作,但是当我直接运行 exe 文件时,情况并非如此。

using SHDocVw;
.

<code>

.
InternetExplorer ie = new InternetExplorer();
IWebBrowserApp wb = (IWebBrowserApp)ie;
.

<code>

.   
wb.Visible = true;
wb.Document.focus();

我的意思是wb.Document.focus(); 会将焦点放在 IE 上,但这不起作用。 也试过eb.Document.focus(); 有人有什么建议吗?

【问题讨论】:

    标签: c# embed


    【解决方案1】:

    将 ie.Document 转换为 mshtml.HTMLDocument 然后设置焦点。

    //Like so
    InternetExplorer ie = new InternetExplorer();
    ((mshtml.HTMLDocument)ie.Document).focus();
    

    【讨论】:

      【解决方案2】:

      这不可靠。可靠的解决方案是使用 API(我的发现:-)):

      vb:

      Private Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdSHow As Long) As Long
      
      Dim ie As SHDocVw.InternetExplorerClass
      
      ie = New SHDocVw.InternetExplorer : Application.DoEvents()
      
      
      
      'the reliable focus:
      
      ShowWindow(ie.HWND, 0) : ShowWindow(ie.HWND, 1)
      

      【讨论】:

      • 看起来问题是在寻找 C#。您可能应该更新您的答案并添加code formatting
      猜你喜欢
      • 1970-01-01
      • 2014-04-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-19
      相关资源
      最近更新 更多