Link to home
Start Free TrialLog in
Avatar of donb1
donb1

asked on

Exit Button Disable?

I know how to disabel the maximize and minimize symbols in the upper right corner of a form.  How can I disable the "X" exit symbol?
Avatar of mcrider
mcrider

You can disable it unless you show a form without a titlebar.  What you can do is ignore it...

In the QueryUnload event, do this:

Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
    If UnloadMode = vbFormControlMenu Then Cancel = True
End Sub


The X will now be ignored and the form will stay loaded and visible...


Cheers!
To get rid of the Min and Max buttons, set both the MaxButton and MinButton properties of the form False in design time.
Here is code to disable and grey out the [X] button by removing the Close menu item.  Be sure you have code to end your program if you disable the Close button!

Option Explicit
Private Const MF_BYPOSITION = &H400&
Private Declare Function GetSystemMenu Lib "user32" _
    (ByVal hwnd As Long, ByVal bRevert As Long) As Long
Private Declare Function RemoveMenu Lib "user32" _
    (ByVal hMenu As Long, ByVal nPosition As Long, _
    ByVal wFlags As Long) As Long

Private Sub Form_Load()
    Dim hSysMenu As Long
    hSysMenu = GetSystemMenu(Me.hwnd, 0)
    'Remove the Close menu item
    Call RemoveMenu(hSysMenu, 6, MF_BYPOSITION)
    'Remove the Separator bar (optional)
    Call RemoveMenu(hSysMenu, 5, MF_BYPOSITION)
End Sub
Of course, you could simply set the ControlBox property false.  This will remove the min, max, close, and icon from the titlebar, leaving only the caption.
donb1,

Go ahead and reject my answer and take Erick37's The API's work SWEET!

Cheers!
ASKER CERTIFIED SOLUTION
Avatar of Erick37
Erick37
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial