Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

270 lines
6.6 KiB

  1. #include <windows.h>
  2. #include <commdlg.h>
  3. #include <stdio.h>
  4. #include <tchar.h>
  5. #include <shlobj.h>
  6. #include <shlobjp.h>
  7. #include <objbase.h>
  8. #include "prwiziid.h"
  9. #include "resource.h"
  10. const GUID CLSID_PrintPhotosWizard = {0x4c649c49, 0xc48f, 0x4222, {0x9a, 0x0d, 0xcb, 0xbf, 0x42, 0x31, 0x22, 0x1d}};
  11. const GUID CLSID_PrintPhotosDropTarget = {0x60fd46de, 0xf830, 0x4894, {0xa6, 0x28, 0x6f, 0xa8, 0x1b, 0xc0, 0x19, 0x0d}};
  12. const GUID IID_ISetWaitEventForTesting = {0xd61e2fe1, 0x4af8, 0x4dbd, {0xb8, 0xad, 0xe7, 0xe0, 0x7a, 0xdc, 0xf9, 0x0f}};
  13. HINSTANCE g_hInstance = NULL;
  14. INT CountItems( LPTSTR pString )
  15. {
  16. INT iCount = 0;
  17. if (pString && *pString)
  18. {
  19. do
  20. {
  21. do
  22. {
  23. pString++;
  24. }
  25. while ( *pString );
  26. pString++;
  27. iCount++;
  28. }
  29. while ( *pString );
  30. }
  31. return iCount;
  32. }
  33. void RunWizard(HWND hwndOwner)
  34. {
  35. #define BUFFER_SIZE 65535
  36. TCHAR szBuffer[BUFFER_SIZE];
  37. TCHAR szParent[MAX_PATH];
  38. TCHAR szChild[MAX_PATH];
  39. LPCITEMIDLIST aPidl[ 512 ];
  40. OPENFILENAME ofn;
  41. LPITEMIDLIST pidlParent = NULL, pidlFull = NULL, pidlItem = NULL;
  42. HRESULT hr;
  43. IDataObject *pdo = NULL;
  44. INT index = 0;
  45. ZeroMemory(szBuffer,BUFFER_SIZE);
  46. ZeroMemory(&ofn, sizeof(ofn));
  47. ZeroMemory(aPidl, sizeof(aPidl));
  48. //
  49. // launch the file common dialog so the user
  50. // can select what files to pre-populate the
  51. // the wizard with...
  52. //
  53. ofn.lStructSize = sizeof(ofn);
  54. ofn.hInstance = g_hInstance;
  55. ofn.hwndOwner = hwndOwner;
  56. ofn.lpstrFilter = TEXT("All Files\0*.*\0JPEG Files\0*.jpg;*.jpeg\0TIFF Files\0*.tif;*.tiff\0GIF Files\0*.gif\0Bitmap Files\0*.bmp\0PNG Files\0*.png\0DIB Files\0*.DIB\0EMF Files\0*.EMF\0WMF Files\0*.WMF\0Icon Files\0*.ICO\0\0\0\0");
  57. ofn.nFilterIndex = 1;
  58. ofn.lpstrFile = szBuffer;
  59. ofn.nMaxFile = BUFFER_SIZE;
  60. ofn.lpstrTitle = TEXT("Select files to populate Print Photos Wizard with!");
  61. ofn.Flags = OFN_ALLOWMULTISELECT | OFN_EXPLORER | OFN_DONTADDTORECENT | OFN_FILEMUSTEXIST | OFN_PATHMUSTEXIST;
  62. if (GetOpenFileName( &ofn ))
  63. {
  64. INT iCount = CountItems( ofn.lpstrFile );
  65. if (iCount == 1)
  66. {
  67. hr = SHILCreateFromPath( ofn.lpstrFile, &pidlFull, NULL );
  68. if (SUCCEEDED(hr))
  69. {
  70. pidlItem = ILFindLastID( pidlFull );
  71. aPidl[index++] = ILClone( pidlItem );
  72. ILRemoveLastID( pidlFull );
  73. pidlParent = ILClone( pidlFull );
  74. ILFree( pidlFull );
  75. }
  76. }
  77. else if (iCount > 1)
  78. {
  79. LPTSTR pCur = ofn.lpstrFile;
  80. LPITEMIDLIST pidlTemp = NULL;
  81. LPITEMIDLIST pidlRel = NULL;
  82. lstrcpy( szParent, pCur );
  83. pCur += (lstrlen(pCur) + 1);
  84. //
  85. // Create parent idlist
  86. //
  87. hr = SHILCreateFromPath( szParent, &pidlParent, NULL );
  88. //
  89. // move to first file...
  90. if (SUCCEEDED(hr) && pidlParent)
  91. {
  92. for ( ; pCur && (*pCur); pCur += (lstrlen(pCur)+1))
  93. {
  94. //
  95. // Now, walk through and create each child item...
  96. //
  97. lstrcpy( szChild, szParent );
  98. lstrcat( szChild, TEXT("\\") );
  99. lstrcat( szChild, pCur );
  100. //
  101. // Create a pidl
  102. //
  103. hr = SHILCreateFromPath( szChild, &pidlTemp, NULL );
  104. if (SUCCEEDED(hr))
  105. {
  106. pidlRel = ILFindLastID( pidlTemp );
  107. aPidl[index++] = ILClone( pidlRel );
  108. ILFree( pidlTemp );
  109. }
  110. }
  111. }
  112. }
  113. else
  114. {
  115. TCHAR szError[1024];
  116. wsprintf( szError, TEXT("GetOpenFileName failed, Err=%d"), CommDlgExtendedError() );
  117. MessageBox( hwndOwner, szError, TEXT("Unit Test Error"), MB_OK | MB_ICONWARNING );
  118. }
  119. if (index)
  120. {
  121. //
  122. // If there's anything in the pidl array, then create dataobject
  123. //
  124. hr = SHCreateFileDataObject( pidlParent, index, aPidl, NULL, &pdo );
  125. if (SUCCEEDED(hr) && pdo)
  126. {
  127. IDropTarget * pdt = NULL;
  128. //
  129. // Got a data object, now start the wizard & do drop operation...
  130. //
  131. hr = CoCreateInstance( CLSID_PrintPhotosDropTarget, NULL, CLSCTX_INPROC_SERVER, IID_IDropTarget, (LPVOID *)&pdt );
  132. if (SUCCEEDED(hr) && pdt)
  133. {
  134. DWORD pdwEffect = DROPEFFECT_NONE;
  135. POINTL pt;
  136. pt.x = 0;
  137. pt.y = 0;
  138. hr = pdt->Drop( pdo, 0, pt, &pdwEffect );
  139. pdt->Release();
  140. }
  141. pdo->Release();
  142. }
  143. //
  144. // Free pidls from aPidl
  145. //
  146. if (pidlParent)
  147. {
  148. ILFree( pidlParent );
  149. }
  150. for (INT i=0; i<index; i++)
  151. {
  152. if (aPidl[i])
  153. {
  154. ILFree( (LPITEMIDLIST)aPidl[i] );
  155. }
  156. }
  157. }
  158. }
  159. else
  160. {
  161. TCHAR szError[1024];
  162. wsprintf( szError, TEXT("GetOpenFileName failed, Err=%d"), CommDlgExtendedError() );
  163. MessageBox( hwndOwner, szError, TEXT("Unit Test Error"), MB_OK | MB_ICONWARNING );
  164. }
  165. }
  166. INT_PTR CALLBACK TestDlgProc( HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam )
  167. {
  168. switch( uMsg )
  169. {
  170. case WM_INITDIALOG:
  171. return TRUE;
  172. case WM_COMMAND:
  173. switch (LOWORD(wParam))
  174. {
  175. case IDOK:
  176. EndDialog( hDlg, 0 );
  177. break;
  178. case IDC_RUN_WIZARD:
  179. RunWizard(hDlg);
  180. break;
  181. }
  182. }
  183. return FALSE;
  184. }
  185. int PASCAL WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow )
  186. {
  187. TCHAR sz[ MAX_PATH ];
  188. INT_PTR res;
  189. CoInitializeEx( NULL, COINIT_APARTMENTTHREADED );
  190. g_hInstance = hInstance;
  191. res = DialogBox( hInstance, MAKEINTRESOURCE(IDD_PHOTOTST_DIALOG), NULL, TestDlgProc );
  192. if (res == -1)
  193. {
  194. wsprintf( sz, TEXT("DialogBox failed with GLE=%d"), GetLastError() );
  195. MessageBox( NULL, sz, TEXT("Unit Test Error"), MB_OK | MB_ICONWARNING );
  196. }
  197. CoUninitialize();
  198. }