|
-
May 1st, 2000, 03:25 AM
#1
Hi all,
I have been thinking of this problem-I am new to VB and have been finding difficulty solving this problem.
How do you draw squares on the form using the mouse where the upper left coordinate is the location where the user first pressed the mouse button and the lower right coordinate is where the user releases the mouse button. Also is it possible for the user to see the size of the square as he drags the mouse so he will know exactly what the square size will be when the mouse button is released?
-
May 1st, 2000, 04:08 AM
#2
Addicted Member
The following code will draw a square on a form as you requested...Just click and drag out your square.
VB Code:
Option Explicit
Private Rubberbanding As Boolean
Private OldMode As Integer
Private OldStyle As Integer
Private FirstX As Single
Private FirstY As Single
Private LastX As Single
Private LastY As Single
' Start rubberbanding.
Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
' Let MouseMove know we are rubberbanding.
Rubberbanding = True
' Save values so we can restore them later.
OldMode = DrawMode
OldStyle = DrawStyle
DrawMode = vbInvert
DrawStyle = vbDot
' Save the starting coordinates.
FirstX = X
FirstY = Y
' Draw the initial rubberband box.
LastX = X
LastY = Y
Line (FirstX, FirstY)-(LastX, LastY), , B
End Sub
' Continue rubberbanding.
Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
' If we are not rubberbanding, do nothing.
If Not Rubberbanding Then Exit Sub
' Erase the previous rubberband box.
Line (FirstX, FirstY)-(LastX, LastY), , B
' Draw the new rubberband box.
LastX = X
LastY = Y
Line (FirstX, FirstY)-(LastX, LastY), , B
End Sub
' Stop rubberbanding.
Private Sub Form_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
Dim oldfill As Integer
Dim oldcolor As Long
' If we are not rubberbanding, do nothing.
If Not Rubberbanding Then Exit Sub
' We are no longer rubberbanding.
Rubberbanding = False
' Erase the previous rubberband box.
Line (FirstX, FirstY)-(LastX, LastY), , B
' Restore the original DrawMode and DrawStyle.
DrawMode = OldMode
DrawStyle = OldStyle
' Fill the final box with a random color.
oldfill = FillStyle
oldcolor = FillColor
FillStyle = vbSolid
FillColor = QBColor(Int(Rnd * 16))
Line (FirstX, FirstY)-(LastX, LastY), , B
FillStyle = oldfill
FillColor = oldcolor
End Sub
Hope this helps you
GRAHAM
-
May 1st, 2000, 04:38 AM
#3
You can do the same thing with the Line function, but if you place a Shape control on your form and initially set its Visible property to False, this should do waht you want.
VB Code:
Option Explicit
Private sglXStart As Single
Private sglYStart As Single
Private bDraw As Boolean
Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
sglXStart = X
sglYStart = Y
bDraw = True
End Sub
Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
DrawTheSquare X, Y
End Sub
Public Sub DrawTheSquare(X As Single, Y As Single)
If bDraw Then
Shape1.Top = sglYStart
Shape1.Left = sglXStart
Shape1.Height = Y - sglYStart
Shape1.Width = Shape1.Height
Shape1.Visible = True
End If
End Sub
Private Sub Form_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
bDraw = False
End Sub
-
May 1st, 2000, 05:37 AM
#4
Hi Graham and MartinLiss,
Thank you both very much.
Regards,
Mashi.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|