- UID
- 14
- 积分
- 8264
- 精华
- 贡献
-
- 威望
-
- 活跃度
-
- D豆
-
- 在线时间
- 小时
- 注册时间
- 2002-1-4
- 最后登录
- 1970-1-1
|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
×
- Option Explicit
- ' THE API STRIKES AGAIN
- '@~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~@
- ' A Painful introduction into using the
- ' The WIN32API to fill in the gaps between
- ' VBA and Visual Basic.
- ' These are TYPES - Like an Array, but each
- ' Element can be a different data type.
- Private Type MENUITEMINFO
- cbSize As Long
- fMask As Long
- fType As Long
- fState As Long
- wID As Long
- hSubMenu As Long
- hbmpChecked As Long
- hbmpUnchecked As Long
- dwItemData As Long
- dwTypeData As String
- cch As Long
- End Type
- Private Type POINTAPI
- x As Long
- y As Long
- End Type
- ' These are Functions in Windows Dynamic Link
- ' Libraries. By using the syntax you see here,
- ' We use these functions in our VBA projects
- ' Perhaps the most important thing to remember
- ' When you start experimenting with this code:
- ' SAVE - SAVE - SAVE
- Private Declare Function GetCursorPos Lib "user32" _
- (lpPoint As POINTAPI) As Long
- Private Declare Function CreatePopupMenu Lib _
- "user32" () As Long
- Private Declare Function TrackPopupMenuEx Lib "user32" _
- (ByVal hMenu As Long, ByVal un As Long, _
- ByVal n1 As Long, ByVal n2 As Long, _
- ByVal hwnd As Long, lpTPMParams As Any) As Long
- Private Declare Function InsertMenuItem Lib "user32" _
- Alias "InsertMenuItemA" (ByVal hMenu As Long, _
- ByVal un As Long, ByVal bool As Long, _
- lpcMenuItemInfo As MENUITEMINFO) As Long
- Private Declare Function DestroyMenu Lib "user32" _
- (ByVal hMenu As Long) As Long
- Private Declare Function FindWindow Lib "user32" _
- Alias "FindWindowA" (ByVal lpClassName As String, _
- ByVal lpWindowName As String) As Long
- ' These are constants (Fixed values) used in
- ' Calls to the functions listed above.
- Private Const MF_STRING = &H0&
- Private Const TPM_RETURNCMD = &H100&
- Private Const MIIM_ID = &H2
- Private Const MIIM_TYPE = &H10
- Private Const MIIM_DATA = &H20
- ' These are variables used in the
- ' Events and procedures..
- Dim lngMnu As Long
- Dim lngHwnd As Long
- Dim lngID As Long
- Dim PT As POINTAPI
- Dim objMNU As MENUITEMINFO
- 'And the Events Begin!
- Private Sub UserForm_Initialize()
- Dim strArray(1 To 3) As String
- Dim lngCnt As Long
- strArray(1) = "Cut"
- strArray(2) = "Copy"
- strArray(3) = "Paste"
- lngMnu = CreatePopupMenu()
- lngHwnd = FindWindow(vbNullString, Me.Caption)
- For lngCnt = 1 To 3
- With objMNU
- .cbSize = Len(objMNU)
- .fMask = MIIM_TYPE Or MIIM_ID Or MIIM_DATA
- .dwTypeData = strArray(lngCnt)
- .cch = Len(strArray(lngCnt))
- .fType = MF_STRING
- .wID = lngCnt
- End With
- Call InsertMenuItem(lngMnu, lngCnt, 1, objMNU)
- Next lngCnt
- End Sub
- Private Sub TextBox1_MouseUp(ByVal Button As Integer, _
- ByVal Shift As Integer, ByVal x As Single, ByVal y As Single)
- If Button = 2 Then
- GetCursorPos PT
- lngID = TrackPopupMenuEx(lngMnu, TPM_RETURNCMD, PT.x, _
- PT.y, lngHwnd, ByVal 0&)
- Select Case lngID
- Case 1
- TextBox1.Cut
- Case 2
- TextBox1.Copy
- Case 3
- TextBox1.Paste
- End Select
- End If
- End Sub
- Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
- Call DestroyMenu(lngMnu)
- End Sub
|
|