Advertisement
Guest User

Untitled

a guest
Aug 29th, 2013
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.23 KB | None | 0 0
  1. // ConsoleApplication2.cpp: определяет точку входа для консольного приложения.
  2. //
  3.  
  4. #include <Windows.h>
  5. #include <iostream>
  6.  
  7. using namespace std;
  8.  
  9. // C++ with Microsoft extensions
  10.  
  11. #define _WIN32_DCOM
  12.  
  13. #include <objbase.h>
  14. #include <shlobj.h>
  15. #include <atltypes.h>
  16.  
  17. // Это приложение выводит на экран
  18. // окно свойств "Мой компьютер".
  19.  
  20. HRESULT hr;
  21. LPMALLOC pMalloc = NULL;
  22. LPSHELLFOLDER desktop = NULL;
  23. LPITEMIDLIST pidlDrives = NULL;
  24. CMINVOKECOMMANDINFO cmd;
  25.  
  26. IContextMenu  *g_pcm;
  27. IContextMenu2 *g_pcm2;
  28. IContextMenu3 *g_pcm3;
  29.  
  30. HWND hMain;
  31.  
  32. #include <WinUser.h>
  33.  
  34. LRESULT CALLBACK mainWndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  35. {
  36.     LRESULT ret;
  37.  
  38.     if(g_pcm3 != NULL)
  39.     {
  40.         if( SUCCEEDED( g_pcm3->HandleMenuMsg2(uMsg,wParam,lParam,&ret) ) )
  41.             return ret;
  42.     }
  43.  
  44.     if(g_pcm2 != NULL)
  45.     {
  46.         if( SUCCEEDED( g_pcm2->HandleMenuMsg(uMsg,wParam,lParam) ) )
  47.             return 0;
  48.     }
  49.  
  50.     switch(uMsg)
  51.     {
  52.  
  53.         case WM_DESTROY:
  54.  
  55.             PostQuitMessage(0);
  56.             return 0;
  57.  
  58.         case WM_QUIT:
  59.  
  60.             return 1;
  61.  
  62.         break;
  63.  
  64.         default:
  65.            
  66.             return DefWindowProc(hwnd,uMsg,wParam,lParam);
  67.  
  68.         break;
  69.     }
  70.  
  71.     return 0;
  72. }
  73.  
  74. void subp()
  75. {
  76.  
  77.   if( SUCCEEDED( hr ) )
  78.   __try
  79.   {
  80.     hr = SHGetMalloc( &pMalloc );
  81.     if( SUCCEEDED( hr ) )
  82.     __try
  83.     {
  84.       hr = SHGetDesktopFolder( &desktop );
  85.       if( SUCCEEDED( hr ) )
  86.       __try
  87.       {
  88.         hr = SHGetSpecialFolderLocation( NULL, CSIDL_DRIVES, &pidlDrives );
  89.         if( SUCCEEDED( hr ) )
  90.         __try
  91.         {
  92.            
  93.           char * folderName = "G:\\Moriarty\\Sources\\C++\\UsingMenuFile\\Debug\\";
  94.           char * fileName = "BuildLog.htm";
  95.           char szBuf[MAX_PATH+1];
  96.           LPITEMIDLIST ParentPidl;
  97.           DWORD Eaten;
  98.           OLECHAR olePath[MAX_PATH];
  99.           MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, folderName, -1, olePath, MAX_PATH);
  100.          
  101.           // Получаем интерфейс IShellFolder рабочего стола
  102.           desktop->ParseDisplayName(NULL, NULL, olePath, &Eaten, &ParentPidl, 0);
  103.           LPSHELLFOLDER ParentFolder;
  104.           desktop->BindToObject(ParentPidl, NULL, IID_IShellFolder, (void **)&ParentFolder);
  105.          
  106.            // Преобразуем путь в LPITEMIDLIST
  107.           LPITEMIDLIST Pidl;
  108.           MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, fileName, -1, olePath, MAX_PATH);
  109.           ParentFolder->ParseDisplayName(NULL, NULL, olePath, &Eaten, &Pidl, 0);
  110.  
  111.           // Получаем интерфейс IContextMenu для заданного файла (папки)
  112.           hr = ParentFolder->GetUIObjectOf( NULL, 1, const_cast<LPCITEMIDLIST*>(&Pidl), IID_IContextMenu, NULL, (void**) &g_pcm );
  113.  
  114.           // Создаём меню
  115.           HMENU PopupMenu = CreatePopupMenu();
  116.  
  117.           g_pcm->QueryInterface(IID_IContextMenu2,(void**)&g_pcm2);
  118.           g_pcm->QueryInterface(IID_IContextMenu3,(void**)&g_pcm3);
  119.           // Заполняем меню
  120.           g_pcm->QueryContextMenu(PopupMenu, 0, 1, 0x7FFF, CMF_NORMAL | CMF_EXPLORE);
  121.  
  122.          
  123.           CPoint point;
  124.           GetCursorPos(&point);
  125.  
  126.           // Отображаем меню
  127.           UINT nCmd = TrackPopupMenu(PopupMenu,
  128.           TPM_LEFTALIGN|TPM_LEFTBUTTON|TPM_RIGHTBUTTON|TPM_RETURNCMD,
  129.           point.x, point.y, 10, hMain, NULL);
  130.  
  131.  
  132.           if( SUCCEEDED(g_pcm->GetCommandString(nCmd - 1,GCS_VERBA,NULL,szBuf,MAX_PATH-1)) )
  133.           {
  134.               /*значит можно использовать szBuf как cmd.lpVerb*/
  135.               cout<<szBuf<<endl;
  136.           }
  137.  
  138.          
  139.           if( SUCCEEDED( hr ) )
  140.           __try
  141.           {
  142.             memset( & cmd, 0, sizeof( cmd ) );
  143.             cmd.cbSize = sizeof( cmd );
  144.             cmd.fMask = 0;
  145.             cmd.hwnd = hMain;
  146.             cmd.hwnd = 0;
  147.             cmd.lpVerb = MAKEINTRESOURCEA(nCmd - 1);
  148.             //cmd.nShow = SW_SHOWNORMAL;
  149.             hr = g_pcm3->InvokeCommand( & cmd );
  150.           }
  151.           __finally
  152.           {
  153.             g_pcm->Release();
  154.           }
  155.          
  156.         }
  157.         __finally
  158.         {
  159.           pMalloc->Free( pidlDrives );
  160.         }
  161.       }
  162.       __finally
  163.       {
  164.         desktop->Release();
  165.       }
  166.     }
  167.     __finally
  168.     {
  169.       pMalloc->Release();
  170.     }
  171.   }
  172.   __finally
  173.   {
  174.     CoUninitialize();
  175.   }
  176. }
  177.  
  178.  
  179. void main( int argc, char *argv[])
  180. {
  181.     OleInitialize(NULL);
  182.     WNDCLASSEXW wndC;
  183.     MSG msg;
  184.     DWORD wndStyle = WS_VISIBLE | WS_OVERLAPPEDWINDOW | WS_GROUP | WS_HSCROLL;
  185.     memset(&wndC,0,sizeof(WNDCLASSEX));
  186.  
  187.     g_pcm = g_pcm2 = g_pcm3 = NULL;
  188.  
  189.     wndC.cbSize = sizeof(WNDCLASSEXW);
  190.     wndC.style = CS_HREDRAW | CS_VREDRAW;
  191.     wndC.hInstance = GetModuleHandle(NULL);
  192.     wndC.lpfnWndProc = mainWndProc;
  193.     wndC.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  194.     wndC.hIcon = LoadIcon(0,IDI_WINLOGO);
  195.     wndC.hCursor = LoadCursor(0,IDC_HAND);
  196.     wndC.lpszClassName = L"MyCoolClass";
  197.     wndC.hIconSm = LoadIcon(0,IDI_ASTERISK);
  198.  
  199.     if(RegisterClassExW(&wndC) == 0)
  200.     {
  201.         MessageBoxW(0,L"fuck register class",L"error",MB_OK | MB_ICONERROR);
  202.         return;
  203.     }
  204.  
  205.     hMain = CreateWindowExW(0,L"MyCoolClass",NULL,wndStyle,40,40,500,500,0,0,wndC.hInstance,NULL);
  206.  
  207.     subp();
  208.  
  209.     while(GetMessage(&msg,0,0,0))
  210.     {
  211.         TranslateMessage(&msg);
  212.         DispatchMessage(&msg);
  213.     }
  214.  
  215. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement