Page 181 - DCAP408_WEB_PROGRAMMING
P. 181
Unit 10: Menus
10.6.2 The System Menu Notes
This example adds an About... item to the forms System Menu (shown by clicking the forms
icon, or right clicking on its button on the taskbar)
!
Caution This uses subclassing, so DO NOT use the Stop button on the VB toolbar, or
attempt to debug the WindowProc procedure (unless you like VB crashing!).
First, add the following code to a form
‘// form_load event. Catch all those messages!
Private Sub Form_Load()
Dim lhSysMenu As Long, lRet As Long
On Error Resume Next
‘// add about menu
lhSysMenu = GetSystemMenu(hWnd, 0&)
lRet = AppendMenu(lhSysMenu, MF_SEPARATOR, 0&, vbNullString)
lRet = AppendMenu(lhSysMenu, MF_STRING, IDM_ABOUT, “About...”)
Show
‘// saves the previous window message handler. Always restore this value
‘// Address Of command sends the address of the WindowProc procedure
‘// to windows
ProcOld = SetWindowLong(hWnd, GWL_WNDPROC, Address Of WindowProc)
End Sub
’// form_queryunload event. Return control to windows/vb
Private Sub Form_Unload(Cancel As Integer)
‘// give message processing control back to VB
‘// if you don’t do this you WILL crash!!!
Call SetWindowLong(hWnd, GWL_WNDPROC, ProcOld)
End Sub
Then, add the code below to a module
’// variable that stores the previous message handler
Public ProcOld As Long
’// Windows API Call for catching messages
Public Declare Function SetWindowLong Lib “user32” Alias “SetWindowLongA”
(ByVal hWnd
As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
’// Windows API call for calling window procedures
Public Declare Function CallWindowProc Lib “user32” Alias “CallWindowProcA”
(ByVal lpPrevWndFunc As Long, ByVal hWnd As Long, ByVal Msg As Long, ByVal
wParam As Long,
LOVELY PROFESSIONAL UNIVERSITY 175