免责声明:我用过这个 所以。我尝试用VB6向另一个程序添加弹出菜单 我的记事本测试成功了。然后我用 Paint 尝试了相同的代码,但菜单项呈灰色,即使我尝试了很多方法来启用菜单。
这是代码:
Option Explicit
Const MENUID = 56 'our unique identifier
Dim hWndSubMenu As Long
'Forgot these declares in a previous post
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 Const MIIM_STATE = &H1
Private Const MIIM_ID = &H2
Private Const MIIM_STRING = &H40
Private Const MIIM_FTYPE = &H100
Private Const MFT_SEPARATOR = &H800
Private Const MFT_STRING = &H0
Private Const MFS_ENABLED = &H0
Private Const MFS_CHECKED = &H8
Private Const WM_COMMAND = &H111
Private Const MF_BYCOMMAND = &H0&
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" ( _
ByVal lpClassName As String, _
ByVal lpWindowName As String _
) As Long
Private Declare Function GetMenu Lib "user32" ( _
ByVal hwnd As Long _
) As Long
Private Declare Function GetSubMenu Lib "user32" ( _
ByVal hMenu As Long, _
ByVal nPos As Long _
) As Long
Private Declare Function RemoveMenu Lib "user32" ( _
ByVal hMenu As Long, _
ByVal nPosition As Long, _
ByVal wFlags As Long _
) As Long
Private Declare Function GetMenuItemCount Lib "user32.dll" ( _
ByVal hMenu As Long _
) As Long
Private Declare Function InsertMenuItem Lib "user32.dll" Alias "InsertMenuItemA" ( _
ByVal hMenu As Long, _
ByVal uItem As Long, _
ByVal fByPosition As Long, _
lpmii As MENUITEMINFO _
) As Long
Private Declare Function DrawMenuBar Lib "user32" ( _
ByVal hwnd As Long _
) As Long
Private Sub Command1_Click()
Dim hWndNotepad As Long
Dim hWndMenu As Long
Dim count As Long
Dim mii As MENUITEMINFO
Dim appCaption As String
'Get a handle to Notepad
appCaption = "untitled - Paint"
hWndNotepad = FindWindow(vbNullString, appCaption)
While hWndNotepad = 0
If MsgBox("Open Notepad", vbOKCancel) = vbCancel Then Exit Sub
hWndNotepad = FindWindow(vbNullString, appCaption)
Wend
'Get handle to Notepad's main menu
hWndMenu = GetMenu(hWndNotepad)
'Get handle to 'File' submenu
hWndSubMenu = GetSubMenu(hWndMenu, 5)
'Get number of items in sub menu
count = GetMenuItemCount(hWndSubMenu)
'Setup data structure for InsertMenuItem call
With mii
.cbSize = Len(mii)
.fMask = MIIM_STATE Or MIIM_ID Or MIIM_STRING Or MIIM_FTYPE
.fType = MFT_STRING
.fState = &H0
.wID = MENUID 'our unique identifier
.dwTypeData = "My New Menu"
.cch = Len(.dwTypeData)
End With
' Add this to the menu.
Call InsertMenuItem(hWndSubMenu, count + 1, 1, mii)
Call DrawMenuBar(hWndNotepad)
'Now set up hook, we want to monitor the WM_COMMAND message
With Hook
.TargethWnd = hWndNotepad
.AddMessage WM_COMMAND, "WM_COMMAND"
.SetHook
End With
End Sub
Private Sub Form_Unload(Cancel As Integer)
'remove our hook
Hook.RemoveAllHooks
'remove our menu item
Call RemoveMenu(hWndSubMenu, MENUID, MF_BYCOMMAND)
End Sub
Private Sub Hook_PostedMessage(uMsg As Long, wParam As Long, lParam As Long)
'here is where our messages will arrive
'look for our special menu ID
If (wParam And &HFFFF) = MENUID Then
'our menu item has been selected
MsgBox "Why did you do that?"
End If
End Sub
我尝试使用 FState 和 EnableMenuItem 函数以不同的方式启用菜单。但没有一个起作用。菜单呈灰色