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
Printable View
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
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.
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 '******************************************************************************* ' '-------------------------------------------------------------------------------
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.
That looks great! Do you also have the source code for the EnableCloseButton procedure or did you just make that up? :)Quote:
Originally Posted by planethax
But the ControlBox property can't be changed during run-time.Quote:
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)...Quote:
Originally Posted by Jacob Roman
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).
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
thnx planethax....yours code's working nicely.
thnx longwolf....even ur code is nice but i failed to enable it afterwards. can u explain how can i do that.
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.VB Code:
If UnloadMode = 0 Then
Use the VB help files to pick the modes to block.
@Joacim, its originally posted by Hack on VBF but originated over at CodeGuru. I knew I seen that code before. :D
Disable x
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!
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
Quote:
Originally Posted by Harsh Gupta
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
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.
Quote:
Originally Posted by Joacim Andersson
Right that makes sence too me :)
Thanks for the info even though I did not aks for it Joacim Andersson! :)
Cheers,
RyanJ
No you didn't ask for it. I was replying to Harsh Gupta that did ask about it.Quote:
Originally Posted by sciguyryan
oooookkkkk thnx Joacim Andersson
Yes, I know Harsh Gupta did but it was still useful to know :)Quote:
Originally Posted by Joacim Andersson
I meant it in the context that, though I did not ask for it I would still say thanks :D
Cheers,
RyanJ
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
@SuperDre,
There was no need to reply to this topic since it over 8 years old. Please start a new topic.
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
as too mean you were still having a problem. :oQuote:
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..