.net c#动态IE不呈现给前端可见

本文关键字:前端 动态 IE net | 更新日期: 2023-09-27 18:17:21

 dynamic ie =Activator.CreateInstance(Type.GetTypeFromProgID("InternetExplorer.Application"));
ie.AddressBar = false;
ie.MenuBar = false;
ie.ToolBar = false;
ie.Visible = true;
ie.Navigate("www.google.com");

我使用了那里提供的代码,但无法让它在打开时出现在前面。

是否有一种简单的方法可以使它始终在顶部或只是放在前面…

最好是第一个总是在上面。

谢谢

.net c#动态IE不呈现给前端可见

尝试使用SetForegroundWindow API:

[DllImport("User32.dll")]
public static extern Int32 SetForegroundWindow(int hWnd);  

然后像这样使用:

SetForegroundWindow(ie.Hwnd);

使用user32 API中的SetWindowPos:

static readonly IntPtr HWND_TOPMOST = new IntPtr(-1);//-2 would make it not topmost
const UInt32 SWP_SHOWWINDOW = 0x0040;
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter,
                                           int x, int y, int cx, int cy, uint uFlags);

然后在你的代码中:

dynamic ie = Activator.CreateInstance(Type.GetTypeFromProgID("InternetExplorer.Application"));
ie.AddressBar = false;
ie.MenuBar = false;
ie.ToolBar = false;
ie.Visible = true;
ie.Navigate2("www.google.com");
SetWindowPos((IntPtr)ie.HWND, HWND_TOPMOST, 0, 0, 1280, 700, SWP_SHOWWINDOW);