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

屬性值

表示控制項工作區維度的 SizeA 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 Forms 控制項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.

適用於

另請參閱