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.

526 lines
14 KiB

  1. #include "precomp.h"
  2. #pragma hdrstop
  3. extern "C"
  4. {
  5. #include <stdexts.h>
  6. };
  7. #define ARRAYSIZE(x) (sizeof(x)/sizeof(x[0]))
  8. #define MIN_WIDTH 96
  9. #define MIN_HEIGHT 64
  10. #define BORDER_WIDTH 32
  11. typedef struct _DRAW_ICON_STRUCT
  12. {
  13. HBITMAP hBitmap;
  14. BYTE* psBits;
  15. BITMAPINFO bmi;
  16. HICON hIcon;
  17. int zoom;
  18. } DRAW_ICON_STRUCT;
  19. /************************************************************************\
  20. * Procedure: CreateDIBSectionFromIcon
  21. *
  22. * Description:
  23. *
  24. * Creates a DIB section given the handle to an icon
  25. *
  26. * 3/28/2001 - Created bretan
  27. *
  28. \************************************************************************/
  29. BOOL CreateDIBSectionFromIcon(DRAW_ICON_STRUCT* pDIS)
  30. {
  31. ICONINFO info;
  32. if (!pDIS->hIcon)
  33. {
  34. Print("Invalid HICON handle\n");
  35. return FALSE;
  36. }
  37. if (GetIconInfo (pDIS->hIcon, &info))
  38. {
  39. HDC hdc = NULL;
  40. HDC dc = NULL;
  41. HBRUSH hBrush = NULL;
  42. BITMAP bitmap;
  43. hdc = CreateDC(L"DISPLAY", NULL, NULL, NULL);
  44. if (!hdc)
  45. {
  46. Print("CreateDC failed\n");
  47. return FALSE;
  48. }
  49. dc = CreateCompatibleDC(hdc);
  50. if (!dc)
  51. {
  52. Print("CreateCompatiblDC failed\n");
  53. return FALSE;
  54. }
  55. if (!GetObject (info.hbmColor, sizeof (BITMAP), &bitmap))
  56. {
  57. Print("GetObject failed\n");
  58. return FALSE;
  59. }
  60. pDIS->bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
  61. pDIS->bmi.bmiHeader.biWidth = bitmap.bmWidth;
  62. pDIS->bmi.bmiHeader.biHeight = bitmap.bmHeight;
  63. pDIS->bmi.bmiHeader.biPlanes = 1;
  64. pDIS->bmi.bmiHeader.biBitCount = bitmap.bmPlanes * bitmap.bmBitsPixel;
  65. if (pDIS->bmi.bmiHeader.biBitCount <= 1)
  66. {
  67. pDIS->bmi.bmiHeader.biBitCount = 1;
  68. }
  69. else if (pDIS->bmi.bmiHeader.biBitCount <= 4)
  70. {
  71. pDIS->bmi.bmiHeader.biBitCount = 4;
  72. }
  73. else if (pDIS->bmi.bmiHeader.biBitCount <= 8)
  74. {
  75. pDIS->bmi.bmiHeader.biBitCount = 8;
  76. }
  77. else
  78. {
  79. pDIS->bmi.bmiHeader.biBitCount = 24;
  80. }
  81. pDIS->bmi.bmiHeader.biCompression = BI_RGB;
  82. // Create a DIB section so we don't have to worry about the display settings
  83. pDIS->hBitmap = CreateDIBSection(dc, (const BITMAPINFO *)&pDIS->bmi, DIB_RGB_COLORS, (void**)&pDIS->psBits, NULL, 0);
  84. if (pDIS->hBitmap == NULL)
  85. {
  86. Print("CreateDIBSection failed\n");
  87. return FALSE;
  88. }
  89. SelectObject (dc, pDIS->hBitmap);
  90. hBrush = (HBRUSH)GetStockObject(BLACK_BRUSH);
  91. DrawIconEx (dc, 0, 0, pDIS->hIcon, 0, 0, 0, hBrush, DI_NORMAL);
  92. if (NULL == pDIS->psBits)
  93. {
  94. Print("CreateDIBSection failed.\n");
  95. return FALSE;
  96. }
  97. DeleteObject (info.hbmMask);
  98. DeleteObject (info.hbmColor);
  99. DeleteDC (hdc);
  100. DeleteDC (dc);
  101. }
  102. return TRUE;
  103. }
  104. /************************************************************************\
  105. * Procedure: ZoomIn
  106. *
  107. * Description:
  108. *
  109. * Zooms the window in by a factor of 2
  110. *
  111. * 3/28/2001 - Created bretan
  112. *
  113. \************************************************************************/
  114. void ZoomIn(HWND hwnd)
  115. {
  116. DRAW_ICON_STRUCT* pDIS = (DRAW_ICON_STRUCT*)GetWindowLongPtr(hwnd, GWLP_USERDATA);
  117. int w, h;
  118. pDIS->zoom++;
  119. w = pDIS->bmi.bmiHeader.biWidth * pDIS->zoom + BORDER_WIDTH;
  120. h = pDIS->bmi.bmiHeader.biHeight * pDIS->zoom + BORDER_WIDTH;
  121. if (w < MIN_WIDTH) w = MIN_WIDTH;
  122. if (h < MIN_HEIGHT) h = MIN_HEIGHT;
  123. SetWindowPos(hwnd, 0, 0, 0, w, h, SWP_NOZORDER | SWP_NOMOVE);
  124. InvalidateRect(hwnd, NULL, TRUE);
  125. }
  126. /************************************************************************\
  127. * Procedure: ZoomOut
  128. *
  129. * Description:
  130. *
  131. * Zooms the window out by a factor of 2
  132. *
  133. * 3/28/2001 - Created bretan
  134. *
  135. \************************************************************************/
  136. void ZoomOut(HWND hwnd)
  137. {
  138. DRAW_ICON_STRUCT* pDIS = (DRAW_ICON_STRUCT*)GetWindowLongPtr(hwnd, GWLP_USERDATA);
  139. pDIS->zoom--;
  140. if (pDIS->zoom < 1)
  141. {
  142. pDIS->zoom = 1;
  143. }
  144. else
  145. {
  146. int w = pDIS->bmi.bmiHeader.biWidth * pDIS->zoom + BORDER_WIDTH;
  147. int h = pDIS->bmi.bmiHeader.biHeight * pDIS->zoom + BORDER_WIDTH;
  148. if (w < MIN_WIDTH) w = MIN_WIDTH;
  149. if (h < MIN_HEIGHT) h = MIN_HEIGHT;
  150. SetWindowPos(hwnd, 0, 0, 0, w, h, SWP_NOZORDER | SWP_NOMOVE);
  151. InvalidateRect(hwnd, NULL, TRUE);
  152. }
  153. }
  154. /************************************************************************\
  155. * Procedure: WndProc
  156. *
  157. * Description:
  158. *
  159. * The wndproc for the icon window
  160. *
  161. * 3/28/2001 - Created bretan
  162. *
  163. \************************************************************************/
  164. LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
  165. {
  166. switch (msg)
  167. {
  168. case WM_NCHITTEST: return HTCAPTION;
  169. case WM_PAINT:
  170. {
  171. PAINTSTRUCT ps;
  172. HDC hdc = BeginPaint(hwnd, &ps);
  173. HDC buf = CreateCompatibleDC(hdc);
  174. HBITMAP oldbuf;
  175. RECT r;
  176. DRAW_ICON_STRUCT* pDIS = (DRAW_ICON_STRUCT*)GetWindowLongPtr(hwnd, GWLP_USERDATA);
  177. GetClientRect(hwnd, &r);
  178. FillRect(hdc, &r, (HBRUSH)GetStockObject(WHITE_BRUSH));
  179. if (pDIS)
  180. {
  181. int x, y, w, h;
  182. w = pDIS->bmi.bmiHeader.biWidth * pDIS->zoom;
  183. h = pDIS->bmi.bmiHeader.biHeight * pDIS->zoom;
  184. x = r.right/2 - w/2;
  185. y = r.bottom/2 - h/2;
  186. oldbuf = (HBITMAP)SelectObject(buf, pDIS->hBitmap);
  187. StretchBlt(hdc, x, y, w, h, buf, 0, 0, w/pDIS->zoom, h/pDIS->zoom, SRCCOPY);
  188. }
  189. SelectObject(buf, oldbuf);
  190. DeleteDC(buf);
  191. EndPaint(hwnd, &ps);
  192. }
  193. break;
  194. case WM_NCLBUTTONDBLCLK:
  195. {
  196. ZoomIn(hwnd);
  197. }
  198. break;
  199. case WM_NCRBUTTONDBLCLK:
  200. {
  201. ZoomOut(hwnd);
  202. }
  203. break;
  204. case WM_KEYDOWN:
  205. {
  206. switch (wParam)
  207. {
  208. case 187: // '+'
  209. {
  210. ZoomIn(hwnd);
  211. }
  212. break;
  213. case 189: // '-'
  214. {
  215. ZoomOut(hwnd);
  216. }
  217. break;
  218. case 81: // 'q'
  219. {
  220. PostQuitMessage(0);
  221. }
  222. break;
  223. /*default:
  224. {
  225. TCHAR szTemp[256];
  226. wsprintf(szTemp, L"%d", wParam);
  227. MessageBox(hwnd, szTemp, szTemp, MB_OK);
  228. }
  229. break;*/
  230. }
  231. break;
  232. }
  233. break;
  234. case WM_SYSCOMMAND:
  235. {
  236. switch (wParam)
  237. {
  238. case SC_CLOSE:
  239. {
  240. PostQuitMessage(0);
  241. }
  242. break;
  243. }
  244. }
  245. break;
  246. case WM_TIMER:
  247. {
  248. if (IsCtrlCHit())
  249. {
  250. PostQuitMessage(0);
  251. }
  252. }
  253. break;
  254. }
  255. return DefWindowProc(hwnd, msg, wParam, lParam);
  256. }
  257. /************************************************************************\
  258. * Procedure: DrawIconWindow
  259. *
  260. * Description:
  261. *
  262. * Draws the icon in a window on the remote side
  263. *
  264. * 3/28/2001 - Created bretan
  265. *
  266. \************************************************************************/
  267. DWORD WINAPI DrawIconWindow(LPVOID inpDIS)
  268. {
  269. MSG msg;
  270. WNDCLASS wc;
  271. HWND hwnd;
  272. DRAW_ICON_STRUCT* pDIS = (DRAW_ICON_STRUCT*)inpDIS;
  273. LPCTSTR szClassName = L"ntsd - drawicon";
  274. TCHAR szTitle[32] = {0};
  275. int w, h;
  276. memset(&wc,0,sizeof(wc));
  277. wc.lpfnWndProc = WndProc;
  278. wc.hInstance = NULL;
  279. wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  280. wc.style = CS_DBLCLKS;
  281. wc.lpszClassName = szClassName;
  282. RegisterClass(&wc);
  283. w = pDIS->bmi.bmiHeader.biWidth + BORDER_WIDTH;
  284. h = pDIS->bmi.bmiHeader.biHeight + BORDER_WIDTH;
  285. if (w < MIN_WIDTH) w = MIN_WIDTH;
  286. if (h < MIN_HEIGHT) h = MIN_HEIGHT;
  287. StringCchPrintf(szTitle, ARRAYSIZE(szTitle), TEXT("%08x"), pDIS->hIcon);
  288. hwnd = CreateWindowEx(WS_EX_TOOLWINDOW,
  289. szClassName, szTitle,
  290. WS_POPUPWINDOW | WS_CAPTION | WS_VISIBLE,
  291. 10, 10,
  292. w, h,
  293. NULL, NULL, NULL, NULL);
  294. if (!hwnd) return FALSE;
  295. SetWindowLongPtr(hwnd, GWLP_USERDATA, (LONG_PTR)pDIS);
  296. SetTimer(hwnd, 0, 100, NULL);
  297. SetForegroundWindow(hwnd);
  298. SetFocus(hwnd);
  299. while (GetMessage(&msg, (HWND) NULL, 0, 0))
  300. {
  301. TranslateMessage(&msg);
  302. DispatchMessage(&msg);
  303. }
  304. KillTimer(hwnd, 0);
  305. DestroyWindow(hwnd);
  306. UnregisterClass(szClassName, NULL);
  307. return TRUE;
  308. }
  309. /************************************************************************\
  310. * Procedure: DrawIconASCII
  311. *
  312. * Description:
  313. *
  314. * Prints the icon in ASCII format through the ntsd session
  315. *
  316. * 3/28/2001 - Created bretan
  317. *
  318. \************************************************************************/
  319. BOOL DrawIconASCII(DRAW_ICON_STRUCT* pDIS, BOOL bColor)
  320. {
  321. HANDLE hConsole;
  322. DWORD dwWritten;
  323. WORD wOldColorAttrs;
  324. CONSOLE_SCREEN_BUFFER_INFO csbiInfo;
  325. if (bColor)
  326. {
  327. hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
  328. if (hConsole)
  329. {
  330. if (!GetConsoleScreenBufferInfo(hConsole, &csbiInfo))
  331. {
  332. bColor = FALSE;
  333. }
  334. }
  335. else
  336. {
  337. bColor = FALSE;
  338. }
  339. wOldColorAttrs = csbiInfo.wAttributes;
  340. }
  341. /*
  342. // Uncomment this to get a nice big list of all the chars to choose from
  343. for (int x = 0; x < 256; x++)
  344. {
  345. TCHAR szPrint[10];
  346. wsprintf(szPrint, L"%d = %c\n", x, x);
  347. WriteConsole(hConsole, szPrint, lstrlen(szPrint), &dwWritten, NULL);
  348. }
  349. */
  350. for (int y = pDIS->bmi.bmiHeader.biHeight*3 - 3; y; y-=3)
  351. {
  352. int Offset = y * pDIS->bmi.bmiHeader.biWidth;
  353. for (int x = 0; x < pDIS->bmi.bmiHeader.biWidth*3; x+=3)
  354. {
  355. RGBQUAD* prgb = (RGBQUAD*)&pDIS->psBits[x + Offset];
  356. if (bColor)
  357. {
  358. TCHAR szPrint[3];
  359. WORD wForeground = 0;
  360. if (prgb->rgbRed > 128)
  361. {
  362. wForeground |= FOREGROUND_RED;
  363. }
  364. if (prgb->rgbGreen > 128)
  365. {
  366. wForeground |= FOREGROUND_GREEN;
  367. }
  368. if (prgb->rgbBlue > 128)
  369. {
  370. wForeground |= FOREGROUND_BLUE;
  371. }
  372. if (prgb->rgbRed > 192 || prgb->rgbGreen > 192 || prgb->rgbBlue > 192)
  373. {
  374. wForeground |= FOREGROUND_INTENSITY;
  375. }
  376. SetConsoleTextAttribute(hConsole, wForeground);
  377. StringCchPrintf(szPrint, ARRAYSIZE(szPrint), TEXT("%c%c"), 15, 15);
  378. WriteConsole(hConsole, szPrint, 2, &dwWritten, NULL);
  379. }
  380. else
  381. {
  382. int val = (prgb->rgbRed + prgb->rgbGreen + prgb->rgbBlue) / 3;
  383. if (val > 0 && val <= 25) Print(" ");
  384. else if (val > 25 && val <= 100) Print("%c%c", 176, 176);
  385. else if (val > 100 && val <= 165) Print("%c%c", 177, 177);
  386. else if (val > 165 && val <= 215) Print("%c%c", 178, 178);
  387. else if (val > 215 && val <= 255) Print("%c%c", 219, 219);
  388. else Print(" ");
  389. }
  390. }
  391. if (bColor)
  392. {
  393. WriteConsole(hConsole, L"\n", 1, &dwWritten, NULL);
  394. }
  395. else
  396. {
  397. Print("\n");
  398. }
  399. }
  400. if (bColor)
  401. {
  402. WriteConsole(hConsole, L"\n", 1, &dwWritten, NULL);
  403. SetConsoleTextAttribute(hConsole, wOldColorAttrs);
  404. }
  405. else
  406. {
  407. Print("\n");
  408. }
  409. return TRUE;
  410. }
  411. /************************************************************************\
  412. * Procedure: Idrawicon
  413. *
  414. * Description:
  415. *
  416. * Draws the given icon in ASCII or in a popup window
  417. *
  418. * 3/28/2001 - Created bretan
  419. *
  420. \************************************************************************/
  421. extern "C" BOOL Idrawicon(DWORD dwOpts,
  422. LPVOID pArg )
  423. {
  424. DRAW_ICON_STRUCT DIS;
  425. BOOL ret=FALSE;
  426. HICON hIcon = (HICON)pArg;
  427. DIS.zoom = 1;
  428. DIS.hIcon = hIcon;
  429. ret = CreateDIBSectionFromIcon(&DIS);
  430. if (ret)
  431. {
  432. if (OFLAG(w) & dwOpts)
  433. {
  434. ret = DrawIconWindow(&DIS);
  435. }
  436. else
  437. {
  438. ret = DrawIconASCII(&DIS, (OFLAG(c) & dwOpts)? TRUE : FALSE);
  439. }
  440. DeleteObject(DIS.hBitmap);
  441. }
  442. return ret;
  443. }