Link to home
Start Free TrialLog in
Avatar of CraigLazar
CraigLazar

asked on

Need to get rid of X on Screen

Hi
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

thanx

Craig
ASKER CERTIFIED SOLUTION
Avatar of mcrider
mcrider

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
Avatar of caraf_g
caraf_g

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

If you don't want that,

(thanks to Dalin)

Try


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, _
    MF_BYCOMMAND Or MF_GRAYED, _
    -10, _
    "Close")
End Sub

Thanks chengman, it's always good to get improvements on one's code.
Avatar of CraigLazar

ASKER

Thanx allot for the help from everyone
MCrider had it perfect

thanx again

Craig
Thanks for the points! Glad I could help!


Cheers!