简体   繁体   中英

Bring to Front event in Winform

I have an application that has 3 attached forms. I have set it all Top Most, so every form will look like the extension of parent form. Now users doensn't want it to be on top. When I set the TopMost to false, the forms seems to be separated. 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). 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?

The Winform "Activated" event is fired when a form is brought to top-most of the GUI.

Try setting the parent in the ctor of every child form:

ChildForm frm = new ChildForm(parentForm);

Try to use Win32-functions to hack the foreground behavior:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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