Graphics.FromHwnd(IntPtr) 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
從指定的 handle 建立一個新的 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
把手拿到窗邊。
傳回
此方法會回傳指定的視窗 handle 的新值 Graphics 。
範例
以下程式碼範例是為 Windows Forms 設計的,並需要 PaintEventArgse,這是 Paint 事件處理程序的參數,以及 thisForm,即本例的 Form。 此程式碼會執行下列動作:
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 方法所產生的相關資源。