Control.ClientSize Control.ClientSize Control.ClientSize Control.ClientSize Property

定义

获取或设置控件的工作区的高度和宽度。Gets or sets the height and width of the client area of the control.

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; }
member this.ClientSize : System.Drawing.Size with get, set
Public Property ClientSize As Size

属性值

Size,表示控件的工作区的维数。A Size that represents the dimensions of the client area of the control.

示例

下面的代码示例将调整指定控件的大小,以便控件容纳其格式化文本。The following code example resizes the specified control so the control will accommodate its formatted text. 带格式的文本是Text Font应用了应用于文本的控件的属性。The formatted text is the Text property with the control's assigned Font applied to the text. AutoSizeControl示例中的方法还具有一个textPadding参数,该参数表示要应用于控件的所有边缘的边距。The AutoSizeControl method in this example also has a textPadding parameter that represents the padding to apply to all edges of the control. 若要使填充看起来相等,请将文本与ContentAlignment.MiddleCenter值对齐(如果控件支持它)。To make the padding appear equal, align the text with the ContentAlignment.MiddleCenter value, if your control supports it.

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

注解

控件的工作区是控件的边界,而非工作区元素(如滚动条、边框、标题栏和菜单)的边界。The client area of a control is the bounds of the control, minus the nonclient elements such as scroll bars, borders, title bars, and menus. 调用方法来ClientSize设置属性。 SetClientSizeCoreThe SetClientSizeCore method is called to set the ClientSize property. 属性并非始终通过其set方法进行更改SetClientSizeCore ,因此您应该重写方法,以ClientSize确保在设置属性时执行您的代码。 ClientSizeThe ClientSize property is not always changed through its set method so you should override the SetClientSizeCore method to ensure that your code is executed when the ClientSize property is set.

Size.WidthSize.Height属性表示控件的工作区的宽度和高度。The Size.Width and Size.Height properties represent the width and height of the client area of the control. 您可以使用此属性获取控件的工作区的大小(如在控件的图面上绘制)。You can use this property to obtain the size of the client area of the control for tasks such as drawing on the surface of the control.

有关在控件上绘制的详细信息,请参阅呈现 Windows 窗体控件For more information about drawing on controls, see Rendering a Windows Forms Control.

备注

不能将应用程序设置绑定到此属性。You cannot bind application settings to this property. 有关应用程序设置的详细信息,请参阅应用程序设置概述For more information on application settings, see Application Settings Overview.

适用于

另请参阅