VB and VBA Users Source Code: Removing dialog system menus (inc. the terminate or X menu)
[
Home
|
Contents
|
Search
|
Reply
| Previous | Next ]
VB/VBA Source Code
Removing dialog system menus (inc. the terminate or X menu)
By:
Andrew Baker
Email (spam proof):
Email the originator of this post
Date:
Thursday, July 06, 2000
Hits:
943
Category:
Windows API
Article:
The following routines demonstrate how to enable/disable the system control menus (inc. the terminate button or X button, maximise, minimise and resizing grip) for a dialog/userform: Option Explicit Private Declare Function GetSystemMenu Lib "user32" (ByVal hwnd As Long, ByVal bRevert As Long) As Long Private Declare Function DeleteMenu Lib "user32" (ByVal hMenu As Long, ByVal nPosition As Long, ByVal wFlags As Long) As Long Private Declare Function FindWindowA Lib "user32" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long Private Declare Function DrawMenuBar Lib "user32" (ByVal hwnd As Long) As Long 'Purpose : Deletes all the menu items on a dialog. 'Inputs : [sDialogCaption] The caption of the dialog to delete the system menus from. ' [lHandle] If specified operates on this window, else finds the handle from ' the dialog caption. 'Outputs : Returns True on success. 'Author : Andrew Baker 'Date : 30/05/2000 'Notes : Must supply either a caption or a window handle. ' eg. to call in VB use ' DialogDeleteSystemMenus , Form1.Hwnd ' to call in VBA use ' DialogDeleteSystemMenus UserForm1.Caption 'Revisions : Added code to refresh menu bar. Public Function DialogDeleteSystemMenus(Optional sDialogCaption As String, Optional lHandle As Long) As Boolean Dim lhwndMenu As Long 'Menu constants Const MF_BYCOMMAND = &H0&, SC_ARRANGE = &HF110 Const SC_CLOSE = &HF060, SC_HOTKEY = &HF150 Const SC_HSCROLL = &HF080, SC_KEYMENU = &HF100 Const SC_MAXIMIZE = &HF030, SC_MINIMIZE = &HF020 Const SC_MOVE = &HF010, SC_NEXTWINDOW = &HF040 Const SC_PREVWINDOW = &HF050, SC_RESTORE = &HF120 Const SC_SIZE = &HF000, SC_VSCROLL = &HF070 Const SC_TASKLIST = &HF130, MF_BYPOSITION = &H400& On Error GoTo ErrFailed If lHandle = 0 Then 'Get window handle lHandle = DialogHwnd(sDialogCaption) End If If lHandle <> 0 Then 'Get the window handle for the system menu lhwndMenu = GetSystemMenu(lHandle, False) 'Delete Maximise menu item DeleteMenu lhwndMenu, SC_MAXIMIZE, MF_BYCOMMAND 'Delete Minimse menu item DeleteMenu lhwndMenu, SC_MINIMIZE, MF_BYCOMMAND 'Delete the size menu item (stops the form being resized) DeleteMenu lhwndMenu, SC_SIZE, MF_BYCOMMAND 'Delete the size menu item (stops the form being moved) DeleteMenu lhwndMenu, SC_MOVE, MF_BYCOMMAND 'Delete the restore menu item (the right click menu on the task bar) DeleteMenu lhwndMenu, SC_RESTORE, MF_BYCOMMAND 'Delete the next window menu item DeleteMenu lhwndMenu, SC_NEXTWINDOW, MF_BYCOMMAND 'Delete the next close menu item (the X) DeleteMenu lhwndMenu, SC_CLOSE, MF_BYCOMMAND 'Delete the top level system menu DeleteMenu lhwndMenu, 0, MF_BYPOSITION 'Redraw the menu Call DrawMenuBar(lHandle) DialogDeleteSystemMenus = True End If Exit Function ErrFailed: 'Error occurred DialogDeleteSystemMenus = False On Error GoTo 0 End Function 'Purpose : Deletes the terminate button (or X button) on a dialog. 'Inputs : [sDialogCaption] The caption of the dialog whose X (terminate) menu you want to disable. ' [lHandle] If specified operates on this window, else finds the handle from ' the dialog caption. 'Outputs : Returns True on success. 'Author : Andrew Baker 'Date : 30/05/2000 'Notes : Must supply either a caption or a window handle. ' eg. to call in VB use ' DialogDisableX , Form1.Hwnd ' to call in VBA use ' DialogDisableX UserForm1.Caption 'Revisions : Added code to refresh menu bar. Public Function DialogDisableX(Optional sDialogCaption As String, Optional lHandle As Long) As Boolean Const clXIndex As Long = 6 'The index of the X menu item Const MF_BYPOSITION = &H400& 'Deletes the menus by their index/position. If lHandle = 0 Then 'Get window handle lHandle = DialogHwnd(sDialogCaption) End If If lHandle <> 0 Then 'Have the window handle for the dialog DialogDisableX = DeleteMenu(GetSystemMenu(lHandle, False), clXIndex, MF_BYPOSITION) 'Redraw the menu Call DrawMenuBar(lHandle) End If End Function 'Purpose : Restores the system control menu (eg the terminate menu) of the specified dialog. 'Inputs : sDialogCaption The caption of the dialog whose control menu you want to restore. ' [lHandle] If specified operates on this window, else finds the handle from ' the dialog caption. 'Outputs : Returns True on success 'Author : Andrew Baker 'Date : 30/05/2000 'Notes : Must supply either a caption or a window handle. ' eg. to call in VB use ' DialogRestoreMenu , Form1.Hwnd ' to call in VBA use ' DialogRestoreMenu UserForm1.Caption 'Revisions : Added code to refresh menu bar. Public Function DialogRestoreMenu(Optional sDialogCaption As String, Optional lHandle As Long) As Boolean If lHandle = 0 Then 'Get window handle from dialog caption lHandle = DialogHwnd(sDialogCaption) End If If lHandle Then 'Passing True to the bRevert argument of the GetSystemMenu API restores 'the control menu of the specified window. DialogRestoreMenu = GetSystemMenu(lHandle, True) 'Redraw the menu Call DrawMenuBar(lHandle) End If End Function 'Purpose : Returns the Windows Handle of a Dialog based on its caption (and class name). 'Inputs : sDialogCaption as String The Caption of the dialog to find the handle. ' [sClassName] The Class name of the dialog to find the handle of. 'Outputs : The Dialogs Window Handle 'Author : Andrew Baker 'Date : 30/05/2000 'Notes : To Call in VBA use ' lHwnd = DialogHwnd(Me.Caption) 'Revisions : Function DialogHwnd(ByVal sDialogCaption As String, Optional sClassName As String = vbNullString) As Long On Error Resume Next DialogHwnd = FindWindowA(sClassName, sDialogCaption) On Error GoTo 0 End Function 'Demonstration code 'Removes the X button on a form '(place code inside form) Sub Test() 'Kill the X button DialogDisableX Me.Caption MsgBox "Dialog X disabled..." 'Restore the dialog menu DialogRestoreMenu Me.Caption MsgBox "Dialog X enabled..." End Sub 'Demonstration code. 'Removes the X button from Excel's menu bar Sub KillExcelX() DialogDisableX Application.Caption End Sub 'Purpose : Deletes a single system menu item from a dialog. 'Inputs : MenuItem The menu item to delete ' [sDialogCaption] The caption of the dialog to delete the menu item from. ' [lHandle] If specified operates on this window, else finds the handle from ' sDialogCaption (the dialog caption). 'Outputs : Returns True on success. 'Author : Andrew Baker 'Date : 30/05/2000 'Notes : Must supply either a caption or a window handle. ' eg. to call in VB use ' DialogDeleteSystemMenu SC_CLOSE, ,Form1.Hwnd ' to call in VBA use ' DialogDeleteSystemMenu SC_CLOSE, UserForm1.Caption 'Revisions : Added code to refresh menu bar. Public Function DialogDeleteSystemMenu(MenuItem As eMenuItem, Optional sDialogCaption As String, Optional lHandle As Long) As Boolean Dim lhwndMenu As Long 'Menu constants Const MF_BYCOMMAND = &H0&, MF_BYPOSITION = &H400& On Error GoTo ErrFailed If lHandle = 0 Then 'Get window handle lHandle = DialogHwnd(sDialogCaption) End If If lHandle <> 0 Then 'Get the window handle for the system menu lhwndMenu = GetSystemMenu(lHandle, False) 'Delete menu item DeleteMenu lhwndMenu, MenuItem, MF_BYCOMMAND 'Redraw the menu Call DrawMenuBar(lHandle) DialogDeleteSystemMenu = True End If Exit Function ErrFailed: 'Error occurred DialogDeleteSystemMenu = False On Error GoTo 0 End Function 'Purpose : Refreshs a forms system menus (useful for menus which have been created using API calls) 'Inputs : lFormHandle The window handle of the form to refresh (eg Me.hwnd) 'Outputs : Returns True on success. 'Author : Andrew Baker 'Date : 30/05/2000 'Notes : For MDI child forms that whose system menus have been created, call this on the child ' then the parent form when you maximise the child. Public Function RefreshSystemMenu(lFormHandle As Long) As Boolean On Error GoTo ErrFailed If lHandle <> 0 Then 'Redraw the menu Call DrawMenuBar(lHandle) RefreshSystemMenu = True End If Exit Function ErrFailed: 'Error occurred RefreshSystemMenu = False On Error GoTo 0 End Function
Terms and Conditions
Support this site
Download a trial version of the Excel Workbook Rebuilder