i am making a makeup and hairstyle application in vb and i dont have an idea of how i can do it, one of the posters here in daniweb suggested this code for hairstyles to me. and i cant understand a thing in it. XD please help me guys, my application needs make-up and haistyle. if someone can understand this code, kindly put some comments there for me, thanks :)
Public Class Form3
Private HairStyles As New List(Of HairStyle)
Private ActiveHairStyle As HairStyle
Private MouseIsDown As Boolean
Private CurrentAction As Action
Public Enum Action
None
Move
ResizeNW
ResizeSW
ResizeLeft
End Enum
Public Class HairStyle
Public Pic As Bitmap
Public bBox As Rectangle
Public referencePoint As Point
End Class
Private Sub picHair_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles picHair.MouseDown, PictureBox1.MouseDown
'Start the drag drop operation with the new hairstyle object
Dim thisStyle As New HairStyle
thisStyle.Pic = CType(CType(sender, PictureBox).Image, Bitmap)
DoDragDrop(thisStyle, DragDropEffects.All)
End Sub
Private Sub Form1_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles Me.DragEnter
'If this is a Hairstyle then allow the drop
If e.Data.GetDataPresent(GetType(HairStyle)) Then
e.Effect = DragDropEffects.All
End If
End Sub
Private Sub Form1_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles Me.DragDrop
If e.Data.GetDataPresent(GetType(HairStyle)) Then
'Set the active hair style as the one being dropped and drop it
'at the cursor location
HairStyles.Add(CType(e.Data.GetData(GetType(HairStyle)), HairStyle))
ActiveHairStyle = HairStyles(HairStyles.Count - 1)
ActiveHairStyle.bBox = New Rectangle(picMain.PointToClient(Cursor.Position), ActiveHairStyle.Pic.Size)
picMain.Invalidate()
End If
End Sub
Private Sub picMain_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles picMain.MouseDown
ActiveHairStyle = GetHairStyle(e.Location)
If ActiveHairStyle IsNot Nothing Then
CurrentAction = GetAction(ActiveHairStyle.bBox, e.Location)
If CurrentAction <> Action.None Then
MouseIsDown = True
If picMain.Cursor = Cursors.Arrow Then picMain.Cursor = Cursors.Hand
ActiveHairStyle.referencePoint = e.Location
End If
End If
picMain.Invalidate()
End Sub
Private Sub picMain_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles picMain.MouseMove
If ActiveHairStyle IsNot Nothing Then
If MouseIsDown Then
Select Case CurrentAction
Case Action.Move
'move the hairstyle to its new location
ActiveHairStyle.bBox.Location = ActiveHairStyle.bBox.Location + (CType(e.Location, Size) - CType(ActiveHairStyle.referencePoint, Size))
ActiveHairStyle.referencePoint = e.Location
Case Action.ResizeLeft
'move the hair style over to the left and increase its
'width by how much it moved
ActiveHairStyle.bBox.X = e.Location.X
ActiveHairStyle.bBox.Width += ActiveHairStyle.referencePoint.X - e.Location.X
ActiveHairStyle.referencePoint = e.Location
Case Action.ResizeNW
ActiveHairStyle.bBox.Location = e.Location
ActiveHairStyle.bBox.Width += ActiveHairStyle.referencePoint.X - e.Location.X
ActiveHairStyle.bBox.Height += ActiveHairStyle.referencePoint.Y - e.Location.Y
ActiveHairStyle.referencePoint = e.Location
End Select
picMain.Invalidate()
Else
'Do a hit test and change the cursor.
Select Case GetAction(ActiveHairStyle.bBox, e.Location)
Case Action.None
picMain.Cursor = Cursors.Arrow
Case Action.Move
picMain.Cursor = Cursors.Hand
Case Action.ResizeLeft
picMain.Cursor = Cursors.SizeWE
Case Action.ResizeNW
picMain.Cursor = Cursors.SizeNWSE
Case Action.ResizeSW
picMain.Cursor = Cursors.SizeNESW
End Select
End If
Else
If Not picMain.Cursor = Cursors.Arrow Then picMain.Cursor = Cursors.Arrow
End If
End Sub
Private Sub picMain_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles picMain.MouseUp
'reset the mouse is down
MouseIsDown = False
End Sub
Private Sub picMain_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles picMain.Paint
'Draw the hair style to the current size which the bBox provides.
For i = 0 To HairStyles.Count - 1
If ActiveHairStyle IsNot Nothing AndAlso ActiveHairStyle.Equals(HairStyles(i)) Then
Continue For
End If
e.Graphics.DrawImage(HairStyles(i).Pic, HairStyles(i).bBox)
Next
If ActiveHairStyle IsNot Nothing Then
e.Graphics.DrawImage(ActiveHairStyle.Pic, ActiveHairStyle.bBox)
'Draw a dotted line around the hairstyle
Dim dotPen As New Pen(Color.Black, 1)
dotPen.DashStyle = Drawing2D.DashStyle.Dot
e.Graphics.DrawRectangle(dotPen, ActiveHairStyle.bBox)
dotPen.Dispose()
End If
End Sub
Private Function GetAction(ByVal hairBox As Rectangle, ByVal ScreenLocation As Point) As Action
'Perform some hit testing.
'Stretch the box to add a little buffer
hairBox.Inflate(4, 4)
'Check if we're inside the hairbox
If hairBox.Contains(ScreenLocation) Then
'Checks if mouse if over the left side
If ScreenLocation.X <= ActiveHairStyle.bBox.Left + 4 Then
'the cursor is over the left side
If ScreenLocation.Y <= ActiveHairStyle.bBox.Top + 4 Then
'the cursor is over the top left corner
Return Action.ResizeNW
ElseIf ScreenLocation.Y >= ActiveHairStyle.bBox.Bottom - 4 Then
'the cursor is over the bottom left corner
Return Action.ResizeSW
Else
'the cursor is over the left side
Return Action.ResizeLeft
End If
Else
'the location is in the box. Check the other sides.
Return Action.Move
End If
Else
'Not in the box
Return Action.None
End If
End Function
Private Function GetHairStyle(ByVal screenLocation As Point)
Dim hairBox As Rectangle
For i = HairStyles.Count - 1 To 0 Step -1
hairBox = HairStyles(i).bBox
hairBox.Inflate(4, 4)
If hairBox.Contains(screenLocation) Then
Return HairStyles(i)
End If
Next
Return Nothing
End Function
Private Function GetFinalImage() As Bitmap
Dim finalImage As Bitmap = picMain.Image.Clone
Dim g As Graphics = Graphics.FromImage(finalImage)
g.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic
For i = 0 To HairStyles.Count - 1
g.DrawImage(HairStyles(i).Pic, HairStyles(i).bBox)
Next
g.Dispose()
Return finalImage
End Function
Private Sub PictureBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.Click
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
GetFinalImage()
End Sub
End Class