简体   繁体   中英

What is powerful way to force a form to bring front?

什么是强制形式使用Windows c#应用程序将所有其他应用程序置于最前面的有效方法?

强制用户单击任务栏中的应用程序窗口图标。

将Form.TopMost设置为true

Here is the code that worked for me:

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();

对我有用。

Some more information about this that may be useful. I used Charles Jenkins' answer to make my winforms app become topmost by calling the "MakeTopMost" function in the form.load event handler. I needed this function as my winforms app was launched by an MSI install and the Windows installer progress bar would show on top.

However, this then put the main form on top of other sub-forms that the main form wanted to show. To get around this I called my function MakeWindowNormal in the form.shown event handler to put the main form back as a normal window as it was now in front of the Windows installer progress bar as it had been loaded and activated (see http://msdn.microsoft.com/en-us/library/86faxx0d(v=vs.110).aspx for event order). This allows the sub-forms (and other windows if the user moves them) to now go in front of the main form.

static public void MakeWindowNormal(Form f)
{
    SetWindowPos(f.Handle, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
}

使用SetWindowsPos()API函数

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM