Hide the X Close Button on Userform ???

jjfletcher

Board Regular
Joined
Sep 10, 2008
Messages
152
Hi everyone...

I am wanting to Hide the X Close Button on Userform ???

Is there a way to modify the vbFormControlMenu in the Macro so that the X Close button is hidden or can it be colored the same as the caption area.... Something???

Code:
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
If CloseMode = vbFormControlMenu Then Cancel = True
End Sub

Is there a way to rewrite this so Cancel = True could be replaced somehow with Visible = False..... I tried this but it failed...

Best Regards,

John
 
Looking closer at the code, you have a bit of a mess here. You've declared the FindWindowEx function but are using the FindWindow function which is totally different.

Give me a minute and I'll put together something for both bitnesses.
 
Upvote 0

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
This should work, though I can't actuallly test on 64bit currently:
Code:
Option Explicit

#If Win64 Then
    'Find the Dialog's Window
    Private Declare PtrSafe Function FindWindow Lib "user32" Alias "FindWindowA" _
            (ByVal lpClassName As String, ByVal lpWindowName As String) As LongPtr
    'Get the current window style
    Public Declare PtrSafe Function GetWindow Lib "user32" _
           (ByVal hWnd As LongPtr, ByVal wCmd As LongPtr) As LongPtr
    'Set the new window style
    Private Declare PtrSafe Function SetWindowLongPtr Lib "user32" Alias "SetWindowLongPtrA" (ByVal hWnd As LongPtr, ByVal nIndex As Long, ByVal dwNewLong As LongPtr) As LongPtr
    Declare PtrSafe Function GetWindowLongPtr Lib "user32" Alias "GetWindowLongA" (ByVal hWnd As LongPtr, ByVal nIndex As Long) As LongPtr
    Dim hWnd                  As LongPtr
    Dim lStyle as LongPtr

#Else
    'Find the Dialog's Window
    Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
                                        (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    'Get the current window style
    Public Declare Function GetWindow Lib "user32" _
                                      (ByVal hWnd As Long, ByVal wCmd As Long) As Long
    'Set the new window style
    Private Declare Function SetWindowLongPtr Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
    Declare Function GetWindowLongPtr Lib "user32" Alias "GetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long) As Long
    Dim hWnd                  As Long
    Dim lStyle As Long
#End If
Const GWL_STYLE = -16
Const WS_CAPTION = &HC00000
Const WS_SYSMENU = &H80000
 
 'Routine to hide the close button on a userform or dialogsheet
 '   oDialog is either the Userform or Dialog object
 
Sub HideCloseButton(oDialog As Object)
    If TypeName(oDialog) = "DialogSheet" Then
        Select Case Int(Val(Application.Version))
        Case 5
        Case 7
            hWnd = FindWindow("bosa_sdm_XL", oDialog.DialogFrame.Caption)
        Case 8
            hWnd = FindWindow("bosa_sdm_XL8", oDialog.DialogFrame.Caption)
        Case Else
            hWnd = FindWindow("bosa_sdm_XL9", oDialog.DialogFrame.Caption)
        End Select
    Else
        Select Case Int(Val(Application.Version))
        Case 8
            hWnd = FindWindow("ThunderXFrame", oDialog.Caption)
        Case Else
            hWnd = FindWindow("ThunderDFrame", oDialog.Caption)
        End Select
    End If
    lStyle = GetWindowLongPtr(hWnd, GWL_STYLE)
    SetWindowLongPtr hWnd, GWL_STYLE, lStyle And Not WS_SYSMENU
End Sub
 
Last edited:
Upvote 0
With permission, I think Rory missed the FindWindow API return in the WIN64 section to be declared as LongPtr ... Also, the lStyle Variable

Code:
Option Explicit

#If Win64 Then
    'Find the Dialog's Window
    Private Declare PtrSafe Function FindWindow Lib "user32" Alias "FindWindowA" _
            (ByVal lpClassName As String, ByVal lpWindowName As String) As LongPtr
    'Get the current window style
    Public Declare PtrSafe Function GetWindow Lib "user32" _
           (ByVal hWnd As LongPtr, ByVal wCmd As LongPtr) As LongPtr
    'Set the new window style
    Private Declare PtrSafe Function SetWindowLongPtr Lib "user32" Alias "SetWindowLongPtrA" (ByVal hWnd As LongPtr, ByVal nIndex As Long, ByVal dwNewLong As LongPtr) As LongPtr
    Declare PtrSafe Function GetWindowLongPtr Lib "user32" Alias "GetWindowLongA" (ByVal hWnd As LongPtr, ByVal nIndex As Long) As LongPtr
    Dim hWnd                  As LongPtr
 Dim lStyle As LongPtr
#Else
    'Find the Dialog's Window
    Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
                                        (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    'Get the current window style
    Public Declare Function GetWindow Lib "user32" _
                                      (ByVal hWnd As Long, ByVal wCmd As Long) As Long
    'Set the new window style
    Private Declare Function SetWindowLongPtr Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
    Declare Function GetWindowLongPtr Lib "user32" Alias "GetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long) As Long
    Dim hWnd                  As Long
    Dim lStyle As Long
#End If
Const GWL_STYLE = -16
Const WS_CAPTION = &HC00000
Const WS_SYSMENU = &H80000
 
 'Routine to hide the close button on a userform or dialogsheet
 '   oDialog is either the Userform or Dialog object
 
Public Sub HideCloseButton(oDialog As Object)
'    Dim lStyle As Long
    If TypeName(oDialog) = "DialogSheet" Then
        Select Case Int(Val(Application.Version))
        Case 5
        Case 7
            hWnd = FindWindow("bosa_sdm_XL", oDialog.DialogFrame.Caption)
        Case 8
            hWnd = FindWindow("bosa_sdm_XL8", oDialog.DialogFrame.Caption)
        Case Else
            hWnd = FindWindow("bosa_sdm_XL9", oDialog.DialogFrame.Caption)
        End Select
    Else
        Select Case Int(Val(Application.Version))
        Case 8
            hWnd = FindWindow("ThunderXFrame", oDialog.Caption)
        Case Else
            hWnd = FindWindow("ThunderDFrame", oDialog.Caption)
        End Select
    End If
    lStyle = GetWindowLongPtr(hWnd, GWL_STYLE)
    SetWindowLongPtr hWnd, GWL_STYLE, lStyle And Not WS_SYSMENU
End Sub
 
Last edited:
Upvote 0
Thanks Jaafar. I'll update my code too in case anyone copies it later!
 
Upvote 0

Forum statistics

Threads
1,217,373
Messages
6,136,183
Members
449,997
Latest member
satyam7054

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