正在阅读:VB打造超酷个性化菜单(1)VB打造超酷个性化菜单(1)

2005-05-13 10:06 出处: 作者:goodname008 责任编辑:xietaoming

  (3)窗体代码:

Option Explicit

Private Declare Function GetCursorPos Lib "user32.dll" (lpPoint As POINTAPI) As Long

Private Type POINTAPI
    X As Long
    Y As Long
End Type

 Dim menu As cMenu
 
Private Sub Form_Load()
    ' 初始化菜单并添加菜单项
    Set menu = New cMenu
    menu.CreateMenu
    menu.AddItem "open", LoadPicture("images\open.ico"), "打开", MIT_STRING
    menu.AddItem "save", LoadPicture("images\save.ico"), "保存", MIT_STRING
    menu.AddItem "print", LoadPicture("images\print.ico"), "打印", MIT_STRING
    menu.AddItem "find", LoadPicture("images\find.ico"), "查找", MIT_STRING
    menu.AddItem "sep1", LoadPicture(), "", MIT_SEPARATOR
    menu.AddItem "undo", LoadPicture("images\undo.ico"), "撤消", MIT_STRING
    menu.AddItem "redo", LoadPicture("images\redo.ico"), "重复", MIT_STRING
    menu.AddItem "sep2", LoadPicture(), "", MIT_SEPARATOR
    menu.AddItem "cut", LoadPicture("images\cut.ico"), "剪切", MIT_STRING
    menu.AddItem "copy", LoadPicture("images\copy.ico"), "复制", MIT_STRING
    menu.AddItem "paste", LoadPicture("images\paste.ico"), "粘贴", MIT_STRING
    menu.AddItem "sep3", LoadPicture(), "", MIT_SEPARATOR
    menu.AddItem "check", LoadPicture("images\check.ico"), "一个 CheckBox", MIT_CHECKBOX
    menu.AddItem "exit", LoadPicture("images\exit.ico"), "退出", MIT_STRING
End Sub

Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    ' 单击鼠标右建弹出菜单
    If Button = vbRightButton Then
        Dim pos As POINTAPI
        GetCursorPos pos
        menu.PopupMenu pos.X, pos.Y, POPUP_LEFTALIGN Or POPUP_TOPALIGN
    End If
End Sub

Private Sub Form_Unload(Cancel As Integer)
    ' 释放资源, 卸载窗体
    Set menu = Nothing
    Dim frm As Form
    For Each frm In Forms
        Unload frm
    Next
End Sub

Private Sub opnStyle_Click(Index As Integer)
    ' 设置菜单风格
    Select Case Index
        Case 0                                  ' Windows 标准
            menu.Style = STYLE_WINDOWS
        Case 1                                  ' XP 风格
            menu.Style = STYLE_XP
        Case 2                                  ' 3D 立体风格
            menu.Style = STYLE_3D
        Case 3                                  ' 渐变风格
            menu.Style = STYLE_SHADE
        Case 4                                  ' 多彩风格
            menu.Style = STYLE_COLORFUL
    End Select

End Sub

  代码中创建了一个cMenu类的对象,我们的编程重点将会放在cMenu类上,上面的代码只是简单地调用cMenu。在后面的文章中,我们会看到其实cMenu有多达30个方法和属性供我们调用,它的Style属性只提供了5种内置风格,在实际应用中,我们可以利用cMenu类提供的方法和属性制作出各种各样风格的菜单,为自己的程序锦上添花。

键盘也能翻页,试试“← →”键

关注我们

最新资讯离线随时看 聊天吐槽赢奖品