Graphics.GetHdc Metoda

Definicja

Pobiera uchwyt do kontekstu urządzenia skojarzonego z tym Graphicselementem .

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

Zwraca

IntPtr

nativeint

Dojmij do kontekstu urządzenia skojarzonego z tym Graphicselementem .

Implementuje

Przykłady

Poniższy przykład kodu jest przeznaczony do użycia z Windows Forms i wymaga PaintEventArgseparametru Paint programu obsługi zdarzeń. W przykładzie pokazano wywołanie funkcji GDI systemu Windows w celu wykonania tego samego zadania co metoda GDI+ Graphics . Kod wykonuje następujące akcje:

  • Definiuje atrybut współdziałania DllImportAttribute dla pliku DLL systemu Windows gdi32.dll. Ta biblioteka DLL zawiera żądaną funkcję GDI.

  • Rectangle Definiuje funkcję w tej bibliotece DLL jako zewnętrzną.

  • Tworzy czerwone pióro.

  • Za pomocą pióra rysuje prostokąt na ekran przy użyciu metody GDI+ DrawRectangle .

  • Definiuje wewnętrzną zmienną hdc typu wskaźnika i ustawia jej wartość na uchwyt w kontekście urządzenia formularza.

  • Rysuje prostokąt na ekranie przy użyciu funkcji GDI Rectangle .

  • Zwalnia kontekst urządzenia reprezentowany hdc przez parametr .

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

Uwagi

Kontekst urządzenia jest strukturą systemu Windows opartą na GDI, która definiuje zestaw obiektów graficznych i skojarzonych z nimi atrybutów, a także tryby graficzne wpływające na dane wyjściowe. Ta metoda zwraca ten kontekst urządzenia z wyjątkiem czcionki. Ponieważ czcionka nie jest zaznaczona, wywołania FromHdc metody przy użyciu uchwytu zwróconego z metody zakończy się niepowodzeniem GetHdc .

Wywołania metod GetHdc i ReleaseHdc muszą być wyświetlane w parach. W zakresie GetHdc pary metod i ReleaseHdc zwykle wykonujesz tylko wywołania funkcji GDI. Wywołania w tym zakresie wykonane do metod GDI+ parametru Graphicshdc , który wygenerował parametr kończy się niepowodzeniem z powodu błędu ObjectBusy . Ponadto GDI+ ignoruje wszelkie zmiany stanu wprowadzone w Graphics parametrze hdc w kolejnych operacjach.

Dotyczy