如何将 Windows 窗体置于顶部?

Ahm*_*igh 5 c# winforms

I am developing a windows form application in C#. This application shows some text on the Form when a special kind of external event occurs (For example suppose that I want to write "Mouse is on upper line" on form when in the mouse's position y=0). I need to bring the Form to top of every other window when the event occurs.

小智 5

在您的表单类中使用它:

public void BringToTop()
{
    //Checks if the method is called from UI thread or not
    if (this.InvokeRequired)
    {
        this.Invoke(new Action(BringToTop));
    }
    else
    {
        if (this.WindowState == FormWindowState.Minimized)
        {
            this.WindowState = FormWindowState.Normal;
        }
        //Keeps the current topmost status of form
        bool top = TopMost;
        //Brings the form to top
        TopMost = true;
        //Set form's topmost status back to whatever it was
        TopMost = top;
    }
}
Run Code Online (Sandbox Code Playgroud)