Graphics.FromHwnd(IntPtr) 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
從指定的句柄建立新的 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。 程式代碼會執行下列動作:
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 和相關資源。