English 中文(简体)
窗体.置顶有时候有效。
原标题:Form.TopMost works sometimes

似乎最高属性有时会使我的应用程序在其他所有应用程序之上,但在我的测试过程中,它非常奇怪,有时它可以正常工作,窗口保持在所有其他(外部应用程序)窗口之上,但有时它根本不起作用。我甚至尝试使用WS_EX_TOPMOST标志,通过使用Win32 API调用setWindowLong()来设置它。没有一个能够保持窗口在最上层。除了使用最高层外,是否还有其他方法可以将窗口保持在所有打开窗口的最上层?或者关于最高层还有什么我应该知道的事情吗?

问题回答

我只是使用这个:

form.TopLevel = true;
form.TopMost  = true;

这使得窗口成为顶层窗口(即它没有父窗口并且行为像应用程序的主窗体),然后确保它是最顶层的(即在所有非最顶层的窗口之上显示)。它总是能像魔术一样奏效。

请注意,在显示窗口之前我会这样做。

我运用了以下Win32 API调用,取得了好运:

const int SW_SHOW = 5;
BringWindowToTop(form.Handle);
ShowWindow(form.Handle, SW_SHOW);

除了 Form.TopMost,你可以尝试使用 Win32 API SetForegroundWindow

[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("user32", CharSet=CharSet.Ansi, SetLastError=true, ExactSpelling=true)]
public static extern bool SetForegroundWindow(IntPtr hwnd);

然后调用SetForegroundWindow(this.Handle)

工作100%!

User32.AllowSetForegroundWindow((uint)Process.GetCurrentProcess().Id);
User32.SetForegroundWindow(Handle);
User32.ShowWindow(Handle, User32.SW_SHOWNORMAL);




相关问题
Bring window to foreground after Mutex fails

I was wondering if someone can tell me what would be the best way to bring my application to the foreground if a mutex was not able to be created for a new instance. E.g.: Application X is running ...

How to start WinForm app minimized to tray?

I ve successfully created an app that minimizes to the tray using a NotifyIcon. When the form is manually closed it is successfully hidden from the desktop, taskbar, and alt-tab. The problem occurs ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

Handle DataTable.DataRow cell change event

I have a DataTable that has several DataColumns and DataRow. Now i would like to handle an event when cell of this DataRow is changed. How to do this in c#?

Apparent Memory Leak in DataGridView

How do you force a DataGridView to release its reference to a bound DataSet? We have a rather large dataset being displayed in a DataGridView and noticed that resources were not being freed after the ...

ALT Key Shortcuts Hidden

I am using VS2008 and creating forms. By default, the underscore of the character in a textbox when using an ampersand is not shown when I run the application. ex. "&Goto Here" is not ...

WPF-XAML window in Winforms Application

I have a Winforms application coded in VS C# 2008 and want to insert a WPF window into the window pane of Winforms application. Could you explain me how this is done.

热门标签