3、VS2005(c/c++)外壳扩展编程之windows右键菜单(3)

简介: 第三部分 SHELL Additional  一、编程中的一些关键点  1、 We implemented the IShellExtInit interface, which was how Explorer initialized our object.

第三部分 SHELL Additional 

一、编程中的一些关键点 

1We implemented the IShellExtInit interface, which was how Explorer initialized our object. There is another initialization interface used for some shell extensions, IPersistFile, and this is the one an infotip extension uses. IShellExtInit::Initialize() receives an IDataObject pointer with which it can enumerate all of the files that were selected. Extensions that can only ever operate on a single file use IPersistFile. Since the mouse can't hover over more than one object at a time, an infotip extension only works on one file at a time, so it uses IPersistFile. 

2AFX_MANAGE_STATE(AfxGetStaticModuleState()); // init MFC

The AFX_MANAGE_STATE macro is necessary for MFC to work properly. Because our DLL is being loaded by a non-MFC app, every exported function that uses MFC must initialize MFC manually. If you don't include that line, many MFC functions (mostly the ones related to resources) will break or have assertion failures.

3In order to have a dialog which has the XP theme enabled, it was necessary to set the following values in stdafx.h before any of the #includes:

#define VC_EXTRALEAN //necessary

#ifndef _WIN32_WINNT

#define _WIN32_WINNT 0x501

#endif

#define _ATL_APARTMENT_THREADED

#define ISOLATION_AWARE_ENABLED 1 //XP style awareness

4A shell extension is a COM DLL

5Note that DllRegisterServer() and DllUnregisterServer() are not called by Explorer, but by the installer. If you downloaded the source files instead of the installer, you will have to register the DLL yourself using the command line tool regsvr32. If you compile the source from within Visual C++, then regsvr32 runs as part as the build process.

6、整体而言,使用SHELL时流程基本如下:

In short, this is what happens: When the user right-clicks inside an Explorer window, Explorer calls CtxMenu's Initialize() function. At this point, the context menu isn't visible yet. Explorer then calls the QueryContextMenu() function to add the Select... item to the menu, and shows it. When the user moves over this menu item, Explorer obtains a description from GetCommandString(). Finally, when the user picks the Select... item, Explorer calls InvokeCommand().

The only place where we can figure out whether the user clicked on a file or on the background of the window is in Initialize().

http://www.codeproject.com/KB/shell/wildcardselect.aspx

http://www.allyoursoftware.com/

第四部分 相关的API

1GlobalLock Function

Locks a global memory object and returns a pointer to the first byte of the object's memory block.

2DragQueryFile Function

Retrieves the names of dropped files that result from a successful drag-and-drop operation.

http://msdn.microsoft.com/en-us/library/bb776408%28VS.85%29.aspx

3GetProcAddress Function

Retrieves the address of an exported function or variable from the specified dynamic-link library (DLL).

http://msdn.microsoft.com/en-us/library/ms683212%28VS.85%29.aspx

4InsertMenu Function

Inserts a new menu item into a menu, moving other items down the menu.

Note The InsertMenu function has been superseded by the InsertMenuItem function. You can still use InsertMenu, however, if you do not need any of the extended features of InsertMenuItem.

http://msdn.microsoft.com/en-us/library/ms647987%28VS.85%29.aspx

5SetMenuItemBitmaps Function

Associates the specified bitmap with a menu item. Whether the menu item is selected or clear, the system displays the appropriate bitmap next to the menu item.

http://msdn.microsoft.com/en-us/library/ms647998%28VS.85%29.aspx

6GetCurrentDirectory Function

Retrieves the current directory for the current process.

SetCurrentDirectory

7lstrcpyn Function

Copies a specified number of characters from a source string into a buffer.

Warning Do not use. Consider using StringCchCopy instead. See Remarks.

8CStdioFile::ReadString

Reads text data into a buffer, up to a limit of nMax1 characters, from the file associated with the CStdioFile object.

Reading is stopped by the first newline character. If, in that case, fewer than nMax1 characters have been read, a newline character is stored in the buffer. A null character ('\0') is appended in either case.

http://msdn.microsoft.com/en-us/library/x5t0zfyf%28VS.80%29.aspx

