Graphics.GetHdc メソッド

定義

この Graphics に関連付けられているデバイス コンテキストを識別するハンドルを取得します。

public:
 virtual IntPtr GetHdc();
public:
 IntPtr GetHdc();
public IntPtr GetHdc ();
abstract member GetHdc : unit -> nativeint
override this.GetHdc : unit -> nativeint
member this.GetHdc : unit -> nativeint
Public Function GetHdc () As IntPtr

戻り値

IntPtr

nativeint

この Graphics に関連付けられているデバイス コンテキストを識別するハンドル。

実装

次のコード例は、Windows フォームで使用するように設計されており、イベント ハンドラーのPaintパラメーターである が必要PaintEventArgseです。 この例では、GDI+ Graphics メソッドと同じタスクを実行するために Windows GDI 関数を呼び出す方法を示します。 コードは、次のアクションを実行します。

  • Windows DLL ファイル gdi32.dll の相互運用性 DllImportAttribute 属性を定義します。 この DLL には、目的の GDI 関数が含まれています。

  • その DLL 内の Rectangle 関数を外部として定義します。

  • 赤いペンを作成します。

  • ペンを使用して、GDI+ DrawRectangle メソッドを使用して画面に四角形を描画します。

  • 内部ポインター型変数 hdc を定義し、その値をフォームのデバイス コンテキストのハンドルに設定します。

  • GDI Rectangle 関数を使用して、画面に四角形を描画します。

  • パラメーターで表されるデバイス コンテキストを hdc 解放します。

private:
   [System::Runtime::InteropServices::DllImportAttribute("gdi32.dll")]
   static bool Rectangle( IntPtr hdc, int ulCornerX, int ulCornerY, int lrCornerX, int lrCornerY );

public:
   void GetHdcForGDI1( PaintEventArgs^ e )
   {
      // Create pen.
      Pen^ redPen = gcnew Pen( Color::Red,1.0f );

      // Draw rectangle with GDI+.
      e->Graphics->DrawRectangle( redPen, 10, 10, 100, 50 );

      // Get handle to device context.
      IntPtr hdc = e->Graphics->GetHdc();

      // Draw rectangle with GDI using default pen.
      Rectangle( hdc, 10, 70, 110, 120 );

      // Release handle to device context.
      e->Graphics->ReleaseHdc( hdc );
   }
public class GDI
{
    [System.Runtime.InteropServices.DllImport("gdi32.dll")]
    internal static extern bool Rectangle(
       IntPtr hdc,
       int ulCornerX, int ulCornerY,
       int lrCornerX, int lrCornerY);
}

private void GetHdcForGDI1(PaintEventArgs e)
{
    // Create pen.
    Pen redPen = new Pen(Color.Red, 1);

    // Draw rectangle with GDI+.
    e.Graphics.DrawRectangle(redPen, 10, 10, 100, 50);

    // Get handle to device context.
    IntPtr hdc = e.Graphics.GetHdc();

    // Draw rectangle with GDI using default pen.
    GDI.Rectangle(hdc, 10, 70, 110, 120);

    // Release handle to device context.
    e.Graphics.ReleaseHdc(hdc);
}
Public Class GDI
    <System.Runtime.InteropServices.DllImportAttribute("gdi32.dll")> _
    Friend Shared Function Rectangle(ByVal hdc As IntPtr, _
    ByVal ulCornerX As Integer, ByVal ulCornerY As Integer, ByVal lrCornerX As Integer, _
    ByVal lrCornerY As Integer) As Boolean
    End Function
End Class

<System.Security.Permissions.SecurityPermission( _
System.Security.Permissions.SecurityAction.LinkDemand, Flags:= _
System.Security.Permissions.SecurityPermissionFlag.UnmanagedCode)> _
Private Sub GetHdcForGDI1(ByVal e As PaintEventArgs)

    ' Create pen.
    Dim redPen As New Pen(Color.Red, 1)

    ' Draw rectangle with GDI+.
    e.Graphics.DrawRectangle(redPen, 10, 10, 100, 50)

    ' Get handle to device context.
    Dim hdc As IntPtr = e.Graphics.GetHdc()

    ' Draw rectangle with GDI using default pen.
    GDI.Rectangle(hdc, 10, 70, 110, 120)

    ' Release handle to device context.
    e.Graphics.ReleaseHdc(hdc)
End Sub

注釈

デバイス コンテキストは、GDI に基づく Windows 構造であり、一連のグラフィカル オブジェクトとその関連する属性、および出力に影響を与えるグラフィカル モードを定義します。 このメソッドは、フォントを除き、そのデバイス コンテキストを返します。 フォントが選択されていないため、 メソッドから返されたハンドルを FromHdc 使用したメソッドの GetHdc 呼び出しは失敗します。

メソッドと ReleaseHdc メソッドのGetHdc呼び出しは、ペアで指定する必要があります。 メソッドと ReleaseHdc メソッドのペアのGetHdcスコープ中は、通常、GDI 関数の呼び出しのみを行います。 パラメーターを生成hdcした の GDI+ メソッドに対するそのスコープ内のGraphics呼び出しはエラーでObjectBusy失敗します。 また、GDI+ は、後続の操作で パラメーターの に対して Graphics 行われたすべての状態変更を hdc 無視します。

適用対象