Control.ClientSize 属性

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

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

语法

声明
Public Property ClientSize As Size
用法
Dim instance As Control
Dim value As Size

value = instance.ClientSize

instance.ClientSize = value
public Size ClientSize { get; set; }
public:
property Size ClientSize {
    Size get ();
    void set (Size value);
}
/** @property */
public Size get_ClientSize ()

/** @property */
public void set_ClientSize (Size value)
public function get ClientSize () : Size

public function set ClientSize (value : Size)

属性值

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

备注

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

Size.WidthSize.Height 属性表示控件的工作区的宽度和高度。可以使用该属性为诸如在控件的表面上绘制之类的任务获得控件工作区的大小。

有关在控件上绘制的更多信息,请参见 呈现 Windows 窗体控件

示例

下面的代码示例调整指定控件的大小,以便该控件能够容纳带格式的文本。带格式的文本是将控件的分配 Font 应用于文本的 Text 属性。该示例中的 AutoSizeControl 方法还有一个 textPadding 参数,该参数表示应用于控件的所有边缘的空白。要使边距相等,请将文本和 ContentAlignment.MiddleCenter 值对齐,如果您的控件支持该操作。

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
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:
   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.get_Text(), control.
        get_Font()).ToSize();
    // Pad the text and resize the control.
    control.set_ClientSize(new Size(preferredSize.get_Width() 
        + textPadding * 2, preferredSize.get_Height() + textPadding * 2));
    // Clean up the Graphics object.
    g.Dispose();
} //AutoSizeControl

平台

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、1.0

请参见

参考

Control 类
Control 成员
System.Windows.Forms 命名空间
Control.ClientRectangle 属性
DrawMode