次の方法で共有


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

属性

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

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

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

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

適用対象

こちらもご覧ください