How to show form in front in C#

后端 未结 11 1972
长发绾君心
长发绾君心 2020-12-06 09:35

Folks,

Please does anyone know how to show a Form from an otherwise invisible application, and have it get the focus (i.e. appear on top of other windows)?

11条回答
  •  孤城傲影
    2020-12-06 10:12

    I got a code in the project.

    private static extern bool SetForegroundWindow(
    IntPtr hWnd); 
    public static void ShowToFront(Form form)
    {
        FormWindowState oldState = form.WindowState;
        form.WindowState = FormWindowState.Minimized;
        form.Show();
    
        form.Activate();
        form.TopLevel = false;
        form.TopLevel = true;
        form.SelectNextControl(form.ActiveControl, true, true, true, true);
        SetForegroundWindow(form.Handle);
        form.Focus();
        form.WindowState = oldState;
    }
    

提交回复
热议问题