Share via

Excel VBA - How to exclude Minimize and Esc button on Userform

Anonymous
2023-05-12T08:12:14+00:00

Dear All,

by the below code I maximize an Excel VBA userform and all the controls on it:

Private Declare PtrSafe Function FindWindowA Lib "user32" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long

    Private Declare PtrSafe Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long) As Long 

    Private Declare PtrSafe Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long

Private Const GWL_STYLE As Long = (-16)

Private Const WS_THICKFRAME As Long = &H40000

Private Const WS_MAXIMIZEBOX As Long = &H10000

Private Const WS_MINIMIZEBOX = &H20000

Public Sub Sizable(FormCaption As String) 

'****Userform and its controls gets resized**** 

    Dim hWndForm As Long 

    Dim iStyle As Long 

    If Val(Application.Version) < 9 Then 

        hWndForm = FindWindow("ThunderXFrame", FormCaption)

    Else 

        hWndForm = FindWindow("ThunderDFrame", FormCaption)

    End If 

    iStyle = GetWindowLong(hWndForm, GWL_STYLE) 

    iStyle = iStyle Or WS_THICKFRAME 

    SetWindowLong hWndForm, GWL_STYLE, iStyle 

End Sub 

Public Sub MinMax(FormCaption As String) 

'****Used to show the Minimized and Maximized button on Userform**** 

    Dim hWndForm As Long 

    Dim iStyle As Long 

    If Val(Application.Version) < 9 Then 

        hWndForm = FindWindow("ThunderXFrame", FormCaption) 

    Else 

        hWndForm = FindWindow("ThunderDFrame", FormCaption) 

    End If 

    iStyle = GetWindowLong(hWndForm, GWL_STYLE) 

    iStyle = iStyle Or WS_MAXIMIZEBOX 

    iStyle = iStyle Or WS_MINIMIZEBOX 

    SetWindowLong hWndForm, GWL_STYLE, iStyle 

End Sub

but I would like to keep only the maximized button and exclude Minimize and Esc button as below image

any suggestion?

Thanks

Microsoft 365 and Office | Excel | Other | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

11 answers

