|
-
May 8th, 2005, 06:50 PM
#1
disabling close button
hi all!!
can neone tell me how to disable the CLOSE button (X) on the top right of the form while some process is on the run??
thnx
-
May 8th, 2005, 06:56 PM
#2
Re: disabling close button
Well I could show you how to remove the close command and therefor disable the close button but as I understand it you want to enable it again after awhile and that's another matter. The easiest thing you could do is to simply set some boolean flag while you're running some code and use the QueryUnload event of the Form to check if the Form is being unloaded because someone pressed the close button (or picked Close in the system menu) and then simply ignore it if the boolean flag is still set to True.
-
May 8th, 2005, 07:01 PM
#3
Fanatic Member
Re: disabling close button
PrivateSub Form_Load()
EnableCloseButton me.hWnd, false
End Sub
And to re-enable it later:
private Sub Command1_Click()
EnableCloseButton me.hWnd, true
End Sub
oops sorry, I didnt finish reading the example I found on google lol, my bad.
Ne ways, heres the module that was with that example.
I thought it was simple than that 
VB Code:
Option Explicit
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
'
'-------------------------------------------------------------------------------
' Parameters:
'
' hWnd The window handle of the form whose close button is to be enabled/
' disabled / greyed out.
'
' Enable True if the close button is to be enabled, or False if it is to
' be disabled / greyed out.
'
'-------------------------------------------------------------------------------
' Example:
'
' Add a form window to your project, and place a button on the form. Add the
' following in the form's code window:
'
' Option Explicit
'
' Private m_blnCloseEnabled As Boolean
'
' Private Sub Form_Load()
' m_blnCloseEnabled = True
' Command1.Caption = "Disable"
' End Sub
'
' Private Sub Command1_Click()
' m_blnCloseEnabled = Not m_blnCloseEnabled
' EnableCloseButton Me.hwnd, m_blnCloseEnabled
'
' If m_blnCloseEnabled Then
' Command1.Caption = "Disable"
' Else
' Command1.Caption = "Enable"
' End If
' End Sub
'
'-------------------------------------------------------------------------------
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
' Activate the non-client area of the window to update the titlebar, and
' draw the close button in its new state.
SendMessage hWnd, WM_NCACTIVATE, True, 0
EnableCloseButton = 0
End Function
'*******************************************************************************
'
'-------------------------------------------------------------------------------
Last edited by planethax; May 8th, 2005 at 08:08 PM.
-
May 8th, 2005, 07:07 PM
#4
Re: disabling close button
There's also the Controlbox property of the form you can set to false, but that would eliminate the minimize and maximize buttons as well.
-
May 8th, 2005, 07:15 PM
#5
Re: disabling close button
 Originally Posted by planethax
PrivateSub Form_Load()
EnableCloseButton me.hWnd, false
End Sub
And to re-enable it later:
private Sub Command1_Click()
EnableCloseButton me.hWnd, true
End Sub
That looks great! Do you also have the source code for the EnableCloseButton procedure or did you just make that up? 
 Originally Posted by Jacob Roman
There's also the Controlbox property of the form you can set to false, but that would eliminate the minimize and maximize buttons as well.
But the ControlBox property can't be changed during run-time.
-
May 8th, 2005, 07:21 PM
#6
Re: disabling close button
I know it can't be changed during runtime. Just another one of them limitations.
-
May 8th, 2005, 07:49 PM
#7
Re: disabling close button
 Originally Posted by Jacob Roman
