Graphics.GetHdc Metodo

Definizione

Ottiene l'handle per il contesto di dispositivo associato a questo oggetto 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

Restituisce

IntPtr

nativeint

Handle per il contesto di dispositivo associato a questo oggetto Graphics.

Implementazioni

Esempio

L'esempio di codice seguente è progettato per l'uso con Windows Forms e richiede PaintEventArgse, che è un parametro del Paint gestore eventi. L'esempio illustra la chiamata di una funzione GDI di Windows per eseguire la stessa attività di un metodo GDI+ Graphics . Il codice esegue le azioni seguenti:

  • Definisce l'attributo di interoperabilità DllImportAttribute per il file DLL di Windows gdi32.dll. Questa DLL contiene la funzione GDI desiderata.

  • Definisce la Rectangle funzione in tale DLL come esterna.

  • Crea una penna rossa.

  • Con la penna, disegna un rettangolo sullo schermo usando il metodo GDI+ DrawRectangle .

  • Definisce una variabile di tipo puntatore hdc interno e ne imposta il valore sull'handle sul contesto di dispositivo del modulo.

  • Disegna un rettangolo sullo schermo usando la funzione GDI Rectangle .

  • Rilascia il contesto di dispositivo rappresentato dal hdc parametro .

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

Commenti

Il contesto di dispositivo è una struttura di Windows basata su GDI che definisce un set di oggetti grafici e i relativi attributi associati, nonché le modalità grafiche che influiscono sull'output. Questo metodo restituisce il contesto del dispositivo ad eccezione di un tipo di carattere. Poiché un tipo di carattere non è selezionato, le chiamate al FromHdc metodo utilizzando un handle restituito dal GetHdc metodo avranno esito negativo.

Le chiamate ai GetHdc metodi e ReleaseHdc devono essere visualizzate in coppie. Durante l'ambito di una GetHdc coppia di metodi e ReleaseHdc , in genere, si effettuano solo chiamate alle funzioni GDI. Le chiamate in tale ambito effettuate ai metodi GDI+ dell'oggetto Graphics che ha generato il hdc parametro hanno esito negativo con un ObjectBusy errore. GDI+ ignora inoltre tutte le modifiche apportate allo Graphics stato del hdc parametro nelle operazioni successive.

Si applica a