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 コントロールが割り当てられたプロパティで、テキストに適用されます FontAutoSizeControlこの例のメソッドには、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 メソッドは、プロパティを設定するために呼び出されます ClientSize 。 プロパティは ClientSize メソッドによって set 常に変更されるとは限らないので、プロパティが設定されたときにコードが実行されるように、メソッドをオーバーライド SetClientSizeCore する ClientSize 必要があります。

プロパティ Size.WidthSize.Height 、コントロールのクライアント領域の幅と高さを表します。 このプロパティを使用すると、コントロールのサーフェス上での描画などのタスクのコントロールのクライアント領域のサイズを取得できます。

コントロールでの描画の詳細については、「Windows フォーム コントロールのレンダリング」を参照してください。

注意

アプリケーション設定をこのプロパティにバインドすることはできません。 アプリケーション設定の詳細については、「アプリケーション 設定の概要」を参照してください。

適用対象

こちらもご覧ください