Control.CreateGraphics メソッド
アセンブリ: System.Windows.Forms (system.windows.forms.dll 内)

Dim instance As Control Dim returnValue As Graphics returnValue = instance.CreateGraphics
コントロールの Graphics。

返された Graphics は、必要がなくなった時点で、Dispose メソッドを呼び出して破棄する必要があります。Graphics は、現在のウィンドウのメッセージの存続期間だけ有効です。
![]() |
---|
InvokeRequired プロパティ以外に、スレッド セーフである 4 つのメソッド、Invoke、BeginInvoke、EndInvoke、および CreateGraphics がコントロールにあります。その他のすべてのメソッドの呼び出しについては、Invoke メソッドの 1 つを使用して、その呼び出しをコントロールのスレッドにマーシャリングする必要があります。 |

指定したコントロールのサイズを変更して、書式設定されたテキストを収容できるようにするコード例を次に示します。書式設定されたテキストは、コントロールに割り当てられた Font がテキストに適用されている Text プロパティです。この例では AutoSizeControl メソッドにも、コントロールのすべての端に適用する埋め込みを表す textPadding パラメータがあります。埋め込みが均等に表示されるようにするには、System.Drawing.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 によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


Weblioに収録されているすべての辞書からControl.CreateGraphics メソッドを検索する場合は、下記のリンクをクリックしてください。

- Control.CreateGraphics メソッドのページへのリンク