Link to home
Start Free TrialLog in
Avatar of tayleeming
tayleeming

asked on

Disable the Query Unload event

how can i disable a form's cross button at the upper right of a VB form/MDI Form so that user cannot use this button to close the form?
Avatar of marconovaro
marconovaro


Copy this code:

Private Const MF_BYCOMMAND As Long = &H0
Private Const SWP_DRAWFRAME As Long = &H20
Private Const SWP_NOMOVE As Long = &H2
Private Const SWP_NOSIZE As Long = &H1
Private Const SWP_NOZORDER As Long = &H4
Private Const SWP_FLAGS = SWP_NOZORDER Or SWP_NOSIZE Or _
                         SWP_NOMOVE Or SWP_DRAWFRAME

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 Declare Function DrawMenuBar Lib "user32" _
   (ByVal hwnd As Long) As Long

Private Declare Function SetWindowPos Lib "user32" _
    (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, _
     ByVal x As Long, ByVal Y As Long, _
     ByVal cx As Long, ByVal cy As Long, _
     ByVal wFlags As Long) As Long


Private Sub RemoveMenuByCmd(m_hWnd as Long, itemId As Long)
    Dim hMenu As Long

    'get the system menu handle
    hMenu = GetSystemMenu(m_hWnd, 0)

    RemoveMenu hMenu, itemId, MF_BYCOMMAND

    'force a redraw of the non-client
    'area of the form to cause the
    'disabled X to paint correctly
    Call SetWindowPos(m_hWnd, 0, 0, 0, 0, 0, SWP_FLAGS)
    Call DrawMenuBar(m_hWnd)

End Sub

And use it like this:

private const SYS_CLOSE = &HF060&


(inside Form_Load)
RemoveMenuByCmd( me.hwnd, SYS_CLOSE )


Hope this helps.
M



For more control on system menu, have a look to my code here:

http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=36243&lngWId=1

M
Hi,

there is a pretty much easier way:

Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
   If (UnloadMode = vbFormControlMenu) Then
      Call MsgBox("I don't want you to close me in that way!")
      Cancel = True
   endif
End Sub

If you want it in silent mode, just leave the MsgBox(... away.

Greetings

Andy
ASKER CERTIFIED SOLUTION
Avatar of TimCottee
TimCottee
Flag of United Kingdom of Great Britain and Northern Ireland 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
Or simply setting
ControlBox property of the form to false in design time
hi,
 Wht Mr.fluglash  said is absolutely correct and simplest answer.
@fluglash

but there is one problem, if you put ControlBox to False, you disable the minimize, maximize, a.s.o. If that is what tayleeming wants to have, you've posted the simplest way (much simpler than my simple way mentioned above)

Greeting Andy
@fluglash

but there is one problem, if you put ControlBox to False, you disable the minimize, maximize, a.s.o. If that is what tayleeming wants to have, you've posted the simplest way (much simpler than my simple way mentioned above)

Greeting Andy
Avatar of tayleeming

ASKER

hi,
  thanks for everyone help, all of the answer are great but TimCottee's answer also provide some concept as well..

Thanks a lot