다음을 통해 공유


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 Forms에서 사용하도록 설계되었으며 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 구조입니다. 이 메서드는 글꼴을 제외하고 해당 디바이스 컨텍스트를 반환합니다. 글꼴이 선택되지 않았으므로 GetHdc 메서드에서 반환된 핸들을 사용하여 FromHdc 메서드를 호출하지 못합니다.

GetHdcReleaseHdc 메서드에 대한 호출은 쌍으로 표시되어야 합니다. GetHdcReleaseHdc 메서드 쌍의 범위 동안 일반적으로 GDI 함수만 호출합니다. hdc 매개 변수를 생성한 Graphics GDI+ 메서드에 대한 해당 범위의 호출은 ObjectBusy 오류와 함께 실패합니다. 또한 GDI+는 후속 작업에서 hdc 매개 변수의 Graphics 대한 상태 변경을 무시합니다.

적용 대상