Need to get rid of X on Screen

Is there a way i can hide the X on the top left hand corner of my form. I found in the form properties to hide minimise and maximise but i also need to get rid of the close mark X


Who is Participating?
mcriderConnect With a Mentor Commented:
Set the ControlBox property to FALSE.

.only thing is that this gets rid of your icon too.

If you don't want that,

(thanks to Dalin)


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
There are a few problems with Caraf Q's code.
It doesn't work with MDI Forms and there are it messes up other menu items.
(For example Move doesn't work)

This code works for me.

Private Declare Function GetSystemMenu _
  Lib "user32" _
  (ByVal hwnd As Long, _
   ByVal bRevert As Long) _
  As Long

Private Declare Function ModifyMenu _
  Lib "user32" _
  Alias "ModifyMenuA" _
  (ByVal hMenu As Long, _
   ByVal nPosition As Long, _
   ByVal wFlags As Long, _
   ByVal wIDNewItem As Long, _
   ByVal lpString As Any) _
  As Long

Private Declare Function GetMenuItemID _
  Lib "user32" _
  (ByVal hMenu As Long, _
   ByVal nPos As Long) _
  As Long

Private Const MF_BYCOMMAND = &H0&
Private Const MF_BYPOSITION = &H400&
Private Const MF_GRAYED = &H1&

Public Sub DisableCloseMenu(frmIn As Form)

  Dim lngResult As Long
  Dim lnghMenu As Long
  Dim lnghItem As Long

  ' get handle to form's system menu
  lnghMenu = GetSystemMenu(frmIn.hwnd, 0)
  ' get handle to the 6th item (Close)
  lnghItem = GetMenuItemID(lnghMenu, 6)
  ' gray out this item
  lngResult = ModifyMenu( _
    lnghMenu, _
    lnghItem, _
    -10, _
End Sub

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Thanks chengman, it's always good to get improvements on one's code.
CraigLazarAuthor Commented:
Thanx allot for the help from everyone
MCrider had it perfect

thanx again

Thanks for the points! Glad I could help!

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.

All Courses

From novice to tech pro — start learning today.