共用方式為


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 事件處理程式的參數,以及 thisForm,此範例 Form。 程式代碼會執行下列動作:

  • 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 方法來釋放 FromHwnd 方法所建立的 Graphics 和相關資源。

適用於