How do I disable the close window button (the X) in the form's control box?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12

Thread: How do I disable the close window button (the X) in the form's control box?

  1. #1
    Join Date
    Apr 2001
    Location
    Salt Lake City, UT
    Posts
    135

    How do I disable the close window button (the X) in the form's control box?


    Hi All,

    I was wondering if it is possible to only disable the Close Window (X) in the form's control box (in the title bar). I want to leave the resize/maximize buttons, but I don't want the user to be able to just close the form. Is this possible or am I dreaming for a simple way around user errors?
    Any Ideas?


    Thanks~
    ~goddess
    goddess_spanky@hotmail.com
    "There are no stupid questions, but there are a lot of inquisitive idiots."

  2. #2
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    Re: How do I disable the close window button (the X) in the form's control box?

    You can do it in code:
    in the Unload_Form event, you can check for unloadmode
    if it is 1 or 0 (I do not remember, you have to try) it means user has hit the X, otherwise, it means unloaded from code.
    You can then set
    cancel = true
    to stop unload process.
    Hope it helps
    Cesare Imperiali

    Special thanks to Lothar "the Great" Haensler. Come back soon, you Guru.
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

  3. #3
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    Re: How do I disable the close window button (the X) in the form's control box?

    'here it is. Do not forget to put the commandbutton with the unload statement -' or to provide a way to unload...

    Option Explicit

    Private Sub Command1_Click()
    Unload Me
    End Sub

    Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
    If UnloadMode = 0 Then
    Cancel = True
    End If
    End Sub


    Special thanks to Lothar "the Great" Haensler. Come back soon, you Guru.
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

  4. #4
    Join Date
    Apr 2001
    Location
    Salt Lake City, UT
    Posts
    135

    Re: How do I disable the close window button (the X) in the form's control box?

    Thanks for the info, but I'm looking to DISABLE the button. I know how to handle the unload event when it is unwanted, but I would rather just disable the button altogether. Is it possible to just disable the button without turning off the control box?


    Thanks~
    ~goddess
    goddess_spanky@hotmail.com
    "There are no stupid questions, but there are a lot of inquisitive idiots."

  5. #5
    Join Date
    Mar 2000
    Location
    Arizona, USA
    Posts
    493

    Re: How do I disable the close window button (the X) in the form's control box?

    Here is how I do it. There are constants that tell you how VB is trying to be closed.

    private Sub Form_QueryUnload(Cancel as Integer, UnloadMode as Integer)

    on error resume next

    '//If App is Being Requested to Close From X Button Or Task Manager
    '//then Don't Close. If Windows is Trying to Close It
    '//then let It Close.
    If (UnloadMode = vbFormControlMenu) Or (UnloadMode = vbAppTaskManager) then
    '//Unload The App
    Call Form_Unload(Cancel)
    else
    '//Cancel The Close Request And Exit Procedure
    Cancel = true
    Exit Sub
    End If

    End Sub





    Kris
    Software Engineer
    Phoenix,AZ
    Kris
    Software Engineer
    Phoenix, AZ USA

  6. #6
    Join Date
    Aug 1999
    Location
    Bangalore, INDIA
    Posts
    70

    Re: How do I disable the close window button (the X) in the form's control box?

    Hi,
    if you are lokking for the code to remove the X button, here it is.

    Code:
    private Const MF_BYPOSITION = &H400
      private Const MF_REMOVE = &H1000
    
      private Declare Function DrawMenuBar Lib "user32" (byval hwnd as Long) as Long
      private Declare Function GetMenuItemCount Lib "user32" (byval hMenu as Long) as Long
      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
    public Sub RemoveX(frm as Form)
    
    ' ---------------------------------------------------------------
    ' for completeness, you may want to confirm that the
    ' menuItemCount matches the value you expect before performing
    ' the removal. for example, on a normal form with a full
    ' system menu, menuItemCount will return seven.
    '
    ' But what if your application is an MDI app and you want to
    ' disable the close button on the parent? Just pass
    ' MDIForm1.hwnd as the form hwnd parameter in the
    ' GetSystemMenu() call.
    ' ---------------------------------------------------------------
        
    ' ---------------------------------------------------------------
    ' Define local variables
    ' ---------------------------------------------------------------
      Dim hMenu as Long
      Dim menuItemCount as Long
    
    ' ---------------------------------------------------------------
    ' Obtain the handle to the form's system menu
    ' ---------------------------------------------------------------
      hMenu = GetSystemMenu(frm.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(frm.hwnd)
    
       End If
        
    End Sub
    Last edited by Cimperiali; April 4th, 2003 at 12:18 PM.

  7. #7
    Join Date
    Mar 1999
    Location
    Nepal
    Posts
    540

    Re: How do I disable the close window button (the X) in the form's control box?

    Here's the code that you want.

    Code:
    option Explicit
    private Declare Function GetSystemMenu Lib "user32" (byval hWnd as Long, byval bRevert as Long) as Integer
    private Declare Function DeleteMenu Lib "user32" (byval hMenu as Long, byval iditem as Long, byval wFlags as Long) as Integer
    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 Const SC_CLOSE = &HF060
    private Const MF_BYCOMMAND = &H0
    private Const MF_GRAYED = &H1
    
    Sub Form_Load()
    Dim hWnd as Integer, hMenu as Integer, Success as Integer, wFlags as Integer
    Dim nPosition as Integer
    Dim idNewItem as Integer
    Dim s as string
    
        hMenu = GetSystemMenu(me.hWnd, 0)
    
        'to delete the item entirely, use this:
        'Success as long = DeleteMenu(hMenu as long, SC_MOVE, MF_BYCOMMAND)
         
        'to disable the menu item, use this:
          nPosition = SC_CLOSE
          idNewItem = -10
          s = "Close"
           
          wFlags = MF_BYCOMMAND Or MF_GRAYED
          Success = ModifyMenu(hMenu, nPosition, wFlags, idNewItem, s)
        
    End Sub
    Last edited by Cimperiali; April 4th, 2003 at 12:19 PM.

  8. #8
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    Re: How do I disable the close window button (the X) in the form's control box?

    At least I am not the only one to miss the point....:-)
    (but your code seems cleaner then mine!)

    Special thanks to Lothar "the Great" Haensler. Come back soon, you Guru.
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

  9. #9
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    Re: How do I disable the close window button (the X) in the form's control box?

    Whooops...
    Did I tell you you will soon enter in top ten?
    (If I only have another rate to give for today, it would be your)
    Best regards
    Cesare Imperiali

    Special thanks to Lothar "the Great" Haensler. Come back soon, you Guru.
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

  10. #10
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    Re: How do I disable the close window button (the X) in the form's control box?

    Sorry.

    Special thanks to Lothar "the Great" Haensler. Come back soon, you Guru.
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

  11. #11
    Join Date
    Feb 2001
    Location
    PA
    Posts
    163

    Re: How do I disable the close window button (the X) in the form's control box?

    Here's is some code will disable the 'X'. Just call the ChangeMenu sub and if the 'X' is disabled it will enable it and vice versa.
    I disable the 'X' when someone is creating a new record or editing a record then enable it when they're finished.
    Code:
    'Form Load
        Dim Ret as Long
        hMenu = GetSystemMenu(me.hwnd, 0)
        MII.cbSize = len(MII)
        MII.dwTypeData = string(80, 0)
        MII.cch = len(MII.dwTypeData)
        MII.fMask = MIIM_STATE
        MII.wID = SC_CLOSE
        Ret = GetMenuItemInfo(hMenu, MII.wID, false, MII)
         
        Call ChangeMenu
    
          'In general declarations of a form
          'Declarations.
          private Declare Function GetSystemMenu Lib "user32" (byval hwnd as Long, byval bRevert as Long) as Long
          private Declare Function GetMenuItemInfo Lib "user32" Alias "GetMenuItemInfoA" (byval hMenu as Long, byval un as Long, byval b as Boolean, lpMenuItemInfo as MENUITEMINFO) as Long
          private Declare Function SetMenuItemInfo Lib "user32" Alias "SetMenuItemInfoA" (byval hMenu as Long, byval un as Long, byval bool as Boolean, lpcMenuItemInfo as MENUITEMINFO) as Long
          private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (byval hwnd as Long, byval wMsg as Long, byval wParam as Long, lParam as Any) as Long
    
          'Application-specific constants and variables.
          private Const xSC_CLOSE as Long = -10
          private Const SwapID    as Long = 1
          private Const ResetID   as Long = 2
    
          'Menu item constants.
          private Const SC_CLOSE      as Long = &HF060&
    
          'SetMenuItemInfo fMask constants.
          private Const MIIM_STATE    as Long = &H1&
          private Const MIIM_ID       as Long = &H2&
    
          'SetMenuItemInfo fState constants.
          private Const MFS_GRAYED    as Long = &H3&
          private Const MFS_CHECKED   as Long = &H8&
    
          'SendMessage constants.
          private Const WM_NCACTIVATE as Long = &H86
    
          'User-defined Types.
          private Type MENUITEMINFO
              cbSize       as Long
              fMask        as Long
              fType        as Long
              fState       as Long
              wID          as Long
              hSubMenu     as Long
              hbmpChecked  as Long
              hbmpUnchecked as Long
              dwItemData   as Long
              dwTypeData   as string
              cch          as Long
          End Type
    
          private hMenu as Long
          private MII   as MENUITEMINFO
    
    Sub ChangeMenu()
              ' Enable/disable close button( disable during adding, editing, deleteing, creating reports)
              Dim Ret as Long
    
              Ret = SetId(SwapID)
              If Ret <> 0 then
    
                  If MII.fState = (MII.fState Or MFS_GRAYED) then
                      MII.fState = MII.fState - MFS_GRAYED
                  else
                      MII.fState = (MII.fState Or MFS_GRAYED)
                  End If
    
                  MII.fMask = MIIM_STATE
                  Ret = SetMenuItemInfo(hMenu, MII.wID, false, MII)
                  If Ret = 0 then
                      Ret = SetId(ResetID)
                  End If
    
                  Ret = SendMessage(me.hwnd, WM_NCACTIVATE, true, 0)
                   
              End If
    End Sub
    
    private Function SetId(Action as Long) as Long
        Dim MenuID as Long
        Dim Ret as Long
    
        MenuID = MII.wID
        If MII.fState = (MII.fState Or MFS_GRAYED) then
            If Action = SwapID then
                MII.wID = SC_CLOSE
            else
                MII.wID = xSC_CLOSE
            End If
        else
            If Action = SwapID then
                MII.wID = xSC_CLOSE
            else
                MII.wID = SC_CLOSE
            End If
        End If
    
        MII.fMask = MIIM_ID
               
        Ret = SetMenuItemInfo(hMenu, MenuID, false, MII)
               
        If Ret = 0 then
            MII.wID = MenuID
        End If
        SetId = Ret
    End Function
    Last edited by Cimperiali; April 4th, 2003 at 12:20 PM.

  12. #12
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    Re: How do I disable the close window button (the X) in the form's control box?

    Sorry, I am out of vote for today. I will rate your answer on friday, 26

    Special thanks to Lothar "the Great" Haensler. Come back soon, you Guru.
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center