五笔打字通主页
VB给图片填充颜色代码
有时我们需要实现一个功能,就像WINDOWS自带画图软件上的“用颜色填充”功能,只要用鼠标点击图片的某个地方,颜色就自动填充到鼠标所在位置的封闭区域。
VB填充颜色这个功能,需要用VB 加 API函数来实现。
先在窗体建一个PictureBox控件,导入一个图片,这个图片里的图形要有封闭的区域,否则,填充时,颜色就会漏出来的。
我们先在窗体上申明API
Private Declare Function ExtFloodFill Lib "gdi32" (ByVal hdc As Long, ByVal X As Long, ByVal Y As Long, ByVal colorCode As Long, ByVal fillType As Long) As Long
然后建立一个专门填充的函数
Sub AreaFill(obj As Object, ByVal X As Long, ByVal Y As Long, ByVal colorCode As Long, Optional borderColor As Variant)
Dim X2 As Long, Y2 As Long
Dim saveFillStyle As Long
Dim saveFillColor As Long
With obj
X2 = .ScaleX(X, .ScaleMode, vbPixels)
Y2 = .ScaleY(Y, .ScaleMode, vbPixels)
saveFillStyle = .FillStyle
saveFillColor = .FillColor
.FillStyle = 0
.FillColor = colorCode
If IsMissing(borderColor) Then
borderColor = .Point(X, Y)
ExtFloodFill .hdc, X2, Y2, borderColor, 1
Else
ExtFloodFill .hdc, X2, Y2, borderColor, 0
End If
.FillStyle = saveFillStyle
.FillColor = saveFillColor
End With
End Sub
最后在 Picture1_MouseDown事件中写入以下代码
Private Sub Picture1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
AreaFill Picture1, X, Y, vbRed’最后一个参数,是表明用红色填充,你也可以改成别的颜色
End Sub
运行程序,用鼠标点击图片,你会发现你想要的用VB编程来实现颜色填充功能真的实现了。
程序效果图如下:
来源:济亨网
本文链接:https://wb98.com/post/169.html