Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ConsoleApplication2.cpp: определяет точку входа для консольного приложения.
- //
- #include <Windows.h>
- #include <iostream>
- using namespace std;
- // C++ with Microsoft extensions
- #define _WIN32_DCOM
- #include <objbase.h>
- #include <shlobj.h>
- #include <atltypes.h>
- // Это приложение выводит на экран
- // окно свойств "Мой компьютер".
- HRESULT hr;
- LPMALLOC pMalloc = NULL;
- LPSHELLFOLDER desktop = NULL;
- LPITEMIDLIST pidlDrives = NULL;
- CMINVOKECOMMANDINFO cmd;
- IContextMenu *g_pcm;
- IContextMenu2 *g_pcm2;
- IContextMenu3 *g_pcm3;
- HWND hMain;
- #include <WinUser.h>
- LRESULT CALLBACK mainWndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
- {
- LRESULT ret;
- if(g_pcm3 != NULL)
- {
- if( SUCCEEDED( g_pcm3->HandleMenuMsg2(uMsg,wParam,lParam,&ret) ) )
- return ret;
- }
- if(g_pcm2 != NULL)
- {
- if( SUCCEEDED( g_pcm2->HandleMenuMsg(uMsg,wParam,lParam) ) )
- return 0;
- }
- switch(uMsg)
- {
- case WM_DESTROY:
- PostQuitMessage(0);
- return 0;
- case WM_QUIT:
- return 1;
- break;
- default:
- return DefWindowProc(hwnd,uMsg,wParam,lParam);
- break;
- }
- return 0;
- }
- void subp()
- {
- if( SUCCEEDED( hr ) )
- __try
- {
- hr = SHGetMalloc( &pMalloc );
- if( SUCCEEDED( hr ) )
- __try
- {
- hr = SHGetDesktopFolder( &desktop );
- if( SUCCEEDED( hr ) )
- __try
- {
- hr = SHGetSpecialFolderLocation( NULL, CSIDL_DRIVES, &pidlDrives );
- if( SUCCEEDED( hr ) )
- __try
- {
- char * folderName = "G:\\Moriarty\\Sources\\C++\\UsingMenuFile\\Debug\\";
- char * fileName = "BuildLog.htm";
- char szBuf[MAX_PATH+1];
- LPITEMIDLIST ParentPidl;
- DWORD Eaten;
- OLECHAR olePath[MAX_PATH];
- MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, folderName, -1, olePath, MAX_PATH);
- // Получаем интерфейс IShellFolder рабочего стола
- desktop->ParseDisplayName(NULL, NULL, olePath, &Eaten, &ParentPidl, 0);
- LPSHELLFOLDER ParentFolder;
- desktop->BindToObject(ParentPidl, NULL, IID_IShellFolder, (void **)&ParentFolder);
- // Преобразуем путь в LPITEMIDLIST
- LPITEMIDLIST Pidl;
- MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, fileName, -1, olePath, MAX_PATH);
- ParentFolder->ParseDisplayName(NULL, NULL, olePath, &Eaten, &Pidl, 0);
- // Получаем интерфейс IContextMenu для заданного файла (папки)
- hr = ParentFolder->GetUIObjectOf( NULL, 1, const_cast<LPCITEMIDLIST*>(&Pidl), IID_IContextMenu, NULL, (void**) &g_pcm );
- // Создаём меню
- HMENU PopupMenu = CreatePopupMenu();
- g_pcm->QueryInterface(IID_IContextMenu2,(void**)&g_pcm2);
- g_pcm->QueryInterface(IID_IContextMenu3,(void**)&g_pcm3);
- // Заполняем меню
- g_pcm->QueryContextMenu(PopupMenu, 0, 1, 0x7FFF, CMF_NORMAL | CMF_EXPLORE);
- CPoint point;
- GetCursorPos(&point);
- // Отображаем меню
- UINT nCmd = TrackPopupMenu(PopupMenu,
- TPM_LEFTALIGN|TPM_LEFTBUTTON|TPM_RIGHTBUTTON|TPM_RETURNCMD,
- point.x, point.y, 10, hMain, NULL);
- if( SUCCEEDED(g_pcm->GetCommandString(nCmd - 1,GCS_VERBA,NULL,szBuf,MAX_PATH-1)) )
- {
- /*значит можно использовать szBuf как cmd.lpVerb*/
- cout<<szBuf<<endl;
- }
- if( SUCCEEDED( hr ) )
- __try
- {
- memset( & cmd, 0, sizeof( cmd ) );
- cmd.cbSize = sizeof( cmd );
- cmd.fMask = 0;
- cmd.hwnd = hMain;
- cmd.hwnd = 0;
- cmd.lpVerb = MAKEINTRESOURCEA(nCmd - 1);
- //cmd.nShow = SW_SHOWNORMAL;
- hr = g_pcm3->InvokeCommand( & cmd );
- }
- __finally
- {
- g_pcm->Release();
- }
- }
- __finally
- {
- pMalloc->Free( pidlDrives );
- }
- }
- __finally
- {
- desktop->Release();
- }
- }
- __finally
- {
- pMalloc->Release();
- }
- }
- __finally
- {
- CoUninitialize();
- }
- }
- void main( int argc, char *argv[])
- {
- OleInitialize(NULL);
- WNDCLASSEXW wndC;
- MSG msg;
- DWORD wndStyle = WS_VISIBLE | WS_OVERLAPPEDWINDOW | WS_GROUP | WS_HSCROLL;
- memset(&wndC,0,sizeof(WNDCLASSEX));
- g_pcm = g_pcm2 = g_pcm3 = NULL;
- wndC.cbSize = sizeof(WNDCLASSEXW);
- wndC.style = CS_HREDRAW | CS_VREDRAW;
- wndC.hInstance = GetModuleHandle(NULL);
- wndC.lpfnWndProc = mainWndProc;
- wndC.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
- wndC.hIcon = LoadIcon(0,IDI_WINLOGO);
- wndC.hCursor = LoadCursor(0,IDC_HAND);
- wndC.lpszClassName = L"MyCoolClass";
- wndC.hIconSm = LoadIcon(0,IDI_ASTERISK);
- if(RegisterClassExW(&wndC) == 0)
- {
- MessageBoxW(0,L"fuck register class",L"error",MB_OK | MB_ICONERROR);
- return;
- }
- hMain = CreateWindowExW(0,L"MyCoolClass",NULL,wndStyle,40,40,500,500,0,0,wndC.hInstance,NULL);
- subp();
- while(GetMessage(&msg,0,0,0))
- {
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement