Captionless UserForm

CraigM

Active Member
Joined
Feb 27, 2003
Messages
320
Does anybody know if it is possible to create a userform without the blue caption strip across the top? And without the close button?
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
Here's some literature i have on the X

Removing The Quit/X Button On An Userform

To remove the Quit/X button in the upper right corner of a userform can be done, but it requires some Windows API magic developed by Stephen Bullen.


'at the top of a module put the following function.
'each function must be on a single line
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

'place these two constant statements at the top of the module
Const GWL_STYLE = (-16)
Const WS_SYSMENU = &H80000

'place this in the userform's code module
Private Sub UserForm_Initialize()
Dim hWnd As Long, a As Long

hWnd = FindWindow("ThunderXFrame", Me.Caption)
a = GetWindowLong(hWnd, GWL_STYLE)
SetWindowLong hWnd, GWL_STYLE, a And Not WS_SYSMENU
End Sub

'be sure to put a button on the form so that you can exit the form. For example
Private Sub CommandButton1_Click()
Me.Hide
End Sub

If you forget to the above button on the form and you have the Visual Basic Editor active, you can close the form by pressing ALT-Tab to get to the VB editor and then clicking on the reset button.
 
Upvote 0
CraigM said:
Does anybody know if it is possible to create a userform without the blue caption strip across the top? And without the close button?

Yes you can, BUT you must bear in mind that you won't be able to move
the form.....you can get around this as well.
But 1st, why do you want this as there maybe easier options ?
 
Upvote 0
Hiding The Exit X On A Userform

To hide the little X that appears at the top right of a userform, you can use the following code that you would put in the userform's module:

'this goes at the top of the module:

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
Const GWL_STYLE = (-16)
Const WS_SYSMENU = &H80000

Private Sub UserForm_Initialize()
'this hides the X on the caption line
Dim hWnd As Long, a As Long

hWnd = FindWindow("ThunderXFrame", Me.Caption)
a = GetWindowLong(hWnd, GWL_STYLE)
SetWindowLong hWnd, GWL_STYLE, a And Not WS_SYSMENU
End Sub
 
Upvote 0
Displaying A UserForm Without A Blue Title Bar

If you remove a userform's blue title bar, then the user will not be able to move the userform nor click on the Quit/X button and close the form. Such a user form could be displayed as a task selection bar, as a modeless userform, or just as a normal userform but without the title bar. Since it is displayed without a title bar, there is no need to turn Application.ScreenUpdating back on in case the users moves the userform around.

The first step is to create a class module. Name the class module "cTitleBarHider" and put the following code in it:

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

Private Const GWL_STYLE As Long = (-16)
'The offset of a window's style
Private Const WS_CAPTION As Long = &HC00000

'Style to add a title bar

Public Property Set Form(oForm As Object)
Dim iStyle As Long
Dim hWndForm As Long
If Val(Application.Version) < 9 Then
'XL97
hWndForm = FindWindow("ThunderXFrame", oForm.Caption)
Else
'XL2000
hWndForm = FindWindow("ThunderDFrame", oForm.Caption)
End If
iStyle = GetWindowLong(hWndForm, GWL_STYLE)

iStyle = iStyle And Not WS_CAPTION
SetWindowLong hWndForm, GWL_STYLE, iStyle
DrawMenuBar hWndForm
End Property


Then, in the code module of any userform you wish to be titleless, put the following code:


Dim oTitleBarHider As New cTitleBarHider

Private Sub UserForm_Activate()
Set oTitleBarHider.Form = Me
End Sub


Lastly, change the userform's border style property to 1-fmBorderStyleSingle in the userform's property box. This will improve the appearance of the userform when it is displayed.

When the form is displayed, the UserForm_Activate procedure will run and in turn run the code in the class module which hides the title bar.
 
Upvote 0
PLEASE NOTE THAT MY LAST 3 POSTS ARE NOT MY IDEAS :LOL:

