C# this.TopMost = true 不起作用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16862057/
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
this.TopMost = true not working?
提问by Kungfauxn00b
I'm very new to C# and still trying to get my head round it (with help of some very patient friends).
我对 C# 非常陌生,并且仍在努力理解它(在一些非常有耐心的朋友的帮助下)。
I have an issue with setting a new windows form's TopMostproperty to true. I have two (almost) identical forms; 1 which works OK and one which doesn't.
我在将新 Windows 窗体的TopMost属性设置为true. 我有两个(几乎)相同的形式;1 个工作正常,一个没有。
Both of the forms have the TopMostproperty set to true.
这两种形式的TopMost属性都设置为true。
- Form1 shows the window and when I try to click behind it, the form flashes a few times and issues a windows beep.
- Form2 also shows the form but when I click behind it, the form greys out (or loses focus) and I can click away on the main form.
- Form1 显示窗口,当我尝试在它后面单击时,表单闪烁几次并发出窗口哔声。
- Form2 也显示表单,但是当我在它后面单击时,表单变灰(或失去焦点),我可以单击主表单。
I've searched for an answer to this issue and found an answer which suggested putting this.TopMost = true;in the form's load event but that didn't work.
我已经搜索了这个问题的答案,并找到了一个建议放入this.TopMost = true;表单加载事件的答案,但这没有用。
The only thing I have changed which may or may not have had an effect is that Form1 was created with .NET 4.5 set in the properties and before creating Form2, I changed this to .NET 3.5 (client profile). I've tried changing it back but it hasn't helped. Before I delete and create Form2 again, does anyone have any ideas?
我更改的唯一可能有影响也可能没有影响的是,Form1 是使用在属性中设置的 .NET 4.5 创建的,在创建 Form2 之前,我将其更改为 .NET 3.5(客户端配置文件)。我试过把它改回来,但没有帮助。在我删除并再次创建 Form2 之前,有人有任何想法吗?
Many thanks in advance. (If you need any more information, please just let me know)
提前谢谢了。(如果您需要更多信息,请告诉我)
采纳答案by Sayse
TopMostis a property that is used to make sure one window is always shown above all others within an application. Microsofts example was a find and replace tool.
TopMost是一种属性,用于确保一个窗口始终显示在应用程序中的所有其他窗口之上。微软的例子是一个查找和替换工具。
The difference you are finding is that Form1 was created as a modal dialog through the use of ShowDialog. Show dialog makes sure that your form must be closed before all other windows in the application can be used again. For example; using a form to gain user data to enter into a parent forms database.
您发现的不同之处在于 Form1 是通过使用ShowDialog创建为模态对话框的。显示对话框确保您的表单必须关闭,然后才能再次使用应用程序中的所有其他窗口。例如; 使用表单获取用户数据以输入到父表单数据库中。
Showis used when you don't mind if your user has finished with their dialog or not, such as allowing your user the chance to use some utility (e.g timer, stopwatch) that will assist within the main function of a program.
如果您不介意您的用户是否完成了他们的对话,则使用Show,例如允许您的用户有机会使用某些实用程序(例如计时器、秒表)来协助程序的主要功能。
The only visual difference I can think of when using different .Net frameworks, is different windows dialogs such as the OpenFileDialog, that have been updated throughout the framework
在使用不同的 .Net 框架时,我能想到的唯一视觉差异是不同的 Windows 对话框,例如 OpenFileDialog,已在整个框架中更新
回答by Furkan Ekinci
It may help you;
它可能会帮助你;
frm.TopLevel = true;
frm.TopMost = true;
回答by Guillermo Gutiérrez
This link from Microsoft confirm that could be a Bug in Windows 7 and Windows Server 2008 R2 I've faced it in a Windows 7 Embedded system and the provided patch fix the problem so please consider to take a look :)
来自 Microsoft 的此链接确认这可能是 Windows 7 和 Windows Server 2008 R2 中的错误
http://support.microsoft.com/kb/2587473/en-us
http://support.microsoft.com/kb/2587473/en-us
Hope it help!
希望有帮助!
回答by Martin.Martinsson
The sledge hammer way to do it! Works 100%!
大锤的方法来做到这一点!100% 有效!
public static class User32
{
public const int SW_HIDE = 0;
public const int SW_SHOW = 5;
public const int SW_SHOWNORMAL = 1;
public const int SW_SHOWMAXIMIZED = 3;
public const int SW_RESTORE = 9;
[DllImport("user32.dll")]
public static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll")]
public static extern bool AllowSetForegroundWindow(uint dwProcessId);
[DllImport("user32.dll")]
public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
}
User32.AllowSetForegroundWindow((uint)Process.GetCurrentProcess().Id);
User32.SetForegroundWindow(Handle);
User32.ShowWindow(Handle, User32.SW_SHOWNORMAL);
回答by termigrator
I hat a similar problem in my solution. After using the overloaded Show-function it worked:
我在我的解决方案中遇到了类似的问题。使用重载的 Show-function 后,它起作用了:
frm.TopLevel = true;
frm.TopMost = true;
frm.Show(this)
回答by Melody
Add the following code in the Shownevent:
在Shown事件中添加以下代码:
this.TopMost = true;
this.Focus();
this.TopMost = true;

