添加菜单到系统菜单

添加菜单到系统菜单

Private Sub Form_Load()

    OldWindowProc = GetWindowLong(Form1.hwnd, GWL_WNDPROC)
    ' 取得窗口函数的地址
    Call SetWindowLong(Form1.hwnd, GWL_WNDPROC, AddressOf SubClass1_WndMessage)
    ' 用SubClass1_WndMessage代替窗口函数处理消息

    SysMenuHwnd = GetSystemMenu(Form1.hwnd, False)

    Call AppendMenu(SysMenuHwnd, MF_SEPARATOR, 2000, vbNullString)
    Call AppendMenu(SysMenuHwnd, MF_STRING, 2001, "关于本程序(&A)")
    Call AppendMenu(SysMenuHwnd, MF_SEPARATOR, 2002, vbNullString)
    Call AppendMenu(SysMenuHwnd, MF_STRING, 2003, "恢复系统菜单(&R)")
End Sub

Private Sub Form_Unload(Cancel As Integer)
    If OldWindowProc <> GetWindowLong(Form1.hwnd, GWL_WNDPROC) Then
        Call SetWindowLong(Form1.hwnd, GWL_WNDPROC, OldWindowProc)
    End If
End Sub

Option Explicit

' API函数声明
Public Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long,
ByVal nIndex 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 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 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
Public Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal
lpDirectory As String, ByVal nShowCmd As Long) As Long

' 常数声明
Public Const WM_SYSCOMMAND = &H112
' 单击控制框产生此消息
Public Const MF_SEPARATOR = &H800&
' 为菜单加一条分隔线
Public Const MF_STRING = &H0&
' 在菜单中加一个字符串
Public Const GWL_WNDPROC = (-4)

' 全局变量
Public OldWindowProc As Long
' 保存默认的窗口函数地址
Public SysMenuHwnd As Long
' 保存系统菜单句柄

Public Function SubClass1_WndMessage(ByVal hwnd As Long, ByVal Msg As Long, ByVal wp As Long,
ByVal lp As Long) As Long
    If Msg <> WM_SYSCOMMAND Then
        SubClass1_WndMessage = CallWindowProc(OldWindowProc, hwnd, Msg, wp, lp)
        ' 如果消息不是WM_SYSCOMMAND,就调用默认的窗口函数处理
        Exit Function
    End If

    Select Case wp
        Case 2001
            Call MsgBox("本程序实现了修改系统菜单的功能  ", vbOKOnly + vbInformation)
        Case 2003
            Call GetSystemMenu(Form1.hwnd, True)
            Call SetWindowLong(Form1.hwnd, GWL_WNDPROC, OldWindowProc)
            Call MsgBox("已经恢复了默认的系统菜单  ", vbOKOnly + vbInformation)
        Case Else
            SubClass1_WndMessage = CallWindowProc(OldWindowProc, hwnd, Msg, wp, lp)
            Exit Function
    End Select

    SubClass1_WndMessage = True

End Function

 

       以上代码来自: 源代码数据库(SourceDataBase)
           当前版本: 1.0.538
               作者: Shawls
           个人主页: Http://Shawls.Yeah.Net
             E-Mail: ShawFile@163.Net
                 QQ: 9181729

  • 0
    点赞
  • 0
    评论
  • 0
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

打赏
文章很值,打赏犒劳作者一下
相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页

打赏

shawls

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值