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

/Samples/Chap17/EZTest/EZTest.d

http://github.com/AndrejMitrovic/DWinProgramming
D | 241 lines | 179 code | 54 blank | 8 comment | 12 complexity | 34dbde0afba33d438338b3733f42d2fb MD5 | raw file
  1. /+
  2. + Copyright (c) Charles Petzold, 1998.
  3. + Ported to the D Programming Language by Andrej Mitrovic, 2011.
  4. +/
  5. module EZTest;
  6. import core.memory;
  7. import core.runtime;
  8. import core.thread;
  9. import std.conv;
  10. import std.math;
  11. import std.range;
  12. import std.string;
  13. import std.utf : count, toUTFz;
  14. auto toUTF16z(S)(S s)
  15. {
  16. return toUTFz!(const(wchar)*)(s);
  17. }
  18. pragma(lib, "gdi32.lib");
  19. pragma(lib, "comdlg32.lib");
  20. import core.sys.windows.windef;
  21. import core.sys.windows.winuser;
  22. import core.sys.windows.wingdi;
  23. import core.sys.windows.winbase;
  24. import core.sys.windows.commdlg;
  25. import EZFont;
  26. import resource;
  27. string appName = "FontDemo";
  28. string description = "EZTest: Test of EZFONT";
  29. HINSTANCE hinst;
  30. extern (Windows)
  31. int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int iCmdShow)
  32. {
  33. int result;
  34. try
  35. {
  36. Runtime.initialize();
  37. result = myWinMain(hInstance, hPrevInstance, lpCmdLine, iCmdShow);
  38. Runtime.terminate();
  39. }
  40. catch (Throwable o)
  41. {
  42. MessageBox(null, o.toString().toUTF16z, "Error", MB_OK | MB_ICONEXCLAMATION);
  43. result = 0;
  44. }
  45. return result;
  46. }
  47. int myWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int iCmdShow)
  48. {
  49. hinst = hInstance;
  50. HACCEL hAccel;
  51. HWND hwnd;
  52. MSG msg;
  53. WNDCLASS wndclass;
  54. wndclass.style = CS_HREDRAW | CS_VREDRAW;
  55. wndclass.lpfnWndProc = &WndProc;
  56. wndclass.cbClsExtra = 0;
  57. wndclass.cbWndExtra = 0;
  58. wndclass.hInstance = hInstance;
  59. wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  60. wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
  61. wndclass.hbrBackground = cast(HBRUSH) GetStockObject(WHITE_BRUSH);
  62. wndclass.lpszMenuName = appName.toUTF16z;
  63. wndclass.lpszClassName = appName.toUTF16z;
  64. if (!RegisterClass(&wndclass))
  65. {
  66. MessageBox(NULL, "This program requires Windows NT!", appName.toUTF16z, MB_ICONERROR);
  67. return 0;
  68. }
  69. hwnd = CreateWindow(appName.toUTF16z, // window class name
  70. description.toUTF16z, // window caption
  71. WS_OVERLAPPEDWINDOW, // window style
  72. CW_USEDEFAULT, // initial x position
  73. CW_USEDEFAULT, // initial y position
  74. CW_USEDEFAULT, // initial x size
  75. CW_USEDEFAULT, // initial y size
  76. NULL, // parent window handle
  77. NULL, // window menu handle
  78. hInstance, // program instance handle
  79. NULL); // creation parameters
  80. ShowWindow(hwnd, iCmdShow);
  81. UpdateWindow(hwnd);
  82. while (GetMessage(&msg, NULL, 0, 0))
  83. {
  84. TranslateMessage(&msg);
  85. DispatchMessage(&msg);
  86. }
  87. return msg.wParam;
  88. }
  89. void PaintRoutine(HWND hwnd, HDC hdc, int cxArea, int cyArea)
  90. {
  91. HFONT hFont;
  92. int y, iPointSize;
  93. LOGFONT lf;
  94. string szBuffer;
  95. TEXTMETRIC tm;
  96. // Set Logical Twips mapping mode
  97. SetMapMode(hdc, MM_ANISOTROPIC);
  98. SetWindowExtEx(hdc, 1440, 1440, NULL);
  99. SetViewportExtEx(hdc, GetDeviceCaps(hdc, LOGPIXELSX), GetDeviceCaps(hdc, LOGPIXELSY), NULL);
  100. for (iPointSize = 80; iPointSize <= 120; iPointSize++)
  101. {
  102. hFont = EzCreateFont(hdc, "Times New Roman", iPointSize, 0, 0, TRUE);
  103. GetObject(hFont, LOGFONT.sizeof, &lf);
  104. SelectObject(hdc, hFont);
  105. GetTextMetrics(hdc, &tm);
  106. szBuffer = format("Times New Roman font of %s.%s points, lf.lfHeight = %s, tm.tmHeight = %s",
  107. iPointSize / 10, iPointSize % 10, lf.lfHeight, tm.tmHeight);
  108. TextOut(hdc, 0, y, szBuffer.toUTF16z, szBuffer.count);
  109. DeleteObject(SelectObject(hdc, GetStockObject(SYSTEM_FONT)));
  110. y += tm.tmHeight;
  111. }
  112. }
  113. extern (Windows)
  114. LRESULT WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) nothrow
  115. {
  116. scope (failure) assert(0);
  117. static DOCINFO di = DOCINFO(DOCINFO.sizeof, "Font Demo: Printing");
  118. static int cxClient, cyClient;
  119. static PRINTDLG pd = PRINTDLG(PRINTDLG.sizeof);
  120. BOOL fSuccess;
  121. HDC hdc, hdcPrn;
  122. int cxPage, cyPage;
  123. PAINTSTRUCT ps;
  124. switch (message)
  125. {
  126. case WM_COMMAND:
  127. switch (wParam)
  128. {
  129. case IDM_PRINT:
  130. // Get printer DC
  131. pd.hwndOwner = hwnd;
  132. pd.Flags = PD_RETURNDC | PD_NOPAGENUMS | PD_NOSELECTION;
  133. if (!PrintDlg(&pd))
  134. return 0;
  135. if (NULL == (hdcPrn = pd.hDC))
  136. {
  137. MessageBox(hwnd, "Cannot obtain Printer DC",
  138. appName.toUTF16z, MB_ICONEXCLAMATION | MB_OK);
  139. return 0;
  140. }
  141. // Get size of printable area of page
  142. cxPage = GetDeviceCaps(hdcPrn, HORZRES);
  143. cyPage = GetDeviceCaps(hdcPrn, VERTRES);
  144. fSuccess = FALSE;
  145. // Do the printer page
  146. SetCursor(LoadCursor(NULL, IDC_WAIT));
  147. ShowCursor(TRUE);
  148. if ((StartDoc(hdcPrn, &di) > 0) && (StartPage(hdcPrn) > 0))
  149. {
  150. PaintRoutine(hwnd, hdcPrn, cxPage, cyPage);
  151. if (EndPage(hdcPrn) > 0)
  152. {
  153. fSuccess = TRUE;
  154. EndDoc(hdcPrn);
  155. }
  156. }
  157. DeleteDC(hdcPrn);
  158. ShowCursor(FALSE);
  159. SetCursor(LoadCursor(NULL, IDC_ARROW));
  160. if (!fSuccess)
  161. MessageBox(hwnd,
  162. "Error encountered during printing",
  163. appName.toUTF16z, MB_ICONEXCLAMATION | MB_OK);
  164. return 0;
  165. case IDM_ABOUT:
  166. MessageBox(hwnd, "Font Demonstration Program\n(c) Charles Petzold, 1998",
  167. appName.toUTF16z, MB_ICONINFORMATION | MB_OK);
  168. return 0;
  169. default:
  170. }
  171. break;
  172. case WM_SIZE:
  173. cxClient = LOWORD(lParam);
  174. cyClient = HIWORD(lParam);
  175. return 0;
  176. case WM_PAINT:
  177. hdc = BeginPaint(hwnd, &ps);
  178. PaintRoutine(hwnd, hdc, cxClient, cyClient);
  179. EndPaint(hwnd, &ps);
  180. return 0;
  181. case WM_DESTROY:
  182. PostQuitMessage(0);
  183. return 0;
  184. default:
  185. }
  186. return DefWindowProc(hwnd, message, wParam, lParam);
  187. }