Graphics.FromHwnd(IntPtr) Methode

Definition

Erstellt ein neues Graphics vom angegebenen Handle zu einem Fenster.

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

Parameter

hwnd
IntPtr

nativeint

Behandeln eines Fensters.

Gibt zurück

Diese Methode gibt ein neues Graphics für das angegebene Fensterhandle zurück.

Beispiele

Das folgende Codebeispiel wurde für die Verwendung mit Windows Forms entwickelt und erfordert PaintEventArgse, bei dem es sich um einen Parameter des Paint-Ereignishandlers sowie thisForm, dem Form für das Beispiel handelt. Der Code führt die folgenden Aktionen aus:

  • Erstellt eine neue interne Zeigervariable hwnd und legt sie auf das Handle des Formulars des Beispiels fest.

  • Erstellt ein neues Graphics aus dem Handle.

  • Zeichnet ein Rechteck mit einem roten Stift auf das neue Graphics .

  • Entfernt das neue 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

Hinweise

Sie sollten die Methode immer aufrufen, um die DisposeGraphics von der FromHwnd Methode erstellten und zugehörigen Ressourcen freizugeben.

Gilt für: