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

第三部分 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)

你可能感兴趣的:(windows)