このトピックはまだ評価されていません - このトピックを評価する

Control.CreateGraphics メソッド

コントロールの Graphics を作成します。

名前空間: System.Windows.Forms
アセンブリ: System.Windows.Forms (system.windows.forms.dll 内)

public Graphics CreateGraphics ()
public Graphics CreateGraphics ()
public function CreateGraphics () : Graphics

戻り値

コントロールの Graphics

返された Graphics は、必要がなくなった時点で、Dispose メソッドを呼び出して破棄する必要があります。Graphics は、現在のウィンドウのメッセージの存続期間だけ有効です。

メモメモ

InvokeRequired プロパティ以外に、スレッド セーフである 4 つのメソッド、InvokeBeginInvokeEndInvoke、および CreateGraphics がコントロールにあります。その他のすべてのメソッドの呼び出しについては、Invoke メソッドの 1 つを使用して、その呼び出しをコントロールのスレッドにマーシャリングする必要があります。

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

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.
    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

  • UIPermission   (セーフ サブウィンドウでこのメソッドを呼び出すために必要なアクセス許可)。UIPermissionWindowSafeSubWindows 値 (関連する列挙体)

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 によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。

.NET Framework

サポート対象 : 2.0、1.1、1.0

.NET Compact Framework

サポート対象 : 2.0、1.0
この情報は役に立ちましたか。
(残り 1500 文字)
コミュニティ コンテンツ 追加
注釈 FAQ