Creating Context Menu / 创建上下文菜单项 / Win32, VC++, Windows, DLL, ATL, COM

创建上下文菜单项

1、新建一个ATL Project。

2、建议将Project Property中Linker – General - “Register Output” 设为no,C/C++ - “Code Generation” - “Runtime Library” 设为 /MTd。

3、在Solution Explorer中右键Add Class,选择ATL Simple Object。并在弹出的对话框中为该Class命名。

4、添加完成后建议Build一下Project,MIDL compiler将根据 .idl文件生成IIDs and CLSIDs。

5、(可选)在Solution Explorer中右键Add Resource导入图标资源。

6、切换到新增Class的 .h文件中,使其继承接口IShellExtInit和IContextMenu。并在 .cpp文件中,参照MSDN给出实现。

 1 // MyContextMenu.h : Declaration of the CMyContextMenu
 2
 3 #pragma once
 4 #include "resource.h"       // main symbols
 5
 6
 7
 8 #include "ContextMenuExample_i.h"
 9 #include <Shlobj.h>
10
11
12
13 #if defined(_WIN32_WCE) && !defined(_CE_DCOM) && !defined(_CE_ALLOW_SINGLE_THREADED_OBJECTS_IN_MTA)
14 #error "Single-threaded COM objects are not properly supported on Windows CE platform, such as the Windows Mobile platforms that do not include full DCOM support. Define _CE_ALLOW_SINGLE_THREADED_OBJECTS_IN_MTA to force ATL to support creating single-thread COM object‘s and allow use of it‘s single-threaded COM object implementations. The threading model in your rgs file was set to ‘Free‘ as that is the only threading model supported in non DCOM Windows CE platforms."
15 #endif
16
17 using namespace ATL;
18
19
20 // CMyContextMenu
21
22 class ATL_NO_VTABLE CMyContextMenu :
23     public CComObjectRootEx<CComSingleThreadModel>,
24     public CComCoClass<CMyContextMenu, &CLSID_MyContextMenu>,
25     public IDispatchImpl<IMyContextMenu, &IID_IMyContextMenu,
26     &LIBID_ContextMenuExampleLib, /*wMajor =*/ 1, /*wMinor =*/ 0>,
27     public IShellExtInit,
28     public IContextMenu
29 {
30 public:
31     CMyContextMenu()
32     {
33     }
34
35 DECLARE_REGISTRY_RESOURCEID(IDR_MYCONTEXTMENU)
36
37
38 BEGIN_COM_MAP(CMyContextMenu)
39     COM_INTERFACE_ENTRY(IMyContextMenu)
40     COM_INTERFACE_ENTRY(IDispatch)
41     COM_INTERFACE_ENTRY(IShellExtInit)
42     COM_INTERFACE_ENTRY(IContextMenu)
43 END_COM_MAP()
44
45
46
47     DECLARE_PROTECT_FINAL_CONSTRUCT()
48
49     HRESULT FinalConstruct();
50
51     void FinalRelease();
52
53 public:
54     // IShellExtInit Method
55     HRESULT STDMETHODCALLTYPE Initialize(
56         _In_opt_  PCIDLIST_ABSOLUTE pidlFolder,
57         _In_opt_  IDataObject *pdtobj,
58         _In_opt_  HKEY hkeyProgID);
59
60     // IContextMenu Method
61     HRESULT STDMETHODCALLTYPE QueryContextMenu(
62         _In_  HMENU hmenu,
63         _In_  UINT indexMenu,
64         _In_  UINT idCmdFirst,
65         _In_  UINT idCmdLast,
66         _In_  UINT uFlags);
67
68     HRESULT STDMETHODCALLTYPE InvokeCommand(
69         _In_  CMINVOKECOMMANDINFO *pici);
70
71     HRESULT STDMETHODCALLTYPE GetCommandString(
72         _In_  UINT_PTR idCmd,
73         _In_  UINT uType,
74         _Reserved_  UINT *pReserved,
75         _Out_writes_bytes_((uType & GCS_UNICODE) ? (cchMax * sizeof(wchar_t)) : cchMax) _When_(!(uType & (GCS_VALIDATEA | GCS_VALIDATEW)), _Null_terminated_)  CHAR *pszName,
76         _In_  UINT cchMax);
77
78 private:
79     HBITMAP MenuIcon1;
80     HBITMAP MenuIcon2;
81     HBITMAP MenuIcon3;
82     HBITMAP MenuIcon4;
83
84
85 };
86
87 OBJECT_ENTRY_AUTO(__uuidof(MyContextMenu), CMyContextMenu)

