我正在使用C#开发一个Windows窗体应用程序。当特定类型的外部事件发生时(例如,当鼠标位置y=0时,我想在窗体上写上“鼠标在上方线上”),该应用程序会在窗体上显示一些文本。当事件发生时,我需要将该窗体置于所有其他窗口之上。
我正在使用C#开发一个Windows窗体应用程序。当特定类型的外部事件发生时(例如,当鼠标位置y=0时,我想在窗体上写上“鼠标在上方线上”),该应用程序会在窗体上显示一些文本。当事件发生时,我需要将该窗体置于所有其他窗口之上。
在您的表单类中使用以下内容:
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;
}
}
尝试使用这个
yourForm.TopMost = true;