Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

435 lines
17 KiB

  1. #include "CPerson.h"
  2. #include "CSweeper.h"
  3. #include "CSqueegee.h"
  4. #include <mmsystem.h>
  5. #include <commctrl.h>
  6. #include <stdio.h>
  7. #include <commdlg.h>
  8. enum ENUM_CLEANER {
  9. eSweeper=0,
  10. eSqueegee,
  11. eRandom
  12. };
  13. #define MAXOBJECTS 100
  14. #define CROWDSPEED_DEFAULT 19
  15. #define RANDOMCLEANINTERVAL_DEFAULT 1
  16. #define CLEANDELAY_DEFAULT 10
  17. #define USEBITMAP_DEFAULT 0
  18. #define FILENAME_DEFAULT "GDI+ Logo"
  19. #define CLEANER_DEFAULT eSweeper
  20. #define CROWDSIZE_DEFAULT 3
  21. char szCrowdSpeed[]="CrowdSpeed";
  22. char szRandomCleanInterval[]="RandomCleanInterval";
  23. char szCleanDelay[]="CleanDelay";
  24. char szUseBitmap[]="UseBitmap";
  25. char szFilename[]="Filename";
  26. char szCleaner[]="Cleaner";
  27. char szCrowdSize[]="CrowdSize";
  28. int g_cObjectMax;
  29. CObject *g_rgpaObject[MAXOBJECTS];
  30. CObject *g_paCleaner;
  31. TextureBrush *g_paBrCleanBkg=NULL;
  32. Bitmap *g_paBmDirtyBkg=NULL;
  33. DWORD g_dwCleanDelay;
  34. DWORD g_dwSimulateDelay;
  35. BOOL g_bRandomCleanInterval;
  36. BOOL g_bUseBitmap;
  37. ENUM_CLEANER g_eCleaner;
  38. char g_szFile[MAX_PATH];
  39. BOOL WINAPI RegisterDialogClasses(HANDLE hInst)
  40. {
  41. return TRUE;
  42. }
  43. void Browse(HWND hDlg)
  44. {
  45. char szFile[MAX_PATH];
  46. OPENFILENAME ofn;
  47. HWND hWnd;
  48. ZeroMemory(szFile,sizeof(szFile));
  49. ZeroMemory(&ofn,sizeof(OPENFILENAME));
  50. ofn.lStructSize=sizeof(OPENFILENAME);
  51. ofn.hwndOwner=hDlg;
  52. ofn.lpstrFile=szFile;
  53. ofn.nMaxFile=sizeof(szFile);
  54. ofn.lpstrFilter="All Supported\0*.bmp;*.jpg;*.jpeg;*.tif;*.tiff;*.png;*.gif;*.wmf;*.ico;*.emf\0Bitmap\0*.bmp\0JPEG\0*.jpg;*.jpeg\0TIFF\0*.tif;*.tiff\0PNG\0*.png\0GIF\0*.gif\0WMF\0*.wmf\0ICON\0*.ico\0EMF\0*.emf\0";
  55. ofn.nFilterIndex=1;
  56. ofn.lpstrFileTitle=NULL;
  57. ofn.nMaxFileTitle=0;
  58. ofn.lpstrInitialDir=NULL;
  59. ofn.Flags=OFN_PATHMUSTEXIST|OFN_FILEMUSTEXIST|OFN_EXPLORER;
  60. if (GetOpenFileName(&ofn)) {
  61. hWnd=GetDlgItem(hDlg,IDC_FILENAME);
  62. SendMessage(hWnd,WM_SETTEXT,0,(LPARAM)szFile);
  63. }
  64. }
  65. BOOL WINAPI ScreenSaverConfigureDialog(HWND hDlg,UINT Msg,WPARAM wParam,LPARAM lParam)
  66. {
  67. HWND hWnd;
  68. LONG lCrowdSpeed=CROWDSPEED_DEFAULT;
  69. char szTemp[MAX_PATH];
  70. switch(Msg) {
  71. case WM_INITDIALOG:
  72. InitCommonControls();
  73. LoadString(hMainInstance,idsAppName,szAppName,40);
  74. LoadString(hMainInstance,idsIniFile,szIniFile,MAXFILELEN);
  75. lCrowdSpeed=GetPrivateProfileInt(szAppName,szCrowdSpeed,CROWDSPEED_DEFAULT,szIniFile);
  76. g_bRandomCleanInterval=GetPrivateProfileInt(szAppName,szRandomCleanInterval,RANDOMCLEANINTERVAL_DEFAULT,szIniFile);
  77. g_dwCleanDelay=GetPrivateProfileInt(szAppName,szCleanDelay,CLEANDELAY_DEFAULT,szIniFile);
  78. g_bUseBitmap=GetPrivateProfileInt(szAppName,szUseBitmap,USEBITMAP_DEFAULT,szIniFile);
  79. GetPrivateProfileString(szAppName,szFilename,FILENAME_DEFAULT,g_szFile,MAX_PATH,szIniFile);
  80. g_eCleaner=(ENUM_CLEANER)GetPrivateProfileInt(szAppName,szCleaner,CLEANER_DEFAULT,szIniFile);
  81. g_cObjectMax=GetPrivateProfileInt(szAppName,szCrowdSize,CROWDSIZE_DEFAULT,szIniFile);
  82. hWnd=GetDlgItem(hDlg,IDC_CROWDSPEED);
  83. SendMessage(hWnd,TBM_SETRANGE,(WPARAM)true,(LPARAM)MAKELONG(0,20));
  84. SendMessage(hWnd,TBM_SETPAGESIZE,0,(LPARAM)20/8);
  85. SendMessage(hWnd,TBM_SETPOS,(WPARAM)true,(LPARAM)lCrowdSpeed);
  86. hWnd=GetDlgItem(hDlg,IDC_CROWDSIZE);
  87. SendMessage(hWnd,TBM_SETRANGE,(WPARAM)true,(LPARAM)MAKELONG(1,10));
  88. SendMessage(hWnd,TBM_SETPAGESIZE,0,(LPARAM)1);
  89. SendMessage(hWnd,TBM_SETPOS,(WPARAM)true,(LPARAM)g_cObjectMax);
  90. hWnd=GetDlgItem(hDlg,IDC_FILENAME);
  91. SendMessage(hWnd,WM_SETTEXT,0,(LPARAM)g_szFile);
  92. hWnd=GetDlgItem(hDlg,IDC_USEBITMAP);
  93. if (g_bUseBitmap) {
  94. SendMessage(hWnd,BM_SETCHECK,(WPARAM)true,0);
  95. }
  96. else {
  97. hWnd=GetDlgItem(hDlg,IDC_BROWSE);
  98. EnableWindow(hWnd,false);
  99. hWnd=GetDlgItem(hDlg,IDC_FILENAME);
  100. EnableWindow(hWnd,false);
  101. }
  102. if (g_bRandomCleanInterval) {
  103. hWnd=GetDlgItem(hDlg,IDC_CLEANERRANDOMINTERVAL);
  104. SendMessage(hWnd,BM_SETCHECK,(WPARAM)true,0);
  105. }
  106. else {
  107. hWnd=GetDlgItem(hDlg,IDC_CLEANERGIVENINTERVAL);
  108. SendMessage(hWnd,BM_SETCHECK,(WPARAM)true,0);
  109. }
  110. hWnd=GetDlgItem(hDlg,IDC_CLEANERGIVENINTERVAL);
  111. if (SendMessage(hWnd,BM_GETCHECK,0,0)==BST_CHECKED) {
  112. hWnd=GetDlgItem(hDlg,IDC_CLEANERINTERVAL);
  113. EnableWindow(hWnd,true);
  114. }
  115. else {
  116. hWnd=GetDlgItem(hDlg,IDC_CLEANERINTERVAL);
  117. EnableWindow(hWnd,false);
  118. }
  119. switch(g_eCleaner) {
  120. case eSweeper:
  121. hWnd=GetDlgItem(hDlg,IDC_SWEEPER);
  122. SendMessage(hWnd,BM_SETCHECK,(WPARAM)true,0);
  123. break;
  124. case eSqueegee:
  125. hWnd=GetDlgItem(hDlg,IDC_SQUEEGEE);
  126. SendMessage(hWnd,BM_SETCHECK,(WPARAM)true,0);
  127. break;
  128. case eRandom:
  129. hWnd=GetDlgItem(hDlg,IDC_RANDOMCLEANER);
  130. SendMessage(hWnd,BM_SETCHECK,(WPARAM)true,0);
  131. break;
  132. }
  133. hWnd=GetDlgItem(hDlg,IDC_CLEANERINTERVAL);
  134. _itoa(g_dwCleanDelay,szTemp,10);
  135. SendMessage(hWnd,WM_SETTEXT,0,(LPARAM)szTemp);
  136. return TRUE;
  137. case WM_COMMAND:
  138. switch(LOWORD(wParam)) {
  139. case IDC_USEBITMAP:
  140. if (HIWORD(wParam)==BN_CLICKED) {
  141. hWnd=GetDlgItem(hDlg,IDC_BROWSE);
  142. EnableWindow(hWnd,!IsWindowEnabled(hWnd));
  143. hWnd=GetDlgItem(hDlg,IDC_FILENAME);
  144. EnableWindow(hWnd,!IsWindowEnabled(hWnd));
  145. }
  146. break;
  147. case IDC_CLEANERGIVENINTERVAL:
  148. hWnd=GetDlgItem(hDlg,IDC_CLEANERINTERVAL);
  149. EnableWindow(hWnd,true);
  150. break;
  151. case IDC_CLEANERRANDOMINTERVAL:
  152. hWnd=GetDlgItem(hDlg,IDC_CLEANERINTERVAL);
  153. EnableWindow(hWnd,false);
  154. break;
  155. case IDC_BROWSE:
  156. Browse(hDlg);
  157. break;
  158. case ID_OK:
  159. hWnd=GetDlgItem(hDlg,IDC_FILENAME);
  160. SendMessage(hWnd,WM_GETTEXT,(WPARAM)MAX_PATH,(LPARAM)g_szFile);
  161. hWnd=GetDlgItem(hDlg,IDC_CROWDSPEED);
  162. lCrowdSpeed=SendMessage(hWnd,TBM_GETPOS,0,0);
  163. sprintf(szTemp,"%ld",lCrowdSpeed);
  164. WritePrivateProfileString(szAppName,szCrowdSpeed,szTemp,szIniFile);
  165. hWnd=GetDlgItem(hDlg,IDC_CROWDSIZE);
  166. g_cObjectMax=SendMessage(hWnd,TBM_GETPOS,0,0);
  167. sprintf(szTemp,"%ld",g_cObjectMax);
  168. WritePrivateProfileString(szAppName,szCrowdSize,szTemp,szIniFile);
  169. hWnd=GetDlgItem(hDlg,IDC_CLEANERRANDOMINTERVAL);
  170. g_bRandomCleanInterval=SendMessage(hWnd,BM_GETCHECK,0,0)==BST_CHECKED;
  171. sprintf(szTemp,"%ld",g_bRandomCleanInterval);
  172. WritePrivateProfileString(szAppName,szRandomCleanInterval,szTemp,szIniFile);
  173. hWnd=GetDlgItem(hDlg,IDC_CLEANERINTERVAL);
  174. SendMessage(hWnd,WM_GETTEXT,(WPARAM)MAX_PATH,(LPARAM)szTemp);
  175. WritePrivateProfileString(szAppName,szCleanDelay,szTemp,szIniFile);
  176. hWnd=GetDlgItem(hDlg,IDC_USEBITMAP);
  177. g_bUseBitmap=SendMessage(hWnd,BM_GETCHECK,0,0)==BST_CHECKED;
  178. sprintf(szTemp,"%ld",g_bUseBitmap);
  179. WritePrivateProfileString(szAppName,szUseBitmap,szTemp,szIniFile);
  180. hWnd=GetDlgItem(hDlg,IDC_FILENAME);
  181. SendMessage(hWnd,WM_GETTEXT,(WPARAM)MAX_PATH,(LPARAM)szTemp);
  182. WritePrivateProfileString(szAppName,szFilename,szTemp,szIniFile);
  183. hWnd=GetDlgItem(hDlg,IDC_SWEEPER);
  184. if (SendMessage(hWnd,BM_GETCHECK,0,0)==BST_CHECKED) {
  185. g_eCleaner=eSweeper;
  186. }
  187. hWnd=GetDlgItem(hDlg,IDC_SQUEEGEE);
  188. if (SendMessage(hWnd,BM_GETCHECK,0,0)==BST_CHECKED) {
  189. g_eCleaner=eSqueegee;
  190. }
  191. hWnd=GetDlgItem(hDlg,IDC_RANDOMCLEANER);
  192. if (SendMessage(hWnd,BM_GETCHECK,0,0)==BST_CHECKED) {
  193. g_eCleaner=eRandom;
  194. }
  195. sprintf(szTemp,"%ld",g_eCleaner);
  196. WritePrivateProfileString(szAppName,szCleaner,szTemp,szIniFile);
  197. case ID_CANCEL:
  198. EndDialog(hDlg,LOWORD(wParam)==ID_OK);
  199. return TRUE;
  200. }
  201. }
  202. return FALSE;
  203. }
  204. LRESULT WINAPI ScreenSaverProc(HWND hWnd,UINT Msg,WPARAM wParam,LPARAM lParam)
  205. {
  206. static bool GdiplusInitialized = false;
  207. static ULONG_PTR gpToken;
  208. const UINT_PTR NEWCLEANER_TIMERID=1;
  209. const UINT_PTR SIMULATE_TIMERID=2;
  210. static bFirstRun=true;
  211. Graphics *g;
  212. int i;
  213. int nRand;
  214. RECT rDesktop;
  215. HDC hdcBkgBitmap;
  216. HDC hdcScreen;
  217. TextureBrush *paBrDirtyBkg=NULL;
  218. Bitmap *paBmCleanBkg=NULL;
  219. WCHAR wsFile[MAX_PATH];
  220. LONG lCrowdSpeed;
  221. GdiplusStartupInput sti;
  222. switch(Msg) {
  223. case WM_CREATE:
  224. // Initialize GDI+
  225. if (GdiplusStartup(&gpToken, &sti, NULL) == Ok)
  226. {
  227. GdiplusInitialized = true;
  228. }
  229. else
  230. {
  231. break;
  232. }
  233. LoadString(hMainInstance,idsAppName,szAppName,10);
  234. LoadString(hMainInstance,idsIniFile,szIniFile,MAXFILELEN);
  235. // Load variables
  236. lCrowdSpeed=GetPrivateProfileInt(szAppName,szCrowdSpeed,CROWDSPEED_DEFAULT,szIniFile);
  237. g_dwSimulateDelay=(20-lCrowdSpeed)*10;
  238. g_bRandomCleanInterval=GetPrivateProfileInt(szAppName,szRandomCleanInterval,RANDOMCLEANINTERVAL_DEFAULT,szIniFile);
  239. g_dwCleanDelay=GetPrivateProfileInt(szAppName,szCleanDelay,CLEANDELAY_DEFAULT,szIniFile)*1000;
  240. g_bUseBitmap=GetPrivateProfileInt(szAppName,szUseBitmap,USEBITMAP_DEFAULT,szIniFile);
  241. GetPrivateProfileString(szAppName,szFilename,FILENAME_DEFAULT,g_szFile,MAX_PATH,szIniFile);
  242. g_eCleaner=(ENUM_CLEANER)GetPrivateProfileInt(szAppName,szCleaner,CLEANER_DEFAULT,szIniFile);
  243. g_cObjectMax=GetPrivateProfileInt(szAppName,szCrowdSize,CROWDSIZE_DEFAULT,szIniFile);
  244. srand((unsigned int)timeGetTime());
  245. // Get desktop dimensions
  246. GetClientRect(hWnd,&rDesktop);
  247. g_paBmDirtyBkg=new Bitmap(rDesktop.right,rDesktop.bottom,PixelFormat32bppPARGB);
  248. if (g_bUseBitmap) {
  249. mbstowcs(wsFile,g_szFile,sizeof(g_szFile));
  250. paBmCleanBkg=new Bitmap(wsFile);
  251. if (paBmCleanBkg->GetLastStatus()!=Ok) {
  252. delete paBmCleanBkg;
  253. paBmCleanBkg=new Bitmap((HINSTANCE)GetModuleHandle(NULL),(WCHAR*)MAKEINTRESOURCE(IDB_LOGO));
  254. }
  255. // Take snapshot of whole desktop for DirtyBkg
  256. g=new Graphics(g_paBmDirtyBkg);
  257. hdcBkgBitmap=g->GetHDC();
  258. hdcScreen=CreateDC("DISPLAY",NULL,NULL,NULL);
  259. StretchBlt(hdcBkgBitmap,0,0,rDesktop.right,rDesktop.bottom,hdcScreen,GetSystemMetrics(SM_XVIRTUALSCREEN),GetSystemMetrics(SM_YVIRTUALSCREEN),rDesktop.right,rDesktop.bottom,SRCCOPY);
  260. g->ReleaseHDC(hdcBkgBitmap);
  261. DeleteDC(hdcScreen);
  262. delete g;
  263. }
  264. else {
  265. // Take snapshot of whole desktop for CleanBkg
  266. paBmCleanBkg=new Bitmap(rDesktop.right,rDesktop.bottom,PixelFormat32bppPARGB);
  267. g=new Graphics(paBmCleanBkg);
  268. hdcBkgBitmap=g->GetHDC();
  269. hdcScreen=CreateDC("DISPLAY",NULL,NULL,NULL);
  270. StretchBlt(hdcBkgBitmap,0,0,rDesktop.right,rDesktop.bottom,hdcScreen,GetSystemMetrics(SM_XVIRTUALSCREEN),GetSystemMetrics(SM_YVIRTUALSCREEN),rDesktop.right,rDesktop.bottom,SRCCOPY);
  271. g->ReleaseHDC(hdcBkgBitmap);
  272. DeleteDC(hdcScreen);
  273. delete g;
  274. g=new Graphics(g_paBmDirtyBkg);
  275. g->DrawImage(paBmCleanBkg,0,0,0,0,rDesktop.right,rDesktop.bottom,UnitPixel);
  276. delete g;
  277. }
  278. // Make brush out of clean desktop
  279. g_paBrCleanBkg=new TextureBrush(paBmCleanBkg,WrapModeTile);
  280. delete paBmCleanBkg;
  281. for (i=0;i<g_cObjectMax;i++) {
  282. g_rgpaObject[i]=new CPerson();
  283. g_rgpaObject[i]->Init(hWnd);
  284. }
  285. SetTimer(hWnd,NEWCLEANER_TIMERID,g_dwCleanDelay,NULL);
  286. SetTimer(hWnd,SIMULATE_TIMERID,g_dwSimulateDelay,NULL);
  287. return 0;
  288. case WM_ERASEBKGND:
  289. return 0;
  290. case WM_TIMER:
  291. if(!GdiplusInitialized)
  292. {
  293. break;
  294. }
  295. switch (wParam) {
  296. case NEWCLEANER_TIMERID:
  297. if (g_paCleaner==NULL) {
  298. switch (g_eCleaner) {
  299. case eSweeper:
  300. g_paCleaner=new CSweeper();
  301. break;
  302. case eSqueegee:
  303. g_paCleaner=new CSqueegee();
  304. break;
  305. case eRandom:
  306. nRand=rand();
  307. if (nRand<RAND_MAX/2) {
  308. g_paCleaner=new CSweeper();
  309. }
  310. else {
  311. g_paCleaner=new CSqueegee();
  312. }
  313. break;
  314. }
  315. g_paCleaner->Init(hWnd);
  316. }
  317. if (g_bRandomCleanInterval) {
  318. nRand=rand();
  319. g_dwCleanDelay=(int)(((float)rand()/(float)RAND_MAX)*15000.0f+10000.0f);
  320. SetTimer(hWnd,NEWCLEANER_TIMERID,g_dwCleanDelay,NULL);
  321. }
  322. break;
  323. case SIMULATE_TIMERID:
  324. g=Graphics::FromHWND(hWnd);
  325. if (bFirstRun) {
  326. // Blit to screen (used inside demo window in the display dialog box)
  327. bFirstRun=false;
  328. GetClientRect(hWnd,&rDesktop);
  329. g->DrawImage(g_paBmDirtyBkg,0,0,0,0,rDesktop.right,rDesktop.bottom,UnitPixel);
  330. }
  331. g->SetSmoothingMode(SmoothingModeNone);
  332. g->SetInterpolationMode(InterpolationModeNearestNeighbor);
  333. for (i=0;i<g_cObjectMax;i++) {
  334. if (!g_rgpaObject[i]->Move(g)) {
  335. // Moved outside desktop, delete it and recreate another
  336. delete g_rgpaObject[i];
  337. g_rgpaObject[i]=new CPerson();
  338. g_rgpaObject[i]->Init(hWnd);
  339. }
  340. }
  341. if (g_paCleaner!=NULL) {
  342. if (!g_paCleaner->Move(g)) {
  343. // Moved outside desktop, delete it and wait for the next
  344. // NEWCLEANER_TIMERID to hit
  345. delete g_paCleaner;
  346. g_paCleaner=NULL;
  347. }
  348. }
  349. delete g;
  350. break;
  351. }
  352. break;
  353. case WM_DESTROY:
  354. if (!GdiplusInitialized)
  355. {
  356. break;
  357. }
  358. if (g_paBrCleanBkg!=NULL) {
  359. delete g_paBrCleanBkg;
  360. g_paBrCleanBkg=NULL;
  361. }
  362. if (g_paBmDirtyBkg!=NULL) {
  363. delete g_paBmDirtyBkg;
  364. g_paBmDirtyBkg=NULL;
  365. }
  366. for (i=0;i<g_cObjectMax;i++) {
  367. if (g_rgpaObject[i]!=NULL) {
  368. delete g_rgpaObject[i];
  369. g_rgpaObject[i]=NULL;
  370. }
  371. }
  372. if (g_paCleaner!=NULL) {
  373. delete g_paCleaner;
  374. g_paCleaner=NULL;
  375. }
  376. KillTimer(hWnd,NEWCLEANER_TIMERID);
  377. KillTimer(hWnd,SIMULATE_TIMERID);
  378. GdiplusShutdown(gpToken);
  379. GdiplusInitialized = false;
  380. break;
  381. }
  382. return DefScreenSaverProc(hWnd, Msg, wParam, lParam);
  383. }