PDA

Click to See Complete Forum and Search --> : VBA - Detect when a form is moved


coox
Nov 14th, 1999, 10:34 PM
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?

Yonatan
Nov 15th, 1999, 11:38 AM
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...

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...

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: RZvika@netvision.net.il
ICQ: 19552879 (http://www.icq.com/19552879)
AIM: RYoni69