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
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
Wish to Re-Propose the question...

Is it possible to change the X close button Color from Black to the color of the Caption section???

John
 
Upvote 0
Hi ,
There is but remember to add a commandbutton to close the userform
Code:
Option Explicit
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 WS_SYSMENU = &H80000
Const GWL_STYLE = (-16)
Private Sub UserForm_Initialize()
    Call HideCloseButton(Me)
End Sub
Sub HideCloseButton(oDialog As Object)
    Dim hWnd As Long, 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 = GetWindowLong(hWnd, GWL_STYLE)
    SetWindowLong hWnd, GWL_STYLE, lStyle And Not WS_SYSMENU
End Sub
Private Sub CommandButton1_Click()
    Unload Me
End Sub
 
Upvote 0
Also please note that we have the same cross posting rules as pretty much every other forum. ;)
 
Upvote 0
Thanks Rory...

Hello everyone - Getting this error

Findwindow in Sub HideCloseButton string below: hWnd = FindWindow("bosa_sdm_XL", oDialog.DialogFrame.Caption) is being highlighted BLUE

Code:
Option Private Module

'Public Const GWL_STYLE = -16
'Public Const WS_CAPTION = &HC00000
' Public Declare Function SendMessageA Lib "user32" (ByVal hWnd As Long, ByVal wMsg As Long, _
                                                    ByVal wParam As Long, lParam As Any) As Long
                                                    
'Public Declare PtrSafe Function SendMessageA Lib "USER32" (ByVal hWnd As LongPtr, ByVal wMsg As Long, _
                                                            ByVal wParam As Long, lParam As Any) As LongPtr
                                                           

'Sub HideTitleBar(Frm As Object)
'    Dim lngWindow As Long
'    Dim lFrmHdl As Long
    
'    lFrmHdl = FindWindowA(vbNullString, Frm.Caption)
'    lngWindow = GetWindowLong(lFrmHdl, GWL_STYLE)
'    lngWindow = lngWindow And (Not WS_CAPTION)
    
'    Call SetWindowLong(lFrmHdl, GWL_STYLE, lngWindow)
'    Call DrawMenuBar(lFrmHdl)
'End Sub

 'Find the Dialog's Window
Private Declare PtrSafe Function FindWindowEx Lib "USER32" _
                                   Alias "FindWindowExA" (ByVal hWnd1 As LongPtr, ByVal hWnd2 As LongPtr, _
                                   ByVal lpsz1 As String, ByVal lpsz2 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
     
Const WS_SYSMENU = &H80000
Const GWL_STYLE = (-16)
 
 'Routine to hide the close button on a userform or dialogsheet
 '   oDialog is either the Userform or Dialog object
 


Sub HideCloseButton(oDialog As Object)
    Dim hWnd As Long, 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 = GetWindowLong(hWnd, GWL_STYLE)
    SetWindowLong hWnd, GWL_STYLE, lStyle And Not WS_SYSMENU
End Sub

Best Regards,

John
 
Last edited:
Upvote 0
You've updated FindWindow for 64 bit compatibility so it now returns a LongPtr, which you are trying to assign to a variable declared as a Long.

Do you need this to work on both 32 and 64 bit?
 
Upvote 0

Forum statistics

Threads
1,216,075
Messages
6,128,657
Members
449,462
Latest member
Chislobog

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