home *** CD-ROM | disk | FTP | other *** search
/ Planet Source Code Jumbo …e CD Visual Basic 1 to 7 / 1_2002.ISO / Data / Zips / CODE_UPLOAD3740332000.psc / Test / Form1.frm (.txt) next >
Encoding:
Visual Basic Form  |  2000-03-02  |  14.0 KB  |  372 lines

  1. VERSION 5.00
  2. Object = "{831FDD16-0C5C-11D2-A9FC-0000F8754DA1}#2.0#0"; "MSCOMCTL.OCX"
  3. Object = "{28D177B5-F05E-11D3-AEFF-08005AD29D41}#1.0#0"; "DynaMenu.ocx"
  4. Begin VB.Form Form1 
  5.    Caption         =   "Dynamic Menu Test"
  6.    ClientHeight    =   4560
  7.    ClientLeft      =   165
  8.    ClientTop       =   450
  9.    ClientWidth     =   5880
  10.    LinkTopic       =   "Form1"
  11.    ScaleHeight     =   4560
  12.    ScaleWidth      =   5880
  13.    StartUpPosition =   2  'CenterScreen
  14.    Begin DynaMenus.DynaMenu DynaMenu1 
  15.       Left            =   2640
  16.       Top             =   2400
  17.       _ExtentX        =   1058
  18.       _ExtentY        =   1005
  19.    End
  20.    Begin VB.CommandButton cmdClearMenu 
  21.       Caption         =   "Clear Menu"
  22.       Height          =   375
  23.       Left            =   2520
  24.       TabIndex        =   3
  25.       Top             =   4080
  26.       Width           =   1095
  27.    End
  28.    Begin VB.CommandButton cmdSaveMenu 
  29.       Caption         =   "Save Menu"
  30.       Height          =   375
  31.       Left            =   120
  32.       TabIndex        =   1
  33.       Top             =   4080
  34.       Width           =   1095
  35.    End
  36.    Begin VB.CommandButton cmdLoadMenu 
  37.       Caption         =   "Load Menu"
  38.       Height          =   375
  39.       Left            =   1320
  40.       TabIndex        =   2
  41.       Top             =   4080
  42.       Width           =   1095
  43.    End
  44.    Begin MSComctlLib.ImageList ImageList1 
  45.       Left            =   1920
  46.       Top             =   2400
  47.       _ExtentX        =   1005
  48.       _ExtentY        =   1005
  49.       BackColor       =   -2147483643
  50.       ImageWidth      =   16
  51.       ImageHeight     =   16
  52.       MaskColor       =   12632256
  53.       _Version        =   393216
  54.       BeginProperty Images {2C247F25-8591-11D1-B16A-00C0F0283628} 
  55.          NumListImages   =   4
  56.          BeginProperty ListImage1 {2C247F27-8591-11D1-B16A-00C0F0283628} 
  57.             Picture         =   "Form1.frx":0000
  58.             Key             =   "FOLDER1"
  59.          EndProperty
  60.          BeginProperty ListImage2 {2C247F27-8591-11D1-B16A-00C0F0283628} 
  61.             Picture         =   "Form1.frx":0452
  62.             Key             =   "FOLDER2"
  63.          EndProperty
  64.          BeginProperty ListImage3 {2C247F27-8591-11D1-B16A-00C0F0283628} 
  65.             Picture         =   "Form1.frx":08A4
  66.             Key             =   "ITEM1"
  67.          EndProperty
  68.          BeginProperty ListImage4 {2C247F27-8591-11D1-B16A-00C0F0283628} 
  69.             Picture         =   "Form1.frx":0CF6
  70.             Key             =   "ITEM2"
  71.          EndProperty
  72.       EndProperty
  73.    End
  74.    Begin MSComctlLib.TreeView TreeView1 
  75.       Height          =   3975
  76.       Left            =   120
  77.       TabIndex        =   0
  78.       Top             =   0
  79.       Width           =   5655
  80.       _ExtentX        =   9975
  81.       _ExtentY        =   7011
  82.       _Version        =   393217
  83.       Indentation     =   441
  84.       LineStyle       =   1
  85.       Style           =   7
  86.       ImageList       =   "ImageList1"
  87.       Appearance      =   1
  88.    End
  89.    Begin VB.Menu mnuFile 
  90.       Caption         =   "&File"
  91.       Begin VB.Menu mnuExit 
  92.          Caption         =   "E&xit"
  93.       End
  94.    End
  95.    Begin VB.Menu mnuView 
  96.       Caption         =   "&View"
  97.       Begin VB.Menu mnuViewOptions 
  98.          Caption         =   "Options"
  99.       End
  100.       Begin VB.Menu Sep1 
  101.          Caption         =   "-"
  102.       End
  103.       Begin VB.Menu mnuDM 
  104.          Caption         =   "DynaMenu"
  105.          Begin VB.Menu mnuDMC 
  106.             Caption         =   "DynaMenuChild"
  107.             Index           =   0
  108.          End
  109.       End
  110.    End
  111.    Begin VB.Menu mnuHelp 
  112.       Caption         =   "&Help"
  113.       Begin VB.Menu mnuAbout 
  114.          Caption         =   "&About"
  115.       End
  116.    End
  117.    Begin VB.Menu mnuPopup 
  118.       Caption         =   "-Popup Menu-"
  119.       Visible         =   0   'False
  120.       Begin VB.Menu mnuAddMenuItem 
  121.          Caption         =   "Add Menu Item"
  122.       End
  123.       Begin VB.Menu mnuAddPopupMenu 
  124.          Caption         =   "Add Popup Menu"
  125.       End
  126.       Begin VB.Menu mnuDelete 
  127.          Caption         =   "Delete"
  128.       End
  129.       Begin VB.Menu mnuRename 
  130.          Caption         =   "Rename"
  131.       End
  132.    End
  133. Attribute VB_Name = "Form1"
  134. Attribute VB_GlobalNameSpace = False
  135. Attribute VB_Creatable = False
  136. Attribute VB_PredeclaredId = True
  137. Attribute VB_Exposed = False
  138. Option Explicit
  139. Private Const MIIM_STATE = &H1
  140. Private Const MIIM_ID = &H2
  141. Private Const MIIM_SUBMENU = &H4
  142. Private Const MIIM_CHECKMARKS = &H8
  143. Private Const MIIM_TYPE = &H10
  144. Private Const MIIM_DATA = &H20
  145. Private Const MIIM_ALL = MIIM_STATE Or MIIM_ID Or MIIM_SUBMENU Or MIIM_CHECKMARKS Or MIIM_TYPE Or MIIM_DATA
  146. Private Declare Function GetMenu Lib "user32" (ByVal hwnd As Long) As Long
  147. Private Type MENUITEMINFO
  148.     cbSize As Long
  149.     fMask As Long
  150.     fType As Long
  151.     fState As Long
  152.     wID As Long
  153.     hSubMenu As Long
  154.     hbmpChecked As Long
  155.     hbmpUnchecked As Long
  156.     dwItemData As Long
  157.     dwTypeData As String
  158.     cch As Long
  159. End Type
  160. Private Declare Function GetMenuItemInfo Lib "user32" Alias "GetMenuItemInfoA" (ByVal hMenu As Long, ByVal un As Long, ByVal b As Long, lpMenuItemInfo As MENUITEMINFO) As Long
  161. Private Declare Function GetMenuAPI Lib "user32" Alias "GetMenu" (ByVal hwnd As Long) As Long
  162. Private Declare Function IsMenu Lib "user32" (ByVal hMenu As Long) As Long
  163. Private Const INSERT_AT_INDEX = 0   ' Append menus
  164. 'Private Const INSERT_AT_INDEX = 1   ' Insert new menu items @ start
  165. Private m_lngNextID As Long
  166. '*******************************************************************************
  167. ' Initialise the Dynamic Menus and the TreeView
  168. '-------------------------------------------------------------------------------
  169. Private Sub Form_Load()
  170.     ' Important - All three of these menu properties must be set for the
  171.     ' control to work correctly
  172.     Set DynaMenu1.ParentMenu = mnuView
  173.     Set DynaMenu1.PopupMenu = mnuDM
  174.     Set DynaMenu1.ChildMenuArray = mnuDMC     ' Must Be an Array
  175.     ' Initialise the TreeView (the menus will already be empty)
  176.     Clear
  177. '    Dim hMenu As Long
  178. '    hMenu = GetMenuAPI(Me.hwnd)
  179. '    Dim miView As MENUITEMINFO
  180. '    miView.cbSize = Len(miView)
  181. '    miView.fMask = MIIM_ALL
  182. '    Debug.Assert GetMenuItemInfo(hMenu, 1, 1, miView)
  183. '    Debug.Assert IsMenu(miView.hSubMenu)
  184. '    MsgBox "View Menu Handle = " & miView.hSubMenu
  185.     cmdLoadMenu_Click
  186. End Sub
  187. '*******************************************************************************
  188. ' Menu event handler
  189. '-------------------------------------------------------------------------------
  190. Private Sub mnuDMC_Click(Index As Integer)
  191.     ' Determine the CMenu item that was clicked on
  192.     ' NOTE - the index of the CMenu does not necessarily correspond with
  193.     ' the index of the VB menu item used by the dynamic menu, and therefore
  194.     ' can not be used to access DynaMenu's Menu collection.
  195.     Dim mnu As CMenu
  196.     Set mnu = DynaMenu1.ItemByMenuIndex(Index)
  197.     If Not mnu Is Nothing Then
  198.         ' Having determined which menu item was clicked on, do something
  199.         ' useful depending on the key of the menu item.
  200.         MsgBox "You clicked on " & mnu.Caption & vbCrLf & _
  201.                             "Internal Key = " & mnu.Key
  202.         Set mnu = Nothing
  203.     Else
  204.         MsgBox "Error - unrecognised menu item !"
  205.     End If
  206. End Sub
  207. '*******************************************************************************
  208. ' Basic Infrastructure of the Form - irrelevant to the operation of the
  209. ' dynamic menus
  210. '-------------------------------------------------------------------------------
  211. Private Function Max(ByVal a As Long, ByVal b As Long) As Long
  212.     Max = IIf(a >= b, a, b)
  213. End Function
  214. Private Sub mnuAbout_Click()
  215.     MsgBox "About Box"
  216. End Sub
  217. Private Sub mnuExit_Click()
  218.     Unload Me
  219. End Sub
  220. Private Sub mnuViewOptions_Click()
  221.     MsgBox "Options Dialog"
  222. End Sub
  223. Private Sub Form_Resize()
  224.     Dim lng As Long
  225.     lng = Max(ScaleHeight - cmdLoadMenu.Height - TreeView1.Left, 0)
  226.     cmdLoadMenu.Top = lng
  227.     cmdSaveMenu.Top = lng
  228.     cmdClearMenu.Top = lng
  229.     TreeView1.Width = Max(ScaleWidth - 2 * TreeView1.Left, 0)
  230.     TreeView1.Height = Max(cmdLoadMenu.Top - TreeView1.Left, 0)
  231. End Sub
  232. Private Sub TreeView1_BeforeLabelEdit(Cancel As Integer)
  233.     If TreeView1.SelectedItem.Key = "!0" Then Cancel = 1
  234. End Sub
  235. Private Sub TreeView1_AfterLabelEdit(Cancel As Integer, NewString As String)
  236.     DynaMenu1.Menu(TreeView1.SelectedItem.Key).Caption = NewString
  237. End Sub
  238. '*******************************************************************************
  239. ' Trap the mouse down event to display the context menu
  240. '-------------------------------------------------------------------------------
  241. Private Sub TreeView1_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single)
  242.     If (Button And vbRightButton) Then
  243.         Dim n As Node
  244.         Set n = TreeView1.HitTest(x, y)
  245.         If Not n Is Nothing Then
  246.             Set TreeView1.SelectedItem = n
  247.             If n.Key = "!0" Then
  248.                 mnuAddMenuItem.Enabled = True
  249.                 mnuAddPopupMenu.Enabled = True
  250.                 mnuDelete = False
  251.                 mnuRename = 0
  252.             Else
  253.                 Dim m As CMenu
  254.                 Set m = DynaMenu1.Menu(n.Key)
  255.                 mnuAddMenuItem.Enabled = m.IsPopup
  256.                 mnuAddPopupMenu.Enabled = m.IsPopup
  257.                 mnuDelete = True
  258.                 mnuRename = True
  259.             End If
  260.             Me.PopupMenu mnuPopup
  261.         End If
  262.     End If
  263. End Sub
  264. '*******************************************************************************
  265. ' Context menu event handlers
  266. '-------------------------------------------------------------------------------
  267. Private Sub mnuAddMenuItem_Click()
  268.     ' Add a menu item to the tree and menu
  269.     ' Make sure that you use a meaningful key!
  270.     m_lngNextID = m_lngNextID + 1
  271.     AddMenuItem "!" & m_lngNextID, TreeView1.SelectedItem.Key, "New Menu Item"
  272.     ' Edit the default caption of the menu item
  273.     Set TreeView1.SelectedItem = TreeView1.Nodes("!" & m_lngNextID)
  274.     TreeView1.StartLabelEdit
  275. End Sub
  276. Private Sub mnuAddPopupMenu_Click()
  277.     ' Add a popup menu to the tree and menu
  278.     ' Make sure that you use a meaningful key!
  279.     m_lngNextID = m_lngNextID + 1
  280.     AddPopupMenu "!" & m_lngNextID, TreeView1.SelectedItem.Key, "New Popup Menu"
  281.     ' Edit the default caption of the popup
  282.     Set TreeView1.SelectedItem = TreeView1.Nodes("!" & m_lngNextID)
  283.     TreeView1.StartLabelEdit
  284. End Sub
  285. Private Sub mnuDelete_Click()
  286.     ' Delete the selected menu item / popup menu
  287.     DeleteMenu TreeView1.SelectedItem.Key
  288. End Sub
  289. Private Sub mnuRename_Click()
  290.     ' Rename the menu item / popup menu
  291.     TreeView1.StartLabelEdit
  292. End Sub
  293. '*******************************************************************************
  294. ' Add/Remove items to/from both the TreeView and the Dynamic Menu
  295. '-------------------------------------------------------------------------------
  296. Private Sub AddMenuItem(Key As String, ParentKey As String, Caption As String)
  297.     'Add a node to the tree
  298.     TreeView1.Nodes.Add ParentKey, tvwChild, Key, Caption, "ITEM1", "ITEM2"
  299.     TreeView1.Nodes(ParentKey).Expanded = True
  300.     'Add an item to the menu
  301.     If ParentKey <> "!0" Then
  302.         DynaMenu1.Menu.Add Caption, Key, INSERT_AT_INDEX, ParentKey, False
  303.     Else
  304.         DynaMenu1.Menu.Add Caption, Key, INSERT_AT_INDEX, , False
  305.     End If
  306. End Sub
  307. Private Sub AddPopupMenu(Key As String, ParentKey As String, Caption As String)
  308.     'Add a node to the tree
  309.     TreeView1.Nodes.Add ParentKey, tvwChild, Key, Caption, "FOLDER1", "FOLDER2"
  310.     TreeView1.Nodes(ParentKey).Expanded = True
  311.     'Add an item to the menu
  312.     If ParentKey <> "!0" Then
  313.         DynaMenu1.Menu.Add Caption, Key, INSERT_AT_INDEX, ParentKey, True
  314.     Else
  315.         DynaMenu1.Menu.Add Caption, Key, INSERT_AT_INDEX, , True
  316.     End If
  317. End Sub
  318. Private Sub DeleteMenu(Key As String)
  319.     TreeView1.Nodes.Remove Key
  320.     DynaMenu1.Menu.Remove Key
  321. End Sub
  322. '*******************************************************************************
  323. ' File I/O for saving the menus
  324. '-------------------------------------------------------------------------------
  325. Private Sub cmdLoadMenu_Click()
  326.     Clear
  327.     Dim File As Integer
  328.     File = FreeFile
  329.     Open App.Path & "\MenuFile.txt" For Input As #File
  330.     Dim strKey As String, strParentKey As String
  331.     Dim strCaption As String, blnPopup As Boolean
  332.     Do While EOF(File) = False
  333.         Input #File, strKey, strParentKey, strCaption, blnPopup
  334.         If (Len(strKey) > 0) Then
  335.             If strParentKey = "" Then strParentKey = "!0"
  336.             If blnPopup Then
  337.                 AddPopupMenu strKey, strParentKey, strCaption
  338.             Else
  339.                 AddMenuItem strKey, strParentKey, strCaption
  340.             End If
  341.             
  342.             m_lngNextID = Max(m_lngNextID, CLng(Mid$(strKey, 2)))
  343.         End If
  344.     Loop
  345.     Close #File
  346. End Sub
  347. Private Sub cmdSaveMenu_Click()
  348.     Dim File As Integer
  349.     File = FreeFile
  350.     Open App.Path & "\MenuFile.txt" For Output As #File
  351.     Dim m As CMenu
  352.     For Each m In DynaMenu1.Menu
  353.         Write #File, m.Key, m.ParentKey, m.Caption, m.IsPopup
  354.     Next
  355.     Close #File
  356. End Sub
  357. '*******************************************************************************
  358. ' Clear all items from both the TreeView and the Dynamic Menu
  359. '-------------------------------------------------------------------------------
  360. Private Sub cmdClearMenu_Click()
  361.     Clear
  362. End Sub
  363. Private Sub Clear()
  364.     ' Clear the Menus
  365.     TreeView1.Nodes.Clear
  366.     DynaMenu1.Menu.Clear
  367.     TreeView1.Nodes.Add , , "!0", "Root", "FOLDER1", "FOLDER2"
  368.     m_lngNextID = 0
  369. End Sub
  370. '*******************************************************************************
  371. '-------------------------------------------------------------------------------
  372.