我是C#的新手,仍然试图让我的头脑(在一些非常耐心的朋友的帮助下)。
我在将新窗体的TopMost
属性设置为true
时遇到问题。我有两个(几乎)相同的形式; 1工作正常,而工作正常。
两个表单都将TopMost
属性设置为true
。
我已经搜索了这个问题的答案,并找到了一个答案,建议将this.TopMost = true;
放在表单的加载事件中,但这不起作用。
我唯一可能改变或者可能没有影响的是Form1是在属性中设置.NET 4.5创建的,在创建Form2之前,我将其更改为.NET 3.5(客户端配置文件)。我已经尝试过将其更改回来,但它没有帮助。在我再次删除并创建Form2之前,有没有人有任何想法?
非常感谢提前。 (如果您需要更多信息,请告诉我们)
答案 0 :(得分:13)
TopMost是一个属性,用于确保一个窗口始终显示在应用程序中的所有其他窗口之上。微软的例子是一个查找和替换工具。
您发现的差异是通过使用ShowDialog将Form1创建为模式对话框。显示对话框确保必须先关闭表单,然后才能再次使用应用程序中的所有其他窗口。例如;使用表单获取用户数据以进入父表单数据库。
当您不介意用户是否已完成对话时,使用Show,例如允许您的用户有机会使用一些辅助功能(例如计时器,秒表)程序的功能。
在使用不同的.Net框架时,我能想到的唯一视觉差异是不同的窗口对话框,例如OpenFileDialog,已在整个框架中更新
答案 1 :(得分:6)
它可能对你有帮助;
frm.TopLevel = true;
frm.TopMost = true;
答案 2 :(得分:3)
微软的这个链接确认可能是Windows 7和Windows Server 2008 R2中的一个Bug我在Windows 7嵌入式系统中遇到过这个问题,所提供的补丁修复了这个问题所以请考虑一下:)
http://support.microsoft.com/kb/2587473/en-us
希望有所帮助!
答案 3 :(得分:1)
大锤方式做到了! 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);
答案 4 :(得分:-1)
我在解决方案中遇到了类似的问题。在使用重载的Show-function之后,它起作用了:
frm.TopLevel = true;
frm.TopMost = true;
frm.Show(this)
答案 5 :(得分:-1)
在已显示事件中添加以下代码:
this.TopMost = true;
this.Focus();
this.TopMost = true;