Graphics.GetHdc 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
获取与此 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
返回
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结构,用于定义一组图形对象及其关联的属性,以及影响输出的图形模式。 此方法返回除字体外的设备上下文。 由于未选择字体,因此使用方法FromHdc返回的句柄对GetHdc方法的调用将失败。
对 GetHdc 和 ReleaseHdc 方法的调用必须成对显示。 在方法和GetHdcReleaseHdc方法对的范围内,通常只对 GDI 函数进行调用。 在该范围内对生成Graphics参数的 hdc GDI+ 方法进行的调用失败,并出现ObjectBusy错误。 此外,GDI+ 会忽略在后续操作中对参数所做的Graphicshdc任何状态更改。