Graphics.ReleaseHdc 方法  
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
重载
ReleaseHdc()
- Source:
- Graphics.cs
- Source:
- Graphics.cs
- Source:
- Graphics.cs
- Source:
- Graphics.cs
- Source:
- Graphics.cs
- Source:
- Graphics.cs
public:
 virtual void ReleaseHdc();public void ReleaseHdc();abstract member ReleaseHdc : unit -> unit
override this.ReleaseHdc : unit -> unitPublic Sub ReleaseHdc ()实现
注解
GetHdc 和 ReleaseHdc 是两种方法,可用于获取和释放 Windows 设备的句柄。 完成 Windows 句柄后,应始终按照调用 GetHdcReleaseHdc。
另请参阅
适用于
ReleaseHdc(IntPtr)
- Source:
- Graphics.cs
- Source:
- Graphics.cs
- Source:
- Graphics.cs
- Source:
- Graphics.cs
- Source:
- Graphics.cs
- Source:
- Graphics.cs
public:
 void ReleaseHdc(IntPtr hdc);public void ReleaseHdc(IntPtr hdc);member this.ReleaseHdc : nativeint -> unitPublic Sub ReleaseHdc (hdc As IntPtr)参数
示例
下面的代码示例设计用于 Windows 窗体,它需要 PaintEventArgse,这是 Paint 事件处理程序的参数。 该示例演示如何调用 Windows GDI 函数以执行与 GDI+ Graphics 方法相同的任务。 该代码执行以下操作:
- 定义 Windows DLL 文件 gdi32.dll的互操作性 DllImportAttribute 属性。 此 DLL 包含所需的 GDI 函数,并将该 DLL 中的 Rectangle 函数定义为外部函数。 
- 创建红色笔。 
- 使用笔,使用 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 结构,用于定义一组图形对象及其关联属性,以及影响输出的图形模式。
对 GetHdc 和 ReleaseHdc 方法的调用必须成对显示。 在 GetHdc 和 ReleaseHdc 方法对的范围内,通常只对 GDI 函数进行调用。 对生成 hdc 参数的 Graphics 的 GDI+ 方法进行的调用失败,并出现 ObjectBusy 错误。 此外,GDI+ 会忽略后续操作中对 hdc 参数 Graphics 所做的任何状态更改。