|
-
Oct 19th, 2005, 10:33 PM
#1
Thread Starter
Lively Member
Why can't I disable the close (X) button on the right-top side ?
I want to disable the right-top side of menu. Why can't the following code work ?
Private Declare Function DeleteMenu Lib "user32" (ByVal hMenu As Long, ByVal nPosition As Long, ByVal wFlags As Long) As Long
Private Declare Function GetSystemMenu Lib "user32" (ByVal hwnd As Long, ByVal bRevert As Long) As Long
Private Sub Form_Load()
hMenu = GetSystemMenu(Me.hwnd, 0)
DeleteMenu hMenu, 6, MF_BYPOSITION
End Sub
-
Oct 19th, 2005, 10:41 PM
#2
Re: Why can't I disable the close (X) button on the right-top side ?
Have you declared these anywhere in your code?
VB Code:
Private hMenu As Long
Private Const MF_BYPOSITION As Long = &H400&
Pete
No trees were harmed in the making of this post, however a large number of electrons were greatly inconvenienced.
-
Oct 20th, 2005, 06:03 AM
#3
Re: Why can't I disable the close (X) button on the right-top side ?
Try this
VB Code:
Private Const SC_CLOSE As Long = &HF060&
Private Const MIIM_STATE As Long = &H1&
Private Const MIIM_ID As Long = &H2&
Private Const MFS_GRAYED As Long = &H3&
Private Const WM_NCACTIVATE As Long = &H86
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 String
cch As Long
End Type
Private Declare Function GetSystemMenu Lib "user32" ( _
ByVal hWnd As Long, ByVal bRevert As Long) As Long
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
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
Private Declare Function SendMessage Lib "user32" Alias _
"SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, _
ByVal wParam As Long, lParam As Any) As Long
Private Declare Function IsWindow Lib "user32" _
(ByVal hWnd As Long) As Long
' Enables / Disables the close button on the titlebar and in the system menu
' of the form window passed.
' Return Values:
'
' 0 Close button state changed succesfully / nothing to do.
' -1 Invalid Window Handle (hWnd argument) Passed to the function
' -2 Failed to switch command ID of Close menu item in system menu
' -3 Failed to switch enabled state of Close menu item in system menu
Public Function EnableCloseButton(ByVal hWnd As Long, Enable As Boolean)
As Integer
Const xSC_CLOSE As Long = -10
' Check that the window handle passed is valid
EnableCloseButton = -1
If IsWindow(hWnd) = 0 Then Exit Function
' Retrieve a handle to the window's system menu
Dim hMenu As Long
hMenu = GetSystemMenu(hWnd, 0)
' Retrieve the menu item information for the close menu item/button
Dim MII As MENUITEMINFO
MII.cbSize = Len(MII)
MII.dwTypeData = String(80, 0)
MII.cch = Len(MII.dwTypeData)
MII.fMask = MIIM_STATE
If Enable Then
MII.wID = xSC_CLOSE
Else
MII.wID = SC_CLOSE
End If
EnableCloseButton = -0
If GetMenuItemInfo(hMenu, MII.wID, False, MII) = 0 Then Exit Function
' Switch the ID of the menu item so that VB can not undo the action itself
Dim lngMenuID As Long
lngMenuID = MII.wID
If Enable Then
MII.wID = SC_CLOSE
Else
MII.wID = xSC_CLOSE
End If
MII.fMask = MIIM_ID
EnableCloseButton = -2
If SetMenuItemInfo(hMenu, lngMenuID, False, MII) = 0 Then Exit Function
' Set the enabled / disabled state of the menu item
If Enable Then
MII.fState = (MII.fState Or MFS_GRAYED)
MII.fState = MII.fState - MFS_GRAYED
Else
MII.fState = (MII.fState Or MFS_GRAYED)
End If
MII.fMask = MIIM_STATE
EnableCloseButton = -3
If SetMenuItemInfo(hMenu, MII.wID, False, MII) = 0 Then Exit Function
SendMessage hWnd, WM_NCACTIVATE, True, 0
EnableCloseButton = 0
End Function
'Usage
Private Sub Form_Load()
'enabled
EnableCloseButton Me.hWnd, True
End Sub
Private Sub Command1_Click()
'disabled
EnableCloseButton Me.hWnd, False
End Sub
-
Oct 20th, 2005, 06:26 AM
#4
Re: Why can't I disable the close (X) button on the right-top side ?
Why don't you just intercept the Form Closing Event instead of Sub-Clasing the form:
VB Code:
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
If UnloadMode = vbFormControlMenu Or UnloadMode = 1 Then
'the X has been clicked or the user has pressed Alt+F4
Cancel = True
Me.Hide
End If
End Sub
Regards,
Mark
Please remember to rate posts! Rate any post you find helpful. Use the link to the left - "Rate this Post". Please use [highlight='vb'] your code goes in here [/highlight] tags when posting code. When a question you asked has been resolved, please go to the top of the original post and click "Thread Tools" then select "Mark Thread Resolved."
-
Oct 20th, 2005, 06:36 AM
#5
Re: Why can't I disable the close (X) button on the right-top side ?
 Originally Posted by Mark Gambo
