共用方式為


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

屬性值

A 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

備註

控制項的客戶端區域是控制項的邊界,減去非客戶端元素,如捲軸列、邊框、標題欄和選單。 SetClientSizeCore該方法被呼叫來設定屬性ClientSizeClientSize屬性不一定會透過其set方法被更改,因此你應該覆寫該SetClientSizeCore方法,以確保設定屬性時ClientSize程式碼會被執行。

Size.WidthSize.Height屬性代表控制區客戶端區域的寬度與高度。 你可以利用此特性取得控制區客戶區域的大小,用於繪製控制表表面等任務。

欲了解更多關於在控制項上繪圖的資訊,請參閱 「渲染 Windows 表單控制項」。

備註

你無法將應用程式設定綁定到這個屬性。 欲了解更多應用程式設定資訊,請參閱 應用程式設定概覽

適用於

另請參閱