Results 1 to 10 of 10

Thread: Detecting form movement - how??

  1. #1

    Thread Starter
    Fanatic Member RSINGH's Avatar
    Join Date
    May 2001
    Location
    London
    Posts
    522

    Question Detecting form movement - how??

    I need to detect when my window has been moved by the user. I can't find an event and searches have been quite unhelpful. Can anybody help??
    The liver is bad. It must be punished.

  2. #2
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    You could subclass it for the WM_MOVE or WM_MOVING message.

  3. #3

    Thread Starter
    Fanatic Member RSINGH's Avatar
    Join Date
    May 2001
    Location
    London
    Posts
    522
    Hmm, I have absolutely no idea on how that is done ? (I'm more a db guy)? Do you have a link easily available
    The liver is bad. It must be punished.

  4. #4
    Fanatic Member
    Join Date
    Oct 2000
    Location
    Oregon
    Posts
    962
    You could also do it with the very simple (but slow) way of using a loop or timer.

    VB Code:
    1. Do while Running
    2.  If (me.left <> oldX) or (me.top <> oldY) Then
    3.   'Code for when the form moves.
    4.  End If
    5. Loop

    OR

    VB Code:
    1. Private Sub tmrMove_timmer()
    2.  If (me.left <> oldX) or (me.top <> oldY) Then
    3.   'Code for when the form moves.
    4.  End If
    5. End Sub

    Hope this helps.
    Involved in: Sentience

  5. #5
    Fanatic Member
    Join Date
    Oct 2000
    Location
    Oregon
    Posts
    962
    The loop would be faster, but you have to make sure to place a doevents in it somewhere. You would also have to exit the loop before you could shut down the program (otherwise the program would still use memory & processer power). Thats what the running boolean is for.
    Involved in: Sentience

  6. #6

    Thread Starter
    Fanatic Member RSINGH's Avatar
    Join Date
    May 2001
    Location
    London
    Posts
    522
    The timer could work, but I would get my arse kicked for using one. Hmmm, I'd better do a search on sub-classing. Thanks anyway Gaming_world.
    The liver is bad. It must be punished.

  7. #7
    Fanatic Member
    Join Date
    Oct 2000
    Location
    Oregon
    Posts
    962
    The loop would work too, although it is also slower then sub-classing. It mostly depends on what you are doing. If the program is for something when speed is not critical, a timer would work. If a timer would be too slow, but speed is not life & death, the loop would work. If it is a game, where speed is life & death, you need sub-classing.
    Involved in: Sentience

  8. #8
    Frenzied Member MerrionComputin's Avatar
    Join Date
    Apr 2001
    Location
    Dublin, Ireland
    Posts
    1,616
    The EventVB.dll has a class called ApiWindow which has both a Move and a Moving event which you can use thus:

    VB Code:
    1. Option Explicit
    2.  
    3. Dim WithEvents vbLink As EventVB.APIFunctions
    4. Dim WithEvents vbWnd As EventVB.ApiWindow
    5.  
    6. Private Sub Form_Load()
    7.  
    8. Set vbLink = New APIFunctions
    9.  
    10. Set vbWnd = New ApiWindow
    11. vbWnd.hWnd = Me.hWnd
    12. '\\ Subclass this form
    13. vbLink.SubclassedWindows.Add vbWnd
    14.  
    15. End Sub
    16.  
    17. Private Sub vbWnd_Move(ByVal x As Long, ByVal y As Long, Cancel As Boolean)
    18.  
    19. Me.Caption = "Moved to : " & x & "," & y
    20.  
    21. End Sub
    22.  
    23. Private Sub vbWnd_Moving(ByVal MoveEdges As EventVB.WindowSizingEdges, MoveRectangle As EventVB.APIRect)
    24.  
    25. With MoveRectangle
    26.     Me.Caption = "Moving - " & .Left & "," & .Top
    27. End With
    28.  
    29. End Sub

    The difference is that the Moving event fires while the form is being dragged and the Move event fires after the move has occured.

    If you change the MoveRectangle property in the Moving event you can control how (and if) the form can be moved. For example, to prevent the form being moved below half way down the form....
    VB Code:
    1. Private Sub vbWnd_Moving(ByVal MoveEdges As EventVB.WindowSizingEdges, MoveRectangle As EventVB.APIRect)
    2.  
    3. With MoveRectangle
    4.     If .Top > (Screen.Height / 2) / Screen.TwipsPerPixelY Then
    5.         .Top = (Screen.Height / 2) / Screen.TwipsPerPixelY
    6.     End If
    7. End With
    8.  
    9. End Sub
    10.  
    11. End Sub

    HTH,
    Duncan
    Last edited by MerrionComputin; Apr 19th, 2002 at 10:57 AM.
    ----8<---------------------------------------
    NEW - The .NET printer queue monitor component
    ----8<---------------------------------------
    Now with Examples of use

  9. #9

    Thread Starter
    Fanatic Member RSINGH's Avatar
    Join Date
    May 2001
    Location
    London
    Posts
    522
    Merrion you star! That looks exactly like what I'm looking for. Thank you very much. I'll try it on Monday now as I've fallen out of work mode!
    The liver is bad. It must be punished.

  10. #10
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

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