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)

目录
相关文章
|
1月前
|
关系型数据库 MySQL 开发工具
windows编译poco c++库
windows编译poco c++库
|
4天前
|
存储 编译器 C++
C++新特性 扩展和聚合类型
C++新特性 扩展和聚合类型
12 3
|
1月前
|
Unix 编译器 C语言
C++系列-附录-windows下安装C++环境
C++系列-附录-windows下安装C++环境
|
1月前
|
PyTorch 算法框架/工具 C++
windows上编译安装pytorch的c++扩展
windows上编译安装pytorch的c++扩展
|
1月前
|
网络协议 Linux C语言
005.在Windows下编程让效率起飞
windows开发Linux方式: 先用编辑器编写源代码 然后进入Linux 系统,使用gcc编译器(后面会讲),对源代码进行编译运行。 熟练后推荐使用VS2019 开发Linux C++ 程序 将自己的Ip地址设为静态IP
27 1
|
2月前
|
人工智能 机器人 C#
Windows编程课设(C#)——基于WPF和.net的即时通讯系统(仿微信)
一款参考QQ、微信的即时通讯软件。采用CS结构,客户端基于.Net与WPF开发,服务端使用Java开发。
|
2月前
|
存储 API 数据库
C/C++ 实现Windows注册表操作
Windows注册表(Registry)是Windows操作系统中用于存储系统配置信息、用户设置和应用程序数据的一个集中式数据库。它是一个层次结构的数据库,由键(Key)和值(Value)组成,这些键和值被用于存储各种系统和应用程序的配置信息。
|
2月前
|
Linux 编译器 C语言
C++ | 探究函数重载的原理:函数名修饰【基于Windows + Linux双系统】
C++ | 探究函数重载的原理:函数名修饰【基于Windows + Linux双系统】
49 0
C++ | 探究函数重载的原理:函数名修饰【基于Windows + Linux双系统】
|
3月前
|
Windows
Windows系统如何扩展存盘
Windows系统如何扩展存盘
21 0
|
25天前
|
缓存 网络协议 数据安全/隐私保护
[运维笔记] - (命令).Windows server常用网络相关命令总结
[运维笔记] - (命令).Windows server常用网络相关命令总结
118 0

相关产品

  • 云迁移中心