Graphics.FromHwnd(IntPtr) Methode

Definition

Erstellt ein neues Graphics aus dem angegebenen Handle für ein 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

Das Fensterhandle.

Gibt zurück

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

Beispiele

Das folgende Codebeispiel ist für die Verwendung mit Windows Forms konzipiert und erfordert PaintEventArgse, was ein Parameter des Paint Ereignishandlers ist, sowie thisForm, für Form das Beispiel. Der Code führt die folgenden Aktionen aus:

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

  • Erstellt eine neue Graphics aus dem Handle.

  • Zeichnet ein Rechteck mit einem roten Stift in 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 immer die Dispose -Methode aufrufen, um die von der GraphicsFromHwnd -Methode erstellten und zugehörigen Ressourcen freizugeben.

Gilt für: