void CFilesHashDlg::OnBnClickedContext() { if(m_bLimited) { OSVERSIONINFOEX osvi; BOOL bOsVersionInfoEx; if(GetWindowsVersion(osvi, bOsVersionInfoEx) && osvi.dwMajorVersion >= 6) { if(ElevateProcess()) ExitProcess(0); } } // May not a limited process. CStatic* pWnd = (CStatic *)GetDlgItem(IDC_STATIC_ADDRESULT); CString buttonText = _T(""); m_btnContext.GetWindowText(buttonText); if(buttonText.Compare(MAINDLG_ADD_CONTEXT_MENU) == 0) { RemoveContextMenu(); // Try to delete all items related to fHash if(AddContextMenu()) { pWnd->SetWindowText(MAINDLG_ADD_SUCCEEDED); m_btnContext.SetWindowText(MAINDLG_REMOVE_CONTEXT_MENU); } else { pWnd->SetWindowText(MAINDLG_ADD_FAILED); } } else if(buttonText.Compare(MAINDLG_REMOVE_CONTEXT_MENU) == 0) { if(RemoveContextMenu()) { pWnd->SetWindowText(MAINDLG_REMOVE_SUCCEEDED); m_btnContext.SetWindowText(MAINDLG_ADD_CONTEXT_MENU); } else { pWnd->SetWindowText(MAINDLG_REMOVE_FAILED); } } }
int EAEDisplayFileMenu(WCHAR* file, HWND callingWnd) { int command = 0; POINT pt; GetCursorPos(&pt); WCHAR expandedFile[MAX_PATH]; WCHAR directory[MAX_PATH]; ContextMap::iterator iter; CMINVOKECOMMANDINFO commandInfo; WCHAR tmp[MAX_PATH]; WCHAR delimiter = '|'; UINT i = 0, j = 0; HMENU contextMenu; MENUITEMINFO itemInfo; ZeroMemory(&commandInfo, sizeof(commandInfo)); while (i < wcslen(file)) { if (file[i] == delimiter) { tmp[j] = '\0'; ExpandEnvironmentStrings((LPCTSTR)tmp, (LPTSTR)expandedFile, MAX_PATH); AddContextMenu(expandedFile); j = 0; } else { tmp[j] = file[i]; j++; } i++; } tmp[j] = '\0'; ExpandEnvironmentStrings((LPCTSTR)tmp, (LPTSTR)expandedFile, MAX_PATH); AddContextMenu(expandedFile); if (contextMap.size() == 1) { contextMenu = contextMap.begin()->first; } else { contextMenu = CreatePopupMenu(); ZeroMemory(&itemInfo, sizeof(itemInfo)); itemInfo.fMask = MIIM_STRING | MIIM_SUBMENU; itemInfo.cbSize = sizeof(MENUITEMINFO); i = 0; iter = contextMap.begin(); while (iter != contextMap.end()) { itemInfo.dwTypeData = iter->second.value; itemInfo.hSubMenu = iter->first; InsertMenuItem(contextMenu, i, TRUE, &itemInfo); i++; iter++; } } // Subclass wndproc to handle messages oldWndProc = (WNDPROC)SetWindowLongPtr(callingWnd, GWLP_WNDPROC, (LONG_PTR)MenuProc); // Display menu command = TrackPopupMenuEx(contextMenu, TPM_RETURNCMD | TPM_RECURSE, pt.x, pt.y, callingWnd, NULL); if (oldWndProc) { SetWindowLongPtr(callingWnd, GWLP_WNDPROC, (LONG_PTR)oldWndProc); oldWndProc = NULL; } if ((command >= SYSMENUMIN) && (command <= SYSMENUMAX)) { iter = contextMap.find(selectedMenu); if (iter == contextMap.end()) { return 0; } int offset = command - SYSMENUMIN; commandInfo.cbSize = sizeof(commandInfo); commandInfo.hwnd = callingWnd; commandInfo.lpVerb = (LPCSTR)MAKEINTRESOURCE(offset); commandInfo.nShow = SW_SHOW; iter->second.ic2->InvokeCommand(&commandInfo); } else if (command == 0x8001) { wcscpy(directory, file); PathRemoveFileSpec(directory); ELExecuteFileOrCommand(directory, directory); } while (!contextMap.empty()) { iter = contextMap.begin(); DestroyMenu(iter->first); iter->second.ic2->Release(); contextMap.erase(iter); } DestroyMenu(contextMenu); return command; }