在系统菜单上添加自定义菜单项
---- 首先需要知道一点是系统菜单的工作过程。当我们单击系统菜单中某一项时,应用程序窗口会收到一条WM_SYSCOMMAND消息,该消息包含了系统菜单中所单击那一项的标识符ID。此时,应用程序窗口的默认窗口函数会根据WM_SYSCOMMAND消息以及菜单标识符ID执行相应的操作,完成菜单命令。如果我们能拦截到达窗口的WM_SYSCOMMAND消息,并且识别出菜单的标识符ID,我们就能够在系统菜单上添加自己的菜单项,并且执行指定的动作。下面的例子就是在系统菜单上添加一条分隔符和"关于…"菜单项。
---- 启动Visual Basic,新建标准EXE工程,在工程中添加一标准模块,名称可以是默认的。在标准模块的声明部分加入下列代码:
''菜单API函数声明
Public Declare Function GetSystemMenu Lib "user32"
(ByVal hwnd As Long, ByVal bRevert 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 Any) As Long
''菜单API函数常数声明
Public Const MF_BYCOMMAND = &H0&
Public Const MF_SEPARATOR = &H800&
Public Const MF_STRING = &H0&
''有关窗口函数的API函数声明
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 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 DefWindowProc Lib
"user32" Alias "DefWindowProcA" (ByVal
hwnd As Long, ByVal wMsg As Long,
ByVal wParam As Long, ByVal lParam As Long) As Long
''消息
Public Const GWL_WNDPROC = (-4)
Public Const WM_NCLBUTTONDOWN = &HA1
Public Const WM_NCRBUTTONDOWN = &HA4
Public Const WM_USER = &H400
Public Const WM_SYSCOMMAND = &H112
Public Const HTSYSMENU = 3
Public Const HTCAPTION = 2
''自定义菜单项的标识号偏移量
Public Const IDM_SEPARATOR = 1
Public Const IDM_MYABOUT = 2
''其他变量
Dim sHwnd As Long
Dim OldProc As Long
接着可向标准模块添加下面两个过程:
Public Sub AddMenu(frm As Form)
''置换窗口函数过程
sHwnd = frm.hwnd
OldProc = SetWindowLong(frm.hwnd,
GWL_WNDPROC, AddressOf AddCallBack)
End Sub
Public Sub Release()
''释放自定义窗口函数过程
SetWindowLong sHwnd, GWL_WNDPROC, OldProc
End Sub
最后向标准模块中添加一自定义窗口函数过程:
Public Function AddCallBack(ByVal hwnd As Long,
ByVal wMsg As Long, ByVal wParam As Long,
ByVal lParam As Long) As Long
Select Case wMsg
Case WM_SYSCOMMAND ''系统消息
Select Case wParam ''测试
Case WM_USER + IDM_MYABOUT ''
"关于..."菜单项
''此处可加入用户需要自己处理
"关于…"菜单项的代码
MsgBox "单击了添加的菜单条目", vbOKOnly
Case Else ''其它菜单项交换系统处理
AddCallBack = DefWindowProc(hwnd,
wMsg, wParam, lParam)
End Select
Exit Function
Case Else
AddCallBack = CallWindowProc(OldProc,
hwnd, wMsg, wParam, lParam)
End Select
End Function
关闭标准模块的代码窗口,
打开窗体的代码窗口,
在Form_Load()过程中加入下列代码:
''加载自定义窗口过程
AddMenu Me
''获得系统菜单的句柄
Dim hMenu As Long
hMenu = GetSystemMenu(Me.hwnd, 0)
''在系统菜单中添加自定义2条菜单项
AppendMenu hMenu, MF_SEPARATOR
Or MF_BYCOMMAND,
IDM_SEPARATOR, vbNullString
''分隔符
AppendMenu hMenu, MF_BYCOMMAND
Or MF_STRING,
WM_USER + IDM_MYABOUT, "关于..."
''
"关于…"菜单项
在Form_Unload过程中加入下列代码:
Release ''释放自定义窗口过程
---- 到此,代码的输入工作完成,接下来的是进行测试。单击启动按钮或按F5,启动工程,用鼠标单击窗体左上角的图标弹出系统菜单,看看是否如愿。千万要注意的一点是,在结束工程时,一定要用窗体右上角的关闭按钮或者系统菜单中的关闭菜单项,否则的话,会造成Visual Basic系统崩溃,出现非法操作的错误,所以在测试工程前,最好对工程进行保存。
---- 以上程序在Windows95,Visual Basic6.0环境下调试通过。
Tags:
作者:佚名评论内容只代表网友观点,与本站立场无关!
评论摘要(共 0 条,得分 0 分,平均 0 分)
查看完整评论