Form.TopMost 属性

定义

获取或设置一个值,指示该窗体是否应显示为最顶层窗体。

public:
 property bool TopMost { bool get(); void set(bool value); };
public bool TopMost { get; set; }
member this.TopMost : bool with get, set
Public Property TopMost As Boolean

属性值

如果将窗体显示为最顶层窗体,则为 true;否则为 false。 默认值为 false

示例

以下示例演示如何创建最顶层的窗体。 该示例创建两个窗体,一个是最大化的,一个将显示为最顶部的窗体。 第一个名为 bottomForm的窗体使用 WindowState 属性以最大化方式显示,以更好地演示最顶部窗体的能力。 第二个窗体名为 topMostForm,将 TopMost 属性设置为 true 以将窗体显示为最顶部的窗体。 运行此代码时,单击最大化的窗体不会导致最顶部的窗体显示在最大化窗体的下方。 该示例要求从另一个窗体调用示例中定义的 方法。

private:
   void CreateMyTopMostForm()
   {
      // Create lower form to display.
      Form^ bottomForm = gcnew Form;

      // Display the lower form Maximized to demonstrate effect of TopMost property.
      bottomForm->WindowState = FormWindowState::Maximized;

      // Display the bottom form.
      bottomForm->Show();

      // Create the top most form.
      Form^ topMostForm = gcnew Form;

      // Set the size of the form larger than the default size.
      topMostForm->Size = System::Drawing::Size( 300, 300 );

      // Set the position of the top most form to center of screen.
      topMostForm->StartPosition = FormStartPosition::CenterScreen;

      // Display the form as top most form.
      topMostForm->TopMost = true;
      topMostForm->Show();
   }
private void CreateMyTopMostForm()
{
   // Create lower form to display.
   Form bottomForm = new Form();
   // Display the lower form Maximized to demonstrate effect of TopMost property.
   bottomForm.WindowState = FormWindowState.Maximized;
   // Display the bottom form.
   bottomForm.Show();
   // Create the top most form.
   Form topMostForm = new Form();
   // Set the size of the form larger than the default size.
   topMostForm.Size = new Size(300,300);
   // Set the position of the top most form to center of screen.
   topMostForm.StartPosition = FormStartPosition.CenterScreen;
   // Display the form as top most form.
   topMostForm.TopMost = true;
   topMostForm.Show();
}
Private Sub CreateMyTopMostForm()
   ' Create lower form to display.
   Dim bottomForm As New Form()
   ' Display the lower form Maximized to demonstrate effect of TopMost property.
   bottomForm.WindowState = FormWindowState.Maximized
   ' Display the bottom form.
   bottomForm.Show()
   ' Create the top most form.
   Dim topMostForm As New Form()
   ' Set the size of the form larger than the default size.
   topMostForm.Size = New Size(300, 300)
   ' Set the position of the top most form to center of screen.
   topMostForm.StartPosition = FormStartPosition.CenterScreen
   ' Display the form as top most form.
   topMostForm.TopMost = True
   topMostForm.Show()
End Sub

注解

最顶层窗体是一种与所有其他 (非最顶端) 窗体重叠的窗体,即使它不是活动窗体或前台窗体也是如此。 最顶部的窗体始终显示在桌面上窗口 z 顺序的最高点。 可以使用此属性创建始终显示在应用程序中的窗体,例如“查找和替换”工具窗口。

适用于

另请参阅