通过


Control.ClientSize 属性

定义

获取或设置控件工作区的高度和宽度。

public:
 property System::Drawing::Size ClientSize { System::Drawing::Size get(); void set(System::Drawing::Size value); };
[System.ComponentModel.Browsable(false)]
public System.Drawing.Size ClientSize { get; set; }
[<System.ComponentModel.Browsable(false)>]
member this.ClientSize : System.Drawing.Size with get, set
Public Property ClientSize As Size

属性值

一个 Size 表示控件工作区的维度。

属性

示例

下面的代码示例调整指定控件的大小,以便控件能够容纳其格式化文本。 带格式的文本是 Text 应用了控件 Font 的属性。 AutoSizeControl此示例中的方法还具有一个textPadding参数,该参数表示要应用于控件的所有边缘的填充。 若要使填充看起来相等,请将文本与 ContentAlignment.MiddleCenter 值对齐(如果控件支持)。

private:
   void AutoSizeControl( Control^ control, int textPadding )
   {
      
      // Create a Graphics object for the Control.
      Graphics^ g = control->CreateGraphics();
      
      // Get the Size needed to accommodate the formatted Text.
      System::Drawing::Size preferredSize = g->MeasureString( control->Text, control->Font ).ToSize();
      
      // Pad the text and resize the control.
      control->ClientSize = System::Drawing::Size( preferredSize.Width + (textPadding * 2), preferredSize.Height + (textPadding * 2) );
      
      // Clean up the Graphics object.
      delete g;
   }
private void AutoSizeControl(Control control, int textPadding)
{
   // Create a Graphics object for the Control.
   Graphics g = control.CreateGraphics();

   // Get the Size needed to accommodate the formatted Text.
   Size preferredSize = g.MeasureString(
      control.Text, control.Font).ToSize();

   // Pad the text and resize the control.
   control.ClientSize = new Size(
      preferredSize.Width + (textPadding * 2), 
      preferredSize.Height+(textPadding * 2) );

   // Clean up the Graphics object.
   g.Dispose();
}
Private Sub AutoSizeControl(control As Control, textPadding As Integer)
   ' Create a Graphics object for the Control.
   Dim g As Graphics = control.CreateGraphics()
   
   ' Get the Size needed to accommodate the formatted Text.
   Dim preferredSize As Size = g.MeasureString( _
     control.Text, control.Font).ToSize()
   
   ' Pad the text and resize the control.
   control.ClientSize = New Size( _
     preferredSize.Width + textPadding * 2, _
     preferredSize.Height + textPadding * 2)
   
   ' Clean up the Graphics object.
   g.Dispose()
End Sub

注解

控件的工作区是控件的边界,减去滚动条、边框、标题栏和菜单等非client元素。 调用 SetClientSizeCore 该方法以设置 ClientSize 属性。 属性 ClientSize 并不总是通过其 set 方法更改, SetClientSizeCore 因此应重写该方法,以确保在设置属性时 ClientSize 执行代码。

属性Size.WidthSize.Height表示控件工作区的宽度和高度。 可以使用此属性获取控件的工作区的大小,以执行诸如在控件图面上绘制等任务。

有关在控件上绘图的详细信息,请参阅 呈现 Windows 窗体控件

注释

无法将应用程序设置绑定到此属性。 有关应用程序设置的详细信息,请参阅 应用程序设置概述

适用于

另请参阅