SAMPLE: How To Add New Menu Items to the System MenuID: Q153092
|
Sometimes it is desirable to add a new menu item to a window's System menu.
This can be accomplished by using the AppendMenu API call and a Windows
sub-classing control such as Message Blaster(tm). This article explains how
to implement this action and provides a code sample.
In order to use the Message Blaster control, you can download the sample,
free of charge, from the Microsoft Software Library.
The following file is available for download from the Microsoft Software
Library:
~ Msgblast.exe
For more information about downloading files from the Microsoft Software
Library, please see the following article in the Microsoft Knowledge Base:
Q119591 : How to Obtain Microsoft Support Files from Online Services
Option Explicit
Private Declare Function RemoveMenu% Lib "User" (ByVal hMenu%, ByVal _
nPosition%, ByVal wFlags%)
Private Declare Function GetSystemMenu Lib "User" (ByVal hWnd%, ByVal _
revert%) As Integer
Private Declare Function GetWindowLong Lib "User" (ByVal hWnd As _
Integer,ByVal nIndex As Integer) As Long
Private Declare Function SetWindowLong Lib "User" (ByVal hWnd As _
Integer, ByVal nIndex As Integer, ByVal dwNewLong As Long) As Long
Private Declare Function AppendMenu Lib "User" (ByVal hMenu As _
Integer, ByVal wFlags As Integer, ByVal wIDNewItem As Integer, _
ByVal lpNewItem As Any) As Integer
Private Const MF_STRING = &H0
Private Const MF_BYCOMMAND = &H0
Private Const WM_SYSCOMMAND = &H112
'Enter the line below as one line of code
Private Sub RemoveControlMenuItemByPosition(frm As Form, iPosition As
Integer)
Dim iHSysMenu As Integer
Dim iReturn As Integer
Dim lDW As Long
Dim lRR As Long
iHSysMenu = GetSystemMenu(frm.hWnd, 0)
iReturn = RemoveMenu(iHSysMenu, iPosition, MF_BYPOSITION)
lDW = GetWindowLong(frm.hWnd, -16) 'Window style
lDW = lDW And &HFFFEFFFF 'Turn off bits for Maximize arrow button
lRR = SetWindowLong(frm.hWnd, -16, lDW)
End Sub
Private Sub RemoveControlMenuItemByCommand(frm As Form, lCommand As _
Long)
Dim iHSysMenu As Integer
Dim iReturn As Integer
Dim lDW As Long
Dim lRR As Long
iHSysMenu = GetSystemMenu(frm.hWnd, 0)
iReturn = RemoveMenu(iHSysMenu, lCommand, MF_BYCOMMAND)
lDW = GetWindowLong(frm.hWnd, -16) 'Window style
lDW = lDW And &HFFFEFFFF 'Turn off bits for Maximize arrow button
lRR = SetWindowLong(frm.hWnd, -16, lDW)
End Sub
Private Sub cmdRemoveMenuItems_Click()
Call RemoveControlMenuItemByPosition(Me, 0)
End Sub
Private Sub Form_Load()
MsgBlaster1.hWndTarget = Me.hWnd
MsgBlaster1.MsgList(0) = WM_SYSCOMMAND
End Sub
'Enter the line below as one line of code
Private Sub MsgBlaster1_Message(MsgVal As Integer, wParam As Integer,
lParam As Long, ReturnVal As Long)
Select Case wParam
Case 1
MsgBox "You clicked to About"
Case 2
MsgBox "You clicked on Help"
Case 3
'This option removes the custom added menu items and the separator.
Dim iHSysMenu As Integer
Dim iReturn As Integer
iHSysMenu = GetSystemMenu(Me.hWnd, 0)
Call RemoveControlMenuItemByCommand(Me, 1)
Call RemoveControlMenuItemByCommand(Me, 2)
Call RemoveControlMenuItemByCommand(Me, 3)
End Select
End Sub
Private Sub Command1_Click()
Dim iReturn As Integer
Dim iHSysMenu As Integer
iHSysMenu = GetSystemMenu(Me.hWnd, 0)
iReturn = AppendMenu(iHSysMenu, MF_STRING, 1, "About...")
iReturn = AppendMenu(iHSysMenu, MF_STRING, 2, "Help...")
iReturn = AppendMenu(iHSysMenu, MF_STRING, 3, "Remove items...")
End Sub
For additional information, please see the following articles in the
Microsoft Knowledge Base:
Q110498 : How to Add Items into Control Menu Box of Visual Basic Form
Q110104 : Using MSGBLAST.VBX Control to Process Windows Messages from VB
Keywords : kbsample kb16bitonly kbVBp400 VB4WIN
Version : 4.00 | 4.00
Platform : NT WINDOWS
Issue type :
Last Reviewed: June 18, 1999