×

VB.NET下使用GDI绘图

Kalet Kalet 发表于2009-03-20 12:00:14 浏览458 评论0

抢沙发发表评论

这几天正写这个。。

VB.NET code


Dim lDc as Long

lDc = CreateCompatibleDC(UserControl.hdc) '创建兼容内存DC

Debug.Print SelectObject(lDc, P.Handle) '将位图选入内存DC

'我这里的P是VB中的Picture对象。。我是先使用Loadpicture函数加载的图像。。

'当然你也可以用API LoadImage加载一幅图像。。P.Handle的位置写上Loadimage返回的位图句柄

'你画图的话使用API的绘图函数在这个lDc中绘。。 不过好像这个时候lDc的大小是P对象中图像的大小

'如果要自定义一个大小在lDc中的话要这样

'Dim lBmp

' lBmp = CreateCompatibleBitmap(lDc, Width,Height) '创建一幅自定义大小的位图。。

'SelectObject lDc, lBmp '将刚刚创建的位图选中内存Dc。。然后你就可以在这个lDc中绘图了。。就像在Me.Hdc中绘图一样的

Dim Bc As Long

Bc = GetPixel(lDc, 0, 0) '获得图像坐标0,0的颜色作为透明颜色



'下面是把内存lDc中的图拷到窗口Dc中的。。常用的好像是BitBlt吧。。不过我觉得这个更好用。。

Call GdiTransparentBlt(UserControl.hdc, 5, 5, priButSize - 10, priButSize - 10, lDc, 0, 0, PicDef.Width, PicDef.Height, Bc)





VB.NET code


'API声明。。现在用的都贴出来了。。不选了。。



Private Type POINTAPI

X As Long

Y As Long

End Type



Private Type TypePic

Pic As Picture '位图
VB.NET下使用GDI绘图
Width As Long '位图宽度(像素)

Height As Long '位图高度(像素)

BackColor As Long '背景颜色

ChuDi As Boolean '是否以作处理



End Type



Private Const WINDING = 2



Private Declare Function CreateCompatibleDC Lib "gdi32" (ByVal hdc As Long) As Long

Private Declare Function CreatePatternBrush Lib "gdi32" (ByVal hBitmap As Long) As Long

Private Declare Function FillRgn Lib "gdi32" (ByVal hdc As Long, ByVal hRgn As Long, ByVal hBrush As Long) As Long

Private Declare Function CreateRectRgn Lib "gdi32" (ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long) As Long

Private Declare Function GdiTransparentBlt Lib "gdi32" (ByVal hdc1 As Long, ByVal X1 As Long, ByVal Y1 As Long, ByVal W1 As Long, ByVal H1 As Long, ByVal Hdc2 As Long, ByVal X2 As Long, ByVal Y2 As Long, ByVal W2 As Long, ByVal H2 As Long, ByVal Color As Long) As Long

Private Declare Function SelectObject Lib "gdi32" (ByVal hdc As Long, ByVal hObject As Long) As Long

Private Declare Function BitBlt Lib "gdi32" (ByVal hDestDC As Long, ByVal X As Long, ByVal Y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal dwRop As Long) As Long

Private Declare Function CreateSolidBrush Lib "gdi32" (ByVal crColor As Long) As Long

Private Declare Function CreatePen Lib "gdi32" (ByVal nPenStyle As Long, ByVal nWidth As Long, ByVal crColor As Long) As Long





'获取DC某一点的颜色

Private Declare Function GetPixel Lib "gdi32" (ByVal hdc As Long, ByVal X As Long, ByVal Y As Long) As Long



'删除句柄

Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long



'创建位图

Private Declare Function CreateCompatibleBitmap Lib "gdi32" (ByVal hdc As Long, ByVal nWidth As Long, ByVal nHeight As Long) As Long



'画圆角形

Private Declare Function RoundRect Lib "gdi32" (ByVal hdc As Long, ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long, ByVal X3 As Long, ByVal Y3 As Long) As Long



'画三角形

Private Declare Function PolyPolygon Lib "gdi32" (ByVal hdc As Long, lpPoint As POINTAPI, lpPolyCounts As Long, ByVal nCount As Long) As Long



Private Declare Function Polygon Lib "gdi32" (ByVal hdc As Long, lpPoint As POINTAPI, ByVal nCount As Long) As Long

Private Declare Function CreatePolygonRgn Lib "gdi32" (lpPoint As POINTAPI, ByVal nCount As Long, ByVal nPolyFillMode As Long) As Long
VB.NET下使用GDI绘图


群贤毕至

访客