├── README.md
├── Desktop Snake.vcxproj.filters
├── Desktop Snake.sln
├── .gitignore
├── Desktop Snake.vcxproj
└── Desktop Snake.cpp
/README.md:
--------------------------------------------------------------------------------
1 | # Desktop-Snake
2 | A Snake Game Play With Desktop Icons. Support multi-monitor with rectangle aligned layout.
3 |
--------------------------------------------------------------------------------
/Desktop Snake.vcxproj.filters:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF}
6 | cpp;c;cc;cxx;c++;def;odl;idl;hpj;bat;asm;asmx
7 |
8 |
9 | {93995380-89BD-4b04-88EB-625FBE52EBFB}
10 | h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd
11 |
12 |
13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01}
14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms
15 |
16 |
17 |
18 |
19 | 源文件
20 |
21 |
22 |
--------------------------------------------------------------------------------
/Desktop Snake.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | # Visual Studio Version 16
4 | VisualStudioVersion = 16.0.29911.84
5 | MinimumVisualStudioVersion = 10.0.40219.1
6 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Desktop Snake", "Desktop Snake.vcxproj", "{49239BAC-6D6B-46FB-9849-0A2F34057D44}"
7 | EndProject
8 | Global
9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
10 | Debug|x64 = Debug|x64
11 | Debug|x86 = Debug|x86
12 | Release|x64 = Release|x64
13 | Release|x86 = Release|x86
14 | EndGlobalSection
15 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
16 | {49239BAC-6D6B-46FB-9849-0A2F34057D44}.Debug|x64.ActiveCfg = Debug|x64
17 | {49239BAC-6D6B-46FB-9849-0A2F34057D44}.Debug|x64.Build.0 = Debug|x64
18 | {49239BAC-6D6B-46FB-9849-0A2F34057D44}.Debug|x86.ActiveCfg = Debug|Win32
19 | {49239BAC-6D6B-46FB-9849-0A2F34057D44}.Debug|x86.Build.0 = Debug|Win32
20 | {49239BAC-6D6B-46FB-9849-0A2F34057D44}.Release|x64.ActiveCfg = Release|x64
21 | {49239BAC-6D6B-46FB-9849-0A2F34057D44}.Release|x64.Build.0 = Release|x64
22 | {49239BAC-6D6B-46FB-9849-0A2F34057D44}.Release|x86.ActiveCfg = Release|Win32
23 | {49239BAC-6D6B-46FB-9849-0A2F34057D44}.Release|x86.Build.0 = Release|Win32
24 | EndGlobalSection
25 | GlobalSection(SolutionProperties) = preSolution
26 | HideSolutionNode = FALSE
27 | EndGlobalSection
28 | GlobalSection(ExtensibilityGlobals) = postSolution
29 | SolutionGuid = {2A2122E3-96B6-4B90-909E-C00C285E64A7}
30 | EndGlobalSection
31 | EndGlobal
32 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | ## Ignore Visual Studio temporary files, build results, and
2 | ## files generated by popular Visual Studio add-ons.
3 | ##
4 | ## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore
5 |
6 | # User-specific files
7 | *.rsuser
8 | *.suo
9 | *.user
10 | *.userosscache
11 | *.sln.docstates
12 |
13 | # User-specific files (MonoDevelop/Xamarin Studio)
14 | *.userprefs
15 |
16 | # Mono auto generated files
17 | mono_crash.*
18 |
19 | # Build results
20 | [Dd]ebug/
21 | [Dd]ebugPublic/
22 | [Rr]elease/
23 | [Rr]eleases/
24 | x64/
25 | x86/
26 | [Aa][Rr][Mm]/
27 | [Aa][Rr][Mm]64/
28 | bld/
29 | [Bb]in/
30 | [Oo]bj/
31 | [Ll]og/
32 | [Ll]ogs/
33 |
34 | # Visual Studio 2015/2017 cache/options directory
35 | .vs/
36 | # Uncomment if you have tasks that create the project's static files in wwwroot
37 | #wwwroot/
38 |
39 | # Visual Studio 2017 auto generated files
40 | Generated\ Files/
41 |
42 | # MSTest test Results
43 | [Tt]est[Rr]esult*/
44 | [Bb]uild[Ll]og.*
45 |
46 | # NUnit
47 | *.VisualState.xml
48 | TestResult.xml
49 | nunit-*.xml
50 |
51 | # Build Results of an ATL Project
52 | [Dd]ebugPS/
53 | [Rr]eleasePS/
54 | dlldata.c
55 |
56 | # Benchmark Results
57 | BenchmarkDotNet.Artifacts/
58 |
59 | # .NET Core
60 | project.lock.json
61 | project.fragment.lock.json
62 | artifacts/
63 |
64 | # StyleCop
65 | StyleCopReport.xml
66 |
67 | # Files built by Visual Studio
68 | *_i.c
69 | *_p.c
70 | *_h.h
71 | *.ilk
72 | *.meta
73 | *.obj
74 | *.iobj
75 | *.pch
76 | *.pdb
77 | *.ipdb
78 | *.pgc
79 | *.pgd
80 | *.rsp
81 | *.sbr
82 | *.tlb
83 | *.tli
84 | *.tlh
85 | *.tmp
86 | *.tmp_proj
87 | *_wpftmp.csproj
88 | *.log
89 | *.vspscc
90 | *.vssscc
91 | .builds
92 | *.pidb
93 | *.svclog
94 | *.scc
95 |
96 | # Chutzpah Test files
97 | _Chutzpah*
98 |
99 | # Visual C++ cache files
100 | ipch/
101 | *.aps
102 | *.ncb
103 | *.opendb
104 | *.opensdf
105 | *.sdf
106 | *.cachefile
107 | *.VC.db
108 | *.VC.VC.opendb
109 |
110 | # Visual Studio profiler
111 | *.psess
112 | *.vsp
113 | *.vspx
114 | *.sap
115 |
116 | # Visual Studio Trace Files
117 | *.e2e
118 |
119 | # TFS 2012 Local Workspace
120 | $tf/
121 |
122 | # Guidance Automation Toolkit
123 | *.gpState
124 |
125 | # ReSharper is a .NET coding add-in
126 | _ReSharper*/
127 | *.[Rr]e[Ss]harper
128 | *.DotSettings.user
129 |
130 | # TeamCity is a build add-in
131 | _TeamCity*
132 |
133 | # DotCover is a Code Coverage Tool
134 | *.dotCover
135 |
136 | # AxoCover is a Code Coverage Tool
137 | .axoCover/*
138 | !.axoCover/settings.json
139 |
140 | # Visual Studio code coverage results
141 | *.coverage
142 | *.coveragexml
143 |
144 | # NCrunch
145 | _NCrunch_*
146 | .*crunch*.local.xml
147 | nCrunchTemp_*
148 |
149 | # MightyMoose
150 | *.mm.*
151 | AutoTest.Net/
152 |
153 | # Web workbench (sass)
154 | .sass-cache/
155 |
156 | # Installshield output folder
157 | [Ee]xpress/
158 |
159 | # DocProject is a documentation generator add-in
160 | DocProject/buildhelp/
161 | DocProject/Help/*.HxT
162 | DocProject/Help/*.HxC
163 | DocProject/Help/*.hhc
164 | DocProject/Help/*.hhk
165 | DocProject/Help/*.hhp
166 | DocProject/Help/Html2
167 | DocProject/Help/html
168 |
169 | # Click-Once directory
170 | publish/
171 |
172 | # Publish Web Output
173 | *.[Pp]ublish.xml
174 | *.azurePubxml
175 | # Note: Comment the next line if you want to checkin your web deploy settings,
176 | # but database connection strings (with potential passwords) will be unencrypted
177 | *.pubxml
178 | *.publishproj
179 |
180 | # Microsoft Azure Web App publish settings. Comment the next line if you want to
181 | # checkin your Azure Web App publish settings, but sensitive information contained
182 | # in these scripts will be unencrypted
183 | PublishScripts/
184 |
185 | # NuGet Packages
186 | *.nupkg
187 | # NuGet Symbol Packages
188 | *.snupkg
189 | # The packages folder can be ignored because of Package Restore
190 | **/[Pp]ackages/*
191 | # except build/, which is used as an MSBuild target.
192 | !**/[Pp]ackages/build/
193 | # Uncomment if necessary however generally it will be regenerated when needed
194 | #!**/[Pp]ackages/repositories.config
195 | # NuGet v3's project.json files produces more ignorable files
196 | *.nuget.props
197 | *.nuget.targets
198 |
199 | # Microsoft Azure Build Output
200 | csx/
201 | *.build.csdef
202 |
203 | # Microsoft Azure Emulator
204 | ecf/
205 | rcf/
206 |
207 | # Windows Store app package directories and files
208 | AppPackages/
209 | BundleArtifacts/
210 | Package.StoreAssociation.xml
211 | _pkginfo.txt
212 | *.appx
213 | *.appxbundle
214 | *.appxupload
215 |
216 | # Visual Studio cache files
217 | # files ending in .cache can be ignored
218 | *.[Cc]ache
219 | # but keep track of directories ending in .cache
220 | !?*.[Cc]ache/
221 |
222 | # Others
223 | ClientBin/
224 | ~$*
225 | *~
226 | *.dbmdl
227 | *.dbproj.schemaview
228 | *.jfm
229 | *.pfx
230 | *.publishsettings
231 | orleans.codegen.cs
232 |
233 | # Including strong name files can present a security risk
234 | # (https://github.com/github/gitignore/pull/2483#issue-259490424)
235 | #*.snk
236 |
237 | # Since there are multiple workflows, uncomment next line to ignore bower_components
238 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622)
239 | #bower_components/
240 |
241 | # RIA/Silverlight projects
242 | Generated_Code/
243 |
244 | # Backup & report files from converting an old project file
245 | # to a newer Visual Studio version. Backup files are not needed,
246 | # because we have git ;-)
247 | _UpgradeReport_Files/
248 | Backup*/
249 | UpgradeLog*.XML
250 | UpgradeLog*.htm
251 | ServiceFabricBackup/
252 | *.rptproj.bak
253 |
254 | # SQL Server files
255 | *.mdf
256 | *.ldf
257 | *.ndf
258 |
259 | # Business Intelligence projects
260 | *.rdl.data
261 | *.bim.layout
262 | *.bim_*.settings
263 | *.rptproj.rsuser
264 | *- [Bb]ackup.rdl
265 | *- [Bb]ackup ([0-9]).rdl
266 | *- [Bb]ackup ([0-9][0-9]).rdl
267 |
268 | # Microsoft Fakes
269 | FakesAssemblies/
270 |
271 | # GhostDoc plugin setting file
272 | *.GhostDoc.xml
273 |
274 | # Node.js Tools for Visual Studio
275 | .ntvs_analysis.dat
276 | node_modules/
277 |
278 | # Visual Studio 6 build log
279 | *.plg
280 |
281 | # Visual Studio 6 workspace options file
282 | *.opt
283 |
284 | # Visual Studio 6 auto-generated workspace file (contains which files were open etc.)
285 | *.vbw
286 |
287 | # Visual Studio LightSwitch build output
288 | **/*.HTMLClient/GeneratedArtifacts
289 | **/*.DesktopClient/GeneratedArtifacts
290 | **/*.DesktopClient/ModelManifest.xml
291 | **/*.Server/GeneratedArtifacts
292 | **/*.Server/ModelManifest.xml
293 | _Pvt_Extensions
294 |
295 | # Paket dependency manager
296 | .paket/paket.exe
297 | paket-files/
298 |
299 | # FAKE - F# Make
300 | .fake/
301 |
302 | # CodeRush personal settings
303 | .cr/personal
304 |
305 | # Python Tools for Visual Studio (PTVS)
306 | __pycache__/
307 | *.pyc
308 |
309 | # Cake - Uncomment if you are using it
310 | # tools/**
311 | # !tools/packages.config
312 |
313 | # Tabs Studio
314 | *.tss
315 |
316 | # Telerik's JustMock configuration file
317 | *.jmconfig
318 |
319 | # BizTalk build output
320 | *.btp.cs
321 | *.btm.cs
322 | *.odx.cs
323 | *.xsd.cs
324 |
325 | # OpenCover UI analysis results
326 | OpenCover/
327 |
328 | # Azure Stream Analytics local run output
329 | ASALocalRun/
330 |
331 | # MSBuild Binary and Structured Log
332 | *.binlog
333 |
334 | # NVidia Nsight GPU debugger configuration file
335 | *.nvuser
336 |
337 | # MFractors (Xamarin productivity tool) working folder
338 | .mfractor/
339 |
340 | # Local History for Visual Studio
341 | .localhistory/
342 |
343 | # BeatPulse healthcheck temp database
344 | healthchecksdb
345 |
346 | # Backup folder for Package Reference Convert tool in Visual Studio 2017
347 | MigrationBackup/
348 |
349 | # Ionide (cross platform F# VS Code tools) working folder
350 | .ionide/
351 |
--------------------------------------------------------------------------------
/Desktop Snake.vcxproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Debug
6 | Win32
7 |
8 |
9 | Release
10 | Win32
11 |
12 |
13 | Debug
14 | x64
15 |
16 |
17 | Release
18 | x64
19 |
20 |
21 |
22 | 16.0
23 | {49239BAC-6D6B-46FB-9849-0A2F34057D44}
24 | Win32Proj
25 | DesktopSnake
26 | 10.0
27 |
28 |
29 |
30 | Application
31 | true
32 | v143
33 | Unicode
34 |
35 |
36 | Application
37 | false
38 | v143
39 | true
40 | Unicode
41 |
42 |
43 | Application
44 | true
45 | v143
46 | Unicode
47 |
48 |
49 | Application
50 | false
51 | v143
52 | true
53 | Unicode
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 | true
75 |
76 |
77 | true
78 |
79 |
80 | false
81 |
82 |
83 | false
84 |
85 |
86 |
87 |
88 |
89 | Level3
90 | true
91 | WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)
92 | true
93 |
94 |
95 | Console
96 | true
97 |
98 |
99 |
100 |
101 |
102 |
103 | Level3
104 | true
105 | _DEBUG;_CONSOLE;%(PreprocessorDefinitions)
106 | true
107 |
108 |
109 | Console
110 | true
111 |
112 |
113 |
114 |
115 |
116 |
117 | Level3
118 | true
119 | true
120 | true
121 | WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)
122 | true
123 |
124 |
125 | Console
126 | true
127 | true
128 | true
129 |
130 |
131 |
132 |
133 |
134 |
135 | Level3
136 | true
137 | true
138 | true
139 | NDEBUG;_CONSOLE;%(PreprocessorDefinitions)
140 | true
141 |
142 |
143 | Console
144 | true
145 | true
146 | true
147 |
148 |
149 |
150 |
151 |
152 |
153 |
154 |
155 |
--------------------------------------------------------------------------------
/Desktop Snake.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include
4 | #include
5 | #include
6 | #include
7 | #include
8 | #include
9 | #include
10 | #include
11 | #include
12 | #define KEY_DOWN(VK_NONAME) ((GetAsyncKeyState(VK_NONAME) & 0x8000) ? 1:0)
13 | #define KEY_UP(VK_NONAME) ((GetAsyncKeyState(VK_NONAME) & 0x8000) ? 0:1)
14 | #define DIRECT_NONE 0x0F
15 | #define DIRECT_UP 0x01
16 | #define DIRECT_DOWN 0x02
17 | #define DIRECT_LEFT 0x04
18 | #define DIRECT_RIGHT 0x08
19 | #define SNAKE_BODY 2
20 | #define SNAKE_HEADER 1
21 | #define NOT_SNAKE 0
22 | #define NewGame() \
23 | do{\
24 | moveDirect = DIRECT_NONE;\
25 | snakeLen = 1;\
26 | snakeTailIndex = 0;\
27 | snakeHeaderIndex = 0;\
28 | for (int i = 0; i < iconCnt; i++)\
29 | {\
30 | snakes[i].x = 0;\
31 | snakes[i].y = 0;\
32 | ListView_SetItemPosition(hSysListView32, i, -100, -100);\
33 | }\
34 | ListView_SetItemPosition(hSysListView32, 0, offsetX, offsetY); \
35 | isSnake = std::vector>(blockW, std::vector(blockH, NOT_SNAKE));\
36 | }\
37 | while(0)
38 | #define X2P(pos) (pos * stepX + offsetX)
39 | #define Y2P(pos) (pos * stepY + offsetY)
40 | const WCHAR* TEXT_RES[][2] = {
41 | {TEXT("Snake"), TEXT("贪吃蛇")},
42 | {
43 | TEXT("Use Arrow Key To Move\nF Key Speed Up,S Key Speed Down\nESC Key Exit\nPlease Turn Off Auto Arrange Icons And Align Icons To Grid Before Playing"),
44 | TEXT("使用方向键移动\nF 键可加速,S 键可减速\nESC 键退出\n开始前请关闭自动排列图标和将图标与网格对齐")
45 | },
46 | {TEXT("Error", TEXT("错误"))},
47 | {TEXT("Initialization Fail"), TEXT("初始化失败")},
48 | {TEXT("Game Ended"), TEXT("游戏结束")},
49 | {TEXT("You've Eaten All The Icons, Want To Play Again?"), TEXT("你已经把图标都吃完了,想要再来一遍吗?")},
50 | {TEXT("%d Points"), TEXT("%d分")},
51 | {TEXT("Do You Want To Play Again?"), TEXT("想要重来吗?")}
52 | };
53 | #define TEXT_LANG_EN 0
54 | #define TEXT_LANG_CN_ZH 1
55 | #define TEXT_RES_TITLE 0
56 | #define TEXT_RES_USAGE 1
57 | #define TEXT_RES_ERROR 2
58 | #define TEXT_RES_INIT_FAIL 3
59 | #define TEXT_RES_GAME_OVER 4
60 | #define TEXT_RES_PLAY_AGAIN 5
61 | #define TEXT_RES_POINTS 6
62 | #define TEXT_RES_RETRY 7
63 | int delay = 100;
64 | int stepX = 70;
65 | int stepY = 70;
66 | bool playing = false;
67 | bool pause = false;
68 | int moveDirect = DIRECT_NONE;
69 | HWND hSysListView32 = NULL;
70 | int iconCnt = 0;
71 | PPOINT iconPrevPos;
72 | int useTextRes = TEXT_LANG_EN;
73 | LPCWSTR GetText(int id) {
74 | return TEXT_RES[id][useTextRes];
75 | }
76 | DWORD WINAPI KeyProc(LPVOID args)
77 | {
78 | while (true)
79 | {
80 | if (KEY_DOWN(VK_LEFT) && moveDirect & (DIRECT_DOWN | DIRECT_UP))
81 | {
82 | moveDirect = DIRECT_LEFT;
83 | while (KEY_DOWN(VK_LEFT));
84 | }
85 | else if (KEY_DOWN(VK_RIGHT) && moveDirect & (DIRECT_UP | DIRECT_DOWN))
86 | {
87 | moveDirect = DIRECT_RIGHT;
88 | while (KEY_DOWN(VK_RIGHT));
89 | }
90 | else if (KEY_DOWN(VK_UP) && moveDirect & (DIRECT_LEFT | DIRECT_RIGHT))
91 | {
92 | moveDirect = DIRECT_UP;
93 | while (KEY_DOWN(VK_UP));
94 | }
95 | else if (KEY_DOWN(VK_DOWN) && moveDirect & (DIRECT_LEFT | DIRECT_RIGHT))
96 | {
97 | moveDirect = DIRECT_DOWN;
98 | while (KEY_DOWN(VK_DOWN));
99 | }
100 | else if (KEY_DOWN('P'))
101 | {
102 | pause = !pause;
103 | while (KEY_DOWN('P'));
104 | }
105 | else if (KEY_DOWN('F'))
106 | {
107 | if (delay >= 10) { delay -= 10; }
108 | while (KEY_DOWN('F'));
109 | }
110 | else if (KEY_DOWN('S'))
111 | {
112 | delay += 10;
113 | while (KEY_DOWN('S'));
114 | }
115 | else if (KEY_DOWN(VK_ESCAPE))
116 | {
117 | pause = false;
118 | playing = false;
119 | }
120 | }
121 | }
122 | BOOL CALLBACK FindSysListView32(HWND hwnd, LPARAM lparam)
123 | {
124 | TCHAR a[255];
125 | if (GetClassName(hwnd, a, 255) && lstrcmp(a, TEXT("WorkerW")) == 0)
126 | {
127 | HWND hSHELLDLL_DefView = ::FindWindowEx(hwnd, NULL, TEXT("SHELLDLL_DefView"), NULL);
128 | hSysListView32 = ::FindWindowEx(hSHELLDLL_DefView, NULL, TEXT("SysListView32"), TEXT("FolderView"));
129 | return hSysListView32 == 0;
130 | }
131 | else
132 | {
133 | return TRUE;
134 | }
135 | }
136 | void RecoveryIcon()
137 | {
138 | for (int i = 0; i < iconCnt; i++)
139 | {
140 | ListView_SetItemPosition(hSysListView32, i, iconPrevPos[i].x, iconPrevPos[i].y);
141 | }
142 | ListView_RedrawItems(hSysListView32, 0, iconCnt - 1);
143 | ::UpdateWindow(hSysListView32);
144 | }
145 | BOOL WINAPI BeforeExit(DWORD dwCtrlType)
146 | {
147 | if (dwCtrlType == CTRL_CLOSE_EVENT)
148 | {
149 | RecoveryIcon();
150 | return TRUE;
151 | }
152 | else
153 | {
154 | return FALSE;
155 | }
156 | }
157 | int main()
158 | {
159 | #ifndef _DEBUG
160 | ShowWindow(GetForegroundWindow(), false);
161 | #endif
162 | LANGID langId = GetUserDefaultUILanguage();
163 | if (langId == MAKELANGID(LANG_CHINESE, SUBLANG_CHINESE_SIMPLIFIED)) {
164 | useTextRes = TEXT_LANG_CN_ZH;
165 | }
166 | MessageBox(GetForegroundWindow(), GetText(TEXT_RES_USAGE), GetText(TEXT_RES_TITLE), MB_OK);
167 | SetConsoleCtrlHandler(BeforeExit, TRUE);
168 | HWND hParent = ::FindWindow(TEXT("Progman"), TEXT("Program Manager"));
169 | HWND hSHELLDLL_DefView = ::FindWindowEx(hParent, NULL, TEXT("SHELLDLL_DefView"), NULL);
170 | hSysListView32 = ::FindWindowEx(hSHELLDLL_DefView, NULL, TEXT("SysListView32"), TEXT("FolderView"));
171 | if (hSysListView32 == 0)
172 | {
173 | HWND hDesktop = GetDesktopWindow();
174 | EnumWindows(FindSysListView32, 0);
175 | }
176 | if (hSysListView32 == 0)
177 | {
178 | MessageBox(GetForegroundWindow(), GetText(TEXT_RES_INIT_FAIL), GetText(TEXT_RES_ERROR), MB_OK);
179 | return 0;
180 | }
181 | iconCnt = ListView_GetItemCount(hSysListView32);
182 | COLORREF x = ListView_GetTextColor(hSysListView32);
183 | int r = GetRValue(x);
184 | int g = GetGValue(x);
185 | int b = GetBValue(x);
186 | ListView_SetTextColor(hSysListView32, RGB(0, 0, 0));
187 | ::UpdateWindow(hSysListView32);
188 | ListView_SetTextColor(hSysListView32, x);
189 | iconPrevPos = new POINT[iconCnt];
190 | CreateThread(NULL, 0, KeyProc, NULL, 0, 0);
191 | DWORD processId;
192 | GetWindowThreadProcessId(hSysListView32, &processId);
193 | HANDLE hProcess = OpenProcess(PROCESS_VM_OPERATION | PROCESS_VM_READ | PROCESS_VM_WRITE | PROCESS_QUERY_INFORMATION, FALSE, processId);
194 | RECT rect;
195 | rect.left = LVIR_ICON; rect.right = 0; rect.bottom = 0; rect.top = 0;
196 | LPVOID prect = VirtualAllocEx(hProcess, NULL, sizeof(RECT), MEM_COMMIT, PAGE_READWRITE);
197 | WriteProcessMemory(hProcess, prect, &rect, sizeof(RECT), NULL);
198 | if ((BOOL)::SendMessage(hSysListView32, LVM_GETITEMRECT, 0, (LPARAM)prect))
199 | {
200 | ReadProcessMemory(hProcess, prect, &rect, sizeof(RECT), NULL);
201 | stepX = stepY = rect.bottom - rect.top;
202 | }
203 | VirtualFreeEx(hProcess, prect, 0, MEM_RELEASE);
204 | int realW = GetSystemMetrics(SM_CXVIRTUALSCREEN);
205 | int realH = GetSystemMetrics(SM_CYVIRTUALSCREEN);
206 | int blockW = realW / stepX;
207 | int blockH = realH / stepY;
208 | int offsetX = (realW - blockW * stepX) / 2;
209 | int offsetY = (realH - blockH * stepY) / 2;
210 | srand(time(0));
211 | playing = true;
212 | LPVOID ori = VirtualAllocEx(hProcess, NULL, sizeof(POINT), MEM_COMMIT, PAGE_READWRITE);
213 | for (int i = 0; i < iconCnt; i++)
214 | {
215 | ListView_GetItemPosition(hSysListView32, i, ori);
216 | ReadProcessMemory(hProcess, ori, iconPrevPos + i, sizeof(POINT), NULL);
217 | }
218 | VirtualFreeEx(hProcess, ori, 0, MEM_RELEASE);
219 | CloseHandle(hProcess);
220 | std::vector> isSnake;
221 | PPOINT snakes = new POINT[iconCnt];
222 | int snakeLen, snakeTailIndex, snakeHeaderIndex;
223 | NewGame();
224 | bool newFood = true;
225 | int foodBlockX = 0;
226 | int foodBlockY = 0;
227 | int headerBlockX = 0;
228 | int headerBlockY = 0;
229 | clock_t watch;
230 | while (playing)
231 | {
232 | while (pause);
233 | watch = clock();
234 | if (!newFood && headerBlockX == foodBlockX && headerBlockY == foodBlockY)
235 | {
236 | snakeLen++;
237 | std::cout << snakeLen << std::endl;
238 | if (snakeLen == iconCnt)
239 | {
240 | int ans = MessageBox(GetForegroundWindow(), GetText(TEXT_RES_PLAY_AGAIN), GetText(TEXT_RES_GAME_OVER), MB_YESNO | MB_ICONQUESTION);
241 | if (ans == IDYES)
242 | {
243 | NewGame();
244 | newFood = true;
245 | }
246 | else
247 | {
248 | break;
249 | }
250 | }
251 | newFood = true;
252 | }
253 | if (newFood)
254 | {
255 | do
256 | {
257 | foodBlockX = rand() % blockW;
258 | foodBlockY = rand() % blockH;
259 | } while (isSnake[foodBlockX][foodBlockY] != NOT_SNAKE);
260 | newFood = false;
261 | ListView_SetItemPosition(hSysListView32, snakeLen, X2P(foodBlockX), Y2P(foodBlockY));
262 | #ifdef _DEBUG
263 | std::cout << "Food Pos:" << foodBlockX << "," << foodBlockY << std::endl;
264 | #endif
265 | }
266 | if (moveDirect != DIRECT_NONE)
267 | {
268 | isSnake[snakes[snakeHeaderIndex].x][snakes[snakeHeaderIndex].y] = SNAKE_BODY;
269 | isSnake[snakes[snakeTailIndex].x][snakes[snakeTailIndex].y] = NOT_SNAKE;
270 | headerBlockX = snakes[snakeTailIndex].x = (snakes[snakeHeaderIndex].x
271 | + (moveDirect == DIRECT_RIGHT ? 1 : moveDirect == DIRECT_LEFT ? -1 : 0) + blockW) % blockW;
272 | headerBlockY = snakes[snakeTailIndex].y = (snakes[snakeHeaderIndex].y
273 | + (moveDirect == DIRECT_DOWN ? 1 : moveDirect == DIRECT_UP ? -1 : 0) + blockH) % blockH;
274 | if (isSnake[headerBlockX][headerBlockY] == SNAKE_BODY)
275 | {
276 | WCHAR buf[255];
277 | swprintf_s(buf, GetText(TEXT_RES_POINTS), snakeLen);
278 | int ans = MessageBox(GetForegroundWindow(), GetText(TEXT_RES_RETRY), buf, MB_YESNO | MB_ICONQUESTION);
279 | if (ans == IDYES)
280 | {
281 | NewGame();
282 | newFood = true;
283 | }
284 | else
285 | {
286 | break;
287 | }
288 | }
289 | isSnake[headerBlockX][headerBlockY] = SNAKE_HEADER;
290 | snakeHeaderIndex = snakeTailIndex;
291 | if (snakeTailIndex == 0) { snakeTailIndex = snakeLen - 1; }
292 | else { snakeTailIndex--; }
293 | ListView_SetItemPosition(hSysListView32, snakeHeaderIndex, X2P(headerBlockX), Y2P(headerBlockY));
294 | #ifdef _DEBUG
295 | std::cout << "Snake Header Pos:" << headerBlockX << "," << headerBlockY << std::endl;
296 | #endif
297 | }
298 | //ListView_RedrawItems(hSysListView32, 0, iconCnt - 1);
299 | ::UpdateWindow(hSysListView32);
300 | Sleep((delay - clock() + watch) > 0 ? (delay - clock() + watch) : 0);
301 | }
302 | RecoveryIcon();
303 | delete[] snakes;
304 | delete[] iconPrevPos;
305 | return 0;
306 | }
--------------------------------------------------------------------------------