Link to home
Start Free TrialLog in
Avatar of RunBoris
RunBoris

asked on

Disable CLOSE button

I want to disable the close button (the X in the top right corner). Either gray it out, or display a message that says something like "Can not manually close window".

This window has some key functions and a timer that will make it close automatically after a short amount of time, thus it should not be close prematurely.
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

The following will disable the X in the top right of the form:

Option Explicit

' Disable "X" on window
Private Declare Function DrawMenuBar Lib "User32" (ByVal hWnd As _
  Long) As Long
Private Declare Function GetMenuItemCount Lib "User32" (ByVal hMenu _
  As Long) As Long
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 Const MF_BYPOSITION = &H400
Private Const MF_REMOVE = &H1000

Private Sub Form_Load()
    ' Disable the "X" on the form
    Dim hMenu As Long
    Dim lItemCount As Long
    hMenu = GetSystemMenu(Me.hWnd, 0)
    If hMenu Then
        lItemCount = GetMenuItemCount(hMenu)
        Call RemoveMenu(hMenu, lItemCount - 1, MF_REMOVE Or _
        MF_BYPOSITION)
        Call RemoveMenu(hMenu, lItemCount - 2, MF_REMOVE Or _
        MF_BYPOSITION)
        Call DrawMenuBar(Me.hWnd)
    End If
End Sub
ASKER CERTIFIED SOLUTION
Avatar of RogueSolutions
RogueSolutions

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
This is a bit simpler, and uses VB functionality only

Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
    Select Case vbFormControlMenu
        Case vbFormControlMenu
            MsgBox "Command Close Selected"
            Cancel = True
    End Select
End Sub
Avatar of RogueSolutions
RogueSolutions

Actually, if your Form is running on a timer and doesn't need to interact you could just set the Enabled property to False
I like being "brutal" with my windows...

;-)

~IM
Hi,

 I u dont want the close button

just give the border style as none


if u dont want this RogueSolutions is the best

I will be ever more simple.
There is a property for the form called "ControlBox", just set it to false, and voila - the X disappers.
Hello,
The user expects after pressing the "X" button, the from to be closed. Showing the MessageBox or disabling the entire Form causes his expectation not to happen.

After showing a dialog the user must concentrate on the controls like TextBoxes, Option or Command buttons. Hiding the X button causes that the user thinks he can't close the form so he start to find a way for closing the form.

By see the disabled grayed X button (like dialog of MsgBox function) the user understands that he can't close the form manually and must press OK, Cancel or Abort or Retry button.

I think that the solution of Idle_Mind is the best. It is very user-friendly but needs more code.

Thanks
-FA
Avatar of RunBoris

ASKER

Thanks, FA, for your suggestion. However, in my particular instance the accepted answer from RogueSolutions. As stated in my original query:
"This window has some key functions and a timer that will make it close automatically after a short amount of time, thus it should not be close prematurely."

Just to clear up some confusion as to why I need this, here is the basic outline. My application is meant to get my wife off the computer, because she's an addict. It will automatically shutdown the PC and NOT allow access again based on my admin-defined times. This particular form with the disabled X and timer is a "You have 60 seconds to save your game, or else..." form. If the form is closed, the timer is stopped; so the form must stay open. I'm not worried about her using the TaskMan, because she's not that sneaky. And besides, my app keeps a log, so she knows not to try anything sneaky. By the way, if anyone wants a copy of this, just e-mail me. I can also send the source if you have ideas on improvements. runboris*at*carolina*dot*rr*dot*com.
Thanks a lot, I am single and my girl firend works with PC a little.

Thanks
-FA
RunBoris

Thanks for the points.  

When you wife posts a question about some annoying bit of software that keeps turning her PC off, we'll ignore it.
Haha! Luckily she knows nothing of this site. But just in case, keep an eye out for her!