Hide close [X] button on excel vba userform for my progress bar

前端 未结 6 1481
面向向阳花
面向向阳花 2020-12-28 09:27

I created a userform to show a progress bar when the macro is still importing sheets \"enter

6条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-28 10:02

    This is an improvement of the above answer of @Peter Albert

    • Windows API calls are now Office x64 safe
    • FindWindow call was improved to find Excel UserForms only. The function in the original answer searches every window class (e.g. Explorer windows and other program's windows). Therefore it could happen that the [x] button of other programs or explorer windows have been removed when their name was the same name as the UserForm.

    Private Const mcGWL_STYLE = (-16)
    Private Const mcWS_SYSMENU = &H80000
    
    'Windows API calls to handle windows
    Private Declare PtrSafe Function FindWindow Lib "user32" Alias "FindWindowA" ( _
        ByVal lpClassName As String, ByVal lpWindowName As String) As LongPtr
      
    #If Win64 Then
        Private Declare PtrSafe Function GetWindowLongPtr Lib "user32" Alias "GetWindowLongPtrA" ( _
            ByVal hwnd As LongPtr, ByVal nIndex As Long) As LongPtr
        Private Declare PtrSafe Function SetWindowLongPtr Lib "user32" Alias "SetWindowLongPtrA" ( _
            ByVal hwnd As LongPtr, ByVal nIndex As Long, ByVal dwNewLong As LongPtr) As LongPtr
    #Else
        Private Declare PtrSafe Function GetWindowLongPtr Lib "user32" Alias "GetWindowLongA" ( _
            ByVal hwnd As LongPtr, ByVal nIndex As Long) As LongPtr
        Private Declare PtrSafe Function SetWindowLongPtr Lib "user32" Alias "SetWindowLongA" ( _
            ByVal hwnd As LongPtr, ByVal nIndex As Long, ByVal dwNewLong As LongPtr) As LongPtr
    #End If
    
    Public Sub RemoveCloseButton(objForm As Object)
        Dim lngStyle As LongPtr
        Dim lngHWnd As LongPtr
        
        Dim lpClassName As String
        lpClassName = vbNullString
        If Val(Application.Version) >= 9 Then
           lpClassName = "ThunderDFrame"
        Else
           lpClassName = "ThunderXFrame"
        End If
        
        lngHWnd = FindWindow(lpClassName, objForm.Caption)
        lngStyle = GetWindowLongPtr(lngHWnd, mcGWL_STYLE)
    
        If lngStyle And mcWS_SYSMENU > 0 Then
            SetWindowLongPtr lngHWnd, mcGWL_STYLE, (lngStyle And Not mcWS_SYSMENU)
        End If
    End Sub
    

    ThunderDFrame?
    The UserForms in Excel are actually of the Windows class ThunderDFrame, which is the class for all UserFroms in Microsoft Office applications after 2002. Before that, it was ThunderXFrame.

提交回复
热议问题