Search code examples
c#cominternet-explorer-11activex

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


I have to make the browser window(IE 11) that opens my web page to be always on foreground until minimizes it. Wrote a C# activex dll, registered it. The activex dll implements a simple 'Hello world' printing. The code looks like.

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!";
        }


    }
}

Embedded the dll inside the html like below.

<!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>

How can we control the IE parent window to make it always on foreground(like how the task manager window is working) from the activex dll?


Solution

  • Tried the below code and it worked fine.

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

    }

    Calling the SetIEWindowOnTop() from html as explained in the question will do the magic.