PageRenderTime 55ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/Samples/Chap18/Emf8/Emf8.d

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