|
用一下这段代码已经可以实现给窗体textbox添加右键菜单,但存在bug,每次点选菜单就会自动的更改小键盘的NmLK状态,请高手帮忙看一下问题出在哪儿。在网上搜遍了右键菜单的代码,也就这段好比较好用,就是这个bug暂时还解决不了。
- Sub ShowBar()
- Dim oCmdBar As CommandBar
- Dim oCtrl As CommandBarControl
- On Error Resume Next
- CommandBars("txtBar").Delete
- On Error GoTo 0
- Set oCmdBar = CommandBars.Add(Name:="txtBar", Position:=msoBarPopup, Temporary:=True)
- With oCmdBar
- Set oCtrl = .Controls.Add(Type:=msoControlButton)
- With oCtrl
- .Caption = "复 制 Ctrl+C"
- .OnAction = "Macro1"
- End With
- Set oCtrl = .Controls.Add(Type:=msoControlButton)
- With oCtrl
- .Caption = "剪 切 Ctrl+X"
- .OnAction = "Macro2"
- End With
- Set oCtrl = .Controls.Add(Type:=msoControlButton)
- With oCtrl
- .Caption = "粘 贴 Ctrl+V"
- .OnAction = "Macro3"
- End With
- Set oCtrl = .Controls.Add(Type:=msoControlButton)
- With oCtrl
- .Caption = "清 除 Del"
- .OnAction = "Macro4"
- End With
-
- End With
- oCmdBar.ShowPopup
- End Sub
- Sub Macro1()
- Application.SendKeys "^c", True
- End Sub
- Sub Macro2()
- Application.SendKeys "^x", True
- End Sub
- Sub Macro3()
- Application.SendKeys "^v", True
- End Sub
- Sub Macro4()
- Application.SendKeys "{del}", True
- End Sub
- Private Sub TextBox1_MouseUp(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
- If Button = xlSecondaryButton Then
- ShowBar
- CommandBars("txtBar").Delete
- End If
- End Sub
复制代码
|
|