入吾QQ群183435019 (学习 交流+唠嗑)、
废话不说,咱们来看代码吧
程序结果运行如下
需要如下API
1:GdipCreateFromHDC
功能:创建设备场景相对应的绘图区域(相当于给设备场景创建一个画板),我们所需要画图的地方就是这个API所创建的
声明Private Declare Function GdipCreateFromHDC Lib "gdiplus" (ByVal hDC As Long, graphics As Long) As GpStatus
2:GdipCreateSolidFill
功能:创建一个纯色刷子,最重要支持半透明呦
声明Private Declare Function GdipCreateSolidFill Lib "gdiplus" (ByVal argb As Long, Brush As Long) As GpStatus
其中参数1是颜色值,a代表透明度,那么创建一个半透明刷子所需要传的值就是半 (透明度值+R+G+B)参数2是返回给我们的刷子句柄
例如&HFFFF0000
第一个FF表示透明度。FF是255 也就是不透明,后面的代码RGB颜色
3:GdipFillRectangle
功能,填充一个矩形
声明Private Declare Function GdipFillRectangle Lib "gdiplus" (ByVal Graphics As Long, ByVal Brush As Long, ByVal X As Single, ByVal Y As Single, ByVal Width As Single, ByVal Height As Single) As GpStatus
参数很简单,参数一 GdipCreateFromHDC所创建的dc,参数2 刷子句柄,参数3456,矩形的大小及位置
4:GdiplusStartup
功能:函数初始化GDI+,执行GDI+函数时,必须调用此函数
声明Private Declare Function GdiplusStartup Lib "gdiplus" (token As Long, inputbuf As GdiplusStartupInput, Optional ByVal outputbuf As Long = 0) As GpStatus
5:GdiplusShutdown
功能:清理资源
必须在创建任意的GDI+对象之前使用GdiplusStartup函数。且必须在使用该函数之前删除所有的GDI+的对象
声明Private Declare Sub GdiplusShutdown Lib "gdiplus" (ByVal token As Long)
参数token 为GdiplusStartup调用时的第一个参数
6:GdipDeleteBrush
功能:删除画刷
声明 Private Declare Function GdipDeleteBrush Lib "gdiplus" (ByVal Brush As Long) As GpStatus
不用这个画刷时要记得删除
7:GdipDeleteGraphics
功能:删除一个Graphics ,也就是GdipCreateFromHDC所创建的
声明Private Declare Function GdipDeleteGraphics Lib "gdiplus" (ByVal Graphics As Long) As GpStatus
还有一个GdiplusStartupInput结构体
声明
Private Type GdiplusStartupInput
GdiplusVersion As Long
DebugEventCallback As Long
SuppressBackgroundThread As Long
SuppressExternalCodecs As Long
End Type
一个枚举
Private Enum GpStatus
Ok = 0
GenericError = 1
InvalidParameter = 2
OutOfMemory = 3
ObjectBusy = 4
InsufficientBuffer = 5
NotImplemented = 6
Win32Error = 7
WrongState = 8
Aborted = 9
FileNotFound = 10
ValueOverflow = 11
AccessDenied = 12
UnknownImageFormat = 13
FontFamilyNotFound = 14
FontStyleNotFound = 15
NotTrueTypeFont = 16
UnsupportedGdiplusVersion = 17
GdiplusNotInitialized = 18
PropertyNotFound = 19
PropertyNotSupported = 20
End Enum
程序所有代码如下
Private Type GdiplusStartupInput
GdiplusVersion As Long
DebugEventCallback As Long
SuppressBackgroundThread As Long
SuppressExternalCodecs As Long
End Type
Private Enum GpStatus
Ok = 0
GenericError = 1
InvalidParameter = 2
OutOfMemory = 3
ObjectBusy = 4
InsufficientBuffer = 5
NotImplemented = 6
Win32Error = 7
WrongState = 8
Aborted = 9
FileNotFound = 10
ValueOverflow = 11
AccessDenied = 12
UnknownImageFormat = 13
FontFamilyNotFound = 14
FontStyleNotFound = 15
NotTrueTypeFont = 16
UnsupportedGdiplusVersion = 17
GdiplusNotInitialized = 18
PropertyNotFound = 19
PropertyNotSupported = 20
End Enum
Private Declare Function GdipCreateFromHDC Lib "gdiplus" (ByVal hDC As Long, Graphics As Long) As GpStatus
Private Declare Function GdipCreateSolidFill Lib "gdiplus" (ByVal argb As Long, Brush As Long) As GpStatus
Private Declare Function GdiplusStartup Lib "gdiplus" (token As Long, inputbuf As GdiplusStartupInput, Optional ByVal outputbuf As Long = 0) As GpStatus
Private Declare Sub GdiplusShutdown Lib "gdiplus" (ByVal token As Long)
Private Declare Function GdipDeleteBrush Lib "gdiplus" (ByVal Brush As Long) As GpStatus
Private Declare Function GdipDeleteGraphics Lib "gdiplus" (ByVal Graphics As Long) As GpStatus
Dim mToken As Long, Graphics As Long, Brush As Long
Private Declare Function GdipFillRectangle Lib "gdiplus" (ByVal Graphics As Long, ByVal Brush As Long, ByVal X As Single, ByVal Y As Single, ByVal Width As Single, ByVal Height As Single) As GpStatus
Private Sub Form_Load()
Me.AutoRedraw = True
Dim GdipInput As GdiplusStartupInput
GdipInput.GdiplusVersion = 1
GdiplusStartup mToken, GdipInput
GdipCreateFromHDC Me.hDC, Graphics
GdipCreateSolidFill &H55FF0000, Brush
GdipCreateSolidFill &H550ED64B, Brush1
GdipFillRectangle Graphics, Brush, 0, 0, 100, 100 ‘画一个红色半透明矩形
GdipFillRectangle Graphics, Brush1, 50, 0, 100, 100 ‘画一个绿色半透明矩形
GdipDeleteBrush Brush ‘删除画刷 Brush
GdipDeleteBrush Brush1 ‘删除画刷 Brush1
GdipDeleteGraphics Graphics
GdiplusShutdown mToken
End Sub