PageRenderTime 57ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/Samples/Chap18/Emf10/Emf10.d

http://github.com/AndrejMitrovic/DWinProgramming
D | 318 lines | 240 code | 68 blank | 10 comment | 24 complexity | bf1c8fee587994c4b7390be4c089e687 MD5 | raw file
  1. /+
  2. + Copyright (c) Charles Petzold, 1998.
  3. + Ported to the D Programming Language by Andrej Mitrovic, 2011.
  4. +/
  5. module EMF10;
  6. import core.memory;
  7. import core.runtime;
  8. import core.thread;
  9. import std.algorithm : min, max;
  10. import std.conv;
  11. import std.math;
  12. import std.range;
  13. import std.string;
  14. import std.utf;
  15. auto toUTF16z(S)(S s)
  16. {
  17. return toUTFz!(const(wchar)*)(s);
  18. }
  19. pragma(lib, "gdi32.lib");
  20. pragma(lib, "comdlg32.lib");
  21. import core.sys.windows.windef;
  22. import core.sys.windows.winuser;
  23. import core.sys.windows.wingdi;
  24. import core.sys.windows.winbase;
  25. import core.sys.windows.commdlg;
  26. import resource;
  27. string szClass = "EMF10";
  28. string szTitle = "EMF10: Enhanced Metafile Demo #10";
  29. string appName = "EMF";
  30. string description = "EMF10: Enhanced Metafile Demo #10";
  31. HINSTANCE hinst;
  32. extern (Windows)
  33. int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int iCmdShow)
  34. {
  35. int result;
  36. try
  37. {
  38. Runtime.initialize();
  39. result = myWinMain(hInstance, hPrevInstance, lpCmdLine, iCmdShow);
  40. Runtime.terminate();
  41. }
  42. catch (Throwable o)
  43. {
  44. MessageBox(null, o.toString().toUTF16z, "Error", MB_OK | MB_ICONEXCLAMATION);
  45. result = 0;
  46. }
  47. return result;
  48. }
  49. int myWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int iCmdShow)
  50. {
  51. hinst = hInstance;
  52. HACCEL hAccel;
  53. HWND hwnd;
  54. MSG msg;
  55. WNDCLASS wndclass;
  56. wndclass.style = CS_HREDRAW | CS_VREDRAW;
  57. wndclass.lpfnWndProc = &WndProc;
  58. wndclass.cbClsExtra = 0;
  59. wndclass.cbWndExtra = 0;
  60. wndclass.hInstance = hInstance;
  61. wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  62. wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
  63. wndclass.hbrBackground = cast(HBRUSH) GetStockObject(WHITE_BRUSH);
  64. wndclass.lpszMenuName = appName.toUTF16z;
  65. wndclass.lpszClassName = appName.toUTF16z;
  66. if (!RegisterClass(&wndclass))
  67. {
  68. MessageBox(NULL, "This program requires Windows NT!", appName.toUTF16z, MB_ICONERROR);
  69. return 0;
  70. }
  71. hwnd = CreateWindow(appName.toUTF16z, // window class name
  72. description.toUTF16z, // window caption
  73. WS_OVERLAPPEDWINDOW, // window style
  74. CW_USEDEFAULT, // initial x position
  75. CW_USEDEFAULT, // initial y position
  76. CW_USEDEFAULT, // initial x size
  77. CW_USEDEFAULT, // initial y size
  78. NULL, // parent window handle
  79. NULL, // window menu handle
  80. hInstance, // program instance handle
  81. NULL); // creation parameters
  82. ShowWindow(hwnd, iCmdShow);
  83. UpdateWindow(hwnd);
  84. while (GetMessage(&msg, NULL, 0, 0))
  85. {
  86. TranslateMessage(&msg);
  87. DispatchMessage(&msg);
  88. }
  89. return msg.wParam;
  90. }
  91. void DrawRuler(HDC hdc, int cx, int cy)
  92. {
  93. int iAdj, i, iHeight;
  94. LOGFONT lf;
  95. TCHAR ch;
  96. iAdj = GetVersion() & 0x80000000 ? 0 : 1;
  97. // Black pen with 1-point width
  98. SelectObject(hdc, CreatePen(PS_SOLID, cx / 72 / 6, 0));
  99. // Rectangle surrounding entire pen (with adjustment)
  100. Rectangle(hdc, iAdj, iAdj, cx + iAdj + 1, cy + iAdj + 1);
  101. // Tick marks
  102. for (i = 1; i < 96; i++)
  103. {
  104. if (i % 16 == 0)
  105. iHeight = cy / 2; // inches
  106. else if (i % 8 == 0)
  107. iHeight = cy / 3; // half inches
  108. else if (i % 4 == 0)
  109. iHeight = cy / 5; // quarter inches
  110. else if (i % 2 == 0)
  111. iHeight = cy / 8; // eighths
  112. else
  113. iHeight = cy / 12; // sixteenths
  114. MoveToEx(hdc, i * cx / 96, cy, NULL);
  115. LineTo(hdc, i * cx / 96, cy - iHeight);
  116. }
  117. // Create logical font
  118. lf.lfHeight = cy / 2;
  119. lf.lfFaceName = 0;
  120. auto szFaceName = "Times New Roman\0";
  121. lf.lfFaceName[0..szFaceName.length] = szFaceName.toUTF16;
  122. SelectObject(hdc, CreateFontIndirect(&lf));
  123. SetTextAlign(hdc, TA_BOTTOM | TA_CENTER);
  124. SetBkMode(hdc, TRANSPARENT);
  125. // Display numbers
  126. for (i = 1; i <= 5; i++)
  127. {
  128. ch = cast(TCHAR)(i + '0');
  129. TextOut(hdc, i * cx / 6, cy / 2, &ch, 1);
  130. }
  131. // Clean up
  132. DeleteObject(SelectObject(hdc, GetStockObject(SYSTEM_FONT)));
  133. DeleteObject(SelectObject(hdc, GetStockObject(BLACK_PEN)));
  134. }
  135. void PaintRoutine(HWND hwnd, HDC hdc, int cxArea, int cyArea)
  136. {
  137. ENHMETAHEADER emh;
  138. float fScale;
  139. HENHMETAFILE hemf;
  140. int cxMms, cyMms, cxPix, cyPix, cxImage, cyImage;
  141. RECT rect;
  142. cxMms = GetDeviceCaps(hdc, HORZSIZE);
  143. cyMms = GetDeviceCaps(hdc, VERTSIZE);
  144. cxPix = GetDeviceCaps(hdc, HORZRES);
  145. cyPix = GetDeviceCaps(hdc, VERTRES);
  146. hemf = GetEnhMetaFile("..\\emf8\\emf8.emf");
  147. GetEnhMetaFileHeader(hemf, emh.sizeof, &emh);
  148. cxImage = emh.rclFrame.right - emh.rclFrame.left;
  149. cyImage = emh.rclFrame.bottom - emh.rclFrame.top;
  150. cxImage = cxImage * cxPix / cxMms / 100;
  151. cyImage = cyImage * cyPix / cyMms / 100;
  152. fScale = min(cast(float)cxArea / cxImage, cast(float)cyArea / cyImage);
  153. cxImage = cast(int)(fScale * cxImage);
  154. cyImage = cast(int)(fScale * cyImage);
  155. rect.left = (cxArea - cxImage) / 2;
  156. rect.right = (cxArea + cxImage) / 2;
  157. rect.top = (cyArea - cyImage) / 2;
  158. rect.bottom = (cyArea + cyImage) / 2;
  159. PlayEnhMetaFile(hdc, hemf, &rect);
  160. DeleteEnhMetaFile(hemf);
  161. }
  162. void CreateRoutine(HWND hwnd)
  163. {
  164. }
  165. BOOL PrintRoutine(HWND hwnd)
  166. {
  167. static DOCINFO di;
  168. static PRINTDLG printdlg = PRINTDLG(PRINTDLG.sizeof);
  169. static string szMessage;
  170. BOOL bSuccess = FALSE;
  171. HDC hdcPrn;
  172. int cxPage, cyPage;
  173. printdlg.Flags = PD_RETURNDC | PD_NOPAGENUMS | PD_NOSELECTION;
  174. if (!PrintDlg(&printdlg))
  175. return TRUE;
  176. if (NULL == (hdcPrn = printdlg.hDC))
  177. return FALSE;
  178. cxPage = GetDeviceCaps(hdcPrn, HORZRES);
  179. cyPage = GetDeviceCaps(hdcPrn, VERTRES);
  180. szMessage = szClass ~ ": Printing";
  181. di.cbSize = DOCINFO.sizeof;
  182. di.lpszDocName = szMessage.toUTF16z;
  183. if (StartDoc(hdcPrn, &di) > 0)
  184. {
  185. if (StartPage(hdcPrn) > 0)
  186. {
  187. PaintRoutine(hwnd, hdcPrn, cxPage, cyPage);
  188. if (EndPage(hdcPrn) > 0)
  189. {
  190. EndDoc(hdcPrn);
  191. bSuccess = TRUE;
  192. }
  193. }
  194. }
  195. DeleteDC(hdcPrn);
  196. return bSuccess;
  197. }
  198. extern (Windows)
  199. LRESULT WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) nothrow
  200. {
  201. scope (failure) assert(0);
  202. BOOL bSuccess;
  203. static int cxClient, cyClient;
  204. HDC hdc;
  205. PAINTSTRUCT ps;
  206. switch (message)
  207. {
  208. case WM_CREATE:
  209. CreateRoutine(hwnd);
  210. return 0;
  211. case WM_COMMAND:
  212. switch (wParam)
  213. {
  214. case IDM_PRINT:
  215. SetCursor(LoadCursor(NULL, IDC_WAIT));
  216. ShowCursor(TRUE);
  217. bSuccess = PrintRoutine(hwnd);
  218. ShowCursor(FALSE);
  219. SetCursor(LoadCursor(NULL, IDC_ARROW));
  220. if (!bSuccess)
  221. MessageBox(hwnd,
  222. "Error encountered during printing",
  223. szClass.toUTF16z, MB_ICONASTERISK | MB_OK);
  224. return 0;
  225. case IDM_EXIT:
  226. SendMessage(hwnd, WM_CLOSE, 0, 0);
  227. return 0;
  228. case IDM_ABOUT:
  229. MessageBox(hwnd, "Enhanced Metafile Demo Program\nCopyright (c) Charles Petzold, 1998",
  230. szClass.toUTF16z, MB_ICONINFORMATION | MB_OK);
  231. return 0;
  232. default:
  233. }
  234. break;
  235. case WM_SIZE:
  236. cxClient = LOWORD(lParam);
  237. cyClient = HIWORD(lParam);
  238. return 0;
  239. case WM_PAINT:
  240. hdc = BeginPaint(hwnd, &ps);
  241. PaintRoutine(hwnd, hdc, cxClient, cyClient);
  242. EndPaint(hwnd, &ps);
  243. return 0;
  244. case WM_DESTROY:
  245. PostQuitMessage(0);
  246. return 0;
  247. default:
  248. }
  249. return DefWindowProc(hwnd, message, wParam, lParam);
  250. }