rivate Declare Function GetSubMenu Lib "user32" (ByVal hMenu As Long, ByVal nPos As Long) As Long Private Declare Function GetMenu Lib "user32" (ByVal hwnd As Long) As Long Private Declare Function SetMenuItemBitmaps Lib "user32" (ByVal hMenu As Long, ByVal nPosition As Long, ByVal wFlags As Long, ByVal hBitmapUnchecked As Long, ByVal hBitmapChecked As Long) As Long Const MF_BYPOSITION = &H400&Private Sub Form_Load() mhandle = GetMenu(Form1.hwnd) 'MsgBox mhandle 取得窗体菜单句柄 shandle = GetSubMenu(mhandle, 0) ''这里是第一项菜单的句柄 'MsgBox shandle 取得菜单项句柄 iret = SetMenuItemBitmaps(shandle, 0, MF_BYPOSITION, Image1.Picture, Image1.Picture) 'MsgBox iret 设置相应菜单图标,下面的子菜单iret = SetMenuItemBitmaps(shandle, 1, MF_BYPOSITION, Image2.Picture, Image2.Picture) MsgBox iret End Sub 上面的msgbox 做测试用 下面的函数是对菜单
在注册表父项新建一个名为[Icon]的[字符串值],值为图标文件路径和文件名。 比如: 右键单击文件夹时的在[CMD 窗口中打开]的图标就是Shell32.dll里面的第三个图标。Windows Registry Editor Version 5.00[HKEY_CLASSES_ROOT\Folder\shell\在 CMD 窗口中打开] "Icon"="%systemroot%\\system32\\shell32.dll,2"[HKEY_CLASSES_ROOT\Folder\shell\在 CMD 窗口中打开\command] @="cmd.exe /k cd %1"
Private Declare Function GetMenu Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function SetMenuItemBitmaps Lib "user32" (ByVal hMenu As Long, ByVal nPosition As Long, ByVal wFlags As Long, ByVal hBitmapUnchecked As Long, ByVal hBitmapChecked As Long) As Long
Const MF_BYPOSITION = &H400&Private Sub Form_Load()
mhandle = GetMenu(Form1.hwnd)
'MsgBox mhandle 取得窗体菜单句柄
shandle = GetSubMenu(mhandle, 0) ''这里是第一项菜单的句柄
'MsgBox shandle 取得菜单项句柄
iret = SetMenuItemBitmaps(shandle, 0, MF_BYPOSITION, Image1.Picture, Image1.Picture)
'MsgBox iret 设置相应菜单图标,下面的子菜单iret = SetMenuItemBitmaps(shandle, 1, MF_BYPOSITION, Image2.Picture, Image2.Picture)
MsgBox iret
End Sub
上面的msgbox 做测试用
下面的函数是对菜单
比如:
右键单击文件夹时的在[CMD 窗口中打开]的图标就是Shell32.dll里面的第三个图标。Windows Registry Editor Version 5.00[HKEY_CLASSES_ROOT\Folder\shell\在 CMD 窗口中打开]
"Icon"="%systemroot%\\system32\\shell32.dll,2"[HKEY_CLASSES_ROOT\Folder\shell\在 CMD 窗口中打开\command]
@="cmd.exe /k cd %1"
//创建子菜单
HMenu submenu = Helpers.CreatePopupMenu();
Helpers.AppendMenu(submenu, MFMENU.MF_STRING, new IntPtr(idCmdFirst + id++), "添加的子菜单"); //将子菜单插入到上下文菜单中
Helpers.InsertMenu(hMenu, 1, MFMENU.MF_BYPOSITION | MFMENU.MF_POPUP, submenu.handle, "添加的主菜单"); //为菜单增加图标
Bitmap bpCopy = Resource1.Newegg_Virtual_Driver;
Helpers.SetMenuItemBitmaps(submenu, 0, MFMENU.MF_BYPOSITION, bpCopy.GetHbitmap(), bpCopy.GetHbitmap());
internal static extern bool InsertMenuItem(HMenu hmenu, uint uposition, uint uflags, ref MENUITEMINFO mii);
[DllImport("user32")]
internal static extern bool AppendMenu(HMenu hmenu, MFMENU uflags, IntPtr uIDNewItemOrSubmenu, string text);
[DllImport("user32")]
internal static extern int SetMenuItemBitmaps(HMenu hmenu, int nPosition, MFMENU uflags, IntPtr hBitmapUnchecked, IntPtr hBitmapChecked);2.调用申明的函数添加上下文菜单和图标
//创建子菜单
HMenu submenu = Helpers.CreatePopupMenu();
AppendMenu(submenu, MFMENU.MF_STRING, new IntPtr(idCmdFirst + id++), "review public url");
//将子菜单插入到上下文菜单中
InsertMenu(hMenu, 1, MFMENU.MF_BYPOSITION | MFMENU.MF_POPUP, submenu.handle, "Virtual Driver") //为菜单增加图标
Bitmap bpCopy = Resource1.VirtualDriver;
SetMenuItemBitmaps(submenu, 0, MFMENU.MF_BYPOSITION, bpCopy.GetHbitmap(), bpCopy.GetHbitmap());
SetMenuItemBitmaps(hMenu, 1, MFMENU.MF_BYPOSITION, bpCopy.GetHbitmap(), bpCopy.GetHbitmap());