Control.ClientSize プロパティ
アセンブリ: System.Windows.Forms (system.windows.forms.dll 内)

/** @property */ public Size get_ClientSize () /** @property */ public void set_ClientSize (Size value)
コントロールのクライアント領域の大きさを表す Size。

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

指定したコントロールのサイズを変更して、書式設定されたテキストを収容できるようにするコード例を次に示します。書式設定されたテキストは、コントロールに割り当てられた 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
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: 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.get_Text(), control. get_Font()).ToSize(); // Pad the text and resize the control. control.set_ClientSize(new Size(preferredSize.get_Width() + textPadding * 2, preferredSize.get_Height() + textPadding * 2)); // Clean up the Graphics object. g.Dispose(); } //AutoSizeControl

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


- Control.ClientSize プロパティのページへのリンク