PageRenderTime 44ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/Samples/Chap18/Emf9/Emf9.d

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