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+ 메서드와 동일한 작업을 수행하기 위해 Windows GDI 함수를 호출하는 방법을 보여 줍니다 Graphics . 코드는 다음 작업을 수행합니다.

  • 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 구조체입니다. 이 메서드는 글꼴을 제외 하 고 해당 디바이스 컨텍스트를 반환합니다. 글꼴이 선택되지 않았으므로 메서드에서 반환된 핸들을 사용하여 메서드에 대한 호출 FromHdcGetHdc 실패합니다.

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

적용 대상