什么是强大的方式来强制窗体带来所有其他应用程序使用Windows C#应用程序的前面?
强烈强制用户点击任务栏中的应用程序窗口图标。
将Form.TopMost设置为true
这是我工作的代码:
using System; using System.Runtime.InteropServices; using System.Windows.Forms; namespace LicenseManager { public static class WinApi { [DllImport( "user32.dll" )] static extern bool SetWindowPos( IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags ); static private IntPtr HWND_TOPMOST = new IntPtr( -1 ); private const uint SWP_NOSIZE = 0x0001; private const uint SWP_NOMOVE = 0x0002; static public void MakeTopMost( Form f ) { SetWindowPos( f.Handle, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE ); } } }
this.BringToFront();
这对我有好处。
使用SetWindowsPos()api函数
关于这一点的更多信息可能是有用的。 我使用Charles Jenkins的答案,通过在form.load事件处理程序中调用“MakeTopMost”函数,使我的winforms应用程序成为最顶层。 我需要这个功能,因为我的winforms应用程序是由MSI安装启动的,Windows安装程序进度条会显示在最上面。
然而,这样就把主要形式放在主要形式想显示的其他子形式之上。 为了解决这个问题,我在form.shown事件处理函数中调用了函数MakeWindowNormal,把主窗体放回到正常的窗口中,就像它已经加载并激活一样,它现在位于Windows安装程序进度条的前面(参见http:/ / /msdn.microsoft.com/en-us/library/86faxx0d(v=vs.110).aspx用于事件顺序)。 这允许子窗体(和其他窗口,如果用户移动它们)现在走在主窗体的前面。
static public void MakeWindowNormal(Form f) { SetWindowPos(f.Handle, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE); }