Results 1 to 2 of 2

Thread: VBA - Detect when a form is moved

  1. #1

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

    Post

    Any ideas how to detect when a form is moved (dragged). Failing that, how can I stop the user moving it at all. Oh, and just for good measure, I'd like to be able to hide the stupid title bar too. Why can't VBA be VB?

  2. #2
    Guru Yonatan's Avatar
    Join Date
    Apr 1999
    Location
    Israel
    Posts
    892

    Post

    I couldn't find any way to remove the title bar, and I would have given you code to detect/cancel moving, but the AddressOf keyword is unsupported in VBA so you can't subclass!!!

    This code would have worked otherwise:

    UserForm code...
    Code:
    Option Explicit
    
    
    Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    
    
    Public hWnd As Long
    
    
    Private Sub UserForm_Initialize()
        hWnd = FindWindow("ThunderXFrame", Caption)
        Call Hook
    End Sub
    
    
    Private Sub UserForm_Terminate()
        Call Unhook
    End Sub
    
    
    Public Sub UserForm_Move(Cancel As Boolean)
        ' The UserForm is moving...
        ' To cancel the movement, uncomment the following line:
        ' Cancel = True
    End Sub
    Module code...
    Code:
    Option Explicit
    
    
    Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
    Private 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
    
    
    Public Const GWL_WNDPROC = (-4)
    Public Const WM_MOVE = &H3
    
    
    Public lpPrevWndFunc As Long
    
    
    Function WindowProc(ByVal hWnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
        Dim Cancel As Boolean
        If Msg = WM_MOVE Then
            Call UserForm1.UserForm_Move(Cancel)
            If Cancel Then Exit Function
        End If
        WindowProc = CallWindowProc(lpPrevWndFunc, hWnd, Msg, wParam, lParam)
    End Function
    
    
    Sub Hook()
        ' The following line doesn't work,
        ' since the AddressOf keyword isn't supported in VBA:
        lpPrevWndFunc = SetWindowLong(UserForm1.hWnd, GWL_WNDPROC, AddressOf WindowProc)
    End Sub
    
    
    Sub Unhook()
        Call SetWindowLong(UserForm1.hWnd, GWL_WNDPROC, lpPrevWndFunc)
    End Sub
    Too bad this doesn't work!

    ------------------
    Yonatan
    Teenage Programmer
    E-Mail: [email protected]
    ICQ: 19552879
    AIM: RYoni69

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