I know it can't be changed during runtime. Just another one of them limitations.
Limitation? Well that might be true but the reason for that is that when you create a window you need to determent how this window will look and act. If you want it to have a system menu, what kind of border the window should have, the owner of the window (should it be displayed modally or not)...
All of this VB does for you and all you have to do is to simply set some properties during design time. But if you want to change any of these behaviours you need to destroy the window and recreate it again from scratch.
That is why you can't simply change some of these properties during run-time since it means a new window has to be created, and in VB we design and create our windows during design time (even though they aren't really created before we choose to show them).
-
May 9th, 2005, 12:17 AM
#8
Re: disabling close button
I'd just do it this way
VB Code:
Option Explicit
Private m_bBlockUnload As Boolean
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
If m_bBlockUnload Then
If UnloadMode = 0 Then
Cancel = True
End If
End If
End Sub
-
May 9th, 2005, 09:13 AM
#9
Re: disabling close button
thnx planethax....yours code's working nicely.
-
May 9th, 2005, 09:14 AM
#10
Re: disabling close button
thnx longwolf....even ur code is nice but i failed to enable it afterwards. can u explain how can i do that.
-
May 9th, 2005, 09:47 AM
#11
Re: disabling close button
When ever your program begins a process that you want to protect just set:
m_bBlockUnload = True.
Also the line:
will block the 'X' from closing the app, you may want to check other 'modes' of closing the program and block them too.
Use the VB help files to pick the modes to block.
-
May 10th, 2005, 02:57 PM
#12
Re: disabling close button
@Joacim, its originally posted by Hack on VBF but originated over at CodeGuru. I knew I seen that code before. 
Disable x
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 
-
May 10th, 2005, 04:04 PM
#13
Re: disabling close button
Thanks Rob, that's the same code posted by Planethax above. I was well aware of the code to remove the Close menu item (or any other system menu items). However I've never seen the code to get it back while it was gone before. Probably because I've never found myself in the need of doing so Thanks anyway!
-
May 11th, 2005, 01:26 PM
#14
Re: disabling close button
hey longwolf!! can u explain me one thing in ur code?!?
in the queryunload procedure, a parameter cancel is declared as integer then how can we assign a boolean value to it:-
Cancel = True
-
May 11th, 2005, 01:32 PM
#15
Re: disabling close button
 Originally Posted by Harsh Gupta
hey longwolf!! can u explain me one thing in ur code?!?
in the queryunload procedure, a parameter cancel is declared as integer then how can we assign a boolean value to it:-
Cancel = True
Here is a description:
VB Code:
Private m_bBlockUnload As Boolean ' Global variable to determine if the form should b allowed to unload.
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
If m_bBlockUnload Then ' Are we allowed to unload this form?
If UnloadMode = 0 Then ' Check the forums unload mode.
Cancel = True ' Cancel the close.
End If ' Close the IF statement.
End If ' Close the IF statement.
End Sub
And yes thats odd, I always use a True too, it says its fine in the MSDN documentation so...
Cheers,
RyanJ
-
May 11th, 2005, 01:36 PM
#16
Re: disabling close button
A Boolean is stored as an Integer. The True and False values are Integer values corresponding to -1 and 0 respectively. Before version 4 VB didn't have an Integer data type but the True and False values did exist. We then used an Integer as a Boolean.
When it comes to the QueryUnload or the Unload events of a Form it doesn't really matter what value you give to the Cancel property. As long as it is not equal to 0 it will stop the unloading of the Form.
-
May 11th, 2005, 01:37 PM
#17
Re: disabling close button
 Originally Posted by Joacim Andersson
A Boolean is stored as an Integer. The True and False values are Integer values corresponding to -1 and 0 respectively. Before version 4 VB didn't have an Integer data type but the True and False values did exist. We then used an Integer as a Boolean.
Right that makes sence too me 
Thanks for the info even though I did not aks for it Joacim Andersson! 
Cheers,
RyanJ
-
May 11th, 2005, 01:39 PM
#18
Re: disabling close button
 Originally Posted by sciguyryan
Thanks for the info even though I did not aks for it Joacim Andersson! 
No you didn't ask for it. I was replying to Harsh Gupta that did ask about it.
-
May 12th, 2005, 06:01 AM
#19
Re: disabling close button
oooookkkkk thnx Joacim Andersson
-
May 12th, 2005, 10:29 AM
#20
Re: disabling close button
 Originally Posted by Joacim Andersson
