1 /* 2 *作者:zsh 3 *说明:这段代码实现了通过方向键控制图标。需要注意,退出时请按ESC键,不要直接关闭,否则图标无法复位:)。欢迎大家
进行再创造:) 4 */ 5 #include<Windows.h> 6 #include<CommCtrl.h> 7 #include<Winuser.h> 8 #include<cmath> 9 #include<stdio.h> 10 #include <conio.h> 11 typedef struct _WidowSize{ 12 int Width; 13 int Height; 14 }WindowSize; 15 typedef struct GluttonousSnake{ 16 POINT positon[50]; 17 POINT vel; 18 int num; 19 }GluSnake; 20 21 HWND hDestTop; 22 void gotoxy(POINT Des, int IconSeq); 23 void InitIcon(); 24 void GetWindowSize(WindowSize *winSize); 25 void SnakeMove(GluSnake *iSnake); 26 void SnakeShow(GluSnake *iSnake); 27 void ReadIcoPt(POINT *icoPt, int icoNum); 28 29 int main() 30 { 31 WindowSize WinSize; 32 InitIcon();//初始化,获得句柄 33 GetWindowSize(&WinSize);//获得桌面尺寸 34 int IconNum = SendMessage(hDestTop, LVM_GETITEMCOUNT, 0, 0);//获取图标总数 35 int key; 36 POINT icoPosition[40]; 37 ReadIcoPt(icoPosition, IconNum); 38 GluSnake iSnake; 39 iSnake.num = 2; 40 iSnake.positon[0].x = 330; 41 iSnake.positon[0].y = 330; 42 iSnake.positon[1].x = 280; 43 iSnake.positon[1].y = 330; 44 iSnake.vel.x = 1; 45 iSnake.vel.y = 0; 46 bool zsh = 1; 47 while (1&&zsh) 48 { 49 // 50 //读取键盘值 51 if (_kbhit()) 52 { 53 if ((key = _getch()) == 224) key = _getch(); 54 switch (key) 55 { 56 case 80:iSnake.vel.y != -1 ? (iSnake.vel.x = 0, iSnake.vel.y = 1) : printf("\a"); break;//DOWN 57 case 72:iSnake.vel.y != 1 ? (iSnake.vel.x = 0, iSnake.vel.y = -1) : printf("\a"); break;//UP 58 case 75:iSnake.vel.x != 1 ? (iSnake.vel.x = -1, iSnake.vel.y = 0) : printf("\a"); break;//LEFT 59 case 77:iSnake.vel.x != -1 ? (iSnake.vel.x = 1, iSnake.vel.y = 0) : printf("\a"); break;//RIGHT 60 case 27:zsh = 0; break;//ESC退出 61 } 62 } 63 //每个节点动起来(操作数组) 64 SnakeMove(&iSnake); 65 //节点显示(实际移动) 66 SnakeShow(&iSnake); 67 Sleep(250); 68 } 69 //恢复图标位置 70 for (int cnt = 0; cnt < IconNum; cnt++) 71 { 72 gotoxy(icoPosition[cnt], cnt); 73 } 74 ListView_RedrawItems(hDestTop, 0, ListView_GetItemCount(hDestTop) - 1); 75 UpdateWindow(hDestTop); 76 return 0; 77 } 78 void ReadIcoPt(POINT *icoPt, int icoNum) 79 { 80 DWORD dwProcessId; 81 GetWindowThreadProcessId(hDestTop, 82 &dwProcessId);//通过桌面窗口句柄获取此窗口所在进程的PID,其实就是explorer进程 83 84 HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, dwProcessId);//打开指定PID进程,取得进程句柄 85 LPVOID lpvPt = VirtualAllocEx(hProcess, 86 NULL, 87 sizeof(POINT), 88 MEM_COMMIT, 89 PAGE_READWRITE);//在指定进程里面申请一个POINI结构大小的空间. 90 POINT pt; 91 int cnt; 92 for (cnt = 0; cnt < icoNum; cnt++) 93 { 94 ListView_GetItemPosition(hDestTop, 95 cnt, 96 lpvPt);//获取第一个图标的坐标,存入lpvPt 97 ReadProcessMemory(hProcess, 98 lpvPt, 99 &pt, 100 sizeof(POINT), 101 NULL); 102 //lpvPt不是本进程里面的,不能使用,所以就要 利用ReadProcessMemory从指定进程给读出来 103 icoPt[cnt].x = pt.x; 104 icoPt[cnt].y = pt.y; 105 // printf("%d,%d\n", pt.x, pt.y); 106 } 107 // getchar(); 108 VirtualFreeEx(hProcess, lpvPt, 0, MEM_RELEASE);// 释放申请的空间 109 CloseHandle(hProcess);//关闭句柄 110 } 111 112 void InitIcon() 113 { 114 hDestTop = ::FindWindow("progman", NULL); 115 hDestTop = ::FindWindowEx(hDestTop, 0, "shelldll_defview", NULL); 116 hDestTop = ::FindWindowEx(hDestTop, 0, "syslistview32", NULL); 117 } 118 119 void GetWindowSize(WindowSize *WinSize) 120 { 121 HWND hDesk; 122 RECT rc; 123 hDesk = GetDesktopWindow(); 124 GetWindowRect(hDesk, &rc); 125 WinSize->Width = rc.right - rc.left; 126 WinSize->Height = rc.bottom - rc.top; 127 } 128 void gotoxy(POINT Des, int IconSeq) 129 { 130 SendMessage(hDestTop, LVM_SETITEMPOSITION, IconSeq, MAKELPARAM(Des.x, Des.y)); 131 } 132 void SnakeMove(GluSnake *iSnake) 133 { 134 int cnt; 135 WindowSize iWinSize; 136 GetWindowSize(&iWinSize); 137 for (cnt = 1; cnt < iSnake->num; cnt++)//从后向前,后一个值改为前一个的值 138 { 139 iSnake->positon[iSnake->num - cnt].x = 140 iSnake->positon[iSnake->num - cnt - 1].x; 141 iSnake->positon[iSnake->num - cnt].y = 142 iSnake->positon[iSnake->num - cnt - 1].y; 143 } 144 iSnake->positon[0].x += iSnake->vel.x * 50; 145 iSnake->positon[0].y += iSnake->vel.y * 50; 146 if (iSnake->positon[0].x >= iWinSize.Width || iSnake->positon[0].y >= iWinSize.Height)//出界 147 { 148 iSnake->positon[0].x %= iWinSize.Width; 149 iSnake->positon[0].y %= iWinSize.Height; 150 } 151 if (iSnake->positon[0].x < 0)//出界 152 { 153 iSnake->positon[0].x += iWinSize.Width; 154 } 155 if (iSnake->positon[0].y < 0)//出界 156 { 157 iSnake->positon[0].y += iWinSize.Height; 158 } 159 } 160 void SnakeShow(GluSnake *iSnake) 161 { 162 int cnt; 163 for (cnt = 0; cnt < iSnake->num; cnt++)//依次将所有节点显示到指定位置 164 { 165 gotoxy(iSnake->positon[cnt], cnt + 1); 166 } 167 }
浙公网安备 33010602011771号