Sort by: Most helpful
  1. HansV 462.6K Reputation points
    2023-05-13T20:42:12+00:00

    It is only possible to completely hide the Close button if you also hide the Maximize/Restore button.

    Since you want to display the Maximize/Restore button, the best you can do is to disable the Close button; the tooltip will remain visible.

    As an alternative, hide all buttons in the title bar, and provide a command button on the form to maximize/restore it.

    Was this answer helpful?

    1 person found this answer helpful.
    0 comments No comments
  2. HansV 462.6K Reputation points
    2023-05-12T09:05:15+00:00

    Like this:

    Private Declare PtrSafe Function FindWindow _
        Lib "user32" Alias "FindWindowA" _
        (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    Private Declare PtrSafe Function GetWindowLong _
        Lib "user32" Alias "GetWindowLongA" _
        (ByVal hWnd As Long, ByVal nIndex As Long) As Long
    Private Declare PtrSafe Function SetWindowLong _
        Lib "user32" Alias "SetWindowLongA" _
        (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
    Private Declare PtrSafe Function DeleteMenu _
        Lib "user32" _
        (ByVal hMenu As Long, ByVal nPosition As Long, ByVal wFlags As Long) As Long
    Private Declare PtrSafe Function GetSystemMenu _
        Lib "user32" _
        (ByVal hWnd As Long, ByVal bRevert As Long) As Long
    
    Private Const GWL_STYLE As Long = (-16)
    Private Const WS_THICKFRAME As Long = &H40000
    Private Const WS_MAXIMIZEBOX As Long = &H10000
    Private Const WS_MINIMIZEBOX = &H20000
    Private Const SC_CLOSE = &HF060
    
    Public Sub Sizable(FormCaption As String)
    '****Userform and its controls gets resized****
        Dim hWndForm As Long
        Dim iStyle As Long
        hWndForm = FindWindow("ThunderDFrame", FormCaption)
        iStyle = GetWindowLong(hWndForm, GWL_STYLE)
        iStyle = iStyle Or WS_THICKFRAME
        SetWindowLong hWndForm, GWL_STYLE, iStyle
    End Sub
    
    Public Sub OnlyMax(FormCaption As String)
    '****Used to show the Maximized button on Userform****
        Dim hWndForm As Long
        Dim menuHandle As Long
        Dim iStyle As Long
        hWndForm = FindWindow("ThunderDFrame", FormCaption)
        iStyle = GetWindowLong(hWndForm, GWL_STYLE)
        iStyle = iStyle Or WS_MAXIMIZEBOX
        SetWindowLong hWndForm, GWL_STYLE, iStyle
        menuHandle = GetSystemMenu(hWndForm, 0)
        DeleteMenu menuHandle, SC_CLOSE, 0&
    End Sub
    

    Was this answer helpful?

    1 person found this answer helpful.
    0 comments No comments
  3. HansV 462.6K Reputation points
    2023-05-12T11:01:31+00:00

    When I hide the close button, the maximize button disappears too.

    Was this answer helpful?

    0 comments No comments
  4. Anonymous
    2023-05-12T10:40:30+00:00

    Like this:

    Private Declare PtrSafe Function FindWindow _
        Lib "user32" Alias "FindWindowA" _
        (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    Private Declare PtrSafe Function GetWindowLong _
        Lib "user32" Alias "GetWindowLongA" _
        (ByVal hWnd As Long, ByVal nIndex As Long) As Long
    Private Declare PtrSafe Function SetWindowLong _
        Lib "user32" Alias "SetWindowLongA" _
        (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
    Private Declare PtrSafe Function DeleteMenu _
        Lib "user32" _
        (ByVal hMenu As Long, ByVal nPosition As Long, ByVal wFlags As Long) As Long
    Private Declare PtrSafe Function GetSystemMenu _
        Lib "user32" _
        (ByVal hWnd As Long, ByVal bRevert As Long) As Long
    
    Private Const GWL_STYLE As Long = (-16)
    Private Const WS_THICKFRAME As Long = &H40000
    Private Const WS_MAXIMIZEBOX As Long = &H10000
    Private Const WS_MINIMIZEBOX = &H20000
    Private Const SC_CLOSE = &HF060
    
    Public Sub Sizable(FormCaption As String)
    '****Userform and its controls gets resized****
        Dim hWndForm As Long
        Dim iStyle As Long
        hWndForm = FindWindow("ThunderDFrame", FormCaption)
        iStyle = GetWindowLong(hWndForm, GWL_STYLE)
        iStyle = iStyle Or WS_THICKFRAME
        SetWindowLong hWndForm, GWL_STYLE, iStyle
    End Sub
    
    Public Sub OnlyMax(FormCaption As String)
    '****Used to show the Maximized button on Userform****
        Dim hWndForm As Long
        Dim menuHandle As Long
        Dim iStyle As Long
        hWndForm = FindWindow("ThunderDFrame", FormCaption)
        iStyle = GetWindowLong(hWndForm, GWL_STYLE)
        iStyle = iStyle Or WS_MAXIMIZEBOX
        SetWindowLong hWndForm, GWL_STYLE, iStyle
        menuHandle = GetSystemMenu(hWndForm, 0)
        DeleteMenu menuHandle, SC_CLOSE, 0&
    End Sub
    

    Your code is OK but I would like to hide the Minimize and Esc icon on the userform or, at least, remove the tooltip

    The below line does not work because ControlBox is not recognized

    Private Sub UserForm_Initialize()
    
         Me.ControlBox = False
    
    End Sub
    

    Thanks

    Was this answer helpful?

    0 comments No comments
  5. Anonymous
    2023-05-12T09:00:19+00:00

    Hi Lucausa

    I'm AnnaThomas and I'd happily help you with your question. In this Forum, we are Microsoft consumers just like yourself.

    If you want to keep only the maximize button and exclude the minimize and close buttons on a UserForm, you can modify the MinMax function in your code. You can remove the line iStyle = iStyle Or WS_MINIMIZEBOX to exclude the minimize button.

    To exclude the close button, you can set the ControlBox property of the UserForm to False. You can do this in the UserForm’s properties window or in its code module by adding the line Me.ControlBox = False in the UserForm_Initialize event.

    Here is an example of how your modified MinMax function and UserForm_Initialize event might look:

    Public Sub MinMax(FormCaption As String) '****Used to show the Maximized button on Userform**** Dim hWndForm As Long Dim iStyle As Long

    If Val(Application.Version) < 9 Then hWndForm = FindWindow("ThunderXFrame", FormCaption) Else hWndForm = FindWindow("ThunderDFrame", FormCaption) End If

    iStyle = GetWindowLong(hWndForm, GWL_STYLE) iStyle = iStyle Or WS_MAXIMIZEBOX SetWindowLong hWndForm, GWL_STYLE, iStyle End Sub

    Private Sub UserForm_Initialize() Me.ControlBox = False End Sub

    I hope this helps ;-), let me know if this is contrary to what you need, I would still be helpful to answer more of your questions.

    Best Regards,

    AnnaThomas

    Give back to the community. Help the next person with this problem by indicating whether this answer solved your problem. Click Yes or No at the bottom.

    Was this answer helpful?

    0 comments No comments