Graphics.FromHdc 方法

定义

Graphics从指定句柄创建设备上下文的新句柄。

重载

名称 说明
FromHdc(IntPtr, IntPtr)

从指定句柄创建设备 Graphics 上下文和设备句柄的新句柄。

FromHdc(IntPtr)

Graphics从指定句柄创建设备上下文的新句柄。

FromHdc(IntPtr, IntPtr)

Source:
Graphics.cs
Source:
Graphics.cs
Source:
Graphics.cs
Source:
Graphics.cs
Source:
Graphics.cs
Source:
Graphics.cs
Source:
Graphics.cs
Source:
Graphics.cs

从指定句柄创建设备 Graphics 上下文和设备句柄的新句柄。

public:
 static System::Drawing::Graphics ^ FromHdc(IntPtr hdc, IntPtr hdevice);
public static System.Drawing.Graphics FromHdc(IntPtr hdc, IntPtr hdevice);
static member FromHdc : nativeint * nativeint -> System.Drawing.Graphics
Public Shared Function FromHdc (hdc As IntPtr, hdevice As IntPtr) As Graphics

参数

hdc
IntPtr

nativeint

设备上下文的句柄。

hdevice
IntPtr

nativeint

设备句柄。

返回

此方法为指定的设备上下文和设备返回一个新 Graphics 方法。

注解

应始终调用 Dispose 该方法来释放 Graphics 该方法创建 FromHdc 的相关资源。

即使显示设备具有关联的 ICM 颜色配置文件,GDI+ 也不会默认使用该配置文件。 若要为 A Graphics启用 ICM,请将 Graphics HDC (和 ICM_ON) 传递给 SetICMMode 函数后,从 HDC 构造该函数。 然后, Graphics 将根据与显示设备关联的 ICM 配置文件调整完成的任何绘图。 启用 ICM 将导致性能降低。

调用 FromHdc 时设备上下文(映射模式、逻辑单元等)的状态可能会影响完成的 Graphics呈现。

设备句柄通常用于查询特定打印机功能。

适用于

FromHdc(IntPtr)

Source:
Graphics.cs
Source:
Graphics.cs
Source:
Graphics.cs
Source:
Graphics.cs
Source:
Graphics.cs
Source:
Graphics.cs
Source:
Graphics.cs
Source:
Graphics.cs

Graphics从指定句柄创建设备上下文的新句柄。

public:
 static System::Drawing::Graphics ^ FromHdc(IntPtr hdc);
public static System.Drawing.Graphics FromHdc(IntPtr hdc);
static member FromHdc : nativeint -> System.Drawing.Graphics
Public Shared Function FromHdc (hdc As IntPtr) As Graphics

参数

hdc
IntPtr

nativeint

设备上下文的句柄。

返回

此方法为指定的设备上下文返回一个新 Graphics 方法。

示例

下面的代码示例设计用于 Windows 窗体,它需要 PaintEventArgse,这是 Paint 事件处理程序的参数。 该代码执行以下操作:

  • 创建内部指针类型变量 hdc ,并将其设置为窗体图形对象的设备上下文的句柄。

  • 使用 hdc. 创建一个新的图形对象。

  • 使用新图形对象绘制一个矩形(在屏幕上)。

  • 使用 hdc. 释放新的图形对象。

public:
   void FromHdcHdc( PaintEventArgs^ e )
   {
      // Get handle to device context.
      IntPtr hdc = e->Graphics->GetHdc();

      // Create new graphics object using handle to device context.
      Graphics^ newGraphics = Graphics::FromHdc( hdc );

      // Draw rectangle to screen.
      newGraphics->DrawRectangle( gcnew Pen( Color::Red,3.0f ), 0, 0, 200, 100 );

      // Release handle to device context and dispose of the      // Graphics object
      e->Graphics->ReleaseHdc( hdc );
      delete newGraphics;
   }
private void FromHdcHdc(PaintEventArgs e)
{
    // Get handle to device context.
    IntPtr hdc = e.Graphics.GetHdc();

    // Create new graphics object using handle to device context.
    Graphics newGraphics = Graphics.FromHdc(hdc);

    // Draw rectangle to screen.
    newGraphics.DrawRectangle(new Pen(Color.Red, 3), 0, 0, 200, 100);

    // Release handle to device context and dispose of the      // Graphics object
    e.Graphics.ReleaseHdc(hdc);
    newGraphics.Dispose();
}
<System.Security.Permissions.SecurityPermission( _
System.Security.Permissions.SecurityAction.LinkDemand, Flags := _
System.Security.Permissions.SecurityPermissionFlag.UnmanagedCode)> _
Private Sub FromHdcHdc(ByVal e As PaintEventArgs)

    ' Get handle to device context.
    Dim hdc As IntPtr = e.Graphics.GetHdc()

    ' Create new graphics object using handle to device context.
    Dim newGraphics As Graphics = Graphics.FromHdc(hdc)

    ' Draw rectangle to screen.
    newGraphics.DrawRectangle(New Pen(Color.Red, 3), 0, 0, 200, 100)

    ' Release handle to device context and dispose of the Graphics 	' object
    e.Graphics.ReleaseHdc(hdc)
    newGraphics.Dispose()
End Sub

注解

应始终调用 Dispose 该方法来释放 Graphics 该方法创建 FromHdc 的相关资源。

即使显示设备具有关联的 ICM 颜色配置文件,GDI+ 也不会默认使用该配置文件。 若要为 A Graphics启用 ICM,请将 Graphics HDC (和 ICM_ON) 传递给 SetICMMode 函数后,从 HDC 构造该函数。 然后, Graphics 将根据与显示设备关联的 ICM 配置文件调整完成的任何绘图。 启用 ICM 将导致性能降低。

调用 FromHdc 时设备上下文(映射模式、逻辑单元等)的状态可能会影响完成的 Graphics呈现。

适用于