PDA

View Full Version : Solved: Prevent User from moving userform



callen2468
07-27-2007, 12:55 PM
Any one have thoughts on how to prevent a user from moving a userform by grabbing the title bar?
I set the top and left to 0 and height and width to usableheight and usablewidth. This sets the userform to as large as the user's resolution will permit. The user is still able to grab the title bar and move the form. I can leave a blank sheet behind it and make the rest very hidden but it's a hassle to have to move the form again to access all controls. I can also reset the location of the userform with every event but that's kind of a worse case scenario. I really want to run my user forms full screen and prevent their movement but I can not quite get there.
Anyone with a suggestion on this?

Thanks in advance,
Chris

Bob Phillips
07-27-2007, 01:26 PM
How about this



Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" ( _
ByVal lpClassName As String, _
ByVal lpWindowName As String) As Long
Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" ( _
ByVal hWnd As Long, _
ByVal nIndex As Long) As Long
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 GetSystemMenu Lib "user32" ( _
ByVal hWnd As Long, _
ByVal bRevert As Long) As Long
Private Declare Function DeleteMenu Lib "user32" ( _
ByVal hMenu As Long, _
ByVal nPosition As Long, _
ByVal wFlags As Long) As Long
Private Declare Function ShowWindow Lib "user32" ( _
ByVal hWnd As Long, _
ByVal nCmdShow As Long) As Long
Private Declare Function EnableWindow Lib "user32" ( _
ByVal hWnd As Long, _
ByVal fEnable As Long) As Long
Private Declare Function DrawMenuBar Lib "user32" ( _
ByVal hWnd As Long) As Long
Private Declare Function SetFocus Lib "user32" ( _
ByVal hWnd As Long) As Long
Private Declare Function ExtractIcon Lib "shell32.dll" Alias "ExtractIconA" ( _
ByVal hInst As Long, _
ByVal lpszExeFileName As String, _
ByVal nIconIndex As Long) As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" ( _
ByVal hWnd As Long, _
ByVal wMsg As Long, _
ByVal wParam As Integer, _
ByVal lParam As Long) As Long
Private Declare Function LockWindowUpdate Lib "user32" ( _
ByVal hWndLock As Long) As Long

'Window styles
Private Const GWL_STYLE As Long = (-16)
Private Const GWL_EXSTYLE As Long = (-20)
Private Const WS_CAPTION As Long = &HC00000
Private Const WS_EX_APPWINDOW As Long = &H40000
Private Const WS_EX_TOOLWINDOW As Long = &H80

Private hWnd As Long

Private Sub UserForm_Activate()
Dim lStyle As Long, hMenu As Long

hWnd = FindWindow("ThunderDFrame", Me.Caption)

If hWnd = 0 Then Exit Sub

lStyle = GetWindowLong(hWnd, GWL_STYLE)

lStyle = lStyle And Not WS_CAPTION
SetWindowLong hWnd, GWL_STYLE, lStyle
lStyle = GetWindowLong(hWnd, GWL_EXSTYLE)
lStyle = lStyle And Not WS_EX_APPWINDOW
lStyle = lStyle And Not WS_EX_TOOLWINDOW
SetWindowLong hWnd, GWL_EXSTYLE, lStyle
DrawMenuBar hWnd
SetFocus hWnd

End Sub

callen2468
07-27-2007, 02:01 PM
Thank you for the quick response. I am going to work my way through your code this weekend. Right now it's a bit confusing but I will work it out. I am going to hold off on marking this solved until Monday. I will post responses or marked this solved on monday morning. Again, thanks for you help and have a great weekend!

Chris

Aussiebear
07-27-2007, 02:25 PM
Bob, is this the same in principle as building an interface layer which the user only sees?

Bob Phillips
07-27-2007, 03:55 PM
Similar Ted. Here, you would need to hide the application as well, and then this takes over. Personally, I wouldn't remove the caption, I would just disable Minimize and maximize if I didn't want them messing with it.

Andy Pope
07-28-2007, 06:07 AM
This will not disable the ability to try and move the userform but it will leave the userform in position.


Private m_vntTop
Private m_vntLeft

Private Sub UserForm_Activate()

m_vntTop = Me.Top
m_vntLeft = Me.Left

End Sub
Private Sub UserForm_Layout()

If Not IsEmpty(m_vntTop) Then Me.Move m_vntLeft, m_vntTop

End Sub

callen2468
07-30-2007, 04:38 AM
I started reviewing you code, Bob (I gather that is your name). Looks like I will need to read up on programming with API calls before I start messing with this. Thank you for the direction.
Andy, I have used something like your approach in my activate routine and in every change event to relocate the userform to me.top = 0 and me.left = 0. This keeps the userform infront of the user while in use. I will just keep my data sheets veryhidden to prevent the user from messing with them. Thank you very much for your help. I will consider this solved and mark it as such.
Chris

Bob Phillips
07-30-2007, 08:01 AM
If it works, you don't have to read-up on anything, just use it.