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??
Printable View
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??
You could subclass it for the WM_MOVE or WM_MOVING message.
Hmm, I have absolutely no idea on how that is done ?:confused: (I'm more a db guy)? Do you have a link easily available
You could also do it with the very simple (but slow) way of using a loop or timer.
VB Code:
Do while Running If (me.left <> oldX) or (me.top <> oldY) Then 'Code for when the form moves. End If Loop
OR
VB Code:
Private Sub tmrMove_timmer() If (me.left <> oldX) or (me.top <> oldY) Then 'Code for when the form moves. End If End Sub
Hope this helps.
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.
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 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.
The EventVB.dll has a class called ApiWindow which has both a Move and a Moving event which you can use thus:
VB Code:
Option Explicit Dim WithEvents vbLink As EventVB.APIFunctions Dim WithEvents vbWnd As EventVB.ApiWindow Private Sub Form_Load() Set vbLink = New APIFunctions Set vbWnd = New ApiWindow vbWnd.hWnd = Me.hWnd '\\ Subclass this form vbLink.SubclassedWindows.Add vbWnd End Sub Private Sub vbWnd_Move(ByVal x As Long, ByVal y As Long, Cancel As Boolean) Me.Caption = "Moved to : " & x & "," & y End Sub Private Sub vbWnd_Moving(ByVal MoveEdges As EventVB.WindowSizingEdges, MoveRectangle As EventVB.APIRect) With MoveRectangle Me.Caption = "Moving - " & .Left & "," & .Top End With 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:
Private Sub vbWnd_Moving(ByVal MoveEdges As EventVB.WindowSizingEdges, MoveRectangle As EventVB.APIRect) With MoveRectangle If .Top > (Screen.Height / 2) / Screen.TwipsPerPixelY Then .Top = (Screen.Height / 2) / Screen.TwipsPerPixelY End If End With End Sub End Sub
HTH,
Duncan
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!:D
Check out this link...
http://www.vb2themax.com/HtmlDoc.asp...Articles&ID=20