Results 1 to 7 of 7

Thread: How to detect the VB Form is moved and Resizing of Form is complete?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jun 2005
    Posts
    94

    How to detect the VB Form is moved and Resizing of Form is complete?

    Hello!

    Currently, I'm working on a Website Sticky Notes Desktop Assistant application. The Application resides in System Tray and user clicks on it to get the Compose Note Interface. There are certain Ajax APIs to register the Position and Size of Note Window. I need to syncronize the size and location of Note window on User's Desktop with Online MySQL database. I'm facing problem in detecing Form Resize and change of Form position.

    The Resize event of Form is fired everytime a slightest resize action is taken. However, I can not execute XMLHTTP so fast and so many times! So, I was wondering why Microsoft has not given any event such as Resizing and ResizeComplete. Is there any API trick or some windows subclassing trick which will help me to mimic Resize complete? When User moves his mouse to the edge of form and then clicks mouse button to start resize; then just keeps on moving his mouse and several times Form_Resize() event is fired! I thought that Form MouseUp() will be available once user releases his mouse but no luck! Any suggestion/work-around for this?

    Second problem is even more interesting! There is no default event for VB Form which will show that Form Position is changed, is there any? How to detect that the Form is moved? I'm not finding any specific API or not that familiar with Windows Messages to subclass the event. Please help me to resolve this issue.

    Eagerly waiting for your reply ...

    Regards,

    Ruturaj.

  2. #2
    Frenzied Member pnish's Avatar
    Join Date
    Aug 2002
    Location
    Tassie, Oz
    Posts
    1,918

    Re: How to detect the VB Form is moved and Resizing of Form is complete?

    Here's a simple way to do it without subclassing. You need to put a Timer control on your form and set its Interval property to something fairly small, eg 200
    VB Code:
    1. Option Explicit
    2.  
    3. Dim IsLoading As Boolean
    4. Dim FormHasResized As Boolean
    5. Dim FormHasMoved As Boolean
    6.  
    7. Dim orgLeft As Single
    8. Dim orgTop As Single
    9.  
    10. Private Sub Form_Activate()
    11.  
    12.     orgLeft = Me.Left
    13.     orgTop = Me.Top
    14.    
    15.     IsLoading = False
    16.    
    17. End Sub
    18.  
    19. Private Sub Form_Load()
    20.  
    21.     IsLoading = True
    22.  
    23. End Sub
    24.  
    25. Private Sub Form_Resize()
    26.  
    27.     If IsLoading Then Exit Sub
    28.    
    29.     FormHasResized = True
    30.    
    31. End Sub
    32.  
    33. Private Sub Timer1_Timer()
    34.  
    35.     If (Me.Left <> orgLeft) Or (Me.Top <> orgTop) Then
    36.         MsgBox "Form has been moved"
    37.         FormHasMoved = True
    38.         orgLeft = Me.Left
    39.         orgTop = Me.Top
    40.     End If
    41.    
    42.     If FormHasResized Then
    43.         MsgBox "Form has been resized"
    44.         FormHasResized = False
    45.     End If
    46.  
    47. End Sub
    Pete

    No trees were harmed in the making of this post, however a large number of electrons were greatly inconvenienced.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jun 2005
    Posts
    94

    Re: How to detect the VB Form is moved and Resizing of Form is complete?

    Quote Originally Posted by pnish
    Here's a simple way to do it without subclassing. You need to put a Timer control on your form and set its Interval property to something fairly small, eg 200
    VB Code:
    1. Option Explicit
    2.  
    3. Dim IsLoading As Boolean
    4. Dim FormHasResized As Boolean
    5. Dim FormHasMoved As Boolean
    6.  
    7. Dim orgLeft As Single
    8. Dim orgTop As Single
    9.  
    10. Private Sub Form_Activate()
    11.  
    12.     orgLeft = Me.Left
    13.     orgTop = Me.Top
    14.    
    15.     IsLoading = False
    16.    
    17. End Sub
    18.  
    19. Private Sub Form_Load()
    20.  
    21.     IsLoading = True
    22.  
    23. End Sub
    24.  
    25. Private Sub Form_Resize()
    26.  
    27.     If IsLoading Then Exit Sub
    28.    
    29.     FormHasResized = True
    30.    
    31. End Sub
    32.  
    33. Private Sub Timer1_Timer()
    34.  
    35.     If (Me.Left <> orgLeft) Or (Me.Top <> orgTop) Then
    36.         MsgBox "Form has been moved"
    37.         FormHasMoved = True
    38.         orgLeft = Me.Left
    39.         orgTop = Me.Top
    40.     End If
    41.    
    42.     If FormHasResized Then
    43.         MsgBox "Form has been resized"
    44.         FormHasResized = False
    45.     End If
    46.  
    47. End Sub

    Hmm, interesting solution! It looks perfect for Form Moved. But, I think with Form Resize, I will face the same trouble as I do with Form_Resize() event. It will be thrown several times. Add Debug.Print to Resize event and resize Form by holding edge of Form and moving mouse continuously by holding left-mouse button. See, how many times you get the Debug text printed in your immediate window! Actually, that many time XMLHTTP will be executed!!! Will that be a wise implementation?!?! I'm looking for some work-around for this. Any suggestion?

  4. #4
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: How to detect the VB Form is moved and Resizing of Form is complete?

    here's a simple way to get a 'finished resizing' event:
    VB Code:
    1. Private Sub Form_Resize()
    2.     Timer1.Enabled = False
    3.     Timer1.Enabled = True
    4. End Sub
    5.  
    6. Private Sub Timer1_Timer()
    7.     RedrawForm
    8.     Timer1.Enabled = False
    9. End Sub
    10.  
    11. Private Sub RedrawForm()
    12.     ' Place your 'Finished Resizing' code here
    13.     Debug.Print "Redraw"
    14. End Sub
    Set the timer interval to something like 500

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Jun 2005
    Posts
    94

    Re: How to detect the VB Form is moved and Resizing of Form is complete?

    Quote Originally Posted by bushmobile
    here's a simple way to get a 'finished resizing' event:
    VB Code:
    1. Private Sub Form_Resize()
    2.     Timer1.Enabled = False
    3.     Timer1.Enabled = True
    4. End Sub
    5.  
    6. Private Sub Timer1_Timer()
    7.     RedrawForm
    8.     Timer1.Enabled = False
    9. End Sub
    10.  
    11. Private Sub RedrawForm()
    12.     ' Place your 'Finished Resizing' code here
    13.     Debug.Print "Redraw"
    14. End Sub
    Set the timer interval to something like 500

    Yeh! Great trick! it works. I changed the Interval to something like 1500 and it worked smooth without any load on XMLHTTP object. How about Form Move? Right now, I've implemented the same trick of timer checking the Left and Top of Form; if different then I send change Position XMLHTTP to Server. Any better solution on this? By the way, I'm happy to see that I'm able to avoid subclassing (which is not at all VB IDE friendly!) and it has become possible only because of your valuable tricks. Please let me know if there is any work-around for Form Move. WM_MOVE is fired when Form Move is FINISHED; right?

  6. #6
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: How to detect the VB Form is moved and Resizing of Form is complete?

    Here you go - same principle as with the code in post #4 (in my testing both timer intervals were set to 500):
    VB Code:
    1. Private lOldX As Long, lOldY As Long
    2. Private bCheck As Boolean
    3.  
    4. '======================
    5. ' MoveFinished Code
    6. '----------------------
    7. Private Sub Form_Load()
    8.     lOldX = Me.Left
    9.     lOldY = Me.Top
    10. End Sub
    11.  
    12. Private Sub tmrMove_Timer()
    13.     If Me.Left = lOldX And Me.Top = lOldY Then
    14.         If bCheck Then
    15.             MoveFinished
    16.             bCheck = False
    17.         End If
    18.     Else
    19.         lOldX = Me.Left
    20.         lOldY = Me.Top
    21.         bCheck = Not tmrDraw.Enabled
    22.     End If
    23. End Sub
    24.  
    25. Private Sub MoveFinished()
    26.     Debug.Print "Move Finished " & Rnd
    27. End Sub
    28.  
    29. '======================
    30. ' ResizeFinished Code
    31. '----------------------
    32. Private Sub Form_Resize()
    33.     tmrDraw.Enabled = False
    34.     tmrDraw.Enabled = True
    35. End Sub
    36.  
    37. Private Sub tmrDraw_Timer()
    38.     ResizeFinished
    39.     tmrDraw.Enabled = False
    40. End Sub
    41.  
    42. Private Sub ResizeFinished()
    43.     Debug.Print "Resize Finished " & Rnd
    44. End Sub

    WM_MOVE event is fired once the form has finished moving - WM_MOVING is fired as it moves.

    WM_SIZE for finished sizing.

    Subclassing would be the standard (and best) way of doing this - but if you don't feel confident subclassing then using timers is an option.

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Jun 2005
    Posts
    94

    Re: How to detect the VB Form is moved and Resizing of Form is complete?

    Hello!

    Wow! your code works like charm! I never thought it will be possible with only two timers! It's so optimized and compact! I agree that Subclassing is the proper method for it; but considering my current project scope, I feel Timer method is perfect! Thanks a lot for your efforts and interst!

    Regards,

    Ruturaj.

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