Every now and again, someone on a forum requests to have the title bar removed from their UserForm. The reason may simply be to remove the Exit Button (the red X in the upper right corner) or to get rid of the windows framework which is deemed "ugly" given the UserForm's design. Actually, with some simple Windows API function calls (sorry Mac users, but this code will not work for you), removing the title bar and windows border is quite easy to do. Of course, without a title bar, your user will not be able to move the UserForm around the screen. Again, the Windows API functions come to the rescue... setting up the ability to move a UserForm having no title bar is also quite easy to do. Simply put this code into your UserForm's code window and you are done.
Code:
'**** Start of API Calls To Remove The UserForm's Title Bar ****
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 DrawMenuBar Lib "user32" _
(ByVal hWnd As Long) As Long
'**** End of API Calls To Remove The UserForm's Title Bar ****
'**** Start of API Calls To Allow User To Slide UserForm Around The Screen ****
Private Declare Function SendMessage Lib "user32" _
Alias "SendMessageA" _
(ByVal hWnd As Long, _
ByVal wMsg As Long, _
ByVal wParam As Long, _
lParam As Any) As Long
Private Declare Function ReleaseCapture Lib "user32" () As Long
Private Const WM_NCLBUTTONDOWN = &HA1
Private Const HTCAPTION = 2
'**** End of API Calls To Allow User To Slide UserForm Around The Screen ****
Dim hWndForm As Long
Private Sub UserForm_Initialize()
Dim Style As Long, Menu As Long
hWndForm = FindWindow("ThunderDFrame", Me.Caption)
Style = GetWindowLong(hWndForm, &HFFF0)
Style = Style And Not &HC00000
SetWindowLong hWndForm, &HFFF0, Style
DrawMenuBar hWndForm
End Sub
Private Sub UserForm_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
If Button = xlPrimaryButton And Shift = 1 Then
Call ReleaseCapture
Call SendMessage(hWndForm, WM_NCLBUTTONDOWN, HTCAPTION, ByVal 0&)
End If
End Sub
Note 1
------------
Important -- make sure to provide an "Exit" button for closing the UserForm down (remember, there will be no Exit Button on the Window frame for the users to use to do this, so you have to provide one for them).
Note 2
-----------
To slide the UserForm around, hold the Shift Key down, left click a blank area of the UserForm (NOT a control on the UserForm) and with the left mouse button still depressed, slide the mouse around the screen until you have it located where you want. I chose to use the Shift Key as part of this functionality for its mnemonic value (Shift Key = Shift UserForm Around), but this can be changed as needed in the If..Then statement inside the UserForm_MouseDown event procedure.
Bookmarks