Reputation: 73
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?
Upvotes: 0
Views: 341
Reputation: 73
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.
Upvotes: 1
Reputation: 1062725
"I have to make the browser window(IE 11) that opens my web page to be always on foreground until minimizes it." - this is actively hostile... whoever came up with that requirement hates users;
It sounds like what you're actually after is "kiosk mode"; you can do that very easily simply by making whatever shortcut they're using:
iexplore -k {url}
Upvotes: 0
Reputation: 11335
Generally, When you launch the IE, It will get launch above all windows. Which OS you are using for testing?
In JavaScript, You can refer code below to explicitly do it.
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()}
}
If you are using VBA or any other script then you can first hide the browser window and again make it visible will bring the IE window in foreground.
Upvotes: 1