Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Disabling the 'Close' Button

Status
Not open for further replies.

SPRobson

Programmer
Jul 5, 2001
18
0
0
EU
Good Morning/Afternoon

I have a simple VB ActiveX EXE which has the usual Maximise/Minimise/Close control buttons at the top right of my main window. Is it at all possible to disable the Close control button whilst the maximise/minimise buttons are left enabled? (I'm wanting to prevent the user closing the application this way while retaining the maximise/minimise functions)

Many thanks,

Simon
(in a freezing-cold North East of England)
 
Good morning! You can leave the close button on the screen and distinguish in code (very easily, I might add) if the user is closing the form using the close button or a method you have created to close the form. This happens in the form's QueryUnload Event.


Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)

'Only allow form to be unloaded by code in your program
If UnloadMode <> vbFormCode Then
Cancel = 1
End If

End Sub
 
This is a bit of code that was pulled from this forum. I cannot remember who exactly it was. Sorry.

Paste into your form

Code:
Private Sub Form_Load()
    Dim result As Boolean
    result = DisableCloseButton(Me)
End Sub

Paste into a module

Code:
Option Explicit

Private Declare Function GetSystemMenu Lib &quot;user32&quot; _
    (ByVal hwnd As Long, _
     ByVal bRevert As Long) As Long

Private Declare Function RemoveMenu Lib &quot;user32&quot; _
    (ByVal hMenu As Long, _
     ByVal nPosition As Long, _
     ByVal wFlags As Long) As Long
     
Private Const MF_BYPOSITION = &H400&

Public Function DisableCloseButton(frm As Form) As Boolean
'PURPOSE: Removes X button from a form
'EXAMPLE: DisableCloseButton Me
'RETURNS: True if successful, false otherwise
'NOTES:   Also removes Exit Item from
'         Control Box Menu

    Dim lHndSysMenu As Long
    Dim lAns1 As Long, lAns2 As Long
     
    lHndSysMenu = GetSystemMenu(frm.hwnd, 0)

    'remove close button
    lAns1 = RemoveMenu(lHndSysMenu, 6, MF_BYPOSITION)

   'Remove seperator bar
    lAns2 = RemoveMenu(lHndSysMenu, 5, MF_BYPOSITION)
    
    'Return True if both calls were successful
    DisableCloseButton = (lAns1 <> 0 And lAns2 <> 0)
End Function
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top