VBGood网站全文搜索 Google

首页 - 经验之谈 - 在可编辑控件中创建一个新的上下文菜单
发表评论(0)作者:Antonio Almeida, 平台:VB6.0+Win98, 阅读:8697, 日期:2000-10-17
This routine will permit you to replace the original context menu with your private context menu in an editable control.

Add the following code to your form or to a BAS module:

Private Const WM_RBUTTONDOWN = &H204 Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long

Public Sub OpenContextMenu(FormName As Form, MenuName As Menu)

  注释:Tell system we did a right-click on the mdi
  Call SendMessage(FormName.hwnd, WM_RBUTTONDOWN, 0, 0&)
  注释:Show my context menu
  FormName.PopupMenu MenuName
End Sub

Next, use the Visual Basic Menu Editor and the table below to create a simple menu.


Context Menu

...First Item
...Second Item

Note that the last two items in the menu are indented (...) one level and that only the first item in the list ("Context Menu") has the Visible property set to NO.

Now add a text box to your form and enter the code below in the MouseDown event of the text box.

Private Sub Text1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)

  If Button = vbRightButton Then
    Call OpenContextMenu(Me, Me.mnuContext)
  End If

End Sub

Note: If you just want to kill the system context menu, just comment out the line:
FormName.PopupMenu MenuName
in the OpenContextMenu routine.