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 / modMenuAPI.bas < prev   
Encoding:
BASIC Source File  |  2000-02-18  |  13.5 KB  |  489 lines

  1. Attribute VB_Name = "modMenuAPI"
  2. Option Explicit
  3.  
  4. Public Const MF_BYCOMMAND = &H0&
  5. Public Const MF_BYPOSITION = &H400&
  6.  
  7. Public Const MF_POPUP = &H10&
  8. Public Const MF_STRING = &H0&
  9. Public Const MF_SEPARATOR = &H800&
  10.  
  11. Public Const MF_GRAYED = &H1&
  12. Public Const MF_ENABLED = &H0&
  13. Public Const MF_CHECKED = &H8&
  14. Public Const MF_UNCHECKED = &H0&
  15.  
  16. Public Declare Sub MoveMemory Lib "kernel32" Alias "RtlMoveMemory" (A As Any, B As Any, ByVal C As Long)
  17. Public Declare Function IsMenu Lib "user32" (ByVal hMenu As Long) As Long
  18. Public Declare Function DrawMenuBar Lib "user32" (ByVal hwnd As Long) As Long
  19. Public Declare Function GetMenu Lib "user32" (ByVal hwnd As Long) As Long
  20. Public Declare Function CreatePopupMenu Lib "user32" () As Long
  21. Public Declare Function DestroyMenu Lib "user32" (ByVal hMenu As Long) As Long
  22. 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
  23. Public Declare Function ModifyMenu Lib "user32" Alias "ModifyMenuA" (ByVal hMenu As Long, ByVal nPosition As Long, ByVal wFlags As Long, ByVal wIDNewItem As Long, ByVal lpString As Any) As Long
  24. Public Declare Function DeleteMenu Lib "user32" (ByVal hMenu As Long, ByVal nPosition As Long, ByVal wFlags As Long) As Long
  25. Public Declare Function GetMenuString Lib "user32" Alias "GetMenuStringA" (ByVal hMenu As Long, ByVal uIDItem As Long, ByVal lpString As String, ByVal nMaxCount As Long, ByVal uFlag As Long) As Long
  26. Public Declare Function InsertMenu Lib "user32" Alias "InsertMenuA" (ByVal hMenu As Long, ByVal nPosition As Long, ByVal wFlags As Long, ByVal wIDNewItem As Long, ByVal lpNewItem As Any) As Long
  27. Public Declare Function CheckMenuItem Lib "user32" (ByVal hMenu As Long, ByVal wIDCheckItem As Long, ByVal wCheck As Long) As Long
  28. Public Declare Function EnableMenuItem Lib "user32" (ByVal hMenu As Long, ByVal wIDEnableItem As Long, ByVal wEnable As Long) As Long
  29.  
  30. '*********************************************************************************************
  31. ' Menu flags
  32. '*********************************************************************************************
  33. Enum MenuFlags
  34.     Checked = &H1
  35.     Hidden = &H2
  36.     Grayed = &H4
  37.     PopUp = &H8
  38.     WindowList = &H20
  39.     LastItem = &H100
  40. End Enum
  41.  
  42.  
  43. '*********************************************************************************************
  44. ' Internal menu struct
  45. '*********************************************************************************************
  46. Type MenuStruct
  47. '    Reserved(0 To 48) As Long
  48. '    '              ^
  49. '    '              |
  50. '    '              +---- For VB6 replace the 48 by 54
  51. '    '
  52.     Reserved(0 To 54) As Long
  53.     '              ^
  54.     '              |
  55.     '              +---- For VB6 replace the 48 by 54
  56.     '
  57.     dwFlags As MenuFlags
  58.     lpNextMenu As Long
  59.     lpFirstItem As Long
  60.     lpszName As Long
  61.     hMenu As Long
  62.     wID As Integer
  63.     wShortcut As Integer
  64. End Type
  65.  
  66. '*********************************************************************************************
  67. ' Menu shortcuts
  68. '*********************************************************************************************
  69. Enum MenuShortcuts
  70.     vbNoShortcut
  71.     vbCtrlA
  72.     vbCtrlB
  73.     vbCtrlC
  74.     vbCtrlD
  75.     vbCtrlE
  76.     vbCtrlF
  77.     vbCtrlG
  78.     vbCtrlH
  79.     vbCtrlI
  80.     vbCtrlJ
  81.     vbCtrlK
  82.     vbCtrlL
  83.     vbCtrlM
  84.     vbCtrlN
  85.     vbCtrlO
  86.     vbCtrlP
  87.     vbCtrlQ
  88.     vbCtrlR
  89.     vbCtrlS
  90.     vbCtrlT
  91.     vbCtrlU
  92.     vbCtrlV
  93.     vbCtrlW
  94.     vbCtrlX
  95.     vbCtrlY
  96.     vbCtrlZ
  97.     vbF1
  98.     vbF2
  99.     vbF3
  100.     vbF4
  101.     vbF5
  102.     vbF6
  103.     vbF7
  104.     vbF8
  105.     vbF9
  106.     vbF10
  107.     vbF11
  108.     vbF12
  109.     vbCtrlF1
  110.     vbCtrlF2
  111.     vbCtrlF3
  112.     vbCtrlF4
  113.     vbCtrlF5
  114.     vbCtrlF6
  115.     vbCtrlF7
  116.     vbCtrlF8
  117.     vbCtrlF9
  118.     vbCtrlF10
  119.     vbCtrlF11
  120.     vbCtrlF12
  121.     vbShiftF1
  122.     vbShiftF2
  123.     vbShiftF3
  124.     vbShiftF4
  125.     vbShiftF5
  126.     vbShiftF6
  127.     vbShiftF7
  128.     vbShiftF8
  129.     vbShiftF9
  130.     vbShiftF10
  131.     vbShiftF11
  132.     vbShiftF12
  133.     vbShiftCtrlF1
  134.     vbShiftCtrlF2
  135.     vbShiftCtrlF3
  136.     vbShiftCtrlF4
  137.     vbShiftCtrlF5
  138.     vbShiftCtrlF6
  139.     vbShiftCtrlF7
  140.     vbShiftCtrlF8
  141.     vbShiftCtrlF9
  142.     vbShiftCtrlF10
  143.     vbShiftCtrlF11
  144.     vbShiftCtrlF12
  145.     vbCtrlInsert
  146.     vbShiftInset
  147.     vbDelete
  148.     vbShiftDel
  149.     vbAltBackspace
  150. End Enum
  151.  
  152. '*********************************************************************************************
  153. ' GetFirstChildMenu
  154. '*********************************************************************************************
  155. '
  156. ' Parameters:
  157. '
  158. '   MenuObject:     The menu object of which the
  159. '                   menu handle is wanted.
  160. '
  161. ' Returns:
  162. '
  163. '   The menu handle if it's a popup menu.
  164. '
  165. '*********************************************************************************************
  166. Public Function GethMenu(ByVal MenuObject As VB.Menu) As Long
  167. Dim mnu As MenuStruct
  168.     
  169.     ' Get the menu struct
  170.     
  171.     MoveMemory mnu, ByVal ObjPtr(MenuObject), Len(mnu)
  172.     
  173.     ' Get the hMenu only if the menu
  174.     ' is a popup menu. A popup menu is a
  175.     ' menu with child items.
  176.  
  177.     If mnu.lpFirstItem Then
  178.         GethMenu = mnu.hMenu
  179.     Else
  180.         GethMenu = 0
  181.     End If
  182.     
  183.     If IsMenu(GethMenu) = 0 Then
  184.         MoveMemory GethMenu, ByVal (ObjPtr(MenuObject) + 224), Len(GethMenu)
  185.     End If
  186.     
  187.     Debug.Assert (IsMenu(GethMenu) <> 0)
  188.     
  189. End Function
  190.  
  191. '*********************************************************************************************
  192. ' GetParenthMenu
  193. '*********************************************************************************************
  194. '
  195. ' Parameters:
  196. '
  197. '   MenuObject:     The menu object of which
  198. '                   parent menu handle is wanted.
  199. '
  200. ' Returns:
  201. '
  202. '   The parent menu handle.
  203. '
  204. '*********************************************************************************************
  205. Public Function GetParenthMenu(ByVal MenuObject As VB.Menu) As Long
  206. Dim mnu As MenuStruct
  207.     
  208.     ' Get the menu struct
  209.     
  210.     MoveMemory mnu, ByVal ObjPtr(MenuObject), Len(mnu)
  211.     
  212.     ' Get the hMenu only if the menu
  213.     ' isn't a popup menu.
  214.  
  215.     If mnu.lpFirstItem = 0 Then
  216.         GetParenthMenu = mnu.hMenu
  217.     Else
  218.         GetParenthMenu = 0
  219.     End If
  220.     
  221. End Function
  222.  
  223. '*********************************************************************************************
  224. ' GetShortcut
  225. '*********************************************************************************************
  226. '
  227. ' Parameters:
  228. '
  229. '   MenuObject:     The menu object of which
  230. '                   the shortcut is wanted.
  231. '
  232. ' Returns:
  233. '
  234. '   The shortcut.
  235. '
  236. '*********************************************************************************************
  237. Public Function GetShortcut(ByVal MenuObject As VB.Menu) As MenuShortcuts
  238. Dim mnu As MenuStruct
  239.  
  240.     ' Get the menu struct
  241.     
  242.     MoveMemory mnu, ByVal ObjPtr(MenuObject), Len(mnu)
  243.     
  244.     ' Only non popup items can have a shortcut
  245.     
  246.     If mnu.lpFirstItem = 0 Then
  247.         GetShortcut = mnu.wShortcut
  248.     Else
  249.         GetShortcut = vbNoShortcut
  250.     End If
  251.     
  252. End Function
  253.  
  254.  
  255.  
  256. '*********************************************************************************************
  257. ' GetNextMenu
  258. '*********************************************************************************************
  259. '
  260. ' Parameters:
  261. '
  262. '   MenuObject:     The menu object of which
  263. '                   next menu object is wanted.
  264. '
  265. ' Returns:
  266. '
  267. '   The next menu object or Nothing if this is the
  268. '   last menu.
  269. '
  270. '*********************************************************************************************
  271. Public Function GetNextMenu(ByVal MenuObject As VB.Menu) As VB.Menu
  272. Dim mnu As MenuStruct, Nxt As VB.Menu
  273.  
  274.     ' Get the menu struct
  275.     
  276.     MoveMemory mnu, ByVal ObjPtr(MenuObject), Len(mnu)
  277.             
  278.     ' Get the next menu only if there's one
  279.     ' and this is not the last item
  280.     
  281.     If mnu.lpNextMenu <> 0 And (mnu.dwFlags And LastItem) = 0 Then
  282.         
  283.         ' Get a copy without AddRef
  284.         MoveMemory Nxt, mnu.lpNextMenu, 4
  285.         
  286.         ' Get the object with AddRef
  287.         Set GetNextMenu = Nxt
  288.         
  289.         ' Release the copy
  290.         MoveMemory Nxt, 0&, 4
  291.         
  292.     End If
  293.  
  294. End Function
  295.  
  296. '*********************************************************************************************
  297. ' GetParentMenu
  298. '*********************************************************************************************
  299. '
  300. ' Parameters:
  301. '
  302. '   MenuObject:     The menu object of which
  303. '                   parent menu object is wanted.
  304. '
  305. ' Returns:
  306. '
  307. '   The parent menu object.
  308. '
  309. '*********************************************************************************************
  310. Public Function GetParentMenu(ByVal MenuObject As VB.Menu) As VB.Menu
  311. Dim Nxt As VB.Menu, mnu As MenuStruct
  312.  
  313.     ' Get the next menu until we found the last item
  314.     ' in the menu. In the last menu object the next
  315.     ' menu points to the parent.
  316.  
  317.     Set Nxt = GetNextMenu(MenuObject)
  318.     
  319.     Do While Not Nxt Is Nothing
  320.     
  321.         MoveMemory mnu, ByVal ObjPtr(Nxt), Len(mnu)
  322.         
  323.         If (mnu.dwFlags And LastItem) = LastItem Then
  324.             
  325.             Dim Parent As VB.Menu
  326.             
  327.             MoveMemory Parent, mnu.lpNextMenu, 4
  328.         
  329.             ' Get the object with AddRef
  330.             Set GetParentMenu = Parent
  331.         
  332.             MoveMemory Parent, 0&, 4
  333.             
  334.             Exit Do
  335.             
  336.         End If
  337.         
  338.         Set Nxt = GetNextMenu(Nxt)
  339.         
  340.     Loop
  341.  
  342. End Function
  343.  
  344.  
  345. '*********************************************************************************************
  346. ' GetFirstChildMenu
  347. '*********************************************************************************************
  348. '
  349. ' Parameters:
  350. '
  351. '   MenuObject:     The menu object of which the
  352. '                   first child is wanted.
  353. '
  354. ' Returns:
  355. '
  356. '   The first child menu object.
  357. '
  358. '*********************************************************************************************
  359. Public Function GetFirstChildMenu(ByVal MenuObject As VB.Menu) As VB.Menu
  360. Dim mnu As MenuStruct, Itm As Menu
  361.  
  362.     ' Get menu struct from object
  363.     MoveMemory mnu, ByVal ObjPtr(MenuObject), Len(mnu)
  364.  
  365.     ' Check the pointer. If it's null there's
  366.     ' no child item.
  367.     If mnu.lpFirstItem <> 0 Then
  368.         
  369.         ' Get the object reference. Since
  370.         ' IUnknown::AddRef is not called
  371.         ' DO NOT set this object to Nothing.
  372.         MoveMemory Itm, mnu.lpFirstItem, 4
  373.         
  374.         ' Get a copy with AddRef.
  375.         Set GetFirstChildMenu = Itm
  376.         
  377.         MoveMemory Itm, 0&, 4
  378.         
  379.     End If
  380.     
  381. End Function
  382.  
  383.  
  384.  
  385. '*********************************************************************************************
  386. ' GetShortcut
  387. '*********************************************************************************************
  388. '
  389. ' Parameters:
  390. '
  391. '   MenuObject:     The menu object.
  392. '
  393. ' Returns:
  394. '
  395. '   True if the menu item has children, otherwise False.
  396. '
  397. '*********************************************************************************************
  398. Public Function IsPopupMenu(MenuObject As VB.Menu) As Boolean
  399. Dim mnu As MenuStruct
  400.  
  401.     MoveMemory mnu, ByVal ObjPtr(MenuObject), Len(mnu)
  402.     
  403.     IsPopupMenu = mnu.lpFirstItem
  404.  
  405. End Function
  406.  
  407. '*********************************************************************************************
  408. ' SetShortcut
  409. '*********************************************************************************************
  410. '
  411. ' Changes a menu shortcut
  412. '
  413. ' Parameters:
  414. '
  415. '   MenuObject:     The menu object.
  416. '
  417. '*********************************************************************************************
  418. Public Sub SetShortcut(ByVal MenuObject As VB.Menu, ByVal NewShortcut As MenuShortcuts)
  419. Dim mnu As MenuStruct
  420.  
  421.     ' Get the menu struct
  422.     
  423.     MoveMemory mnu, ByVal ObjPtr(MenuObject), Len(mnu)
  424.     
  425.     ' Only non popup items can have a shortcut
  426.     
  427.     If mnu.lpFirstItem = 0 Then
  428.     
  429.         If NewShortcut > vbAltBackspace Then
  430.             NewShortcut = vbAltBackspace
  431.         ElseIf NewShortcut < 0 Then
  432.             NewShortcut = vbNoShortcut
  433.         End If
  434.         
  435.         mnu.wShortcut = NewShortcut
  436.         
  437.         ' Change only that value
  438.     
  439.         MoveMemory ByVal ObjPtr(MenuObject) + 218, mnu.wShortcut, 2
  440.         
  441.         ' Set the caption to update
  442.         ' the shortcut text
  443.         MenuObject.Caption = MenuObject.Caption
  444.         
  445.     End If
  446.  
  447. End Sub
  448.  
  449. '*********************************************************************************************
  450. ' GetCommand
  451. '*********************************************************************************************
  452. '
  453. ' Parameters:
  454. '
  455. '   MenuObject:     The menu object of which the
  456. '                   command is wanted. Only non
  457. '                   popup items have a command.
  458. '
  459. ' Returns:
  460. '
  461. '   The command.
  462. '
  463. '*********************************************************************************************
  464. Public Function GetCommand(ByVal MenuObject As VB.Menu) As Long
  465. Dim mnu As MenuStruct
  466.     
  467.     ' Get the menu struct from object
  468.     
  469.     MoveMemory mnu, ByVal ObjPtr(MenuObject), Len(mnu)
  470.     
  471.     ' Get the command only if the menu
  472.     ' isn't a popup menu.
  473.  
  474.     If mnu.lpFirstItem = 0 Then
  475.         GetCommand = mnu.wID
  476.     Else
  477.         GetCommand = 0
  478.     End If
  479.     
  480.     If GetCommand = 0 Then
  481.         Dim l As Integer
  482.         MoveMemory l, ByVal (ObjPtr(MenuObject) + 228), Len(l)
  483.         GetCommand = l
  484.     End If
  485.     
  486. End Function
  487.  
  488.  
  489.