PageRenderTime 50ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/Samples/Chap18/Emf12/Emf12.d

http://github.com/AndrejMitrovic/DWinProgramming
D | 337 lines | 250 code | 77 blank | 10 comment | 24 complexity | e90dcade8710130fe7fd8027e4d879cf MD5 | raw file
  1. /+
  2. + Copyright (c) Charles Petzold, 1998.
  3. + Ported to the D Programming Language by Andrej Mitrovic, 2011.
  4. +/
  5. module EMF12;
  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 = "EMF12";
  28. string szTitle = "EMF12: Enhanced Metafile Demo #12";
  29. string appName = "EMF";
  30. string description = "EMF12: Enhanced Metafile Demo #12";
  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 CreateRoutine(HWND hwnd)
  136. {
  137. HDC hdcEMF;
  138. HENHMETAFILE hemf;
  139. int cxMms, cyMms, cxPix, cyPix, xDpi, yDpi;
  140. hdcEMF = CreateEnhMetaFile(NULL, "emf12.emf", NULL,
  141. "EMF13\0EMF Demo #12\0");
  142. cxMms = GetDeviceCaps(hdcEMF, HORZSIZE);
  143. cyMms = GetDeviceCaps(hdcEMF, VERTSIZE);
  144. cxPix = GetDeviceCaps(hdcEMF, HORZRES);
  145. cyPix = GetDeviceCaps(hdcEMF, VERTRES);
  146. xDpi = cxPix * 254 / cxMms / 10;
  147. yDpi = cyPix * 254 / cyMms / 10;
  148. DrawRuler(hdcEMF, 6 * xDpi, yDpi);
  149. hemf = CloseEnhMetaFile(hdcEMF);
  150. DeleteEnhMetaFile(hemf);
  151. }
  152. void PaintRoutine(HWND hwnd, HDC hdc, int cxArea, int cyArea)
  153. {
  154. ENHMETAHEADER emh;
  155. HENHMETAFILE hemf;
  156. POINT pt;
  157. int cxImage, cyImage;
  158. RECT rect;
  159. SetMapMode(hdc, MM_HIMETRIC);
  160. SetViewportOrgEx(hdc, 0, cyArea, NULL);
  161. pt.x = cxArea;
  162. pt.y = 0;
  163. DPtoLP(hdc, &pt, 1);
  164. hemf = GetEnhMetaFile("emf12.emf");
  165. GetEnhMetaFileHeader(hemf, emh.sizeof, &emh);
  166. cxImage = emh.rclFrame.right - emh.rclFrame.left;
  167. cyImage = emh.rclFrame.bottom - emh.rclFrame.top;
  168. rect.left = (pt.x - cxImage) / 2;
  169. rect.top = (pt.y + cyImage) / 2;
  170. rect.right = (pt.x + cxImage) / 2;
  171. rect.bottom = (pt.y - cyImage) / 2;
  172. PlayEnhMetaFile(hdc, hemf, &rect);
  173. DeleteEnhMetaFile(hemf);
  174. }
  175. BOOL PrintRoutine(HWND hwnd)
  176. {
  177. static DOCINFO di;
  178. static PRINTDLG printdlg = PRINTDLG(PRINTDLG.sizeof);
  179. static string szMessage;
  180. BOOL bSuccess = FALSE;
  181. HDC hdcPrn;
  182. int cxPage, cyPage;
  183. printdlg.Flags = PD_RETURNDC | PD_NOPAGENUMS | PD_NOSELECTION;
  184. if (!PrintDlg(&printdlg))
  185. return TRUE;
  186. if (NULL == (hdcPrn = printdlg.hDC))
  187. return FALSE;
  188. cxPage = GetDeviceCaps(hdcPrn, HORZRES);
  189. cyPage = GetDeviceCaps(hdcPrn, VERTRES);
  190. szMessage = szClass ~ ": Printing";
  191. di.cbSize = DOCINFO.sizeof;
  192. di.lpszDocName = szMessage.toUTF16z;
  193. if (StartDoc(hdcPrn, &di) > 0)
  194. {
  195. if (StartPage(hdcPrn) > 0)
  196. {
  197. PaintRoutine(hwnd, hdcPrn, cxPage, cyPage);
  198. if (EndPage(hdcPrn) > 0)
  199. {
  200. EndDoc(hdcPrn);
  201. bSuccess = TRUE;
  202. }
  203. }
  204. }
  205. DeleteDC(hdcPrn);
  206. return bSuccess;
  207. }
  208. extern (Windows)
  209. LRESULT WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) nothrow
  210. {
  211. scope (failure) assert(0);
  212. BOOL bSuccess;
  213. static int cxClient, cyClient;
  214. HDC hdc;
  215. PAINTSTRUCT ps;
  216. switch (message)
  217. {
  218. case WM_CREATE:
  219. CreateRoutine(hwnd);
  220. return 0;
  221. case WM_COMMAND:
  222. switch (wParam)
  223. {
  224. case IDM_PRINT:
  225. SetCursor(LoadCursor(NULL, IDC_WAIT));
  226. ShowCursor(TRUE);
  227. bSuccess = PrintRoutine(hwnd);
  228. ShowCursor(FALSE);
  229. SetCursor(LoadCursor(NULL, IDC_ARROW));
  230. if (!bSuccess)
  231. MessageBox(hwnd,
  232. "Error encountered during printing",
  233. szClass.toUTF16z, MB_ICONASTERISK | MB_OK);
  234. return 0;
  235. case IDM_EXIT:
  236. SendMessage(hwnd, WM_CLOSE, 0, 0);
  237. return 0;
  238. case IDM_ABOUT:
  239. MessageBox(hwnd, "Enhanced Metafile Demo Program\nCopyright (c) Charles Petzold, 1998",
  240. szClass.toUTF16z, MB_ICONINFORMATION | MB_OK);
  241. return 0;
  242. default:
  243. }
  244. break;
  245. case WM_SIZE:
  246. cxClient = LOWORD(lParam);
  247. cyClient = HIWORD(lParam);
  248. return 0;
  249. case WM_PAINT:
  250. hdc = BeginPaint(hwnd, &ps);
  251. PaintRoutine(hwnd, hdc, cxClient, cyClient);
  252. EndPaint(hwnd, &ps);
  253. return 0;
  254. case WM_DESTROY:
  255. PostQuitMessage(0);
  256. return 0;
  257. default:
  258. }
  259. return DefWindowProc(hwnd, message, wParam, lParam);
  260. }