简体   繁体   English

在 Winform 中带到前台事件

[英]Bring to Front event in Winform

I have an application that has 3 attached forms.我有一个附有 3 个表格的应用程序。 I have set it all Top Most, so every form will look like the extension of parent form.我把它都设置为 Top Most,所以每个表单看起来都像父表单的扩展。 Now users doensn't want it to be on top.现在用户不希望它位于顶部。 When I set the TopMost to false, the forms seems to be separated.当我将 TopMost 设置为 false 时,表单似乎是分开的。 I want to bring the all the forms to front, if any of these forms come on top (by clicking, clicking on the taskbar icon, or even using ALT TAB).如果这些表单中的任何一个位于顶部(通过单击、单击任务栏图标,甚至使用 ALT TAB),我想将所有表单放在最前面。 I think, if there was a bring to front event, that will solve my issue.我认为,如果有一个前置事件,那将解决我的问题。

For those who needs quick and accurate response, plesae try this link from which I got my answer:对于那些需要快速准确响应的人,请尝试我得到答案的链接:

which events does BringToFront() method trigger? BringToFront() 方法会触发哪些事件?

The Winform "Activated" event is fired when a form is brought to top-most of the GUI.当窗体被带到 GUI 的最顶部时,会触发 Winform "Activated" 事件。

Try setting the parent in the ctor of every child form:尝试在每个子窗体的 ctor 中设置父级:

ChildForm frm = new ChildForm(parentForm);

Try to use Win32-functions to hack the foreground behavior:尝试使用 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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM