Graphics.FromHwnd(IntPtr) 方法

定義

從指定之視窗控制代碼建立新的 Graphics

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

參數

hwnd
IntPtr

nativeint

視窗的控點。

傳回

這個方法會傳回指定之視窗控制代碼的新 Graphics

範例

下列程式代碼範例是專為搭配 Windows Forms 使用而設計,而且它需要 PaintEventArgse,這是事件處理程式的參數Paint,以及 thisFormForm 範例的 。 此程式碼會執行下列動作:

  • 建立新的內部指標變數 hwnd ,並將其設定為範例窗體的句柄。

  • 從句柄建立新的 Graphics

  • 使用紅色畫筆將矩形繪製到新的 Graphics

  • 處置新的 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

備註

您應該一律呼叫 Dispose 方法,以釋放 Graphics 方法所 FromHwnd 建立的 和相關資源。

適用於