次の方法で共有


Graphics.FromHwnd メソッド

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

Public Shared Function FromHwnd( _
   ByVal hwnd As IntPtr _) As Graphics
[C#]
public static Graphics FromHwnd(IntPtrhwnd);
[C++]
public: static Graphics* FromHwnd(IntPtrhwnd);
[JScript]
public static function FromHwnd(
   hwnd : IntPtr) : Graphics;

パラメータ

  • hwnd
    ウィンドウを識別するハンドル。

戻り値

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

使用例

[Visual Basic, C#] 次の例は、Windows フォームでの使用を意図してデザインされており、 Paint イベント ハンドラのパラメータである PaintEventArgs e と、 Form オブジェクトの例である thisForm が必要です。このコードは次のアクションを実行します。

  • 新しい内部ポインタ変数 hwnd を作成し、この変数をサンプルのフォームのハンドルに設定します。
  • ハンドルから新しい Graphics オブジェクトを作成します。
  • 赤いペンを使用して、新しい Graphics オブジェクトに四角形を描画します。
  • 新しい Graphics オブジェクトを破棄します。
 
Public Sub FromHwndHwnd(e As PaintEventArgs)
' Get handle to form.
Dim hwnd As New IntPtr()
hwnd = thisForm.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
        
[C#] 
public void FromHwndHwnd(PaintEventArgs e)
{
// Get handle to form.
IntPtr hwnd = new IntPtr();
hwnd = thisForm.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();
}
        

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

必要条件

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

参照

Graphics クラス | Graphics メンバ | System.Drawing 名前空間