
Changing Form's BorderStyle property at run time but before form is loaded
Quote:
> Almost done but I need to disable (grayed) X
That one's not so easy, but is doable.
Using the below code
To disable,
m_fCloseEnabled = EnableCloseButton(hwnd, False)
To toggle,
m_fCloseEnabled = EnableCloseButton(hwnd, Not m_fCloseEnabled)
David
Watch for word wrap,
Private Const SC_CLOSE As Long = &HF060&
Private Const MIIM_STATE As Long = &H1&
Private Const MIIM_ID As Long = &H2&
Private Const MF_ENABLED As Long = &H0&
Private Const MF_GRAYED As Long = &H3&
Private Type MENUITEMINFO
cbSize As Long
fMask As Long
fType As Long
fState As Long
wID As Long
hSubMenu As Long
hbmpChecked As Long
hbmpUnchecked As Long
dwItemData As Long
dwTypeData As Long 'String
cch As Long
End Type
'* Retreives the handle to the system menu for the specified window
Private Declare Function GetSystemMenu Lib "user32" ( _
ByVal hwnd As Long, _
ByVal bRevert As Long) As Long
'* Retreives inforamtion about a menu entry
Private Declare Function GetMenuItemInfo Lib "user32" _
Alias "GetMenuItemInfoA" ( _
ByVal hMenu As Long, _
ByVal un As Long, _
ByVal b As Boolean, _
lpMenuItemInfo As MENUITEMINFO) As Long
'* Set the specified information for a menu entry
Private Declare Function SetMenuItemInfo Lib "user32" _
Alias "SetMenuItemInfoA" ( _
ByVal hMenu As Long, _
ByVal un As Long, _
ByVal bool As Boolean, _
lpcMenuItemInfo As MENUITEMINFO) As Long
'* Enables/disables the specified menu item
Private Declare Function EnableMenuItem Lib "user32" ( _
ByVal hMenu As Long, _
ByVal wIDEnableItem As Long, _
ByVal wEnable As Long) As Long
'* Redraws the menu for the specified window
Private Declare Function DrawMenuBar Lib "user32" ( _
ByVal hwnd As Long) As Long
'* Form close button state
Private m_fCloseEnabled As Boolean
Public Function EnableCloseButton(ByVal hwnd As Long, ByVal Enable As
Boolean) As Boolean
'* Purpose : Enables/disables Close system menu item
'* Accepts : hWnd - handle of the window
' Enable - True to enable, False to disable
'* Returns : Boolean - enabled state
'* Modified : 8/14/2006 dwy
'* Note : Alt+F4 must be handled in the Query_Unload event
'* : Errors are handled by the calling procedure
Dim hMenu As Long
Dim mii As MENUITEMINFO
'* Default value
EnableCloseButton = Enable
'* Get a handle to the system menu
hMenu = GetSystemMenu(hwnd, 0)
If hMenu Then
'* Fill in size parameter
mii.cbSize = Len(mii)
If Enable Then
'* Get the menu item information for the close menu item
'* Exit if it fails, item is most likely already enabled
If GetMenuItemInfo(hMenu, -SC_CLOSE, False, mii) = 0 Then
GoTo PROC_EXIT
End If
'* Switch the ID of the menu item so that VB can't reset it
mii.wID = SC_CLOSE
mii.fMask = MIIM_ID
If SetMenuItemInfo(hMenu, -SC_CLOSE, False, mii) = 0 Then
GoTo PROC_EXIT
End If
'* Set enabled state of the menu item
EnableCloseButton = EnableMenuItem(hMenu, SC_CLOSE, MF_ENABLED)
Else
'* Get the menu item information for the close menu item
'* Exit if it fails, item is most likely already
disabled(grayed)
If GetMenuItemInfo(hMenu, SC_CLOSE, False, mii) = 0 Then
GoTo PROC_EXIT
End If
'* Switch the ID of the menu item so that VB can't reset it
mii.wID = -SC_CLOSE
mii.fMask = MIIM_ID
If SetMenuItemInfo(hMenu, SC_CLOSE, False, mii) = 0 Then
GoTo PROC_EXIT
End If
'* Set disabled state of the menu item
EnableCloseButton = EnableMenuItem(hMenu, -SC_CLOSE, MF_GRAYED)
End If
'* Force a redraw of the menu
Call DrawMenuBar(hwnd)
End If
PROC_EXIT:
Exit Function
End Function
Private Sub Form_Load()
m_fCloseEnabled = True
End Sub
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
If UnloadMode = vbFormControlMenu Then
Cancel = Not m_fCloseEnabled
End If
End Sub