gmaxey
10-19-2017, 08:31 AM
I realize that the subject post isn't restricted to Excel, but I recently got valuable assistance in this forum on a the topic and hope for more.
My project requires one userform with a minimize button that is auto "Clicked" following the command button execution and another userform that required the "X" close button to be removed.
When it comes to APIs, I am strictly Monkey see Monkey do. I have absolutely zero understanding how they actually work. Anyway, I have pieced together (from examples posted in this an other forums) some code that works and attached an example file.
I have Windows 7 Office 2106 32 bit so I am unable to confirm that my declarations would work in a 64 bit system or a system using VBA6.
I have made some of the declarations Public because they are used in the in both the standard module and the userform module. Not sure if this is a good or best practice.
If anyone with experience in this area or who has a 64 bit system can comment I appreciate it.
Private Const mcGWL_STYLE = (-16)
Private Const mcWS_SYSMENU = &H80000
Public Const SW_MINIMIZE = 6
'APIs for adding minimize button to userform or removing X button.
#If VBA7 Then
#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
Public Declare PtrSafe Function FindWindow Lib "USER32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As LongPtr
Public Declare PtrSafe Function ShowWindow Lib "USER32" (ByVal hWnd As LongPtr, ByVal nCmdShow As Long) As Long
Private hWnd As LongPtr
Private lngStyle As LongPtr
#Else
Private Declare PtrSafe Function GetWindowLongPtr Lib "USER32" Alias "GetWindowLongA" (ByVal hWnd As LongPtr, ByVal nIndex As Long) As LongPtr
Private Declare Function SetWindowLongPtr Lib "USER32" Alias "SetWindowLongA" (ByVal hWnd As LongPtr, ByVal nIndex As Long, ByVal dwNewLong As LongPtr) As LongPtr
Public Declare PtrSafe Function FindWindow Lib "USER32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As LongPtr
Public Declare PtrSafe Function ShowWindow Lib "USER32" (ByVal hWnd As LongPtr, ByVal nCmdShow As Long) As Long
Public hWnd As LongPtr
Private lngStyle As LongPtr
#End If
#Else
Private Declare Function GetWindowLongPtr Lib "USER32" Alias "GetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long) As Long
Private Declare Function SetWindowLongPtr Lib "USER32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Public Declare Function FindWindow Lib "USER32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Public Declare Function ShowWindow Lib "USER32" (ByVal hWnd As Long, ByVal nCmdShow As Long) As Long
Public hWnd As Long
Private lngStyle As Long
#End If
Sub RemoveCloseButton(strUFCaption As String)
hWnd = FindWindow(vbNullString, strUFCaption)
lngStyle = GetWindowLongPtr(hWnd, mcGWL_STYLE)
If lngStyle And mcWS_SYSMENU > 0 Then
SetWindowLongPtr hWnd, mcGWL_STYLE, (lngStyle And Not mcWS_SYSMENU)
End If
lbl_Exit:
Exit Sub
End Sub
Sub AddMinimizeButton(strUFCaption As String)
hWnd = FindWindow(vbNullString, strUFCaption)
lngStyle = GetWindowLongPtr(hWnd, mcGWL_STYLE)
If (lngStyle And &H20000) = 0 Then
SetWindowLongPtr hWnd, mcGWL_STYLE, lngStyle Or &H20000
End If
lbl_Exti:
Exit Sub
End Sub
My project requires one userform with a minimize button that is auto "Clicked" following the command button execution and another userform that required the "X" close button to be removed.
When it comes to APIs, I am strictly Monkey see Monkey do. I have absolutely zero understanding how they actually work. Anyway, I have pieced together (from examples posted in this an other forums) some code that works and attached an example file.
I have Windows 7 Office 2106 32 bit so I am unable to confirm that my declarations would work in a 64 bit system or a system using VBA6.
I have made some of the declarations Public because they are used in the in both the standard module and the userform module. Not sure if this is a good or best practice.
If anyone with experience in this area or who has a 64 bit system can comment I appreciate it.
Private Const mcGWL_STYLE = (-16)
Private Const mcWS_SYSMENU = &H80000
Public Const SW_MINIMIZE = 6
'APIs for adding minimize button to userform or removing X button.
#If VBA7 Then
#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
Public Declare PtrSafe Function FindWindow Lib "USER32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As LongPtr
Public Declare PtrSafe Function ShowWindow Lib "USER32" (ByVal hWnd As LongPtr, ByVal nCmdShow As Long) As Long
Private hWnd As LongPtr
Private lngStyle As LongPtr
#Else
Private Declare PtrSafe Function GetWindowLongPtr Lib "USER32" Alias "GetWindowLongA" (ByVal hWnd As LongPtr, ByVal nIndex As Long) As LongPtr
Private Declare Function SetWindowLongPtr Lib "USER32" Alias "SetWindowLongA" (ByVal hWnd As LongPtr, ByVal nIndex As Long, ByVal dwNewLong As LongPtr) As LongPtr
Public Declare PtrSafe Function FindWindow Lib "USER32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As LongPtr
Public Declare PtrSafe Function ShowWindow Lib "USER32" (ByVal hWnd As LongPtr, ByVal nCmdShow As Long) As Long
Public hWnd As LongPtr
Private lngStyle As LongPtr
#End If
#Else
Private Declare Function GetWindowLongPtr Lib "USER32" Alias "GetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long) As Long
Private Declare Function SetWindowLongPtr Lib "USER32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Public Declare Function FindWindow Lib "USER32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Public Declare Function ShowWindow Lib "USER32" (ByVal hWnd As Long, ByVal nCmdShow As Long) As Long
Public hWnd As Long
Private lngStyle As Long
#End If
Sub RemoveCloseButton(strUFCaption As String)
hWnd = FindWindow(vbNullString, strUFCaption)
lngStyle = GetWindowLongPtr(hWnd, mcGWL_STYLE)
If lngStyle And mcWS_SYSMENU > 0 Then
SetWindowLongPtr hWnd, mcGWL_STYLE, (lngStyle And Not mcWS_SYSMENU)
End If
lbl_Exit:
Exit Sub
End Sub
Sub AddMinimizeButton(strUFCaption As String)
hWnd = FindWindow(vbNullString, strUFCaption)
lngStyle = GetWindowLongPtr(hWnd, mcGWL_STYLE)
If (lngStyle And &H20000) = 0 Then
SetWindowLongPtr hWnd, mcGWL_STYLE, lngStyle Or &H20000
End If
lbl_Exti:
Exit Sub
End Sub