PageRenderTime 52ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/Samples/Chap06/StokFont/StokFont.d

http://github.com/AndrejMitrovic/DWinProgramming
D | 289 lines | 228 code | 54 blank | 7 comment | 3 complexity | c1e536b69ffdac738e32e914d818e103 MD5 | raw file
  1. /+
  2. + Copyright (c) Charles Petzold, 1998.
  3. + Ported to the D Programming Language by Andrej Mitrovic, 2011.
  4. +/
  5. module StokFont;
  6. import core.runtime;
  7. import core.thread;
  8. import std.algorithm : min, max;
  9. import std.conv;
  10. import std.math;
  11. import std.range;
  12. import std.string;
  13. import std.stdio;
  14. import std.utf : count, toUTFz;
  15. auto toUTF16z(S)(S s)
  16. {
  17. return toUTFz!(const(wchar)*)(s);
  18. }
  19. pragma(lib, "gdi32.lib");
  20. import core.sys.windows.windef;
  21. import core.sys.windows.winuser;
  22. import core.sys.windows.wingdi;
  23. struct StockFont
  24. {
  25. int idStockFont;
  26. string szStockFont;
  27. }
  28. extern(Windows)
  29. int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int iCmdShow)
  30. {
  31. int result;
  32. try
  33. {
  34. Runtime.initialize();
  35. result = myWinMain(hInstance, hPrevInstance, lpCmdLine, iCmdShow);
  36. Runtime.terminate();
  37. }
  38. catch(Throwable o)
  39. {
  40. MessageBox(null, o.toString().toUTF16z, "Error", MB_OK | MB_ICONEXCLAMATION);
  41. result = 0;
  42. }
  43. return result;
  44. }
  45. int myWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int iCmdShow)
  46. {
  47. string appName = "StokFont";
  48. HWND hwnd;
  49. MSG msg;
  50. WNDCLASS wndclass;
  51. wndclass.style = CS_HREDRAW | CS_VREDRAW;
  52. wndclass.lpfnWndProc = &WndProc;
  53. wndclass.cbClsExtra = 0;
  54. wndclass.cbWndExtra = 0;
  55. wndclass.hInstance = hInstance;
  56. wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  57. wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
  58. wndclass.hbrBackground = cast(HBRUSH)GetStockObject(WHITE_BRUSH);
  59. wndclass.lpszMenuName = NULL;
  60. wndclass.lpszClassName = appName.toUTF16z;
  61. if (!RegisterClass(&wndclass))
  62. {
  63. MessageBox(NULL, "This program requires Windows NT!", appName.toUTF16z, MB_ICONERROR);
  64. return 0;
  65. }
  66. hwnd = CreateWindow(appName.toUTF16z, // window class name
  67. "Stock Fonts", // window caption
  68. WS_OVERLAPPEDWINDOW, // window style
  69. CW_USEDEFAULT, // initial x position
  70. CW_USEDEFAULT, // initial y position
  71. CW_USEDEFAULT, // initial x size
  72. CW_USEDEFAULT, // initial y size
  73. NULL, // parent window handle
  74. NULL, // window menu handle
  75. hInstance, // program instance handle
  76. NULL); // creation parameters
  77. ShowWindow(hwnd, iCmdShow);
  78. UpdateWindow(hwnd);
  79. while (GetMessage(&msg, NULL, 0, 0))
  80. {
  81. TranslateMessage(&msg);
  82. DispatchMessage(&msg);
  83. }
  84. return msg.wParam;
  85. }
  86. extern(Windows)
  87. LRESULT WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) nothrow
  88. {
  89. scope (failure) assert(0);
  90. enum stockfont =
  91. [
  92. StockFont(OEM_FIXED_FONT, "OEM_FIXED_FONT"),
  93. StockFont(ANSI_FIXED_FONT, "ANSI_FIXED_FONT"),
  94. StockFont(ANSI_VAR_FONT, "ANSI_VAR_FONT"),
  95. StockFont(SYSTEM_FONT, "SYSTEM_FONT"),
  96. StockFont(DEVICE_DEFAULT_FONT, "DEVICE_DEFAULT_FONT"),
  97. StockFont(SYSTEM_FIXED_FONT, "SYSTEM_FIXED_FONT"),
  98. StockFont(DEFAULT_GUI_FONT, "DEFAULT_GUI_FONT")
  99. ];
  100. static int iFont;
  101. wchar[256] szFaceName;
  102. string szBuffer;
  103. TEXTMETRIC tm;
  104. int cxGrid, cyGrid;
  105. static int cLinesMax, cLines;
  106. static int cxClientMax, cyClientMax, cxClient, cyClient, cxChar, cyChar;
  107. static MSG[] msgArr;
  108. static int msgCount;
  109. static RECT rectScroll;
  110. enum szTop = "Message Key Char Repeat Scan Ext ALT Prev Tran";
  111. enum szUnd = "_______ ___ ____ ______ ____ ___ ___ ____ ____";
  112. enum szFormat = ["%-13s %3s %-15s%1s%6s %4s %3s %3s %4s %4s",
  113. "%-13s 0x%04X%1s%s %6s %4s %3s %3s %4s %4s"];
  114. enum szYes = "Yes";
  115. enum szNo = "No";
  116. enum szDown = "Down";
  117. enum szUp = "Up";
  118. enum szMessage =
  119. [
  120. "WM_KEYDOWN", "WM_KEYUP",
  121. "WM_CHAR", "WM_DEADCHAR",
  122. "WM_SYSKEYDOWN", "WM_SYSKEYUP",
  123. "WM_SYSCHAR", "WM_SYSDEADCHAR"
  124. ];
  125. HDC hdc;
  126. int iType;
  127. PAINTSTRUCT ps;
  128. char[32] szKeyName;
  129. char[] keyName;
  130. int keyLength;
  131. switch (message)
  132. {
  133. case WM_CREATE:
  134. SetScrollRange(hwnd, SB_VERT, 0, stockfont.length - 1, TRUE);
  135. return 0;
  136. case WM_DISPLAYCHANGE:
  137. InvalidateRect(hwnd, NULL, TRUE);
  138. return 0;
  139. case WM_VSCROLL:
  140. switch(LOWORD(wParam))
  141. {
  142. case SB_TOP:
  143. iFont = 0;
  144. break;
  145. case SB_BOTTOM:
  146. iFont = stockfont.length - 1;
  147. break;
  148. case SB_LINEUP:
  149. case SB_PAGEUP:
  150. iFont -= 1;
  151. break;
  152. case SB_LINEDOWN:
  153. case SB_PAGEDOWN:
  154. iFont += 1;
  155. break;
  156. case SB_THUMBPOSITION:
  157. iFont = HIWORD(wParam);
  158. break;
  159. default:
  160. }
  161. iFont = max(0, min(stockfont.length - 1, iFont));
  162. SetScrollPos(hwnd, SB_VERT, iFont, TRUE);
  163. InvalidateRect(hwnd, NULL, TRUE);
  164. return 0;
  165. case WM_KEYDOWN:
  166. {
  167. switch(wParam)
  168. {
  169. case VK_HOME:
  170. SendMessage(hwnd, WM_VSCROLL, SB_TOP, 0);
  171. break;
  172. case VK_END:
  173. SendMessage(hwnd, WM_VSCROLL, SB_BOTTOM, 0);
  174. break;
  175. case VK_PRIOR:
  176. case VK_LEFT:
  177. case VK_UP:
  178. SendMessage(hwnd, WM_VSCROLL, SB_LINEUP, 0);
  179. break;
  180. case VK_NEXT:
  181. case VK_RIGHT:
  182. case VK_DOWN:
  183. SendMessage(hwnd, WM_VSCROLL, SB_PAGEDOWN, 0);
  184. break;
  185. default:
  186. }
  187. return 0;
  188. }
  189. case WM_PAINT:
  190. {
  191. hdc = BeginPaint(hwnd, &ps);
  192. scope(exit) EndPaint(hwnd, &ps);
  193. SelectObject(hdc, GetStockObject(stockfont[iFont].idStockFont));
  194. auto newlength = GetTextFace(hdc, szFaceName.length, szFaceName.ptr);
  195. GetTextMetrics(hdc, &tm);
  196. cxGrid = max(3 * tm.tmAveCharWidth, 2 * tm.tmMaxCharWidth);
  197. cyGrid = tm.tmHeight + 3;
  198. szBuffer = format("%s: Face Name = %s, CharSet = %s",
  199. stockfont[iFont].szStockFont,
  200. szFaceName[0..newlength-1],
  201. tm.tmCharSet);
  202. TextOut(hdc, 0, 0, szBuffer.toUTF16z, szBuffer.count);
  203. SetTextAlign(hdc, TA_TOP | TA_CENTER);
  204. // vertical and horizontal lines
  205. foreach (index; 0 .. 17)
  206. {
  207. MoveToEx(hdc,(index + 2) * cxGrid, 2 * cyGrid, NULL);
  208. LineTo (hdc,(index + 2) * cxGrid, 19 * cyGrid);
  209. MoveToEx(hdc, cxGrid,(index + 3) * cyGrid, NULL);
  210. LineTo (hdc, 18 * cxGrid,(index + 3) * cyGrid);
  211. }
  212. // vertical and horizontal headings
  213. foreach (index; 0 .. 16)
  214. {
  215. szBuffer = format("%X-", index);
  216. TextOut(hdc,(2 * index + 5) * cxGrid / 2, 2 * cyGrid + 2, szBuffer.toUTF16z, szBuffer.count);
  217. TextOut(hdc, 3 * cxGrid / 2,(index + 3) * cyGrid + 2, szBuffer.toUTF16z, szBuffer.count);
  218. }
  219. // draw chars
  220. int temp;
  221. foreach (y; 0 .. 16)
  222. foreach (x; 0 .. 16)
  223. {
  224. temp = 16 * x + y;
  225. TextOut(hdc,(2 * x + 5) * cxGrid / 2, (y + 3) * cyGrid + 2, to!wstring(cast(char)temp).ptr, 1);
  226. }
  227. return 0;
  228. }
  229. case WM_DESTROY:
  230. PostQuitMessage(0);
  231. return 0;
  232. default:
  233. }
  234. return DefWindowProc(hwnd, message, wParam, lParam);
  235. }