Form.TopMost 属性

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

**命名空间:**System.Windows.Forms
**程序集:**System.Windows.Forms(在 system.windows.forms.dll 中)

语法

声明
Public Property TopMost As Boolean
用法
Dim instance As Form
Dim value As Boolean

value = instance.TopMost

instance.TopMost = value
public bool TopMost { get; set; }
public:
property bool TopMost {
    bool get ();
    void set (bool value);
}
/** @property */
public boolean get_TopMost ()

/** @property */
public void set_TopMost (boolean value)
public function get TopMost () : boolean

public function set TopMost (value : boolean)

属性值

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

备注

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

示例

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

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 'CreateMyTopMostForm
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:
   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.set_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.set_Size(new Size(300, 300));

    // Set the position of the top most form to center of screen.
    topMostForm.set_StartPosition(FormStartPosition.CenterScreen);

    // Display the form as top most form.
    topMostForm.set_TopMost(true);
    topMostForm.Show();
} //CreateMyTopMostForm

平台

Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

.NET Compact Framework

受以下版本支持:2.0

请参见

参考

Form 类
Form 成员
System.Windows.Forms 命名空间
Form.TopLevel 属性