Microsoft KB Archive/814562

From BetaArchive Wiki
Knowledge Base


How to programmatically delete the Window Control menu commands in Excel 2002

Article ID: 814562

Article Last Modified on 2/1/2007



APPLIES TO

  • Microsoft Excel 2002 Standard Edition



For a Microsoft Excel 2000 version of this article, see 213502.


For a Microsoft Excel 97 version of this article, see 107689.

SUMMARY

This article describes how to programmatically delete the commands on the Window Control menu (right-click the Excel title bar) and make the window control buttons in the upper-right corner of the Excel window unavailable.

MORE INFORMATION

Microsoft provides programming examples for illustration only, without warranty either expressed or implied, including, but not limited to, the implied warranties of merchantability and/or fitness for a particular purpose. This article assumes that you are familiar with the programming language being demonstrated and the tools used to create and debug procedures. Microsoft support professionals can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific needs. If you have limited programming experience, you may want to contact a Microsoft Certified Partner or the Microsoft fee-based consulting line at (800) 936-5200. For more information about Microsoft Certified Partners, see the following Microsoft Web site:

For additional information about the support options available from Microsoft, visit the following Microsoft Web site:



Note In Microsoft Excel 2002, you do not need the FindWindow or GetActiveWindow API statements as in earlier versions of Excel because the Application object has a new Hwnd property.

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

Declare Function DeleteMenu Lib "user32" (ByVal hMenu As Integer, _
   ByVal nPosition As Integer, ByVal wFlags As Integer) As Integer
   
Private Declare Function SetWindowLong Lib "user32" Alias _
   "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, _
      ByVal dwNewLong As Long) As Long

Private Declare Function GetWindowLong Lib "user32" Alias _
   "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) _
      As Long

Const WS_MINIMIZEBOX = &H20000
Const WS_MAXIMIZEBOX = &H10000
Const GWL_STYLE = (-16)

'The following procedure makes the window control menu unavailable.

Sub Disable_Control()
   
   Dim X As Integer
   
   For X = 1 To 9
      
      'Delete the first menu command and loop until
      'all commands are deleted.
      Call DeleteMenu(GetSystemMenu(Application.hwnd, False), 0, 1024)
   
   Next X

End Sub

'The following procedure restores the control menu.
'Note that to run this procedure, the Declare statements above
'must be in the module.

Sub RestoreSystemMenu()
   
   'restore system menu to original state
   hMenu% = GetSystemMenu(Application.hwnd, 1)

End Sub

'The following procedure makes the Minimize and Maximize commands
'on the window control menu, and the Minimize and Maximize window
'control buttons in the upper-right corner of the Excel window unavailable.

Sub HideMinimizeAndMaximizeButtons()
 
Dim L As Long

   L = GetWindowLong(Application.hwnd, GWL_STYLE)
   L = L And Not (WS_MINIMIZEBOX)
   L = L And Not (WS_MAXIMIZEBOX)
   L = SetWindowLong(Application.hwnd, GWL_STYLE, L)

End Sub

'The following procedure restores the Minimize and Maximize commands on the
'control menu, and the Minimize and Maximize window control buttons
'in the upper-right corner of the Excel window.

Sub RestoreMinimizeAndMaximizeButtons()

   Dim L As Long
   L = GetWindowLong(Application.hwnd, GWL_STYLE)
   L = SetWindowLong(Application.hwnd, GWL_STYLE, WS_MINIMIZEBOX _
      Or WS_MAXIMIZEBOX Or L)

End Sub

The commands on the control menu are numbered, starting at zero. The default control menu items are as follows: Restore is item 0, Move is item 1, Size is item 2, Minimize is item 3, Maximize is item 4, and Close is item . Even if items are deleted, the first item always starts at 0 (zero).

To delete individual items from the control menu without deleting the whole menu, you can specify the menu command to delete. For example, the following two lines of code, when used instead of the For ... Next loop in the Disable_Control macro, delete the Maximize (item 4) and Minimize (item 3) commands and also disable the Maximize and Minimize buttons:

Call DeleteMenu(GetSystemMenu(Application.Hwnd, False), 4, 1024)
Call DeleteMenu(GetSystemMenu(Application.Hwnd, False), 3, 1024)


Additional query words: call register remove api XL2002

Keywords: kbinfo KB814562