62,046
社区成员
发帖
与我相关
我的任务
分享
public class GDI
{
[System.Runtime.InteropServices.DllImport("gdi32.dll")]
internal static extern bool Rectangle(
IntPtr hdc,
int ulCornerX, int ulCornerY,
int lrCornerX, int lrCornerY);
}
[System.Security.Permissions.SecurityPermission(
System.Security.Permissions.SecurityAction.LinkDemand, Flags =
System.Security.Permissions.SecurityPermissionFlag.UnmanagedCode)]
public void GetHdcForGDI1(PaintEventArgs e)
{
//实例化pen.
Pen redPen = new Pen(Color.Red, 1);
// 用GDI+绘制矩形.
e.Graphics.DrawRectangle(redPen, 10, 10, 100, 50);
//获取上下文handle.
IntPtr hdc = e.Graphics.GetHdc();
//用自定义GDI绘制矩形.
GDI.Rectangle(hdc, 10, 70, 110, 120);
//释放上下文handle.
e.Graphics.ReleaseHdc(hdc);
}