In this article you will learn how to modify system menu (control menu). You will also learn how to handle event of newly added menu item.
To modify system menu first you have to call GetSystemMenu api to get handle of system menu and then you can all other menu APIs (i.e. GetMenuItemCount, AppendMenu, RemoveMenu, ModifyMenu etc.)
Step-By-Step Example
- Create a standard exe project - Add a module to the project - Place 3 commandbuttons on the form1 - Add the following code to form1 code window
Form1.frm |
Click here to copy the following block | Private Sub Command1_Click() lhSysMenu = GetSystemMenu(hwnd, 0&) lRet = AppendMenu(lhSysMenu, MF_SEPARATOR, 0&, vbNullString) lRet = AppendMenu(lhSysMenu, MF_STRING, IDM_ABOUT, "About...") End Sub
Private Sub Command2_Click() RemoveSystemMenu Me.hwnd End Sub
Sub RemoveSystemMenu(hwnd As Long) On Error Resume Next Dim hMenu As Long Dim itemCount As Long
hMenu = GetSystemMenu(hwnd, 0)
RemoveMenu hMenu, SC_NEXTWINDOW, MF_BYCOMMAND RemoveMenu hMenu, SC_MOVE, MF_BYCOMMAND RemoveMenu hMenu, SC_RESTORE, MF_BYCOMMAND RemoveMenu hMenu, SC_CLOSE, MF_BYCOMMAND RemoveMenu hMenu, SC_SEPARATOR, MF_BYCOMMAND RemoveMenu hMenu, SC_MAXIMIZE, MF_BYCOMMAND RemoveMenu hMenu, SC_MINIMIZE, MF_BYCOMMAND RemoveMenu hMenu, SC_SIZE, MF_BYCOMMAND End Sub
Private Sub Command3_Click() Unload Me End Sub
Private Sub Form_Load() Dim lhSysMenu As Long, lRet As Long On Error Resume Next
ProcOld = SetWindowLong(hwnd, GWL_WNDPROC, AddressOf WindowProc) End Sub
Private Sub Form_Unload(Cancel As Integer) Call SetWindowLong(hwnd, GWL_WNDPROC, ProcOld) End Sub |
- Add the following code to module1
Module1.bas |
Click here to copy the following block | Public ProcOld As Long
Public Declare Function GetMenuItemCount Lib "user32" ( _ ByVal hMenu As Long) As Long
Public Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" ( _ ByVal hwnd As Long, _ ByVal nIndex As Long, _ ByVal dwNewLong As Long) As Long
Public Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" ( _ ByVal hwnd As Long, _ ByVal nIndex As Long) As Long
Public Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" ( _ ByVal lpPrevWndFunc As Long, _ ByVal hwnd As Long, _ ByVal Msg As Long, _ ByVal wParam As Long, _ ByVal lParam As Long) As Long
Public Declare Function AppendMenu Lib "user32" Alias "AppendMenuA" ( _ ByVal hMenu As Long, _ ByVal wFlags As Long, _ ByVal wIDNewItem As Long, _ ByVal lpNewItem As String) As Long
Public Declare Function GetSystemMenu Lib "user32" ( _ ByVal hwnd As Long, _ ByVal bRevert As Long) As Long
Public Declare Function RemoveMenu Lib "user32.dll" ( _ ByVal hMenu As Long, _ ByVal nPosition As Long, _ ByVal wFlags As Long) As Long
Public Declare Function DeleteMenu Lib "user32.dll" ( _ ByVal hMenu As Long, _ ByVal nPosition As Long, _ ByVal wFlags As Long) As Long
Public Const WM_SYSCOMMAND = &H112 Public Const GWL_WNDPROC = (-4) Public Const IDM_ABOUT As Long = 1010
Public Const WS_MAXIMIZE = &H1000000 Public Const WS_MAXIMIZEBOX = &H10000 Public Const WS_MINIMIZE = &H20000000 Public Const WS_MINIMIZEBOX = &H20000
Public Const MF_SEPARATOR = &H800& Public Const MF_STRING = &H0&
Public Const MF_BYCOMMAND = &H0& Public Const MF_BYPOSITION = &H400&
Public Const MF_ENABLED = &H0 Public Const MF_GRAYED = &H1 Public Const MF_DISABLED = &H2
Public Const SC_ARRANGE = &HF110 Public Const SC_CLOSE = &HF060 Public Const SC_HOTKEY = &HF150 Public Const SC_HSCROLL = &HF080 Public Const SC_KEYMENU = &HF100 Public Const SC_MAXIMIZE = &HF030 Public Const SC_MINIMIZE = &HF020 Public Const SC_MOVE = &HF010 Public Const SC_NEXTWINDOW = &HF040 Public Const SC_PREVWINDOW = &HF050 Public Const SC_RESTORE = &HF120 Public Const SC_SIZE = &HF000
Public Function WindowProc(ByVal hwnd As Long, ByVal iMsg As Long, _ ByVal wParam As Long, ByVal lParam As Long) As Long
Select Case iMsg Case WM_SYSCOMMAND If wParam = IDM_ABOUT Then MsgBox "VB Web Append to System Menu Example", vbInformation, "About" Exit Function End If End Select WindowProc = CallWindowProc(ProcOld, hwnd, iMsg, wParam, lParam) End Function |
|