找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 435|回复: 0

[转贴]:MENUS

[复制链接]

已领礼包: 593个

财富等级: 财运亨通

发表于 2003-11-28 13:25:19 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×

  1. Option Explicit

  2. '          THE API STRIKES AGAIN
  3. '@~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~@
  4. '   A Painful introduction into using the
  5. '   The WIN32API to fill in the gaps between
  6. '   VBA and Visual Basic.

  7. '   These are TYPES - Like an Array, but each
  8. '   Element can be a different data type.

  9. Private Type MENUITEMINFO
  10.     cbSize As Long
  11.     fMask As Long
  12.     fType As Long
  13.     fState As Long
  14.     wID As Long
  15.     hSubMenu As Long
  16.     hbmpChecked As Long
  17.     hbmpUnchecked As Long
  18.     dwItemData As Long
  19.     dwTypeData As String
  20.     cch As Long
  21. End Type

  22. Private Type POINTAPI
  23.   x As Long
  24.   y As Long
  25. End Type

  26. '   These are Functions in Windows Dynamic Link
  27. '   Libraries. By using the syntax you see here,
  28. '   We use these functions in our VBA projects
  29. '   Perhaps the most important thing to remember
  30. '   When you start experimenting with this code:
  31. '   SAVE - SAVE - SAVE

  32. Private Declare Function GetCursorPos Lib "user32" _
  33. (lpPoint As POINTAPI) As Long

  34. Private Declare Function CreatePopupMenu Lib _
  35. "user32" () As Long

  36. Private Declare Function TrackPopupMenuEx Lib "user32" _
  37. (ByVal hMenu As Long, ByVal un As Long, _
  38. ByVal n1 As Long, ByVal n2 As Long, _
  39. ByVal hwnd As Long, lpTPMParams As Any) As Long

  40. Private Declare Function InsertMenuItem Lib "user32" _
  41. Alias "InsertMenuItemA" (ByVal hMenu As Long, _
  42. ByVal un As Long, ByVal bool As Long, _
  43. lpcMenuItemInfo As MENUITEMINFO) As Long

  44. Private Declare Function DestroyMenu Lib "user32" _
  45. (ByVal hMenu As Long) As Long

  46. Private Declare Function FindWindow Lib "user32" _
  47. Alias "FindWindowA" (ByVal lpClassName As String, _
  48. ByVal lpWindowName As String) As Long

  49. '   These are constants (Fixed values) used in
  50. '   Calls to the functions listed above.
  51. Private Const MF_STRING = &H0&
  52. Private Const TPM_RETURNCMD = &H100&
  53. Private Const MIIM_ID = &H2
  54. Private Const MIIM_TYPE = &H10
  55. Private Const MIIM_DATA = &H20

  56. '   These are variables used in the
  57. '   Events and procedures..
  58. Dim lngMnu As Long
  59. Dim lngHwnd As Long
  60. Dim lngID As Long
  61. Dim PT As POINTAPI
  62. Dim objMNU As MENUITEMINFO

  63. 'And the Events Begin!

  64. Private Sub UserForm_Initialize()
  65.   Dim strArray(1 To 3) As String
  66.   Dim lngCnt As Long
  67.   strArray(1) = "Cut"
  68.   strArray(2) = "Copy"
  69.   strArray(3) = "Paste"
  70.   lngMnu = CreatePopupMenu()
  71.   lngHwnd = FindWindow(vbNullString, Me.Caption)
  72.   For lngCnt = 1 To 3
  73.   With objMNU
  74.     .cbSize = Len(objMNU)
  75.     .fMask = MIIM_TYPE Or MIIM_ID Or MIIM_DATA
  76.     .dwTypeData = strArray(lngCnt)
  77.     .cch = Len(strArray(lngCnt))
  78.     .fType = MF_STRING
  79.     .wID = lngCnt
  80.   End With
  81.   Call InsertMenuItem(lngMnu, lngCnt, 1, objMNU)
  82.   Next lngCnt
  83. End Sub

  84. Private Sub TextBox1_MouseUp(ByVal Button As Integer, _
  85. ByVal Shift As Integer, ByVal x As Single, ByVal y As Single)
  86.   If Button = 2 Then
  87.     GetCursorPos PT
  88.     lngID = TrackPopupMenuEx(lngMnu, TPM_RETURNCMD, PT.x, _
  89.     PT.y, lngHwnd, ByVal 0&)
  90.     Select Case lngID
  91.     Case 1
  92.       TextBox1.Cut
  93.     Case 2
  94.       TextBox1.Copy
  95.     Case 3
  96.       TextBox1.Paste
  97.     End Select
  98.   End If
  99. End Sub

  100. Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
  101.   Call DestroyMenu(lngMnu)
  102. End Sub
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|申请友链|Archiver|手机版|小黑屋|辽公网安备|晓东CAD家园 ( 辽ICP备15016793号 )

GMT+8, 2024-11-22 23:52 , Processed in 0.183128 second(s), 32 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表