No you didn't ask for it. I was replying to Harsh Gupta that did ask about it.
Yes, I know Harsh Gupta did but it was still useful to know 
I meant it in the context that, though I did not ask for it I would still say thanks 
Cheers,
RyanJ
-
Jun 14th, 2013, 05:47 AM
#21
Hyperactive Member
Re: disabling close button
Sorry for dragging this topic up again, but the above code doesn't work correctly anymore in at least windows 7 (it doesn't draw the Close button disable).
the following is a version that does work correctly in windows 7. It's based on the code above which changes the ID of the SC_CLOSE due to VB resetting the state if you use the dropdown menu..
It took a while to figure out what went wrong.. It seems Windows 7 relies on the ID SC_CLOSE and some other state to update the X button.
I also tried replacing my usage of EnableMenuItem with the original SetMenuItem but for some strange reason that only seems to work when you won't use the dropdown menu, once you use that, the drawstate of the button isn't updated anymore..
The following code does work in all the situations I tested.. So I hope this will help some other people who stumbled on the same problem as I have..
Code:
Public Declare Function EnableMenuItem Lib "user32.dll" (ByVal hMenu As Long, ByVal wIDEnableItem As Long, ByVal wEnable As Long) As Long
Public Sub EnableCloseButton(ByVal HwndForm As Long, ByVal bEnabled As Boolean)
Dim hMenu As Long
Dim lID As Long
Dim lOldID As Long
Dim lReturn As Long
Dim tMII As MENUITEMINFO
hMenu = GetSystemMenu(HwndForm, 0)
With tMII
.cbSize = Len(tMII)
.dwTypeData = String(80, 0)
.cCH = Len(.dwTypeData)
.fMask = MIIM_STATE
.wID = SC_CLOSE
'--------------------------------------------------------------
'Try to get the SC_Close Menu
lReturn = GetMenuItemInfo(hMenu, SC_CLOSE, False, tMII)
'--------------------------------------------------------------
'Didn't exist? then Try the xSC_Close Menu (When the menu is
'disabled by us we set the ID to xSC_Close)
If lReturn = 0 Then
.wID = xSC_CLOSE
lReturn = GetMenuItemInfo(hMenu, xSC_CLOSE, False, tMII)
End If 'If lReturn=0
'--------------------------------------------------------------
'We got the MenuItemInfo?
If lReturn <> 0 Then
'--------------------------------------------------------------
'Menu still original Close? then change state (before changing ID)
If .wID = SC_CLOSE Then lReturn = EnableMenuItem(hMenu, .wID, IIf(bEnabled, MF_ENABLED, MF_GRAYED))
'--------------------------------------------------------------
'Because VB Changes the State depending on the ID on the fly
'we have to Change the ID to xSC_Close if Disabled and to SC_Close when Enabled
lOldID = .wID
lID = IIf(bEnabled, SC_CLOSE, xSC_CLOSE)
If lID <> lOldID Then
.fMask = MIIM_ID
.wID = lID
lReturn = SetMenuItemInfo(hMenu, lOldID, False, tMII)
If lReturn = 0 Then .wID = lOldID
End If 'If lID<>lOldID
'--------------------------------------------------------------
'Only update the state again if the menuID is the original SC_CLOSE
If .wID = SC_CLOSE Then lReturn = EnableMenuItem(hMenu, .wID, IIf(bEnabled, MF_ENABLED, MF_GRAYED))
End If 'If lReturn<>0
End With 'With tMII
End Sub
-
Jun 14th, 2013, 05:50 AM
#22
Re: disabling close button
@SuperDre,
There was no need to reply to this topic since it over 8 years old. Please start a new topic.
when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
https://get.cryptobrowser.site/30/4111672
-
Jun 14th, 2013, 05:53 AM
#23
Hyperactive Member
Re: disabling close button
Yes it was, as the solutions give don't work properly anymore in newer windows, at least not as the way I expected, and therefore with the new solution it will help other people, as this was also the topic I found when I was looking for a solution.. We're here to help each other aren't we? so any better solution to a problem is IMHO best put in a topic which already discusses the problem and not create another new topic for it.. but that's just MY opinion..
-
Jun 14th, 2013, 06:07 AM
#24
Re: disabling close button
 Originally Posted by SuperDre
Yes it was, as the solutions give don't work properly anymore in newer windows, at least not as the way I expected, and therefore with the new solution it will help other people, as this was also the topic I found when I was looking for a solution.. We're here to help each other aren't we? so any better solution to a problem is IMHO best put in a topic which already discusses the problem and not create another new topic for it.. but that's just MY opinion..
Ah ok! I thought you also, had a problem you were facing! As long as that isn't the case it's fine. I thought you were hi-jacking the thread with your own problem which, is not acceptable.
Edit:
I read
The following code does work in all the situations I tested.. So I hope this will help some other people who stumbled on the same problem as I have..
as too mean you were still having a problem.
Last edited by Nightwalker83; Jun 14th, 2013 at 06:08 AM.
Reason: Adding more!
when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
https://get.cryptobrowser.site/30/4111672
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
|