Form.TopMost 属性

定义

获取或设置一个值,指示该窗体是否应显示为最顶层窗体。Gets or sets a value indicating whether the form should be displayed as a topmost form.

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;否则为 falsetrue to display the form as a topmost form; otherwise, false. 默认值为 falseThe default is false.

示例

下面的示例演示如何创建最顶层的窗体。The following example demonstrates how to create a topmost form. 该示例创建两个窗体,一个是最大化的窗体,另一个将显示为最顶层窗体。The example creates two forms, one that is maximized and one that will be displayed as a topmost form. 第一个窗体(名为 bottomForm)使用 WindowState 属性最大化显示,以更好地说明最顶层窗体的功能。The first form, named bottomForm, is displayed maximized, using the WindowState property, to better demonstrate the abilities of the topmost form. 名为 topMostForm的第二个窗体将 TopMost 属性设置为 true,以将窗体显示为最顶层窗体。The second form, named topMostForm, sets the TopMost property to true to display the form as a topmost form. 此代码运行时,单击最大化的窗体将不会使最顶层的窗体显示在最大化窗体下。When this code is run, clicking on the maximized form will not cause the topmost form to be displayed below the maximized form. 该示例要求在示例中定义的方法是从其他窗体调用的。The example requires that the method defined in the example is called from another form.

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

注解

最顶层窗体是与其他所有(非最顶层)窗体重叠的窗体,即使它不是活动窗体或前景窗体。A topmost form is a form that overlaps all the other (non-topmost) forms even if it is not the active or foreground form. 最顶层窗体始终显示在桌面上 z 顺序的最上方。Topmost forms are always displayed at the highest point in the z-order of the windows on the desktop. 您可以使用此属性创建一个始终显示在您的应用程序中的窗体,如 "查找和替换" 工具窗口。You can use this property to create a form that is always displayed in your application, such as a Find and Replace tool window.

适用于

另请参阅