They are copy's and paste of literature I just happen to have.


Useful Internet Articles On UserForms And DialogSheets

Here are some sources which might prove helpful on creating userform and dialogsheets.

http://support.microsoft.com/support/kb/articles/q164/9/23.asp
How to Fill a UserForm ListBox with Database Values

http://support.microsoft.com/support/kb/articles/q161/5/98.asp
XL97: How to Add Data to a ComboBox or a ListBox

http://support.microsoft.com/support/kb/articles/q183/1/83.asp
XL98: How to Fill ListBox Control with Multiple Ranges (works for XL97
too)

http://support.microsoft.com/support/kb/articles/q165/5/70.asp
XL97: How to Use the TextColumn Property

http://support.microsoft.com/support/kb/articles/q161/3/46.asp

XL97: How to Determine Which Items Are Selected in a ListBox

http://support.microsoft.com/support/kb/articles/q165/5/01.asp
XL97: Returning Values from ListBox Displaying Multiple Columns

http://support.microsoft.com/support/kb/articles/q165/9/35.asp
XL97: How to Display a ComboBox List when UserForm is Displayed

http://support.microsoft.com/support/kb/articles/q165/6/32.asp
XL97: How to Remove All Items from a ListBox or ComboBox
 
Upvote 0
I am trying to do exactly the same as written in the second port.

I have a module with Auto_Open Sub which have to call userform "tmp" immediately when I open my workbook

Sub Auto_Open()
tmp.Show
End Sub

I created this user form wrote in uberform's code the following:

Const GWL_STYLE = (-16)
Const WS_SYSMENU = &H80000

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 Sub UserForm_Initialize()
'this hides the X on the caption line
Dim hWnd As Long, a As Long

hWnd = FindWindow("ThunderXFrame", Me.Caption)
a = GetWindowLong(hWnd, GWL_STYLE)
SetWindowLong hWnd, GWL_STYLE, a And Not WS_SYSMENU
End Sub

-------------------------------------------------------

But when I open my workbook I am getting "tmp" userform WITH blue title bar i.e without modification. Can somebody explain me what I am doing wrong?
 
Upvote 0
CraigM/Eugene,

If you hide the caption bar will not need any code, as such, to hide the X.

Try the following from http://visualbasicforum.com/t97816.html

Place a CommandButton1 on your Form (with the X gone, you need a means to close the Form).

Form module:
Code:
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
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 GWL_STYLE As Long = (-16)
Private wHandle As Long


Private Sub CommandButton1_Click()
Unload Me
End Sub


Private Sub UserForm_Initialize()
    Dim frm As Long, frmstyle As Long
    If Val(Application.Version) >= 9 Then
        wHandle = FindWindow("ThunderDFrame", Me.Caption)
    Else
        wHandle = FindWindow("ThunderXFrame", Me.Caption)
    End If
    If wHandle = 0 Then Exit Sub
    frm = GetWindowLong(wHandle, GWL_STYLE)
    frm = frm Or &HC00000
    SetWindowLong wHandle, -16, frmstyle
    DrawMenuBar wHandle
End Sub


Private Sub UserForm_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
   'Code to drag the form

   If wHandle = 0 Then Exit Sub
   If Button = 1 Then
       ReleaseCapture
       SendMessage wHandle, &HA1, 2, 0
   End If
End Sub
The last macro above enables you to click in the Form and drag it around the worksheet.

Standard module:
Code:
Sub myForm()
UserForm1.Show
End Sub
HTH

Mike
 
Upvote 0
I have one more small problem :rolleyes:

Now this beautiful userform without title bar with many buttons for different purposes :LOL: is located on ALL my sheets. How to do that this form could be located only on one sheet and vanished when I switch to some other sheet?
 
Upvote 0

Forum statistics

Threads
1,213,544
Messages
6,114,249
Members
448,556
Latest member
peterhess2002

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top