MyContextMenu.h

  1 // MyContextMenu.cpp : Implementation of CMyContextMenu
  2
  3 #include "stdafx.h"
  4 #include "MyContextMenu.h"
  5
  6
  7 // CMyContextMenu
  8
  9 HRESULT CMyContextMenu::FinalConstruct()
 10 {
 11     HINSTANCE hInstance = _AtlBaseModule.GetModuleInstance();
 12     MenuIcon1 = LoadBitmap(hInstance, MAKEINTRESOURCE(IDB_BITMAP1));
 13     MenuIcon2 = LoadBitmap(hInstance, MAKEINTRESOURCE(IDB_BITMAP2));
 14     MenuIcon3 = LoadBitmap(hInstance, MAKEINTRESOURCE(IDB_BITMAP3));
 15     MenuIcon4 = LoadBitmap(hInstance, MAKEINTRESOURCE(IDB_BITMAP4));
 16
 17     return S_OK;
 18 }
 19
 20 void CMyContextMenu::FinalRelease()
 21 {
 22     if (MenuIcon1 != NULL)
 23     {
 24         DeleteObject(MenuIcon1);
 25     }
 26     if (MenuIcon2 != NULL)
 27     {
 28         DeleteObject(MenuIcon2);
 29     }
 30     if (MenuIcon3 != NULL)
 31     {
 32         DeleteObject(MenuIcon3);
 33     }
 34     if (MenuIcon4 != NULL)
 35     {
 36         DeleteObject(MenuIcon4);
 37     }
 38 }
 39
 40 HRESULT CMyContextMenu::Initialize(
 41     _In_opt_  PCIDLIST_ABSOLUTE pidlFolder,
 42     _In_opt_  IDataObject *pdtobj,
 43     _In_opt_  HKEY hkeyProgID) {
 44     HRESULT hr;
 45     UINT    nFileCount;
 46
 47     FORMATETC fmt =
 48     {
 49         CF_HDROP,
 50         NULL,
 51         DVASPECT_CONTENT,
 52         -1,
 53         TYMED_HGLOBAL
 54     };
 55
 56     STGMEDIUM sm =
 57     {
 58         TYMED_HGLOBAL
 59     };
 60
 61     hr = pdtobj->GetData(&fmt, &sm);
 62
 63     if (FAILED(hr))
 64     {
 65         return hr;
 66     }
 67
 68     // query quantity of selected files
 69     nFileCount = DragQueryFile((HDROP)sm.hGlobal, 0xFFFFFFFF, NULL, 0);
 70
 71     if (nFileCount == 1) // deal with only one file
 72     {
 73         // analyze selected file
 74
 75     }
 76     else
 77     {
 78         hr = E_INVALIDARG;
 79     }
 80
 81     ReleaseStgMedium(&sm);
 82
 83     return hr;
 84 }
 85
 86 // IContextMenu Method
 87 HRESULT CMyContextMenu::QueryContextMenu(
 88     _In_  HMENU hmenu,
 89     _In_  UINT indexMenu,
 90     _In_  UINT idCmdFirst,
 91     _In_  UINT idCmdLast,
 92     _In_  UINT uFlags) {
 93
 94     UINT uCmdID = idCmdFirst;
 95     LPCWSTR text1 = TEXT("新增层叠菜单项1");
 96     LPCWSTR text2 = TEXT("新增菜单项2");
 97     LPCWSTR text3 = TEXT("新增菜单项3");
 98     LPCWSTR text4 = TEXT("新增菜单项4");
 99     // do nothing when flag includes CMF_DEFAULTONLY.
100     if (uFlags & CMF_DEFAULTONLY)
101     {
102         return MAKE_HRESULT(SEVERITY_SUCCESS, FACILITY_NULL, 0);
103     }
104     InsertMenu(hmenu, indexMenu, MF_SEPARATOR | MF_BYPOSITION, 0, NULL);
105     indexMenu++;
106     HMENU hSubMenu = CreateMenu();
107     if (hSubMenu)
108     {
109         InsertMenu(hSubMenu, 0, MF_STRING | MF_BYPOSITION, uCmdID++, text2);
110         SetMenuItemBitmaps(hSubMenu, 0, MF_BYPOSITION, MenuIcon2, MenuIcon2);
111         InsertMenu(hSubMenu, 1, MF_STRING | MF_BYPOSITION, uCmdID++, text3);
112         SetMenuItemBitmaps(hSubMenu, 1, MF_BYPOSITION, MenuIcon3, MenuIcon3);
113         InsertMenu(hSubMenu, 2, MF_STRING | MF_BYPOSITION, uCmdID++, text4);
114         SetMenuItemBitmaps(hSubMenu, 2, MF_BYPOSITION, MenuIcon4, MenuIcon4);
115         // InsertMenu(hSubMenu, 3, MF_SEPARATOR | MF_BYPOSITION, 0, NULL);//插入分隔线
116     }
117     InsertMenu(hmenu, indexMenu, MF_STRING | MF_POPUP | MF_BYPOSITION, (UINT_PTR)hSubMenu, text1);
118     SetMenuItemBitmaps(hmenu, indexMenu, MF_BYPOSITION, MenuIcon1, MenuIcon1);
119     indexMenu++;
120     InsertMenu(hmenu, indexMenu, MF_SEPARATOR | MF_BYPOSITION, 0, NULL);
121     indexMenu++;
122
123     // inform the explorer how many menu item we have added
124     return MAKE_HRESULT(SEVERITY_SUCCESS, FACILITY_NULL, uCmdID - idCmdFirst);
125 }
126
127 HRESULT CMyContextMenu::InvokeCommand(
128     _In_  CMINVOKECOMMANDINFO *pici) {
129     if (0 != HIWORD(pici->lpVerb))
130         return E_INVALIDARG;
131     // get index of added menu item
132     switch (LOWORD(pici->lpVerb))
133     {
134     case 0:
135     {
136         // 执行新增菜单项2触发的操作
137         STARTUPINFO si = { sizeof(si) };
138         PROCESS_INFORMATION pi;
139         TCHAR szCommandLine[] = TEXT("notepad");
140         BOOL bCreateRet = CreateProcess(NULL, szCommandLine, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
141
142         break;
143     }
144     case 1:
145     {
146         // 执行新增菜单项3触发的操作
147         STARTUPINFO si = { sizeof(si) };
148         PROCESS_INFORMATION pi;
149         TCHAR szCommandLine[] = TEXT("write");
150         BOOL bCreateRet = CreateProcess(NULL, szCommandLine, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
151
152         break;
153     }
154     case 2:
155     {
156         // 执行新增菜单项4触发的操作
157         STARTUPINFO si = { sizeof(si) };
158         PROCESS_INFORMATION pi;
159         TCHAR szCommandLine[] = TEXT("cmd");
160         BOOL bCreateRet = CreateProcess(NULL, szCommandLine, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
161
162         break;
163     }
164     default:
165     {
166         return E_INVALIDARG;
167         break;
168     }
169     }
170     return S_OK;
171 }
172
173 HRESULT CMyContextMenu::GetCommandString(
174     _In_  UINT_PTR idCmd,
175     _In_  UINT uType,
176     _Reserved_  UINT *pReserved,
177     _Out_writes_bytes_((uType & GCS_UNICODE) ? (cchMax * sizeof(wchar_t)) : cchMax) _When_(!(uType & (GCS_VALIDATEA | GCS_VALIDATEW)), _Null_terminated_)  CHAR *pszName,
178     _In_  UINT cchMax) {
179     USES_CONVERSION;
180     LPCTSTR szPrompt;
181     // copy help info to cache when explorer ask
182     if (uType & GCS_HELPTEXT)
183     {
184         switch (idCmd)
185         {
186         case 0:
187             szPrompt = _T("新增菜单项2说明文字");
188             break;
189         case 1:
190             szPrompt = _T("新增菜单项3说明文字");
191             break;
192         case 2:
193             szPrompt = _T("新增菜单项4说明文字");
194             break;
195         default:
196             //ATLASSERT(0);           // should never get here
197             return E_INVALIDARG;
198             break;
199         }
200         if (uType & GCS_UNICODE)
201         {
202             lstrcpynW((LPWSTR)pszName, T2CW(szPrompt), cchMax);
203         }
204         else
205         {
206             lstrcpynA(pszName, T2CA(szPrompt), cchMax);
207         }
208         return S_OK;
209     }
210     return E_INVALIDARG;
211 }

7、在 .rgs文件中添加注册表信息,确保各GUID与 .idl文件中的一致。

 1 HKCR
 2 {
 3     NoRemove CLSID
 4     {
 5         ForceRemove {9C50C98F-E1FF-41CF-BD54-E9A3BBDDDEF8} = s ‘MyContextMenu Class‘
 6         {
 7             ForceRemove Programmable
 8             InprocServer32 = s ‘%MODULE%‘
 9             {
10                 val ThreadingModel = s ‘Apartment‘
11             }
12             TypeLib = s ‘{EB1C2F43-315D-4D8F-9A2A-70E67BE888E2}‘
13             Version = s ‘1.0‘
14         }
15     }
16
17     NoRemove *
18     {
19         NoRemove ShellEx
20         {
21             NoRemove ContextMenuHandlers
22             {
23                 ForceRemove MyContextMenu = s ‘{9C50C98F-E1FF-41CF-BD54-E9A3BBDDDEF8}‘
24             }
25         }
26     }
27 }

8、Build Project 后打开cmd.exe,通过regsvr32命令注册或解注册生成的 .dll文件。

10、查看效果如下图所示。

——————————————————

本文为本人原创,如需转载请注明出处。

时间: 2024-11-05 09:08:28

Creating Context Menu / 创建上下文菜单项 / Win32, VC++, Windows, DLL, ATL, COM的相关文章

Android开发系列(二十九):使用ContextMenu创建上下文菜单

在上一篇我们介绍了创建选项菜单,这里我们介绍下创建上下文菜单. 上下文菜单就是通过长按某一段文字,然后出来相应的菜单.就比如贴吧,当你长安某一楼层,就会弹出来一个"复制"这一楼层文字的菜单,点一下"复制",这段文字就保存在你手机的临时存储的地方了,可以在别的地方黏贴. 开发上下文菜单的步骤: 1.重写Activity的onCreateContextMenu(ContextMenu menu,View source,ContextMenu.Context MenuIn

Android Dialog 创建上下文菜单

Android Dialog中的listview创建上下文菜单 listView.setOnCreateContextMenuListener(new OnCreateContextMenuListener() { public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { menu.add(0, 0, 0, "天天"); menu.add(0, 1, 0, "人人

ContextMenu菜单创建 上下文菜单的基本认识q

MainActivity.class public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); showlistView(); } private void showlis

Creating Dialogbased Win32 Application (4) / 创建基于对话框的Win32应用程序(四)Edit Control的应用、Unicode转ANSI、自动滚动 / Win32, VC++, Windows

创建基于对话框的Win32应用程序(四)——Edit Control的应用.Unicode转ANSI.自动滚动 之前的介绍中,我们用到了Button.Static Text.Checkbox这三个控件.这一节中我们将学习使用Edit Control(编辑框)控件,其中还包括Unicode转ANSI的方法.文本框自动滚动的功能等. 24.首先切换到Reasource View(Ctrl+Shift+E),找到待修改的主窗体,并从Toolbox(Ctrl+Atl+X)中添加Edit Control控

Creating Dialogbased Win32 Application (1) / 创建基于对话框的Win32应用程序(一)新建窗体 / Win32, VC++, Windows

创建基于对话框的Win32应用程序(一) —— 新建窗体 1.新建一个Visual C++的Empty Project.  2.在Solution Explorer中右键Add New Item,添加 .cpp 文件,并提供Win32应用程序的入口点函数.  3.在Solution Explorer或 Resources View 中右键Add Resource,选择Dialog.并在修改相关内容. 4.切换到 .cpp文件中,创建回调函数(Dlg_Proc),并在入口点函数中调用DialogB

Creating Dialogbased Win32 Application (2) / 创建基于对话框的Win32应用程序(二)Button的应用、新建子窗体 / Win32, VC++, Windows

创建基于对话框的Win32应用程序(二) —— Button的应用.新建子窗体 可以发现上一节创建的窗体中,点击OK和Cancel两个按钮是没有任何反应的.现在我们来为他们添加退出对话框的功能. 6.首先定义宏替换如下: 7.再在回调函数(Dlg_Proc)中调用响应消息的函数(Dlg_OnCommand)如下: 8.由于HANDLE_WM_COMMAND是在windowsx.h头文件中定义的,故添加其引用. 9.接下来定义刚才调用的消息响应函数(Dlg_OnCommand)如下: 其中IDOK

create Context Menu in Windows Forms application using C# z

In this article let us see how to create Context Menu in Windows Forms application using C# Introduction In this article we will see how to create Context Menu or Popup Menu or Shortcut menu in Windows Forms application with ContextMenuStrip control

Android 上下文菜单(Context Menu)

一.概述 Android中,上下文菜单是通过onLongClick(...)事件访问的.在事件触发后显示菜单项. 在使用上下文菜单时,通常在onCreate(...)方法中,先行注册上下文菜单.在实现onCreateContextMenu(...)方法和onContextItemSelected(...)方法. 注册菜单方法:registerForContextMenu(...) 注销注册菜单方法:unregisterForContextMenu(...) 二.使用XML文件添加上下文菜单 te

安卓开发复习笔记——Menu菜单组件(选项菜单,上下文菜单,子菜单)

菜单是用户界面中最常见的元素之一,使用非常频繁,在Android中,菜单被分为如下三种,选项菜单(OptionsMenu).上下文菜单(ContextMenu)和子菜单(SubMenu). 菜单的实现方式有2种:一种是通过布局文件xml生成菜单,另一种是通过代码生成. 三种菜单内容有点多,不过大体相似,一次性讲完吧,本人偏好代码动态生成,下面就以代码为例. 1.选项菜单(OptionsMenu) 先来看下选项菜单的效果图:   在一个Activity界面中点击手机Menu键,在屏幕下方弹出的菜单