Here's
a quickie one-form demo that shows how to use the ExtFloodFill API to construct a child's drawing canvas that can save and reload a picture.
When the left mouse is pressed, lines are drawn on the picturebox in
response to a drag. When the right mouse is pressed, a random colour fills the interior of the bounded shape. If the mouse is over the lines,
they change colour instead. The code is based on the "Simple Paint and Fill Demo" by Tanner Helland. |
Option Explicit
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Copyright ©1996-2011 VBnet/Randy Birch, All Rights Reserved.
' Some pages may also contain other copyrights by the author.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Distribution: You can freely use this code in your own
' applications, but you may not reproduce
' or publish this code on any web site,
' online service, or distribute as source
' on any media without express permission.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Option Explicit
Private Declare Function ExtFloodFill Lib "Gdi32" _
(ByVal hdc As Long, _
ByVal X As Long, _
ByVal Y As Long, _
ByVal crColor As Long, _
ByVal wFillType As Long) As Long
'variables used for drawing and filling
Dim X1 As Single
Dim Y1 As Single
Dim bDraw As Boolean
Private Sub Form_Load()
'used to create a random fill colour
Randomize Time
'initialize picturebox
With Picture1
.AutoRedraw = True
.ScaleMode = vbPixels
.FillStyle = vbFSSolid
End With
End Sub
Private Sub Form_Resize()
Picture1.Move 120, 600, Me.ScaleWidth - 240, Me.ScaleHeight - 1300
End Sub
Private Sub Command1_Click()
On Error Resume Next
With CommonDialog1
.CancelError = True
.Flags = cdlOFNExplorer Or cdlOFNFileMustExist Or cdlOFNLongNames
.Filter = "*.bmp|*.bmp"
.ShowOpen
If Len(.FileName) > 0 Then
Picture1.Picture = LoadPicture(.FileName)
End If
End With
load_exit:
Exit Sub
load_error:
Resume load_exit
End Sub
Private Sub Command2_Click()
On Error Resume Next
With CommonDialog1
.CancelError = True
.Flags = cdlOFNExplorer Or cdlOFNFileMustExist Or cdlOFNLongNames
.Filter = "*.bmp|*.bmp"
.ShowSave
If Len(.FileName) > 0 Then
SavePicture Picture1.Image, .FileName
End If
End With
save_exit:
Exit Sub
save_error:
Resume save_exit
End Sub
Private Sub Picture1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
'enable drawing
bDraw = True
'if left mouse button, set up the line drawing
If Button = vbLeftButton Then
X1 = X
Y1 = Y
'if right mouse button, fill
ElseIf Button = vbRightButton Then
Picture1.FillColor = RGB(Int(Rnd * 255), Int(Rnd * 255), Int(Rnd * 255))
ExtFloodFill Picture1.hdc, X, Y, Picture1.Point(X, Y), 1
End If
End Sub
Private Sub Picture1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
'if left mouse button, draw a
'line between the original point
'and the current one, but only
'if drawing is enabled
If bDraw Then
If Button = vbLeftButton Then
Picture1.Line (X1, Y1)-(X, Y)
X1 = X
Y1 = Y
End If
End If
End Sub
Private Sub Picture1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
'disable drawing
bDraw = False
End Sub |