Results 1 to 6 of 6

Thread: Detect when form has been moved

  1. #1

    Thread Starter
    Fanatic Member coox's Avatar
    Join Date
    Oct 1999
    Posts
    550

    Unhappy

    Is there some property/method thing so you can detect when a form is being moved/has been moved? Can't seem to find anything, but then I'm just like that...

  2. #2
    Hyperactive Member
    Join Date
    Jul 2000
    Posts
    352

    Post No property that I know of

    There is no property I know of that detects that but I know a means of detecting if the form has been moved with a little code. I am not sure if it is quite what you are looking for, but it does work.

    Code:
    Private Sub Form_Activate()
    Dim left1 As Integer
    Dim top1 As Integer
    left1 = Form1.left
    top1 = Form1.top
    Do
    If (left1 <> Form1.left) or (top1 <> Form1.top) Then 
    '(your code)
    end if
    DoEvents 'keeps windows processes functioning
    Loop
    End Sub
    You can place that directly into your form's activation. I hope it is what you are looking for.(By the way if you want to know if it has been moved multiple times then change left1 and top1 every time the form is moved)

    Tell me if it works and if you have a question, just post.

    Good Luck,
    Joe

  3. #3
    Guest
    You can Subclass your Form.

    Code for a Module
    Code:
    Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
    Declare Function SetWindowLong& Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long)
    Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hwnd As Long, ByVal msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    Const GWL_WNDPROC = (-4)
    Const WM_MOVE = &H3
    Const WM_EXITSIZEMOVE = &H232
     
    Global WndProcOld As Long
    
    Public Function WindProc(ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    
        If wMsg = WM_EXITSIZEMOVE Then
            'The Form has finished moving
            MsgBox ("Form1 has been moved")
        End If
        
        If wMsg = WM_MOVE Then
            'The Form is moving
            Form1.Print "Form is moving"
        End If
        
        WindProc = CallWindowProc(WndProcOld&, hwnd&, wMsg&, wParam&, lParam&)
        
    End Function
    
    Sub SubClassWnd(hwnd As Long)
        WndProcOld& = SetWindowLong(hwnd, GWL_WNDPROC, AddressOf WindProc)
    End Sub
    
    Sub UnSubclassWnd(hwnd As Long)
        SetWindowLong hwnd, GWL_WNDPROC, WndProcOld&
        WndProcOld& = 0
    End Sub
    Code for Form
    Code:
    Private Sub Form_Load()
        SubClassWnd Me.hwnd
    End Sub
    
    Private Sub Form_Unload(Cancel As Integer)
        UnSubclassWnd Me.hwnd
    End Sub

  4. #4
    Hyperactive Member MPrestonf12's Avatar
    Join Date
    Jun 1999
    Location
    NY
    Posts
    330
    Or you could set its left and top properties. The then under form_move create a if statement to see if the value match.
    Matt

  5. #5
    Guest
    There is no Form_Move event.

  6. #6

    Thread Starter
    Fanatic Member coox's Avatar
    Join Date
    Oct 1999
    Posts
    550
    Thanks chaps.

    - Joe, yup, it works ok but I'm a bit wary of enless loops like that, and it does seem to behave a little strangely when you close it.

    - Megatron, like it - any chance you eplaining to me just what the hell SUBCLASS means? As senior developer here at Microsoft (joke) , I think it's something I should understand.

    Anyway, thanks for all your help guys... -Nick

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