Graphics.FromHwnd(IntPtr) 方法
定义
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
窗口的句柄。Handle to a window.
返回
此方法为指定的窗口句柄返回新的 Graphics。This method returns a new Graphics for the specified window handle.
示例
下面的代码示例旨在与 Windows 窗体一起使用,并且它需要作为 PaintEventArgs e 事件处理程序的参数,以及 Paint thisForm Form 示例的。The following code example is designed for use with Windows Forms, and it requires PaintEventArgse, which is a parameter of the Paint event handler, as well as thisForm, the Form for the example. 此代码执行以下操作:The code performs the following actions:
创建新的内部指针变量
hwnd,并将其设置为该示例的窗体的句柄。Creates a new internal pointer variablehwndand sets it to the handle of the example's form.使用红笔将矩形绘制到新的 Graphics 。Draws a rectangle to the new Graphics using a red pen.
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 。You should always call the Dispose method to release the Graphics and related resources created by the FromHwnd method.