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

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

注意

您無法將應用程式設定系結至此屬性。 如需應用程式設定的詳細資訊,請參閱 應用程式設定概觀

適用於

另請參閱