• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 193
  • Last Modified:

No x code

Can anyone tell me the VB5 code the will no remove or disable the close (x) button on the top left of any window.
The thing is I want to do this without removeing or disabling the minimize button.

So I need the minimize button, but want rid of the close button.

Peat, Thanks.
1 Solution
following code is from VBNet:
Add the following code to a BAS module:

Option Explicit

Public Const MF_BYPOSITION = &H400
Public Const MF_REMOVE = &H1000

Public Declare Function DrawMenuBar Lib "user32" _
      (ByVal hwnd As Long) As Long
Public Declare Function GetMenuItemCount Lib "user32" _
      (ByVal hMenu As Long) As Long
Public Declare Function GetSystemMenu Lib "user32" _
      (ByVal hwnd As Long, _
       ByVal bRevert As Long) As Long
Public Declare Function RemoveMenu Lib "user32" _
      (ByVal hMenu As Long, _
       ByVal nPosition As Long, _
       ByVal wFlags As Long) As Long'--end block--'

 Form Code  
To test, add the following into a command button. Shown here is the code called from the form's Load sub:

Option Explicit

Private Sub Form_Load()

   Dim hMenu As Long
   Dim menuItemCount As Long

  'Obtain the handle to the form's system menu
   hMenu = GetSystemMenu(Me.hwnd, 0)
   If hMenu Then
     'Obtain the number of items in the menu
      menuItemCount = GetMenuItemCount(hMenu)
     'Remove the system menu Close menu item.
     'The menu item is 0-based, so the last
     'item on the menu is menuItemCount - 1
      Call RemoveMenu(hMenu, menuItemCount - 1, _
                      MF_REMOVE Or MF_BYPOSITION)
     'Remove the system menu separator line
      Call RemoveMenu(hMenu, menuItemCount - 2, _
                      MF_REMOVE Or MF_BYPOSITION)
     'Force a redraw of the menu. This
     'refreshes the titlebar, dimming the X
      Call DrawMenuBar(Me.hwnd)

   End If
End Sub

'--end block--'
Below is code to disable unload using the x.  You can still unload using an exit key if you set bExit = true before unload me.  This works if the window is a VB form.


Dim bExit As Boolean

Private Sub Form_Unload(Cancel As Integer)

    If bExit = False Then
        Cancel = True
    End If
End Sub

Private Sub Command1_Click()
    bExit = True
    Unload Me
End Sub

peat042998Author Commented:
Thanks, that was bril!!!
Bought This Question.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now