Graphics.FromHwnd(IntPtr) メソッド

定義

ウィンドウを識別する指定のハンドルから新しい Graphics を作成します。

public:
 static System::Drawing::Graphics ^ FromHwnd(IntPtr hwnd);
public static System.Drawing.Graphics FromHwnd (IntPtr hwnd);
static member FromHwnd : nativeint -> System.Drawing.Graphics
Public Shared Function FromHwnd (hwnd As IntPtr) As Graphics

パラメーター

hwnd
IntPtr

nativeint

ウィンドウへのハンドルです。

戻り値

このメソッドは、指定のウィンドウ ハンドルの新しい Graphics を返します。

次のコード例は、Windows フォームで使用するように設計されており、この例では イベント ハンドラーthisFormFormPaint パラメーターである と、 が必要PaintEventArgseです。 コードは、次のアクションを実行します。

  • 新しい内部ポインター変数 hwnd を作成し、それを例のフォームのハンドルに設定します。

  • ハンドルから新しい Graphics を作成します。

  • 赤いペンを使用して、新しい Graphics に四角形を描画します。

  • 新しい Graphicsを破棄します。

public:
   void FromHwndHwnd( PaintEventArgs^ /*e*/ )
   {
      // Get handle to form.
      IntPtr hwnd = this->Handle;

      // Create new graphics object using handle to window.
      Graphics^ newGraphics = Graphics::FromHwnd( hwnd );

      // Draw rectangle to screen.
      newGraphics->DrawRectangle( gcnew Pen( Color::Red,3.0f ), 0, 0, 200, 100 );

      // Dispose of new graphics.
      delete newGraphics;
   }
private void FromHwndHwnd(PaintEventArgs e)
{

    // Get handle to form.
    IntPtr hwnd = this.Handle;

    // Create new graphics object using handle to window.
    Graphics newGraphics = Graphics.FromHwnd(hwnd);

    // Draw rectangle to screen.
    newGraphics.DrawRectangle(new Pen(Color.Red, 3), 0, 0, 200, 100);

    // Dispose of new graphics.
    newGraphics.Dispose();
}
Private Sub FromHwndHwnd(ByVal e As PaintEventArgs)

    ' Get handle to form.
    Dim hwnd As IntPtr = Me.Handle


    ' Create new graphics object using handle to window.
    Dim newGraphics As Graphics = Graphics.FromHwnd(hwnd)

    ' Draw rectangle to screen.
    newGraphics.DrawRectangle(New Pen(Color.Red, 3), 0, 0, 200, 100)

    ' Dispose of new graphics.
    newGraphics.Dispose()
End Sub

注釈

メソッドによって作成された および 関連リソースを Dispose 解放するには、 Graphics 常に メソッドを呼び出す FromHwnd 必要があります。

適用対象