Results 1 to 6 of 6

Thread: Disable The ControlBox - Close Option

  1. #1

    Thread Starter
    Addicted Member Tiovital's Avatar
    Join Date
    Apr 2000
    Posts
    249
    Greetings all.

    I'm trying to disable the option of "Close the window" But leave the "maximaize" and "minimaze"... Can this be done?
    (controlbox disable all 3 option - i need 2 of them enabled)

    Regards


  2. #2
    Hyperactive Member
    Join Date
    Aug 2000
    Posts
    258
    Did you even try using SEARCH ? This was covered many times


    Code:
    '=====code created by Dennis Wrenn=====
    '[email protected]=====
    ' this goes in a module
    
    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
    
    Public Declare Function DrawMenuBar Lib _
    "user32" (ByVal hwnd As Long) As Long
    Public Const MF_REMOVE = &H1000&
    Public Const MF_INSERT = &H0&
    Public Const MF_ENABLED = &H0&
    Public Const MF_BYPOSITION = &H400&
    
    Public Sub DisableX(frm As Form, blnDisabled As Boolean)
    Dim hMenu As Long
    Dim nCount As Long
    
    If blnDisabled = True Then
    hMenu = GetSystemMenu(frm.hwnd, 0)
    nCount = GetMenuItemCount(hMenu)
    Call RemoveMenu(hMenu, nCount - 1, MF_REMOVE Or MF_BYPOSITION)
    Call RemoveMenu(hMenu, nCount - 2, MF_REMOVE Or MF_BYPOSITION)
    DrawMenuBar frm.hwnd
    Else
    hMenu = GetSystemMenu(frm.hwnd, True)
    DrawMenuBar frm.hwnd
    End If
    End Sub
    
    ' this goes in the form_load or a command button or something
    Call DisableX(Me, True)
    []P
    Visual Basic 6 SP4 on win98se

    QUIT THE RAT RACE BECAUSE YOUR MESSING THE WORLD UP !!!!!

  3. #3

    Thread Starter
    Addicted Member Tiovital's Avatar
    Join Date
    Apr 2000
    Posts
    249
    Hi,
    Thanks For the reply - About the ? Yes i did and all i was found it was about ControlBox and not a word about waht i search - i think i should be more specified in my next search.

    Thanks again and regards

  4. #4
    Hyperactive Member dsy5's Avatar
    Join Date
    Jul 2000
    Location
    Lockport, NY
    Posts
    362
    Found this in Microsoft knowledge base some time ago:

    Example

    1.Start a New standard EXE project. Form1 Is added by default.

    2.Add three CommandButtons To Form1.

    3.Add the following code To Form1:
    Code:
    Option Explicit
    
    '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
    
    '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
    
    Private hMenu  As Long
    Private MII    As MENUITEMINFO
    
    Private Sub Command1_Click()
    	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)
    		SetButtons
    	End If
    End Sub
    
    Private Sub Command2_Click()
    	Dim Ret As Long
    
    	If MII.fState = (MII.fState Or MFS_CHECKED) Then
    		MII.fState = MII.fState - MFS_CHECKED
    	Else
    		MII.fState = (MII.fState Or MFS_CHECKED)
    	End If
    
    	MII.fMask = MIIM_STATE
    	Ret = SetMenuItemInfo(hMenu, MII.wID, False, MII)
    	SetButtons
    End Sub
    
    Private Sub Command3_Click()
    	Unload Me
    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
    
    Private Sub SetButtons()
    	If MII.fState = (MII.fState Or MFS_GRAYED) Then
    		Command1.Caption = "Enable"
    	Else
    		Command1.Caption = "Disable"
    	End If
    	If MII.fState = (MII.fState Or MFS_CHECKED) Then
    		Command2.Caption = "Uncheck"
    	Else
    		Command2.Caption = "Check"
    	End If
    End Sub
    
    Private Sub 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)
    	SetButtons
    	Command3.Caption = "Exit"
    End Sub

    4.Press the F5 key To run the program.

    Additional Notes

    Visual Basic handles enabling And disabling the standard Control menu items, including Restore, Size, Move, Minimize,
    And Maximize (the Close menu Is always enabled). Generally, Visual Basic enables/disables the menu items depending On
    the window's state (minimized, maximized, or normal) by referencing these menu items by their menu item IDs.

    As a result, If we merely Add Or Remove MFS_GRAYED from a menu's fState, Visual Basic immediately resets it. The
    workaround Is To change the menu Item's ID (which also renders the item inoperable).

    Using this workaround, we can effectively disable a menu Item that Visual Basic intends To be enabled, but we cannot
    enable a menu Item Visual Basic intends To be disabled (the menu Item must have its regular ID To be operable).

    Enabling a menu Item that Visual Basic intends To be disabled requires removing the Item And replacing it With a menu
    Item of our own (this Is Not covered In this article).

    When the Close menu Item Is enabled Or disabled, the Close box On the title bar should also be normal Or grayed,
    respectively. Sending the message WM_NCACTIVATE (nonclient activate) notifies the window To refresh its title bar so
    the Close box Is updated To appear normal Or grayed.

    Additional Constants

    'Menu item constants
    Const SC_SIZE As Long = &HF000&
    Const SC_SEPARATOR As Long = &HF00F&
    Const SC_MOVE As Long = &HF010&
    Const SC_MINIMIZE As Long = &HF020&
    Const SC_MAXIMIZE As Long = &HF030&
    Const SC_CLOSE As Long = &HF060&
    Const SC_RESTORE As Long = &HF120&

    'SetMenuItemInfo fMask Constants
    Const MIIM_STATE As Long = &H1&
    Const MIIM_ID As Long = &H2&
    Const MIIM_SUBMENU As Long = &H4&
    Const MIIM_CHECKMARKS As Long = &H8&
    Const MIIM_TYPE As Long = &H10&
    Const MIIM_DATA As Long = &H20&



    Related APIs

    SetMenuDefaultItem -- You must disable Close To use this In Visual Basic
    HiliteMenuItem
    Donald Sy - VB (ab)user

  5. #5
    _______ HeSaidJoe's Avatar
    Join Date
    Jun 1999
    Location
    Canada
    Posts
    3,946

    <?>

    Code:
    'disable the close and greying out the x of the form window
    
    Private Declare Function GetSystemMenu _
    Lib "User32" (ByVal hWnd As Integer, _
    ByVal bRevert As Integer) As Integer
    
    Private Declare Function RemoveMenu Lib _
    "User32" (ByVal hMenu As Integer, ByVal nPosition As Integer, _
    ByVal wFlags As Integer) As Integer
    Const MF_BYPOSITION = &H400
    
    Private Sub Form_Load()
        Call RemoveMenu(GetSystemMenu(hWnd, 0), 6, MF_BYPOSITION)
    End Sub
    "A myth is not the succession of individual images,
    but an integerated meaningful entity,
    reflecting a distinct aspect of the real world."

    ___ Adolf Jensen

  6. #6

    Thread Starter
    Addicted Member Tiovital's Avatar
    Join Date
    Apr 2000
    Posts
    249
    Hi,
    Thanks for the Info, It was a great help !!!.

    Regards

Posting Permissions

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



Click Here to Expand Forum to Full Width