Graphics.GetHdc 方法

定义

获取与此 Graphics 关联的设备上下文的句柄。

public:
 virtual IntPtr GetHdc();
public:
 IntPtr GetHdc();
public IntPtr GetHdc ();
abstract member GetHdc : unit -> nativeint
override this.GetHdc : unit -> nativeint
member this.GetHdc : unit -> nativeint
Public Function GetHdc () As IntPtr

返回

IntPtr

nativeint

与此 Graphics 关联的设备上下文的句柄。

实现

示例

下面的代码示例旨在与 Windows 窗体 一起使用,它需要 PaintEventArgse,这是事件处理程序的参数Paint。 该示例演示了调用 Windows GDI 函数以执行与 GDI+ Graphics 方法相同的任务。 此代码执行以下操作:

  • 定义 Windows DLL 文件gdi32.dll的互操作性 DllImportAttribute 属性。 此 DLL 包含所需的 GDI 函数。

  • Rectangle 该 DLL 中的函数定义为外部函数。

  • 创建红色笔。

  • 使用触控笔,使用 GDI+ DrawRectangle 方法在屏幕上绘制矩形。

  • 定义内部指针类型变量 hdc ,并将其值设置为窗体的设备上下文的句柄。

  • 使用 GDI Rectangle 函数在屏幕上绘制矩形。

  • 释放 参数 hdc 表示的设备上下文。

private:
   [System::Runtime::InteropServices::DllImportAttribute("gdi32.dll")]
   static bool Rectangle( IntPtr hdc, int ulCornerX, int ulCornerY, int lrCornerX, int lrCornerY );

public:
   void GetHdcForGDI1( PaintEventArgs^ e )
   {
      // Create pen.
      Pen^ redPen = gcnew Pen( Color::Red,1.0f );

      // Draw rectangle with GDI+.
      e->Graphics->DrawRectangle( redPen, 10, 10, 100, 50 );

      // Get handle to device context.
      IntPtr hdc = e->Graphics->GetHdc();

      // Draw rectangle with GDI using default pen.
      Rectangle( hdc, 10, 70, 110, 120 );

      // Release handle to device context.
      e->Graphics->ReleaseHdc( hdc );
   }
public class GDI
{
    [System.Runtime.InteropServices.DllImport("gdi32.dll")]
    internal static extern bool Rectangle(
       IntPtr hdc,
       int ulCornerX, int ulCornerY,
       int lrCornerX, int lrCornerY);
}

private void GetHdcForGDI1(PaintEventArgs e)
{
    // Create pen.
    Pen redPen = new Pen(Color.Red, 1);

    // Draw rectangle with GDI+.
    e.Graphics.DrawRectangle(redPen, 10, 10, 100, 50);

    // Get handle to device context.
    IntPtr hdc = e.Graphics.GetHdc();

    // Draw rectangle with GDI using default pen.
    GDI.Rectangle(hdc, 10, 70, 110, 120);

    // Release handle to device context.
    e.Graphics.ReleaseHdc(hdc);
}
Public Class GDI
    <System.Runtime.InteropServices.DllImportAttribute("gdi32.dll")> _
    Friend Shared Function Rectangle(ByVal hdc As IntPtr, _
    ByVal ulCornerX As Integer, ByVal ulCornerY As Integer, ByVal lrCornerX As Integer, _
    ByVal lrCornerY As Integer) As Boolean
    End Function
End Class

<System.Security.Permissions.SecurityPermission( _
System.Security.Permissions.SecurityAction.LinkDemand, Flags:= _
System.Security.Permissions.SecurityPermissionFlag.UnmanagedCode)> _
Private Sub GetHdcForGDI1(ByVal e As PaintEventArgs)

    ' Create pen.
    Dim redPen As New Pen(Color.Red, 1)

    ' Draw rectangle with GDI+.
    e.Graphics.DrawRectangle(redPen, 10, 10, 100, 50)

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

    ' Draw rectangle with GDI using default pen.
    GDI.Rectangle(hdc, 10, 70, 110, 120)

    ' Release handle to device context.
    e.Graphics.ReleaseHdc(hdc)
End Sub

注解

设备上下文是基于 GDI 的 Windows 结构,它定义一组图形对象及其关联属性,以及影响输出的图形模式。 此方法返回设备上下文,但字体除外。 由于未选择字体,因此使用方法GetHdc返回的句柄调用 FromHdc 方法将失败。

GetHdcReleaseHdc 方法的调用必须成对显示。 在 和 ReleaseHdc 方法对的范围内GetHdc,通常只调用 GDI 函数。 在该范围内对生成 hdc 参数的 GDIGraphics+ 方法的调用失败并出现ObjectBusy错误。 此外,GDI+ 会忽略后续操作中对 Graphics 参数 的任何 hdc 状态更改。

适用于