Graphics.GetHdc Méthode

Définition

Obtient le handle vers le contexte de périphérique associé à ce 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

Retours

IntPtr

nativeint

Handle vers le contexte de périphérique associé à ce Graphics.

Implémente

Exemples

L’exemple de code suivant est conçu pour être utilisé avec Windows Forms et nécessite PaintEventArgse, qui est un paramètre du Paint gestionnaire d’événements. L’exemple illustre l’appel d’une fonction GDI Windows pour effectuer la même tâche qu’une méthode GDI+ Graphics . Le code effectue les actions suivantes :

  • Définit l’attribut d’interopérabilité DllImportAttribute pour le fichier DLL Windows gdi32.dll. Cette DLL contient la fonction GDI souhaitée.

  • Définit la Rectangle fonction dans cette DLL comme externe.

  • Crée un stylet rouge.

  • Avec le stylet, dessine un rectangle vers l’écran à l’aide de la méthode GDI+ DrawRectangle .

  • Définit une variable hdc de type pointeur interne et définit sa valeur sur le handle dans le contexte d’appareil du formulaire.

  • Dessine un rectangle vers l’écran à l’aide de la fonction GDI Rectangle .

  • Libère le contexte de l’appareil représenté par le hdc paramètre.

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

Remarques

Le contexte d’appareil est une structure Windows basée sur GDI qui définit un ensemble d’objets graphiques et leurs attributs associés, ainsi que les modes graphiques qui affectent la sortie. Cette méthode retourne ce contexte d’appareil à l’exception d’une police. Étant donné qu’une police n’est pas sélectionnée, les appels à la méthode à l’aide FromHdc d’un handle retourné par la GetHdc méthode échouent.

Les appels aux GetHdc méthodes et ReleaseHdc doivent apparaître par paires. Pendant l’étendue d’une GetHdc paire de méthodes et ReleaseHdc , en général, vous effectuez uniquement des appels aux fonctions GDI. Les appels dans cette étendue effectués aux méthodes GDI+ du Graphics qui ont produit le hdc paramètre échouent avec une ObjectBusy erreur. En outre, GDI+ ignore les modifications d’état apportées au Graphics du paramètre dans les hdc opérations suivantes.

S’applique à