Advertisement
Guest User

Untitled

a guest
Oct 8th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Rem Attribute VBA_ModuleType=VBAModule
  2. Option VBASupport 1
  3. Option Explicit
  4. '//===================================================
  5. '// Routine to Display RightClick Options
  6. '// 28th July 2004
  7. '// After continual crashes & saves!
  8. '//===================================================
  9.  
  10. '// What we need to do is get the Area to our icon in the TaskBar
  11.  
  12. '// Types 1st Before InsertMenuItem !
  13. Public Type MENUITEMINFO
  14.     cbSize          As Long
  15.     fMask           As Long
  16.     fType           As Long
  17.     fState          As Long
  18.     wID             As Long
  19.     hSubMenu        As Long
  20.     hbmpChecked     As Long
  21.     hbmpUnchecked   As Long
  22.     dwItemData      As Long
  23.     dwTypeData      As String
  24.     cch             As Long
  25. End Type
  26.  
  27. Type POINTAPI
  28.         X As Long
  29.         y As Long
  30. End Type
  31.  
  32. Type RECT
  33.         Left As Long
  34.         Top As Long
  35.         Right As Long
  36.         Bottom As Long
  37. End Type
  38.  
  39. '// Menu
  40. Public Declare Function CreatePopupMenu Lib "user32" () As Long
  41.  
  42. Public Declare Function InsertMenuItem _
  43.     Lib "user32" _
  44.     Alias "InsertMenuItemA" ( _
  45.         ByVal hMenu As Long, _
  46.         ByVal un As Long, _
  47.         ByVal bool As Long, _
  48.         lpcMenuItemInfo As MENUITEMINFO) _
  49. As Long
  50.  
  51. Public Declare Function TrackPopupMenuEx _
  52.     Lib "user32" ( _
  53.         ByVal hMenu As Long, _
  54.         ByVal un As Long, _
  55.         ByVal n1 As Long, _
  56.         ByVal n2 As Long, _
  57.         ByVal hwnd As Long, _
  58.         lpTPMParams As Any) _
  59. As Long
  60.  
  61. Public Declare Function GetCursorPos _
  62.     Lib "user32" ( _
  63.         lpPoint As POINTAPI) _
  64. As Long
  65.  
  66. '//
  67. Public Declare Function DestroyMenu _
  68.     Lib "user32" ( _
  69.         ByVal hMenu As Long) _
  70. As Long
  71. ''
  72. Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
  73. Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
  74. Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long
  75.  
  76.  
  77. '// Calls to the functions Menus others added by IFM.
  78. Public Const MF_SEPARATOR = &H800&
  79. Public Const MF_STRING = &H0&
  80. Public Const TPM_RETURNCMD = &H100&
  81. Public Const MIIM_ID = &H2
  82. Public Const MIIM_TYPE = &H10
  83. Public Const MIIM_DATA = &H20
  84.  
  85. Public PT As POINTAPI
  86. Public lMnu As Long       'RightClick Menu
  87. Public lngMNUItem As Long 'Menu Item selected
  88.  
  89. Private Const GW_CHILD As Long = 5
  90. Private Const GW_HWNDNEXT As Long = 2
  91.  
  92. Dim IconRightClickMNU As MENUITEMINFO
  93. '// These variables are used in the
  94. '// Events and procedures for our menu
  95.  
  96. Sub CreateRightClickMenu()
  97. Dim lngCnt As Long
  98. Dim oMnuItemArray As Range
  99.  
  100. '// Initialise the MNU constants
  101. Set oMnuItemArray = ThisWorkbook.Sheets(Sheet2.Index).Range("MNU")
  102.  
  103. lMnu = CreatePopupMenu()
  104.  
  105. For lngCnt = 1 To 15
  106.     With IconRightClickMNU
  107.         .cbSize = Len(IconRightClickMNU)
  108.         .fMask = MIIM_TYPE Or MIIM_ID Or MIIM_DATA
  109.         .dwTypeData = oMnuItemArray.Item(lngCnt)
  110.         .cch = Len(.dwTypeData)
  111.         If .cch = 1 Then
  112.             .fType = MF_SEPARATOR
  113.         Else
  114.             .fType = MF_STRING
  115.         End If
  116.         .wID = lngCnt
  117.     End With
  118.     Call InsertMenuItem(lMnu, lngCnt, 1, IconRightClickMNU)
  119. Next lngCnt
  120.  
  121. End Sub
  122.  
  123. Public Function GetHandleToTray() As Long
  124. '// Get the Handle to the System Tray
  125. Dim lStartWndhdl As Long
  126. Dim lChildWndhdl As Long
  127. Dim strClass As String * 255
  128. Dim lcNameLen As Long
  129.  
  130. '// Get the handle to the start menu.
  131. lStartWndhdl = FindWindow("Shell_TrayWnd", vbNullString)
  132.  
  133. '// Get the handle to the first child window of the start menu.
  134. lChildWndhdl = GetWindow(lStartWndhdl, GW_CHILD)
  135.  
  136. Do
  137.     lcNameLen = GetClassName(lChildWndhdl, strClass, Len(strClass))
  138.     '// Check if it is the Systemtray.
  139.    If InStr(1, strClass, "TrayNotifyWnd") Then
  140.         GetHandleToTray = lChildWndhdl
  141.         Exit Do
  142.     End If
  143.     '// Not found go to the next Child.
  144.    lChildWndhdl = GetWindow(lChildWndhdl, GW_HWNDNEXT)
  145.  
  146. Loop
  147.  
  148. End Function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement