Control.ClientSize プロパティ

コントロールのクライアント領域の高さと幅を取得または設定します。

Public Property ClientSize As Size
[C#]
public Size ClientSize {get; set;}
[C++]
public: __property Size get_ClientSize();public: __property void set_ClientSize(Size);
[JScript]
public function get ClientSize() : Size;public function set ClientSize(Size);

プロパティ値

コントロールのクライアント領域の大きさを表す Size

解説

コントロールのクライアント領域とは、コントロールの範囲から、スクロール バー、境界線、タイトル バー、メニューなどのクライアント以外の要素を除いた部分です。 ClientSize プロパティを設定するために、 SetClientSizeCore メソッドが呼び出されます。 ClientSize プロパティはそのプロパティの set メソッドで変更されるとは限らないため、 SetClientSizeCore メソッドをオーバーライドして、 ClientSize プロパティの設定時に必ずコードが実行されるようにします。

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

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

使用例

[Visual Basic, C#, C++] 指定したコントロールで書式設定されたテキストを使用するように、そのコントロールのサイズを変更する例を次に示します。書式設定されたテキストは、コントロールに割り当てられた Font がテキストに適用されている Text プロパティです。この例では AutoSizeControl メソッドにも、コントロールのすべての端に適用する埋め込みを表す textPadding パラメータがあります。埋め込みが均等に表示されるようにするには、テキストを ContentAlignment.MiddleCenter 値に揃えて配置します (コントロールがこれをサポートしている場合)。

 
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

[C#] 
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();
}

[C++] 
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.
        g->Dispose();
    }

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET

参照

Control クラス | Control メンバ | System.Windows.Forms 名前空間 | ClientRectangle | DrawMode