Graphics.ReleaseHdc 方法

定义

释放通过以前对此 GetHdc()Graphics 方法的调用获得的设备上下文句柄。

重载

ReleaseHdc()

释放通过以前对此 GetHdc()Graphics 方法的调用获得的设备上下文句柄。

ReleaseHdc(IntPtr)

释放通过以前对此 GetHdc()Graphics 方法的调用获得的设备上下文句柄。

ReleaseHdc()

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

释放通过以前对此 GetHdc()Graphics 方法的调用获得的设备上下文句柄。

public:
 virtual void ReleaseHdc();
public void ReleaseHdc ();
abstract member ReleaseHdc : unit -> unit
override this.ReleaseHdc : unit -> unit
Public Sub ReleaseHdc ()

实现

注解

GetHdcReleaseHdc 是两种方法,可用于获取和释放 Windows 设备的句柄。 使用完 Windows 句柄后,应始终按照 GetHdc 调用 和 调用 ReleaseHdc

另请参阅

适用于

ReleaseHdc(IntPtr)

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

释放通过以前对此 GetHdc()Graphics 方法的调用获得的设备上下文句柄。

public:
 void ReleaseHdc(IntPtr hdc);
public void ReleaseHdc (IntPtr hdc);
member this.ReleaseHdc : nativeint -> unit
Public Sub ReleaseHdc (hdc As IntPtr)

参数

hdc
IntPtr

nativeint

通过以前对此 GraphicsGetHdc() 方法的调用获得的设备上下文句柄。

示例

下面的代码示例旨在与 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 Rectangle2( IntPtr hdc, int ulCornerX, int ulCornerY, int lrCornerX, int lrCornerY );

public:
   void GetHdcForGDI2( 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.
      Rectangle2( hdc, 10, 70, 110, 120 );

      // Release handle to device context.
      e->Graphics->ReleaseHdc( hdc );
   }
[System.Runtime.InteropServices.DllImportAttribute("gdi32.dll")]
private static extern bool Rectangle2(
    IntPtr hdc,
    int ulCornerX, int ulCornerY,
    int lrCornerX, int lrCornerY);

private void GetHdcForGDI2(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.
    Rectangle2(hdc, 10, 70, 110, 120);

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

<System.Security.Permissions.SecurityPermission( _
System.Security.Permissions.SecurityAction.LinkDemand, Flags:= _
System.Security.Permissions.SecurityPermissionFlag.UnmanagedCode)> _
Private Sub GetHdcForGDI2(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.
    Rectangle2(hdc, 10, 70, 110, 120)

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

注解

设备上下文是基于 GDI 的 Windows 结构,它定义一组图形对象及其关联属性,以及影响输出的图形模式。

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

适用于