Creating Context Menu / 创建上下文菜单项 / VC++, Wind

作者:杏彩

创制Logo标识 Handlers (续)

开创上下文菜单项 

 

 

1、新建二个ATL Project。

1、新建多个ATL Project。

图片 1

图片 2

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

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

图片 3

 图片 4

图片 5

图片 6

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

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

图片 7

 图片 8

图片 9

图片 10

4、增添完毕后建议 Build 一下 Project,MIDL compiler 将基于 .idl 文件生成 IIDs and CLSIDs。

4、增多实现后建议Build一下Project,MIDL compiler将依据 .idl文件生成IIDs and CLSIDs。

图片 11

 图片 12

5、切换来新增加 Class 的 .h 文件中,使其继续接口 IShellIconOverlayIdentifier。

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

图片 13

 图片 14

图片 15

6、切换成新增加Class的 .h文件中,使其承继袭口IShellExtInit和IContextMenu。并在 .cpp文件中,参照MSDN给出完毕。

图片 16

 图片 17

图片 18

图片 19

图片 20图片 21

图片 22

 1 // MyOverlay.h : Declaration of the CMyOverlay
 2 
 3 #pragma once
 4 #include "resource.h"       // main symbols
 5 
 6 
 7 
 8 #include "Example_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 // CMyOverlay
21 
22 class ATL_NO_VTABLE CMyOverlay :
23     public CComObjectRootEx<CComSingleThreadModel>,
24     public CComCoClass<CMyOverlay, &CLSID_MyOverlay>,
25     public IDispatchImpl<IMyOverlay, &IID_IMyOverlay, &LIBID_ExampleLib, 
26     /*wMajor =*/ 1, /*wMinor =*/ 0>,
27     public IShellIconOverlayIdentifier
28 {
29 public:
30     CMyOverlay()
31     {
32     }
33 
34 DECLARE_REGISTRY_RESOURCEID(IDR_MYOVERLAY)
35 
36 
37 BEGIN_COM_MAP(CMyOverlay)
38     COM_INTERFACE_ENTRY(IMyOverlay)
39     COM_INTERFACE_ENTRY(IDispatch)
40     COM_INTERFACE_ENTRY(IShellIconOverlayIdentifier)
41 END_COM_MAP()
42 
43 
44 
45     DECLARE_PROTECT_FINAL_CONSTRUCT()
46 
47 
48     HRESULT FinalConstruct()
49     {
50         return S_OK;
51     }
52 
53     void FinalRelease()
54     {
55     }
56 
57 public:
58     STDMETHOD(IsMemberOf)(THIS_ _In_ PCWSTR pwszPath, DWORD dwAttrib);
59     STDMETHOD(GetOverlayInfo)(THIS_ _Out_writes_(cchMax) PWSTR pwszIconFile,
60         int cchMax, _Out_ int * pIndex, _Out_ DWORD * pdwFlags);
61     STDMETHOD(GetPriority)(THIS_ _Out_ int * pIPriority);
62 
63 };
64 
65 OBJECT_ENTRY_AUTO(__uuidof(MyOverlay), CMyOverlay)

图片 23

MyOverlay.h

图片 24图片 25

6、根据 MSDN 实现该 Class。

 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)

图片 26

MyContextMenu.h

本文由杏彩发布,转载请注明来源

关键词: