PageRenderTime 41ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 1ms

/Samples/Chap18/Metafile/Metafile.d

http://github.com/AndrejMitrovic/DWinProgramming
D | 167 lines | 132 code | 31 blank | 4 comment | 5 complexity | f60d1b7a34df63435d9e5b9112b1522b MD5 | raw file
  1. /+
  2. + Copyright (c) Charles Petzold, 1998.
  3. + Ported to the D Programming Language by Andrej Mitrovic, 2011.
  4. +/
  5. module Metafile;
  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 = "Metafile";
  26. string description = "Metafile Demonstration";
  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. LRESULT WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) nothrow
  89. {
  90. scope (failure) assert(0);
  91. static HMETAFILE hmf;
  92. static int cxClient, cyClient;
  93. HBRUSH hBrush;
  94. HDC hdc, hdcMeta;
  95. int x, y;
  96. PAINTSTRUCT ps;
  97. switch (message)
  98. {
  99. case WM_CREATE:
  100. hdcMeta = CreateMetaFile(NULL);
  101. hBrush = CreateSolidBrush(RGB(0, 0, 255));
  102. Rectangle(hdcMeta, 0, 0, 100, 100);
  103. MoveToEx(hdcMeta, 0, 0, NULL);
  104. LineTo(hdcMeta, 100, 100);
  105. MoveToEx(hdcMeta, 0, 100, NULL);
  106. LineTo(hdcMeta, 100, 0);
  107. SelectObject(hdcMeta, hBrush);
  108. Ellipse(hdcMeta, 20, 20, 80, 80);
  109. hmf = CloseMetaFile(hdcMeta);
  110. DeleteObject(hBrush);
  111. return 0;
  112. case WM_SIZE:
  113. cxClient = LOWORD(lParam);
  114. cyClient = HIWORD(lParam);
  115. return 0;
  116. case WM_PAINT:
  117. hdc = BeginPaint(hwnd, &ps);
  118. SetMapMode(hdc, MM_ANISOTROPIC);
  119. SetWindowExtEx(hdc, 1000, 1000, NULL);
  120. SetViewportExtEx(hdc, cxClient, cyClient, NULL);
  121. for (x = 0; x < 10; x++)
  122. for (y = 0; y < 10; y++)
  123. {
  124. SetWindowOrgEx(hdc, -100 * x, -100 * y, NULL);
  125. PlayMetaFile(hdc, hmf);
  126. }
  127. EndPaint(hwnd, &ps);
  128. return 0;
  129. case WM_DESTROY:
  130. DeleteMetaFile(hmf);
  131. PostQuitMessage(0);
  132. return 0;
  133. default:
  134. }
  135. return DefWindowProc(hwnd, message, wParam, lParam);
  136. }