PageRenderTime 50ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/Samples/Chap18/Emf5/Emf5.d

http://github.com/AndrejMitrovic/DWinProgramming
D | 155 lines | 119 code | 27 blank | 9 comment | 3 complexity | 151de4f94cbd0161d1b19166e928d86f MD5 | raw file
  1. /+
  2. + Copyright (c) Charles Petzold, 1998.
  3. + Ported to the D Programming Language by Andrej Mitrovic, 2011.
  4. +/
  5. module Emf5;
  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 = "EMF5";
  26. string description = "Enhanced Metafile Demo #5";
  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. ENHMETARECORD* pEmfRecord,
  90. int iHandles, LPARAM pData)
  91. {
  92. PlayEnhMetaFileRecord(hdc, pHandleTable, pEmfRecord, iHandles);
  93. return TRUE;
  94. }
  95. extern (Windows)
  96. LRESULT WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) nothrow
  97. {
  98. scope (failure) assert(0);
  99. HDC hdc;
  100. HENHMETAFILE hemf;
  101. PAINTSTRUCT ps;
  102. RECT rect;
  103. switch (message)
  104. {
  105. case WM_PAINT:
  106. hdc = BeginPaint(hwnd, &ps);
  107. GetClientRect(hwnd, &rect);
  108. rect.left = rect.right / 4;
  109. rect.right = 3 * rect.right / 4;
  110. rect.top = rect.bottom / 4;
  111. rect.bottom = 3 * rect.bottom / 4;
  112. hemf = GetEnhMetaFile("..\\emf3\\emf3.emf");
  113. // @BUG@ WindowsAPI callbacks are not defined properly:
  114. // alias int function(HANDLE, HANDLETABLE*, const(ENHMETARECORD)*, int, int)
  115. //
  116. // should be:
  117. // alias extern(Windows) int function(HANDLE hdc, HANDLETABLE* pHandleTable, ENHMETARECORD* pEmfRecord, int iHandles, int pData)
  118. EnumEnhMetaFile(hdc, hemf, cast(int function(HANDLE, HANDLETABLE*, const(ENHMETARECORD)*, int, int))&EnhMetaFileProc, NULL, &rect);
  119. DeleteEnhMetaFile(hemf);
  120. EndPaint(hwnd, &ps);
  121. return 0;
  122. case WM_DESTROY:
  123. PostQuitMessage(0);
  124. return 0;
  125. default:
  126. }
  127. return DefWindowProc(hwnd, message, wParam, lParam);
  128. }