|
-
Mar 9th, 2005, 02:12 PM
#1
Thread Starter
Addicted Member
how to supress the close button on the form
Since I have my own close button on the form, how to make the vb style close button at the right-upper corner "x" disappear so that it only shows minimize, and maximize.
-
Mar 9th, 2005, 02:28 PM
#2
Re: how to supress the close button on the form
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
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
Private Sub Form_Load()
EnableCloseButton Me.hWnd, False
End Sub
Not only will this disable the close button, but it will also disable the Close menu option of the System menu.
If, at some point, you want to enable those two features, all you have to do is say: EnableCloseButton Me.hWnd, True
-
Mar 9th, 2005, 02:57 PM
#3
PowerPoster
Re: how to supress the close button on the form
Here's a simpler method:
On the form queryunload event
///////////////////////////////////////////////////////////////
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
If UnloadMode <> 1 Then
Cancel = vbTrue
End If
End Sub
//////////////////////////////////////////////////////////////
-
Mar 9th, 2005, 02:58 PM
#4
PowerPoster
Re: how to supress the close button on the form
I guess I need to fully read first. My code just disables the button.
-
Mar 9th, 2005, 08:11 PM
#5
Re: how to supress the close button on the form
redshirtme, while it's OK to disable or bypass the X in control box, most users expect to find it there, so why not just call your close button code from the QueryUnload Event?
-
Mar 10th, 2005, 07:11 AM
#6
Re: how to supress the close button on the form
 Originally Posted by MartinLiss
redshirtme, while it's OK to disable or bypass the X in control box, most users expect to find it there, so why not just call your close button code from the QueryUnload Event?
Martin makes an excellent point. I venture to say if you decide to go the disable the button route, you will get questions about why they can't do that. If you put whatever you need done in, as Martin suggests, the QueryUnload event, this code will execute regardless of how the user closes your application.
Although I keep the code I posted in my Code Library, I don't actually use it anymore. I did once, and got some 'unpleasant' feedback from my customers. From that point on, I started making use of QueryUnload.
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
|