Results 1 to 5 of 5

Thread: [RESOLVED] Rubber band logic

  1. #1

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Resolved [RESOLVED] Rubber band logic

    In a picturebox I have this rectangular shape control that can be resized when dragging on its sides. The basic core of the code I'm using is,
    Code:
    Dim DraggingFlag As Boolean
    Dim X0 As Single, Y0 As Single
    
    Private Sub Form_Load()
       DraggingFlag = False
    End Sub
    
    Private Sub Picture1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
       X0 = X
       Y0 = Y
       DraggingFlag = True
    End Sub
    
    Private Sub Picture1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
       Dim dx As Single, dy As Single
    
       'This is the case for dragging on the right side
       'The rest are similar and I don't transbribe them
       If DraggingFlag Then
          dx = X - X0
          With Shape1
             .Move .Left, .Top, .Width + dx
          End With
       End If
    End Sub
    
    Private Sub Picture1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
       DraggingFlag = False
    End Sub
    The problem is, I'm getting somewhat confused about how I should handle the case when you make the size ever smaller and reach 0 so that you must stretch in the opposite direction. I suppose I'll come out with some working code if I rack my brain a little longer but I'd appreciate it if someone had some handy template.
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

  2. #2
    Addicted Member TBeck's Avatar
    Join Date
    Apr 2006
    Location
    Ontario, Canada
    Posts
    254

    Re: Rubber band logic

    do you mean some thing like this:

    create a new form with a shape named shape1
    vb Code:
    1. Dim DraggingFlag As Boolean
    2. Dim prevX As Single, prevY As Single
    3. Dim drawingRight As Boolean, drawingDown As Boolean
    4.  
    5. Private Sub Form_Load()
    6.    DraggingFlag = False
    7.    drawingRight = True
    8.    drawingDown = True
    9. End Sub
    10.  
    11. Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    12.    prevX = X
    13.    prevY = Y
    14.    DraggingFlag = True
    15. End Sub
    16.  
    17. Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    18. Dim dx As Single, dy As Single
    19. dx = X - prevX
    20. dy = Y - prevY
    21.  
    22. prevX = X
    23. prevY = Y
    24.  
    25.  
    26.    If DraggingFlag Then
    27.       With Shape1
    28.       'determine which way we are drawing
    29.       If ((drawingRight) And (.Width + dx < 0)) Or ((Not drawingRight) And (.Width - dx < 0)) Then
    30.         drawingRight = Not drawingRight
    31.       End If
    32.       If ((drawingDown) And (.Height + dy < 0)) Or ((Not drawingDown) And (.Height - dy < 0)) Then
    33.         drawingDown = Not drawingDown
    34.       End If
    35.      
    36.        'This is the case for dragging on the bottom right side
    37.         If (drawingRight) And (drawingDown) Then
    38.             .Move .Left, .Top, .Width + dx, .Height + dy
    39.        'This is the case for dragging on the bottom left side
    40.         ElseIf (Not drawingRight) And (drawingDown) Then
    41.             .Left = .Left + dx
    42.             .Width = .Width - dx
    43.             .Height = .Height + dy
    44.        'This is the case for dragging on the top right side
    45.         ElseIf (drawingRight) And (Not drawingDown) Then
    46.             .Width = .Width + dx
    47.             .Top = .Top + dy
    48.             .Height = .Height - dy
    49.        'This is the case for dragging on the top left side
    50.         ElseIf (Not drawingRight) And (Not drawingDown) Then
    51.             .Left = .Left + dx
    52.             .Width = .Width - dx
    53.             .Top = .Top + dy
    54.             .Height = .Height - dy
    55.         End If
    56.       End With
    57.    End If
    58. End Sub
    59.  
    60. Private Sub Form_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
    61.    DraggingFlag = False
    62. End Sub

  3. #3
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Re: Rubber band logic

    The code posted by TBeck is working for me, however if you want to restart the procedure completely after releasing the mousebutton, you have to set DrawingRigth and DrawingDown to True in the _MouseUp Eventroutine.
    Presently the shape will remain the left or rigth border and the top or bottom border on the position of the left and top border of the starting shape.
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  4. #4

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Re: Rubber band logic

    Quote Originally Posted by TBeck
    do you mean some thing like this:
    ...
    Yes it's working for me too, all I have to do now is add some cases for when you start pulling from the top or the left side. Thanks!
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

  5. #5

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Re: Rubber band logic

    Quote Originally Posted by krtxmrtz
    Yes it's working for me too, all I have to do now is add some cases for when you start pulling from the top or the left side. Thanks!
    I've added some goodies like dragging on each of the 4 sides, appropriate mousepointers and a moving feature so here's the final code:
    VB Code:
    1. Option Explicit
    2. Dim DraggingFlag As Boolean
    3. Dim prevX As Single, prevY As Single
    4. Dim flgRight As Boolean, flgDown As Boolean
    5. Dim flgLeft As Boolean, flgUp As Boolean
    6. Dim flgMove As Boolean
    7. Dim Delta As Single
    8.  
    9. Private Sub Form_Load()
    10.     DraggingFlag = False
    11.     flgRight = False
    12.     flgDown = False
    13.     flgLeft = False
    14.     flgUp = False
    15.     flgMove = False
    16.     'Optional (if scale is set differently then
    17.     'change the value of Delta accordingly)
    18.     ScaleMode = vbPixels
    19.     '5 pixel area around the shape borders
    20.     'where dragging becomes active
    21.     Delta = 5
    22. End Sub
    23.  
    24. Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    25.     Dim shpRight As Single, shpBottom As Single
    26.    
    27.     prevX = X
    28.     prevY = Y
    29.    
    30.     'Determine which border is going to be dragged on
    31.     With Shape1
    32.         shpRight = .Left + .Width
    33.         shpBottom = .Top + .Height
    34.         If X < .Left + Delta And X > .Left - Delta Then
    35.             'Left border
    36.             If Y > .Top - Delta And Y < .Top + Delta Then
    37.                 'Upper left corner
    38.                 flgUp = True
    39.                 flgLeft = True
    40.             ElseIf Y > shpBottom - Delta And Y < shpBottom + Delta Then
    41.                 'Lower left corner
    42.                 flgDown = True
    43.                 flgLeft = True
    44.             ElseIf Y > .Top + Delta And Y < shpBottom - Delta Then
    45.                 'Rest of the left border
    46.                 flgLeft = True
    47.             End If
    48.         ElseIf X > shpRight - Delta And X < shpRight + Delta Then
    49.             'Right boder
    50.             If Y > .Top - Delta And Y < .Top + Delta Then
    51.                 'Upper right corner
    52.                 flgUp = True
    53.                 flgRight = True
    54.             ElseIf Y > shpBottom - Delta And Y < shpBottom + Delta Then
    55.                 'Lower right corner
    56.                 flgDown = True
    57.                 flgRight = True
    58.             ElseIf Y > .Top + Delta And Y < shpBottom - Delta Then
    59.                 'Rest of the right border
    60.                 flgRight = True
    61.             End If
    62.         ElseIf X > .Left + Delta And X < shpRight - Delta Then
    63.             'Central part of either the upper or the lower border
    64.             If Y > .Top - Delta And Y < .Top + Delta Then
    65.                 'Upper border
    66.                 flgUp = True
    67.             ElseIf Y > shpBottom - Delta And Y < shpBottom + Delta Then
    68.                 'Lower border
    69.                 flgDown = True
    70.             Else
    71.                 'Center of the shape: shape will be moved
    72.                 flgMove = True
    73.             End If
    74.         End If
    75.     End With
    76.    
    77.     DraggingFlag = True
    78.    
    79. End Sub
    80.  
    81. Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    82.     Dim dx As Single, dy As Single
    83.  
    84.     dx = X - prevX
    85.     dy = Y - prevY
    86.    
    87.     prevX = X
    88.     prevY = Y
    89.  
    90.     'Select the appropriate mousepointer
    91.     SetPointer X, Y, Shape1
    92.    
    93.     If DraggingFlag Then
    94.         With Shape1
    95.             'Determine which way we are drawing
    96.             If (flgRight And (.Width + dx < 0)) Then
    97.                 flgRight = False
    98.                 flgLeft = True
    99.                 dx = dx + .Width
    100.             End If
    101.             If (flgLeft And (.Width - dx < 0)) Then
    102.                 flgRight = True
    103.                 flgLeft = False
    104.                 dx = dx - .Width
    105.             End If
    106.             If (flgDown And (.Height + dy < 0)) Then
    107.               flgDown = False
    108.               flgUp = True
    109.               dy = dy + .Height
    110.             End If
    111.             If (flgUp And (.Height - dy < 0)) Then
    112.               flgDown = True
    113.               flgUp = False
    114.               dy = dy - .Height
    115.             End If
    116.            
    117.             If flgMove Then
    118.                 .Move .Left + dx, .Top + dy
    119.             ElseIf flgRight Then
    120.                 If flgUp Then
    121.                     .Move .Left, .Top + dy, .Width + dx, .Height - dy
    122.                 ElseIf flgDown Then
    123.                     .Move .Left, .Top, .Width + dx, .Height + dy
    124.                 Else
    125.                     .Move .Left, .Top, .Width + dx
    126.                 End If
    127.             ElseIf flgLeft Then
    128.                 If flgUp Then
    129.                     .Move .Left + dx, .Top + dy, .Width - dx, .Height - dy
    130.                 ElseIf flgDown Then
    131.                     .Move .Left + dx, .Top, .Width - dx, .Height + dy
    132.                 Else
    133.                     .Move .Left + dx, .Top, .Width - dx
    134.                 End If
    135.             Else
    136.                 If flgUp Then
    137.                     .Move .Left, .Top + dy, .Width, .Height - dy
    138.                 ElseIf flgDown Then
    139.                     .Move .Left, .Top, .Width, .Height + dy
    140.                 End If
    141.             End If
    142.  
    143.         End With
    144.     End If
    145. End Sub
    146. Private Sub Form_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
    147.     DraggingFlag = False
    148.     flgRight = False
    149.     flgDown = False
    150.     flgLeft = False
    151.     flgUp = False
    152.     flgMove = False
    153.     MousePointer = vbDefault
    154. End Sub
    155. Private Sub SetPointer(X As Single, Y As Single, sh As Shape)
    156.     Dim shpRight As Single, shpBottom As Single
    157.    
    158.     With sh
    159.         shpRight = .Left + .Width
    160.         shpBottom = .Top + .Height
    161.         If X < .Left + Delta And X > .Left - Delta Then
    162.             'Left border
    163.             If Y > .Top - Delta And Y < .Top + Delta Then
    164.                 'Upper left corner
    165.                 MousePointer = vbSizeNWSE
    166.             ElseIf Y > shpBottom - Delta And Y < shpBottom + Delta Then
    167.                 'Lower left corner
    168.                 MousePointer = vbSizeNESW
    169.             ElseIf Y > .Top + Delta And Y < shpBottom - Delta Then
    170.                 'Rest of the left border
    171.                 MousePointer = vbSizeWE
    172.             End If
    173.         ElseIf X > shpRight - Delta And X < shpRight + Delta Then
    174.             'Right boder
    175.             If Y > .Top - Delta And Y < .Top + Delta Then
    176.                 'Upper right corner
    177.                 MousePointer = vbSizeNESW
    178.             ElseIf Y > shpBottom - Delta And Y < shpBottom + Delta Then
    179.                 'Lower right corner
    180.                 MousePointer = vbSizeNWSE
    181.             ElseIf Y > .Top + Delta And Y < shpBottom - Delta Then
    182.                 'Rest of the right border
    183.                 MousePointer = vbSizeWE
    184.             End If
    185.         ElseIf X > .Left + Delta And X < shpRight - Delta Then
    186.             'Central part of either the upper or the lower border
    187.             If Y > .Top - Delta And Y < .Top + Delta Then
    188.                 'Upper border
    189.                 MousePointer = vbSizeNS
    190.             ElseIf Y > shpBottom - Delta And Y < shpBottom + Delta Then
    191.                 'Lower border
    192.                 MousePointer = vbSizeNS
    193.             ElseIf Y > .Top + Delta And Y < shpBottom - Delta Then
    194.                 'Center of the shape: shape will be moved
    195.                 MousePointer = vbSizePointer
    196.             Else
    197.                 'Center of the shape but well outside
    198.                 MousePointer = vbDefault
    199.             End If
    200.         Else
    201.             'All other areas
    202.             MousePointer = vbDefault
    203.         End If
    204.     End With
    205. End Sub
    Attached Files Attached Files
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

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