Results 1 to 4 of 4

Thread: Simple(?)question

  1. #1
    Guest

    Exclamation

    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?

  2. #2
    Addicted Member
    Join Date
    Feb 2000
    Location
    CWMBRAN,WALES,UK
    Posts
    146
    The following code will draw a square on a form as you requested...Just click and drag out your square.

    VB Code:
    1. Option Explicit
    2.  
    3. Private Rubberbanding As Boolean
    4. Private OldMode As Integer
    5. Private OldStyle As Integer
    6. Private FirstX As Single
    7. Private FirstY As Single
    8. Private LastX As Single
    9. Private LastY As Single
    10.  
    11. ' Start rubberbanding.
    12. Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    13.  
    14.     ' Let MouseMove know we are rubberbanding.
    15.     Rubberbanding = True
    16.  
    17.     ' Save values so we can restore them later.
    18.     OldMode = DrawMode
    19.     OldStyle = DrawStyle
    20.     DrawMode = vbInvert
    21.     DrawStyle = vbDot
    22.  
    23.     ' Save the starting coordinates.
    24.     FirstX = X
    25.     FirstY = Y
    26.  
    27.     ' Draw the initial rubberband box.
    28.     LastX = X
    29.     LastY = Y
    30.     Line (FirstX, FirstY)-(LastX, LastY), , B
    31. End Sub
    32.  
    33. ' Continue rubberbanding.
    34. Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    35.  
    36.     ' If we are not rubberbanding, do nothing.
    37.     If Not Rubberbanding Then Exit Sub
    38.  
    39.     ' Erase the previous rubberband box.
    40.     Line (FirstX, FirstY)-(LastX, LastY), , B
    41.  
    42.     ' Draw the new rubberband box.
    43.     LastX = X
    44.     LastY = Y
    45.     Line (FirstX, FirstY)-(LastX, LastY), , B
    46. End Sub
    47.  
    48. ' Stop rubberbanding.
    49. Private Sub Form_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
    50.  
    51. Dim oldfill As Integer
    52. Dim oldcolor As Long
    53.  
    54.     ' If we are not rubberbanding, do nothing.
    55.     If Not Rubberbanding Then Exit Sub
    56.  
    57.     ' We are no longer rubberbanding.
    58.     Rubberbanding = False
    59.  
    60.     ' Erase the previous rubberband box.
    61.     Line (FirstX, FirstY)-(LastX, LastY), , B
    62.  
    63.     ' Restore the original DrawMode and DrawStyle.
    64.     DrawMode = OldMode
    65.     DrawStyle = OldStyle
    66.  
    67.     ' Fill the final box with a random color.
    68.     oldfill = FillStyle
    69.     oldcolor = FillColor
    70.     FillStyle = vbSolid
    71.     FillColor = QBColor(Int(Rnd * 16))
    72.  
    73.     Line (FirstX, FirstY)-(LastX, LastY), , B
    74.  
    75.     FillStyle = oldfill
    76.     FillColor = oldcolor
    77. End Sub

    Hope this helps you
    GRAHAM

  3. #3
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431
    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:
    1. Option Explicit
    2.  
    3. Private sglXStart As Single
    4. Private sglYStart As Single
    5. Private bDraw As Boolean
    6.  
    7. Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    8.  
    9.     sglXStart = X
    10.     sglYStart = Y
    11.     bDraw = True
    12.  
    13. End Sub
    14.  
    15. Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    16. DrawTheSquare X, Y
    17. End Sub
    18.  
    19. Public Sub DrawTheSquare(X As Single, Y As Single)
    20.  
    21. If bDraw Then
    22.     Shape1.Top = sglYStart
    23.     Shape1.Left = sglXStart
    24.     Shape1.Height = Y - sglYStart
    25.     Shape1.Width = Shape1.Height
    26.     Shape1.Visible = True
    27. End If
    28.  
    29. End Sub
    30.  
    31. Private Sub Form_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
    32.  
    33.     bDraw = False
    34.    
    35. End Sub

  4. #4
    Guest

    Smile

    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
  •  



Click Here to Expand Forum to Full Width