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

/Samples/Chap18/Emf13/Emf13.d

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