Qt-----GetDesktop

xiaoxiao2021-02-28  68

#define WIN32_LEAN_AND_MEAN #include <list> #include <afxwin.h> #include <stdio.h> #include <windows.h> #include <commctrl.h> #include <tchar.h> #include <iostream> #include <atlstr.h> #include "shlobj.h" //包含头文件 #include <CommonControls.h> #include <CString> using namespace std; #include "QtGuiApplication1.h" #include <QFileInfo> #include <QFileIconProvider> #include <QStandardPaths> #include <QDir> #include <QtWin> typedef struct tagLVITEM64 { UINT mask; int iItem; int iSubItem; UINT state; UINT stateMask; _int64 pszText; int cchTextMax; int iImage; _int64 lParam; #if (_WIN32_IE >= 0x0300) int iIndent; #endif #if (_WIN32_WINNT >= 0x0501) int iGroupId; UINT cColumns; // tile view columns _int64 puColumns; #endif #if _WIN32_WINNT >= 0x0600 _int64 piColFmt; int iGroup; // readonly. only valid for owner data. #endif } LVITEM64; QtGuiApplication1::QtGuiApplication1(QWidget *parent) : QMainWindow(parent) { ui.setupUi(this); //QString despath = QStandardPaths::writableLocation(QStandardPaths::DesktopLocation);//获取桌面路径 //QDir dir(despath); dir.setFilter(QDir::AllEntries | QDir::Executable | QDir::System | QDir::Drives); dir.setSorting(QDir::Size | QDir::Reversed); //QFileInfoList list = dir.entryInfoList(); //std::cout << " Bytes Filename" << std::endl; //for (int i = 0; i < list.size(); ++i) { // QFileInfo fileInfo = list.at(i); // std::cout << qPrintable(QString("%1 %2").arg(fileInfo.size(), 10) // .arg(fileInfo.fileName())); // QString s = fileInfo.fileName(); // std::cout << std::endl; //} //QFileInfo file_info("E:/cc.txt"); //QFileIconProvider icon_provider; //QIcon icon = icon_provider.icon(file_info); //ui.pushButton->setIcon(icon); CoInitialize(NULL);//应该注意的是,在调用SHGetFileInfo()之前,必须使用 CoInitialize 或者OleInitialize 初始化COM,否则表面上能够使用,但是会造成不安全或者丧失部分功能 //test4 LPITEMIDLIST lpItemIDList; SHFILEINFO shinfo; TCHAR szPath[MAX_PATH]; ::SHGetSpecialFolderLocation(NULL, CSIDL_DRIVES, &lpItemIDList); //CSIDL_DESKTOP-------strPath = L"C:\\Users\\LJ\\Desktop" //CSIDL_DESKTOPDIRECTORY-------strPath = L"C:\\Users\\LJ\\Desktop" // CSIDL_CONTROLS-------------- ::SHGetFileInfo((LPCTSTR)lpItemIDList, NULL, &shinfo, sizeof(shinfo), SHGFI_SYSICONINDEX | SHGFI_DISPLAYNAME | SHGFI_ICON | SHGFI_SMALLICON | SHGFI_PIDL); ::SHGetPathFromIDList(lpItemIDList, szPath); CString strDisplayName, strPath; strDisplayName.Format(_T("%s"), shinfo.szDisplayName); strPath.Format(_T("%s"), szPath); strPath = strPath /*+ _T("\\dd.docx")*/; AfxMessageBox(strPath); // Get the icon index using SHGetFileInfo /*SHFILEINFOW*/SHFILEINFO sfi; SHGetFileInfo(/*FileName*/strPath, -1, &sfi, sizeof(sfi), SHGFI_SYSICONINDEX); // Retrieve the system image list. // To get the 48x48 icons, use SHIL_EXTRALARGE // To get the 256x256 icons (Vista only), use SHIL_JUMBO IImageList* imageList; HRESULT hResult = SHGetImageList(SHIL_EXTRALARGE, IID_IImageList, (void**)&imageList); if (hResult == S_OK) { // Get the icon we need from the list. Note that the HIMAGELIST we retrieved // earlier needs to be casted to the IImageList interface before use. HICON hIcon; hResult = (imageList)->GetIcon(sfi.iIcon, /*ILD_TRANSPARENT*/ ILD_IMAGE | ILD_PRESERVEALPHA, &hIcon); if (hResult == S_OK) { // Do something with the icon here. //return hIcon; QPixmap pixmap; pixmap = QtWin::fromHICON(hIcon); ui.label->setPixmap(pixmap); } } CoUninitialize(); // 程序结束时 //--------------------------------------------------------------------------------------------- //HWND hDestTop; //hDestTop = ::FindWindow(_T("Progman"), NULL); //hDestTop = ::FindWindowEx(hDestTop, 0, _T("SHELLDLL_DefView"), NULL); //hDestTop = ::FindWindowEx(hDestTop, 0, _T("SysListView32"), NULL); //int count = (int)::SendMessage(hDestTop, LVM_GETITEMCOUNT, 0, 0); //unsigned long pid; //HANDLE process; 哪个线程创建了这个窗口,返回的就是这个线程的id号 (进程只有一个线程的话,那么线程标志符与进程标志符就是指同一个标志符) 通过桌面窗口句柄获取此窗口所在进程的PID,其实就是explorer进程 //GetWindowThreadProcessId(hDestTop, &pid); OpenProcess 函数用来打开一个已存在的进程对象,并返回进程的句柄。 //process = OpenProcess(PROCESS_VM_OPERATION | PROCESS_VM_READ | PROCESS_VM_WRITE | PROCESS_QUERY_INFORMATION, FALSE, pid); //LVITEM64 lvi, *_lvi; //wchar_t item[512], subitem[512]; //wchar_t *_item, *_subitem; 在指定进程的虚拟地址空间中保留或开辟一段区域。除非MEM_RESET被使用,否则这个函数将会初始化那段内存为0. //_lvi = (LVITEM64*)VirtualAllocEx(process, NULL, sizeof(LVITEM64), MEM_COMMIT, PAGE_READWRITE); //lvi.cchTextMax = 512; //_item = (wchar_t*)VirtualAllocEx(process, NULL, 512 * sizeof(wchar_t), MEM_COMMIT, PAGE_READWRITE); //_subitem = (wchar_t*)VirtualAllocEx(process, NULL, 512 * sizeof(wchar_t), MEM_COMMIT, PAGE_READWRITE); //RECT rc; //rc.left = LVIR_ICON; //这个一定要设定 可以去看MSDN关于LVM_GETITEMRECT的说明 Returns the bounding rectangle of the icon or small icon. //RECT* _rc = (RECT*)VirtualAllocEx(process, NULL, sizeof(RECT), MEM_COMMIT, PAGE_READWRITE); 在指定进程里面申请一个POINI结构大小的空间. //LPVOID lpvPt = VirtualAllocEx(process, NULL, sizeof(POINT), MEM_COMMIT, PAGE_READWRITE); //POINT pt;//pt就是你要的哪个坐标,这个是求第一个图标的坐标 //for (int i = 0; i < count; i++) //{ // lvi.iSubItem = 0; // lvi.pszText = (_int64)_item; // /*BOOL WriteProcessMemory( // HANDLE hProcess, // LPVOID lpBaseAddress, // LPVOID lpBuffer, // DWORD nSize, // LPDWORD lpNumberOfBytesWritten // ); // 参数: // hProcess // 由OpenProcess返回的进程句柄。 // 如参数传数据为 INVALID_HANDLE_VALUE 【即 - 1】目标进程为自身进程 // lpBaseAddress // 要写的内存首地址 // 再写入之前,此函数将先检查目标地址是否可用,并能容纳待写入的数据。 // lpBuffer // 指向要写的数据的指针。 // nSize // 要写入的字节数。*/ // WriteProcessMemory(process, _lvi, &lvi, sizeof(LVITEM64), NULL); // ::SendMessage(hDestTop, LVM_GETITEMTEXT, (WPARAM)i, (LPARAM)_lvi); // lvi.iSubItem = 1; // lvi.pszText = (_int64)_subitem; // WriteProcessMemory(process, _lvi, &lvi, sizeof(LVITEM64), NULL); // ::SendMessage(hDestTop, LVM_GETITEMTEXT, (WPARAM)i, (LPARAM)_lvi); // ::WriteProcessMemory(process, _rc, &rc, sizeof(rc), NULL); // ::SendMessage(hDestTop, LVM_GETITEMRECT, (WPARAM)i, (LPARAM)_rc); // ReadProcessMemory(process, _item, item, 512 * sizeof(wchar_t), NULL); // ReadProcessMemory(process, _subitem, subitem, 512 * sizeof(wchar_t), NULL); // ReadProcessMemory(process, _rc, &rc, sizeof(rc), NULL); // //_cwprintf(L"%s - %s LF:%d TP:%d RT:%d BT:%d\n", item, subitem, rc.left, rc.top, rc.right, rc.bottom); // CString str; // //str.Format("%s - %s LF:%d TP:%d RT:%d BT:%d\n", item, subitem, rc.left, rc.top, rc.right, rc.bottom); // //AfxMessageBox(str); // ListView_GetItemPosition(hDestTop, i, lpvPt);//获取第一个图标的坐标,存入lpvPt // ReadProcessMemory(process, lpvPt, &pt, sizeof(POINT), NULL);//lpvPt不是本进程里面的,不能使用,所以就要利用ReadProcessMemory从指定进程给读出来 // //str.Format("X:%d - Y:%d \n", pt.x, pt.y); // //AfxMessageBox(str); // int x = 400 + 150 * cos(i * 36 * 3.1415926 / 180); // int y = 400 + 150 * sin(i * 36 * 3.1415926 / 180); // //Moves an item to a specified position in a list-view control (must be in icon or small icon view). You can send this message explicitly or by using the ListView_SetItemPosition macro. // //makelparam和makewparam都是一样的,将两个word型合并成一个dword型。一个在高16位,一个在低16位 // ::SendMessage(hDestTop, LVM_SETITEMPOSITION, i, MAKELPARAM(x, y)); // ListView_RedrawItems(hDestTop, 0, ListView_GetItemCount(hDestTop) - 1); // ::UpdateWindow(hDestTop); //} //VirtualFreeEx(process, _lvi, 0, MEM_RELEASE); //VirtualFreeEx(process, _item, 0, MEM_RELEASE); //VirtualFreeEx(process, _subitem, 0, MEM_RELEASE); //VirtualFreeEx(process, _rc, 0, MEM_RELEASE); //VirtualFreeEx(process, lpvPt, 0, MEM_RELEASE); //CloseHandle(process); }
转载请注明原文地址: https://www.6miu.com/read-83403.html

最新回复(0)