Why don't you just intercept the Form Closing Event instead of Sub-Clasing the form:
VB Code:
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
If UnloadMode = vbFormControlMenu Or UnloadMode = 1 Then
'the X has been clicked or the user has pressed Alt+F4
Cancel = True
Me.Hide
End If
End Sub
This certainly would be a lot easier than running through all of the code required to disable the close button.
-
Oct 20th, 2005, 04:16 PM
#6
Re: Why can't I disable the close (X) button on the right-top side ?
 Originally Posted by Hack
This certainly would be a lot easier than running through all of the code required to disable the close button. 
Although the code posted by chu2654 does work if the variable & constant noted in Post #2 are declared.
Pete
No trees were harmed in the making of this post, however a large number of electrons were greatly inconvenienced.
-
Oct 20th, 2005, 05:20 PM
#7
Re: Why can't I disable the close (X) button on the right-top side ?
 Originally Posted by Hack
This certainly would be a lot easier than running through all of the code required to disable the close button. 
But Hack's point is a good one, sometimes subclassing a form causes other problems especially during development.
Regards,
Mark
Please remember to rate posts! Rate any post you find helpful. Use the link to the left - "Rate this Post". Please use [highlight='vb'] your code goes in here [/highlight] tags when posting code. When a question you asked has been resolved, please go to the top of the original post and click "Thread Tools" then select "Mark Thread Resolved."
-
Oct 20th, 2005, 05:41 PM
#8
Re: Why can't I disable the close (X) button on the right-top side ?
 Originally Posted by Mark Gambo
But Hack's point is a good one, sometimes subclassing a form causes other problems especially during development.
Yeah, I agree. Subclassing can be a nightmare if you're not ultra careful, but chu2654's original code didn't involve any subclassing, neither did Hack's.
Personally I think your solution is a nice one. Can't get into any trouble there.
Pete
No trees were harmed in the making of this post, however a large number of electrons were greatly inconvenienced.
-
Dec 31st, 2005, 10:51 PM
#9
Addicted Member
Re: Why can't I disable the close (X) button on the right-top side ?
 Originally Posted by Mark Gambo
Why don't you just intercept the Form Closing Event instead of Sub-Clasing the form:
VB Code:
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
If UnloadMode = vbFormControlMenu Or UnloadMode = 1 Then
'the X has been clicked or the user has pressed Alt+F4
Cancel = True
Me.Hide
End If
End Sub
Note From Newbie in the 2nd Line:
VB Code:
If UnloadMode = vbFormControlMenu Then
Cancel = True
WindowState = 1 ' I minimized it becasue it will be in the SystemTray
End If
is enough because if there is Code to Unload the form it will not be Unloaded
Last edited by _Conan_; Dec 31st, 2005 at 11:41 PM.
-
Dec 31st, 2005, 11:18 PM
#10
Re: Why can't I disable the close (X) button on the right-top side ?
If you remove the system menu's Close menu item it will disable and gray out the 'x'.
http://www.vbforums.com/showpost.php...71&postcount=2
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Dec 31st, 2005, 11:39 PM
#11
Addicted Member
Re: Why can't I disable the close (X) button on the right-top side ?
 Originally Posted by RobDog888
it's cool and it will be handy someday
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|