windows 什么是强制表单放在前面的强大方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1309855/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is powerful way to force a form to bring front?
提问by Anuya
What is powerful way to force a form to bring front of all the other applications using windows c# application?
什么是强制表单使用 windows c# 应用程序将所有其他应用程序置于前面的强大方法?
采纳答案by Anuya
Use SetWindowsPos() api function
使用 SetWindowsPos() api 函数
回答by 1800 INFORMATION
Powerfully force the user to click on your application window icon in the task-bar.
强有力地强制用户单击任务栏中的应用程序窗口图标。
回答by RRUZ
Set Form.TopMost to true
将 Form.TopMost 设置为 true
回答by Charles Jenkins
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 );
}
}
}
回答by jay_t55
this.BringToFront();
this.BringToFront();
It works good for me.
它对我很有用。
回答by NJA
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.
一些可能有用的关于此的更多信息。我使用 Charles Jenkins 的回答通过在 form.load 事件处理程序中调用“MakeTopMost”函数使我的 winforms 应用程序成为最顶层。我需要这个功能,因为我的 winforms 应用程序是通过 MSI 安装启动的,并且 Windows 安装程序进度条会显示在顶部。
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).aspxfor event order). This allows the sub-forms (and other windows if the user moves them) to now go in front of the main form.
但是,这会将主窗体置于主窗体想要显示的其他子窗体之上。为了解决这个问题,我在 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);
}