9FindFirstFile Function

Searches a directory for a file or subdirectory with a name that matches a specific name (or partial name if wildcards are used).

To specify additional attributes to use in a search, use the FindFirstFileEx function.

To perform this operation as a transacted operation, use the FindFirstFileTransacted function.

http://msdn.microsoft.com/en-us/library/aa364418%28VS.85%29.aspx

10SHGetPathFromIDList Function

Converts an item identifier list to a file system path.

http://msdn.microsoft.com/en-us/library/bb762194%28VS.85%29.aspx

11GetTopWindow Function

Examines the Z order of the child windows associated with the specified parent window and retrieves a handle to the child window at the top of the Z order.

http://msdn.microsoft.com/en-us/library/ms633514%28VS.85%29.aspx

12HIWORD Macro

Retrieves the high-order word from the specified 32-bit value.

http://msdn.microsoft.com/en-us/library/ms632657%28VS.85%29.aspx

13SHGetMalloc

Retrieves a pointer to the shell's IMalloc interface

12FindNextFile

Continues a file search from a previous call to the FindFirstFile or FindFirstFileEx function.

http://msdn.microsoft.com/en-us/library/aa364428%28VS.85%29.aspx

 

代码
 
  
1 void CShellExtentionToVirC::RecursiveObtainDirFiles(WCHAR * lpPath)
2 {
3 WCHAR szFind[MAX_PATH];
4 WCHAR szFile[MAX_PATH];
5 WIN32_FIND_DATA FindFileData;
6
7 wcscpy(szFind, lpPath);
8 wcscat(szFind, TEXT( " \\*.* " ));
9
10 HANDLE hFind = ::FindFirstFile((LPCWSTR)szFind, & FindFileData);
11   if (INVALID_HANDLE_VALUE == hFind)
12 return ;
13
14 while (TRUE)
15 {
16 if (FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
17 {
18 if (FindFileData.cFileName[ 0 ] != L ' . ' )
19 {
20 wcscpy(szFile, lpPath);
21 wcscat(szFile, TEXT( " \\ " ));
22 wcscat(szFile, FindFileData.cFileName);
23 RecursiveObtainDirFiles(szFile);
24 }
25 }
26 else
27 {
28 std::wcout << FindFileData.cFileName << std::endl;
29 }
30 if ( ! FindNextFile(hFind, & FindFileData))
31 break ;
32 }
33 FindClose(hFind);
34 }
35
36
37 HRESULT CShellExtentionToVirC::InvokeCommand(LPCMINVOKECOMMANDINFO pInfo)
38 {
39 // If lpVerb really points to a string, ignore this function call and bail out.
40 if ( 0 != HIWORD( pInfo -> lpVerb ))
41 return E_INVALIDARG;
42
43 // Check that lpVerb is one of our commands (0 or 1)
44
45 switch ( LOWORD( pInfo -> lpVerb ))
46 {
47 case 0 :
48 case 1 :
49 {
50 TCHAR szMsg[MAX_PATH + 32 ];
51 wsprintf( szMsg, _T( " Only a test " ));
52 MessageBox( pInfo -> hwnd, szMsg, _T( " VirCS " ),MB_ICONINFORMATION );
53
54 strCurrentDirectory = L " C:\\Intel\\Logs " ;
55 RecursiveObtainDirFiles(const_cast < WCHAR *> (strCurrentDirectory.data()));
56 return S_OK;
57 }
58 break ;
59
60 default :
61 return E_INVALIDARG;
62 break ;
63 }
64 }

参考

[1] http://www.codeproject.com/KB/shell/shellextguideindex.aspx

关 于Shell Extension,CodeProject讲解了更多,如drag and drop handler(用右键拖拽时显示的菜单)property sheet handler(在属性页中显示的菜单),icon handler(不同类型文件图标不同)等。

[2] 相关文档下载地址

http://download.csdn.net/source/2878021

[3] http://www.codeproject.com/KB/shell/wildcardselect.aspx

[4] http://msdn.microsoft.com/en-us/library/bb776426%28VS.85%29.aspx

[5] http://blog.csdn.net/luckyboy101/archive/2009/11/25/4866408.aspx

[6] http://www.programbbs.com/

[7] http://www.cnblogs.com/MaxWoods/archive/2010/06/23/1764036.html

[8] http://www.cnblogs.com/MaxWoods/archive/2010/06/23/1764034.html

[9] http://www.cnblogs.com/lemony/archive/2007/04/16/715833.html

[10] http://www.cnblogs.com/lemony/archive/2007/04/16/715833.html

[11] Dino Esposito's great book Visual C++ Windows Shell Programming (ISBN 1861001843)

目录
相关文章
|
3月前
|
存储 算法 C++
Windows共享文件:探秘C++实现的B树索引算法奇境
在数字化时代,Windows共享文件的高效管理至关重要。B树算法以其自平衡多路搜索特性,在文件索引与存储优化中表现出色。本文探讨B树在Windows共享文件中的应用,通过C++实现具体代码,展示其构建文件索引、优化数据存储的能力,提升文件检索效率。B树通过减少磁盘I/O操作,确保查询高效,为企业和个人提供流畅的文件共享体验。
|
9月前
|
存储 C++ UED
【实战指南】4步实现C++插件化编程,轻松实现功能定制与扩展
本文介绍了如何通过四步实现C++插件化编程,实现功能定制与扩展。主要内容包括引言、概述、需求分析、设计方案、详细设计、验证和总结。通过动态加载功能模块,实现软件的高度灵活性和可扩展性,支持快速定制和市场变化响应。具体步骤涉及配置文件构建、模块编译、动态库入口实现和主程序加载。验证部分展示了模块加载成功的日志和配置信息。总结中强调了插件化编程的优势及其在多个方面的应用。
969 127
|
10月前
|
网络协议 API Windows
MASM32编程调用 API函数RtlIpv6AddressToString,windows 10 容易,Windows 7 折腾
MASM32编程调用 API函数RtlIpv6AddressToString,windows 10 容易,Windows 7 折腾
|
10月前
|
Windows
[原创]用MASM32编程获取windows类型
[原创]用MASM32编程获取windows类型
|
10月前
|
JavaScript 前端开发 API
MASM32编程通过WMI获取Windows计划任务
MASM32编程通过WMI获取Windows计划任务
|
10月前
|
存储 设计模式 编译器
C++(十三) 类的扩展
本文详细介绍了C++中类的各种扩展特性,包括类成员存储、`sizeof`操作符的应用、类成员函数的存储方式及其背后的`this`指针机制。此外,还探讨了`const`修饰符在成员变量和函数中的作用,以及如何通过`static`关键字实现类中的资源共享。文章还介绍了单例模式的设计思路,并讨论了指向类成员(数据成员和函数成员)的指针的使用方法。最后,还讲解了指向静态成员的指针的相关概念和应用示例。通过这些内容,帮助读者更好地理解和掌握C++面向对象编程的核心概念和技术细节。
|
10月前
|
API Windows
MASM32编程获取Windows当前桌面主题名
MASM32编程获取Windows当前桌面主题名
|
10月前
|
图形学 C++ C#
Unity插件开发全攻略:从零起步教你用C++扩展游戏功能,解锁Unity新玩法的详细步骤与实战技巧大公开
【8月更文挑战第31天】Unity 是一款功能强大的游戏开发引擎,支持多平台发布并拥有丰富的插件生态系统。本文介绍 Unity 插件开发基础,帮助读者从零开始编写自定义插件以扩展其功能。插件通常用 C++ 编写,通过 Mono C# 运行时调用,需在不同平台上编译。文中详细讲解了开发环境搭建、简单插件编写及在 Unity 中调用的方法,包括创建 C# 封装脚本和处理跨平台问题,助力开发者提升游戏开发效率。
959 0
|
2月前
|
安全 数据安全/隐私保护 虚拟化
Windows Server 2022 中文版、英文版下载 (2025 年 5 月更新)
Windows Server 2022 中文版、英文版下载 (2025 年 5 月更新)
152 2
|
4月前
|
Unix 虚拟化 Windows
Windows Server 2025 中文版、英文版下载 (2025 年 3 月更新)
Windows Server 2025 中文版、英文版下载 (2025 年 3 月更新)
216 4
Windows Server 2025 中文版、英文版下载 (2025 年 3 月更新)