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

/Samples/Chap18/Emf7/Emf7.d

http://github.com/AndrejMitrovic/DWinProgramming
D | 205 lines | 147 code | 45 blank | 13 comment | 9 complexity | 7fa1b335dd4a7b9bc0a185b75ad01274 MD5 | raw file
  1. /+
  2. + Copyright (c) Charles Petzold, 1998.
  3. + Ported to the D Programming Language by Andrej Mitrovic, 2011.
  4. +/
  5. module Emf7;
  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. string appName = "Emf7";
  26. string description = "Enhanced Metafile #7";
  27. HINSTANCE hinst;
  28. extern (Windows)
  29. int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int iCmdShow)
  30. {
  31. int result;
  32. try
  33. {
  34. Runtime.initialize();
  35. result = myWinMain(hInstance, hPrevInstance, lpCmdLine, iCmdShow);
  36. Runtime.terminate();
  37. }
  38. catch (Throwable o)
  39. {
  40. MessageBox(null, o.toString().toUTF16z, "Error", MB_OK | MB_ICONEXCLAMATION);
  41. result = 0;
  42. }
  43. return result;
  44. }
  45. int myWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int iCmdShow)
  46. {
  47. hinst = hInstance;
  48. HACCEL hAccel;
  49. HWND hwnd;
  50. MSG msg;
  51. WNDCLASS wndclass;
  52. wndclass.style = CS_HREDRAW | CS_VREDRAW;
  53. wndclass.lpfnWndProc = &WndProc;
  54. wndclass.cbClsExtra = 0;
  55. wndclass.cbWndExtra = 0;
  56. wndclass.hInstance = hInstance;
  57. wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  58. wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
  59. wndclass.hbrBackground = cast(HBRUSH) GetStockObject(WHITE_BRUSH);
  60. wndclass.lpszMenuName = appName.toUTF16z;
  61. wndclass.lpszClassName = appName.toUTF16z;
  62. if (!RegisterClass(&wndclass))
  63. {
  64. MessageBox(NULL, "This program requires Windows NT!", appName.toUTF16z, MB_ICONERROR);
  65. return 0;
  66. }
  67. hwnd = CreateWindow(appName.toUTF16z, // window class name
  68. description.toUTF16z, // window caption
  69. WS_OVERLAPPEDWINDOW, // window style
  70. CW_USEDEFAULT, // initial x position
  71. CW_USEDEFAULT, // initial y position
  72. CW_USEDEFAULT, // initial x size
  73. CW_USEDEFAULT, // initial y size
  74. NULL, // parent window handle
  75. NULL, // window menu handle
  76. hInstance, // program instance handle
  77. NULL); // creation parameters
  78. ShowWindow(hwnd, iCmdShow);
  79. UpdateWindow(hwnd);
  80. while (GetMessage(&msg, NULL, 0, 0))
  81. {
  82. TranslateMessage(&msg);
  83. DispatchMessage(&msg);
  84. }
  85. return msg.wParam;
  86. }
  87. extern (Windows)
  88. int EnhMetaFileProc(HDC hdc, HANDLETABLE* pHandleTable,
  89. const ENHMETARECORD* pEmfRecord,
  90. int iHandles, LPARAM pData)
  91. {
  92. HBRUSH hBrush;
  93. HPEN hPen;
  94. LOGBRUSH lb;
  95. if (pEmfRecord.iType != EMR_HEADER && pEmfRecord.iType != EMR_EOF)
  96. PlayEnhMetaFileRecord(hdc, pHandleTable, pEmfRecord, iHandles);
  97. if (pEmfRecord.iType == EMR_RECTANGLE)
  98. {
  99. hBrush = SelectObject(hdc, GetStockObject(NULL_BRUSH));
  100. lb.lbStyle = BS_SOLID;
  101. lb.lbColor = RGB(0, 255, 0);
  102. lb.lbHatch = 0;
  103. hPen = SelectObject(hdc,
  104. ExtCreatePen(PS_SOLID | PS_GEOMETRIC, 5, &lb, 0, NULL));
  105. Ellipse(hdc, 100, 100, 200, 200);
  106. DeleteObject(SelectObject(hdc, hPen));
  107. SelectObject(hdc, hBrush);
  108. }
  109. return TRUE;
  110. }
  111. extern (Windows)
  112. LRESULT WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) nothrow
  113. {
  114. scope (failure) assert(0);
  115. ENHMETAHEADER emh;
  116. HDC hdc, hdcEMF;
  117. HENHMETAFILE hemfOld, hemf;
  118. PAINTSTRUCT ps;
  119. RECT rect;
  120. switch (message)
  121. {
  122. case WM_CREATE:
  123. // Retrieve existing metafile and header
  124. hemfOld = GetEnhMetaFile(r"..\emf3\emf3.emf");
  125. GetEnhMetaFileHeader(hemfOld, ENHMETAHEADER.sizeof, &emh);
  126. // Create a new metafile DC
  127. hdcEMF = CreateEnhMetaFile(NULL, "emf7.emf", NULL,
  128. "EMF7\0EMF Demo #7\0");
  129. // Enumerate the existing metafile
  130. // @BUG@ WindowsAPI callbacks are not defined properly:
  131. // alias int function(HANDLE, HANDLETABLE*, const(ENHMETARECORD)*, int, int)
  132. //
  133. // should be:
  134. // alias extern(Windows) int function(HANDLE hdc, HANDLETABLE* pHandleTable, ENHMETARECORD* pEmfRecord, int iHandles, int pData)
  135. EnumEnhMetaFile(hdcEMF, hemfOld, cast(int function(HANDLE, HANDLETABLE*, const(ENHMETARECORD)*, int, int))&EnhMetaFileProc, NULL,
  136. cast(RECT*)&emh.rclBounds);
  137. // Clean up
  138. hemf = CloseEnhMetaFile(hdcEMF);
  139. DeleteEnhMetaFile(hemfOld);
  140. DeleteEnhMetaFile(hemf);
  141. return 0;
  142. case WM_PAINT:
  143. hdc = BeginPaint(hwnd, &ps);
  144. GetClientRect(hwnd, &rect);
  145. rect.left = rect.right / 4;
  146. rect.right = 3 * rect.right / 4;
  147. rect.top = rect.bottom / 4;
  148. rect.bottom = 3 * rect.bottom / 4;
  149. hemf = GetEnhMetaFile("emf7.emf");
  150. PlayEnhMetaFile(hdc, hemf, &rect);
  151. DeleteEnhMetaFile(hemf);
  152. EndPaint(hwnd, &ps);
  153. return 0;
  154. case WM_DESTROY:
  155. PostQuitMessage(0);
  156. return 0;
  157. default:
  158. }
  159. return DefWindowProc(hwnd, message, wParam, lParam);
  160. }