【问题标题】:Bring to Front event in Winform在 Winform 中将事件置于最前面
【发布时间】:2014-06-21 11:17:17
【问题描述】:

我有一个包含 3 个附加表格的应用程序。我已将其全部设置为 Top Most,因此每个表单看起来都像是父表单的扩展。现在用户不希望它位于顶部。当我将 TopMost 设置为 false 时,表单似乎是分开的。如果这些表单中的任何一个位于顶部,我想将所有表单放在前面(通过单击、单击任务栏图标,甚至使用 ALT TAB)。我认为,如果有一个提前事件,那将解决我的问题。

【问题讨论】:

  • 我认为您完全滥用TopMost,您不需要将它们设置为最顶部,只需设置对话框父项
  • TopMost 属性被滥用太多。它当然不能解决您所抱怨的问题,它不能解决 Z 排序问题。您正在寻找的是一个 owned 窗口。它总是在它的所有者之上,它与它的所有者一起最小化。您已经很了解它们了,Visual Studio 中的各种工具窗口都是自有窗口。您可以通过使用 Show(owner) 重载来创建一个拥有的窗口来显示它。或者通过显式设置 Owner 属性。
  • 感谢朋友的建议……现在,我明白了……

标签: c# winforms


【解决方案1】:

尝试在每个子窗体的 ctor 中设置父级:

ChildForm frm = new ChildForm(parentForm);

尝试使用 Win32 函数破解前台行为:

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

【讨论】:

    【解决方案2】:

    在一个Windows WinForm应用程序中,当用户将一个窗体带到屏幕的最顶层时,我们可以使同一应用程序中的其他窗体自动来到最顶层。这可以通过设置Winform“所有者”属性来实现。下面的例子证明了这一点:

    假设我们有一个主窗体和另外两个窗体

    mainForm otherForm1 otherForm2

    在程序代码中,设置 Owner 属性

    otherForm1.Owner = mainForm;

    otherForm2.Owner = mainForm;

    现在,每当mainForm 被带到前面时,otherForm1 otherForm2 就会跳到前面。此外,您还可以考虑以下几点:

    otherForm1.ShowInTaskBar = False;

    otherForm2.ShowInTaskBar = False;

    三个表单的组在任务栏上共享一个图标。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-02-06
      • 2023-02-17
      • 2010-09-14
      • 2011-05-19
      • 1970-01-01
      • 1970-01-01
      • 2014-09-10
      相关资源
      最近更新 更多