Graphics.FromHwnd(IntPtr) Méthode

Définition

Crée un Graphics à partir du handle spécifié vers une fenêtre.

public:
 static System::Drawing::Graphics ^ FromHwnd(IntPtr hwnd);
public static System.Drawing.Graphics FromHwnd (IntPtr hwnd);
static member FromHwnd : nativeint -> System.Drawing.Graphics
Public Shared Function FromHwnd (hwnd As IntPtr) As Graphics

Paramètres

hwnd
IntPtr

nativeint

Descripteur désignant une fenêtre.

Retours

Cette méthode retourne un nouveau Graphics pour le handle de fenêtre spécifié.

Exemples

L’exemple de code suivant est conçu pour être utilisé avec Windows Forms, et il nécessite PaintEventArgse, qui est un paramètre du Paint gestionnaire d’événements, ainsi que thisForm, pour Form l’exemple. Le code effectue les actions suivantes :

  • Crée une variable hwnd de pointeur interne et la définit sur le handle du formulaire de l’exemple.

  • Crée un nouveau Graphics à partir du handle.

  • Dessine un rectangle vers le nouveau Graphics à l’aide d’un stylet rouge.

  • Supprime le nouveau Graphics.

public:
   void FromHwndHwnd( PaintEventArgs^ /*e*/ )
   {
      // Get handle to form.
      IntPtr hwnd = this->Handle;

      // Create new graphics object using handle to window.
      Graphics^ newGraphics = Graphics::FromHwnd( hwnd );

      // Draw rectangle to screen.
      newGraphics->DrawRectangle( gcnew Pen( Color::Red,3.0f ), 0, 0, 200, 100 );

      // Dispose of new graphics.
      delete newGraphics;
   }
private void FromHwndHwnd(PaintEventArgs e)
{

    // Get handle to form.
    IntPtr hwnd = this.Handle;

    // Create new graphics object using handle to window.
    Graphics newGraphics = Graphics.FromHwnd(hwnd);

    // Draw rectangle to screen.
    newGraphics.DrawRectangle(new Pen(Color.Red, 3), 0, 0, 200, 100);

    // Dispose of new graphics.
    newGraphics.Dispose();
}
Private Sub FromHwndHwnd(ByVal e As PaintEventArgs)

    ' Get handle to form.
    Dim hwnd As IntPtr = Me.Handle


    ' Create new graphics object using handle to window.
    Dim newGraphics As Graphics = Graphics.FromHwnd(hwnd)

    ' Draw rectangle to screen.
    newGraphics.DrawRectangle(New Pen(Color.Red, 3), 0, 0, 200, 100)

    ' Dispose of new graphics.
    newGraphics.Dispose()
End Sub

Remarques

Vous devez toujours appeler la Dispose méthode pour libérer les Graphics ressources et associées créées par la FromHwnd méthode.

S’applique à