Graphics.FromHwnd(IntPtr) Metodo

Definizione

Crea un nuovo oggetto Graphics dall'handle specificato per una finestra.

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

Parametri

hwnd
IntPtr

nativeint

Handle di una finestra.

Restituisce

Questo metodo restituisce un nuovo oggetto Graphics per l'handle di finestra specificato.

Esempio

L'esempio di codice seguente è progettato per l'uso con Windows Forms e richiede PaintEventArgse, che è un parametro del Paint gestore eventi, nonché thisForm, per l'esempioForm. Il codice esegue le azioni seguenti:

  • Crea una nuova variabile del puntatore hwnd interno e la imposta sull'handle del modulo dell'esempio.

  • Crea un nuovo Graphics oggetto dall'handle.

  • Disegna un rettangolo al nuovo Graphics usando una penna rossa.

  • Elimina il nuovo Graphicsoggetto .

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

Commenti

È consigliabile chiamare sempre il Dispose metodo per rilasciare le Graphics risorse correlate e create dal FromHwnd metodo .

Si applica a