Graphics.GetHdc Método

Definición

Obtiene el identificador del contexto de dispositivo asociado a este 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

Devoluciones

IntPtr

nativeint

Identificador del contexto de dispositivo asociado a este Graphics.

Implementaciones

Ejemplos

El ejemplo de código siguiente está diseñado para su uso con Windows Forms y requiere PaintEventArgse, que es un parámetro del Paint controlador de eventos. En el ejemplo se muestra cómo llamar a una función GDI de Windows para realizar la misma tarea que un método GDI+ Graphics . El código realiza las siguientes acciones:

  • Define el atributo de interoperabilidad DllImportAttribute para el archivo DLL de Windows gdi32.dll. Este archivo DLL contiene la función GDI deseada.

  • Define la Rectangle función en ese archivo DLL como externo.

  • Crea un lápiz rojo.

  • Con el lápiz, dibuja un rectángulo en la pantalla mediante el método GDI+ DrawRectangle .

  • Define una variable hdc de tipo de puntero interno y establece su valor en el identificador en el contexto del dispositivo del formulario.

  • Dibuja un rectángulo en la pantalla mediante la función GDI Rectangle .

  • Libera el contexto del dispositivo representado por el hdc parámetro .

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

Comentarios

El contexto del dispositivo es una estructura de Windows basada en GDI que define un conjunto de objetos gráficos y sus atributos asociados, así como los modos gráficos que afectan a la salida. Este método devuelve ese contexto de dispositivo con la excepción de una fuente. Dado que no se selecciona una fuente, se producirá un error en las llamadas al FromHdc método mediante un identificador devuelto por el GetHdc método .

Las llamadas a los GetHdc métodos y ReleaseHdc deben aparecer en pares. Durante el ámbito de un GetHdc par de métodos y ReleaseHdc , normalmente solo se realizan llamadas a funciones GDI. Las llamadas en ese ámbito realizadas a métodos de GDI+ del Graphics que generó el hdc parámetro producen un ObjectBusy error. Además, GDI+ omite los cambios de estado realizados en el Graphicshdc del parámetro en las operaciones posteriores.

Se aplica a