'*****************************************************************************************************
' ___ _ _______ __ _ ____ _ _ _______ ___ _ _ _ ___ _ _.
' // \\ /\\ // // \\ // // // // // // \\ // // // // \\ //| //
' //___// //__\ // //__// // // //__// // // // // // // // // // | //
' // // \\ // // \\ // // // \\ // // // // // // // // // | //
'// // // // // // // //___ // \\ // \\__// //__// //___ \\__// // |//
'****************************************************************************************************
'Window menu setting function
'use of the principle of logical addition
'Author Patricktoulon
'version 1.2
'Date version:22/11/2024
'principle
'1° The principle consists of compiling all the configuration constants of the system menu in positive mode(Or)
'2° then we will compile in negative everything that we do not want in variable suppr
'3° all we have to do is apply the positive parameters and subtract (And Not) the parameters that we don't want with variable [suppr]
'
'Function available for Excel 2007 to Excel 2024 32/64 bytes
'this function display the window if it is not displayed
'----------------------------------------------------------------------------------------------------
Option Explicit
#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
#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
Private Declare PtrSafe Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As LongPtr
#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
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
#End If
Private Const WS_DLGFRAME = &H400000
Private Const WS_BORDER = &H800000
Private Const WS_CAPTION = &HC00000 'WS_BORDER Or WS_DLGFRAME
Private Const WS_SYSMENU = &H80000
Private Const WS_SIZEBOX = &H40000
Private Const WS_MAXIMIZEBOX = &H10000
Private Const WS_MINIMIZEBOX = &H20000
Private Const WS_THICKFRAME = &H40000
Private Const GWL_STYLE = -16
'Private Const WCS_FULL_SYSTEM = &H94CF0080
'Private Const WCS_BASIC_MENU = &H94C80080
'Private Const WCS_NO_MENU = &H94C00080
'Private Const WCS_NO_TitleBar = &H94080080
Sub testOnly_MinimzeButton() 'with Minimize button
ShowFormWhithout UserForm1, True, True, False, False, True
End Sub
Sub test2_Minimize_And_resize() 'with Minimize button and resize
ShowFormWhithout UserForm1, True, True, False, True, True
End Sub
Sub test3_Only_Maximize_Button() 'With Maximize button
ShowFormWhithout UserForm1, True, False, True, False, True
End Sub
Sub test4_Maximize_Button_And_resize() 'With Maximize button and resize
ShowFormWhithout UserForm1, True, False, True, True, True
End Sub
Sub testFulSystem() 'With fufll options for style -16
ShowFormWhithout UserForm1, True, True, True, True, True
End Sub
Sub test5_No_TitleBar() 'without Tile bar
ShowFormWhithout UserForm1, False
End Sub
Sub test6_No_Close_Button() 'without menu(just TitleBar with No button)
ShowFormWhithout UserForm1, True, MenuX:=False
End Sub
Sub ShowFormWhithout(UsF As Object, _
Optional TitleBar As Boolean = False, _
Optional Minimize As Boolean = False, _
Optional Maximize As Boolean = False, _
Optional Resizeb As Boolean = False, _
Optional MenuX As Boolean = False)
Dim FuLL_Menu&, suppr&, OldInW&, OldInH&, DiFFInH&
#If VBA7 Then
Dim H As LongPtr, GWL As LongPtr
#Else
Dim H As Long, GWL As Long
#End If
OldInW = UsF.InsideWidth
OldInH = UsF.InsideHeight
DiFFInH = UsF.Height - OldInH
'FR :Accumulation of all deletion options in positive mode reduced to zero by the boolean argument
'EN : Accumulation of all positive deletion options reduced to zero by the boolean argument
suppr = WS_CAPTION And Not TitleBar Or (WS_MINIMIZEBOX And Not Minimize) Or (WS_MAXIMIZEBOX And Not Maximize) _
Or (WS_SIZEBOX And Not Resizeb) Or (WS_SYSMENU And Not MenuX)
'FR : Accumulation de toute les options en positif (comme si on voulait mettre le menu complet à la fenêtre)
'EN : Accumulation of all the options in positive (as if we wanted to put the complete menu in the window)
FuLL_Menu = WS_DLGFRAME Or WS_BORDER Or WS_SYSMENU Or WS_SIZEBOX Or WS_MAXIMIZEBOX Or WS_MINIMIZEBOX Or WS_THICKFRAME
'we display (show) the window if it is not displayed
If Not UsF.Visible Then UsF.Show
UsF.caption = UsF.caption
'FR :récupère la poignée de la fenêtre(handle)
'EN :get the window handle (handle)
H = FindWindow(vbNullString, UsF.caption)
'FR :Récupération du style actuel de la fenêtre
'EN :Get current style of the window
GWL = GetWindowLongPtr(H, GWL_STYLE)
'FR :application du nouveau style sur la fenêtre
'FR :en mettant tout(GWL Or FuLL_Menu) et en elevant de qui a été demandé dans les arguments
'EN :applying the new style to the window
'EN :by putting everything (GWL Or c) and raising who was asked in the arguments with suppr in negative mode
SetWindowLongPtr H, GWL_STYLE, (GWL Or FuLL_Menu) And Not suppr
With UsF
'To cause the Inside update after the SetWindowLongPtr()
.Width = .Width + 1
.Width = .Width - 1
'approximate correction of dimension errors caused by DWM
.Width = .Width - (.InsideWidth - OldInW)
.Height = .Height - (.InsideHeight - OldInH)
.Top = .Top + DiFFInH * (Abs(Not TitleBar))
End With
End Sub