除非我将其最小化,否则如何使我在 IE 11 浏览器中打开的网页始终处于前台?

How to make my webpage opened in IE 11 browser to be always on foreground unless I minimize it?

我必须让打开我的网页的浏览器 window(IE 11) 始终处于前台,直到将其最小化。 写了一个C#activex dll,注册了它。 activex dll 实现了一个简单的 'Hello world' 打印。代码看起来像。

namespace SampleActX
{

    [ProgId("SampleActX.SampleActX")]
    [ClassInterface(ClassInterfaceType.AutoDual)]
    [Guid("7F6A5914-9C8A-4977-AF5B-DE9D45E01B44")]
    [ComVisible(true)]

    public class SampleActX
    {
        [ComVisible(true)]
        public string SayHello()
        {
            return "Hello World!";
        }


    }
}

将 dll 嵌入到 html 中,如下所示。

<!DOCTYPE>
<html>
   <head>
          <title>SampleActX webpage</title>
   </head>
   <body>
       <OBJECT id="SampleActX" classid="clsid:7F6A5914-9C8A-4977-AF5B-DE9D45E01B44" codebase="SampleActX.cab"></OBJECT>  
        <script type="text/javascript">
            try {
                var obj = document.SampleActX;
                if (obj) {
                    alert(obj.SayHello());
                } else {
                    alert("Object is not created!");
                }
            } catch (ex) {
                alert("Some error happens, error message is: " + ex.Description);
            }      
        </script>
   </body>
</html>

我们如何从 activex dll 控制 IE 父 window 使其始终在前台(例如任务管理器 window 的工作方式)?

通常,当您启动 IE 时,它会首先启动 windows。您正在使用哪个 OS 进行测试?

在JavaScript中,您可以参考下面的代码来明确地做到这一点。

function sample_window() {
 newwindow = window.open('https://microsoft.com', 'popup_return',     'menubar=no,history=no,resizable=yes,scrollbars=yes,toolbar=no,width=800,height=600');
if (window.focus) {newwindow.focus()}
   }

如果您正在使用 VBA 或任何其他脚本,那么您可以先隐藏浏览器 window,然后再次使其可见,这样 IE window 就会出现在前台。

"I have to make the browser window(IE 11) that opens my web page to be always on foreground until minimizes it." - 这是充满敌意的...提出该要求的人讨厌用户;

听起来你真正想要的是"kiosk mode";您只需创建他们正在使用的任何快捷方式即可非常轻松地做到这一点:

iexplore -k {url}

尝试了下面的代码,它工作正常。

命名空间 SampleActX {

[ProgId("SampleActX.SampleActX")]
[ClassInterface(ClassInterfaceType.AutoDual)]
[Guid("7F6A5914-9C8A-4977-AF5B-DE9D45E01B44")]
[DllImport("user32.dll", SetLastError = true)]
static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);
[ComVisible(true)]

public class SampleActX
{
    [ComVisible(true)]
    public string SayHello()
    {
        return "Hello World!";
    }
    public void SetIEWindowOnTop()
    {
        var myId = Process.GetCurrentProcess().Id;//Fetches the pid of the current tab in IE only

        var query = string.Format("SELECT ParentProcessId FROM Win32_Process WHERE ProcessId = {0}", myId); //Finding the actual IE window handle
        var search = new ManagementObjectSearcher("root\CIMV2", query);
        var results = search.Get().GetEnumerator();
        results.MoveNext();
        var queryObj = results.Current;
        var parentId = (uint)queryObj["ParentProcessId"];
        var parent = Process.GetProcessById((int)parentId);

        IntPtr windoHandle = parent.MainWindowHandle;//Fetches the parent window handle

        var bresu = SetWindowPos(windoHandle,   //Sets the window on Top
                    HWND_TOPMOST,
                    0, 0, 0, 0,
                   SWP_NOSIZE|SWP_SHOWWINDOW);
    }

}

}

如问题中所述,从 html 调用 SetIEWindowOnTop() 会产生神奇效果。