WindowApi---getDesktop

来源:互联网 发布:如何看端口通不通 编辑:程序博客网 时间:2024/06/01 19:10
//#include "SetIconPos.h"//////int _tmain(int argc, TCHAR **argv)//{//  DWORD dw1 = GetTickCount();//  CSetIconPath * p = new CSetIconPath;////  while (true)//  {//      p->MoveIconPos(0, 8);//      //Sleep(100);//  }////  _tprintf(_T("use %d ms\n"), GetTickCount() - dw1);//  return 0;//}#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>using namespace std;//LJ----20170708为了解决读取桌面图标问题//typedef struct {//  UINT   mask;//  int    iItem;//  int    iSubItem;//  UINT   state;//  UINT   stateMask;////  int    placeholder1;//  LPTSTR pszText;//  int    placeholder11;////  int    cchTextMax;//  int    iImage;////  LPARAM lParam;//  int    placeholder2;////#if (_WIN32_IE >= 0x0300)//  int    iIndent;//#endif ////#if (_WIN32_WINNT >= 0x0501)//  int    iGroupId;////  UINT   cColumns;//  int    placeholder3;////  UINT   puColumns;//  int    placeholder4;//#endif ////#if (_WIN32_WINNT >= 0x0600)//  int    piColFmt;//  int    placeholder5;////  int    iGroup;//  int    placeholder6;//#endif //} LVITEM64, *LPLVITEM64;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;int main() {    //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);    //system("PAUSE ");    //test1:获取图标个数和图标名字    //HWND m_pwnd = GetDesktopWindow();    //HWND m_hDesktop_Progran = ::FindWindow(_T("Progman"), NULL);    //HWND m_hDesktop_Shelldll_defview = FindWindowEx(m_hDesktop_Progran, NULL, _T("SHELLDLL_DefView"), NULL);    //HWND m_hDesktop_Syslistview32 = FindWindowEx(m_hDesktop_Shelldll_defview, NULL, _T("SysListView32"), NULL);    //int count = (int)::SendMessage(m_hDesktop_Syslistview32, LVM_GETITEMCOUNT, 0, 0);    //LVITEM lvi, *_lvi;    //char item[512], subitem[512];    //char *_item, *_subitem;    //unsigned long pid;    //HANDLE process;      //GetWindowThreadProcessId(m_hDesktop_Syslistview32, &pid);    //process = OpenProcess(PROCESS_VM_OPERATION | PROCESS_VM_READ | PROCESS_VM_WRITE | PROCESS_QUERY_INFORMATION, FALSE, pid);    //_lvi = (LVITEM*)VirtualAllocEx(process, NULL, sizeof(LVITEM), MEM_COMMIT, PAGE_READWRITE);    //_item = (char*)VirtualAllocEx(process, NULL, 512, MEM_COMMIT, PAGE_READWRITE);    //_subitem = (char*)VirtualAllocEx(process, NULL, 512, MEM_COMMIT, PAGE_READWRITE);    //RECT  rc;    //rc.left = LVIR_ICON;  //这个一定要设定 可以去看MSDN关于LVM_GETITEMRECT的说明    //RECT* _rc = (RECT*)VirtualAllocEx(process, NULL, sizeof(RECT), MEM_COMMIT, PAGE_READWRITE);    //lvi.cchTextMax = 512;    //for (int i = 0; i < 10; i++)     //{    //  lvi.iSubItem = 0;    //  lvi.pszText = _item;    //  WriteProcessMemory(process, _lvi, &lvi, sizeof(LVITEM), NULL);    //  ::SendMessage(m_hDesktop_Syslistview32, LVM_GETITEMTEXT, (WPARAM)i, (LPARAM)_lvi);    //  lvi.iSubItem = 1;    //  lvi.pszText = _subitem;    //  WriteProcessMemory(process, _lvi, &lvi, sizeof(LVITEM), NULL);    //  ::SendMessage(m_hDesktop_Syslistview32, LVM_GETITEMTEXT, (WPARAM)i, (LPARAM)_lvi);    //  ::WriteProcessMemory(process, _rc, &rc, sizeof(rc), NULL);    //  ::SendMessage(m_hDesktop_Syslistview32, LVM_GETITEMRECT, (WPARAM)i, (LPARAM)_rc);    //  ReadProcessMemory(process, _item, item, 512, NULL);    //  ReadProcessMemory(process, _subitem, subitem, 512, NULL);    //  ReadProcessMemory(process, _rc, &rc, sizeof(rc), NULL);    //  CString str;    //  str.Format("LF:%d TP:%d RT:%d BT:%d", rc.left, rc.top, rc.right, rc.bottom);    //  AfxMessageBox(str);    //  str.Format("%s - %s/n", item, subitem);    //  AfxMessageBox(str);    //}    //VirtualFreeEx(process, _lvi, 0, MEM_RELEASE);    //VirtualFreeEx(process, _item, 0, MEM_RELEASE);    //VirtualFreeEx(process, _subitem, 0, MEM_RELEASE);    //VirtualFreeEx(process, _rc, 0, MEM_RELEASE);    //CloseHandle(process);    //test2:获取图标位置    //HWND m_pwnd = GetDesktopWindow();    //HWND m_hDesktop_Progran = ::FindWindow(_T("Progman"), NULL);    //HWND m_hDesktop_Shelldll_defview = FindWindowEx(m_hDesktop_Progran, NULL, _T("SHELLDLL_DefView"), NULL);    //HWND m_hDesktop_Syslistview32 = FindWindowEx(m_hDesktop_Shelldll_defview, NULL, _T("SysListView32"), NULL);    //DWORD   pid;    //GetWindowThreadProcessId(m_hDesktop_Syslistview32, &pid);    //HANDLE   hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid);//打开指定PID进程,取得进程句柄    //LPVOID   lpvPt = VirtualAllocEx(hProcess, NULL, sizeof(POINT), MEM_COMMIT, PAGE_READWRITE);//在指定进程里面申请一个POINI结构大小的空间.    //POINT   pt;//pt就是你要的哪个坐标,这个是求第一个图标的坐标    //ListView_GetItemPosition(m_hDesktop_Syslistview32, 2, lpvPt);//获取第一个图标的坐标,存入lpvPt    //ReadProcessMemory(hProcess, lpvPt, &pt, sizeof(POINT), NULL);    ////lpvPt不是本进程里面的,不能使用,所以就要利用ReadProcessMemory从指定进程给读出来    //VirtualFreeEx(hProcess, lpvPt, 0, MEM_RELEASE);    ////释放申请的空间    //CloseHandle(hProcess);//关闭句柄    //test3:移动桌面图标位置    //HWND hDestTop;    //hDestTop = ::FindWindow("progman", NULL);    //hDestTop = ::FindWindowEx(hDestTop, 0, "shelldll_defview", NULL);    //HWND hwndSysListView32 = ::FindWindowEx(hDestTop, 0, "syslistview32", NULL);    //int Nm = (int)::SendMessage(hwndSysListView32, LVM_GETITEMCOUNT, 0, 0);    //int sNm = 0;    //if (Nm >= 10)    //{    //  sNm = 10;    //}    //else    //{    //  sNm = Nm;    //}    //for (int i = 0; i < sNm; i++)    //{    //  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(hwndSysListView32, LVM_SETITEMPOSITION, i, MAKELPARAM(x, y));    //}    ////Forces a list - view control to redraw a range of items.You can use this macro or send the LVM_REDRAWITEMS message explicitly.    //int testNum = ListView_GetItemCount(hwndSysListView32);    //ListView_RedrawItems(hwndSysListView32, 0, ListView_GetItemCount(hwndSysListView32) - 1);    //::UpdateWindow(hwndSysListView32);    //system("PAUSE ");CoInitialize(NULL);//应该注意的是,在调用SHGetFileInfo()之前,必须使用 CoInitialize 或者OleInitialize 初始化COM,否则表面上能够使用,但是会造成不安全或者丧失部分功能    //test4LPITEMIDLIST   lpItemIDList;SHFILEINFO     shinfo;TCHAR szPath[MAX_PATH];::SHGetSpecialFolderLocation(NULL, CSIDL_DESKTOPDIRECTORY, &lpItemIDList);::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("\\aa.txt");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;            int a = 1;        }    }}
原创粉丝点击