PageRenderTime 41ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/Samples/Chap18/Emf3/Emf3.d

http://github.com/AndrejMitrovic/DWinProgramming
D | 173 lines | 134 code | 35 blank | 4 comment | 5 complexity | aeed5df3267b88a825606204bfffc736 MD5 | raw file
  1. /+
  2. + Copyright (c) Charles Petzold, 1998.
  3. + Ported to the D Programming Language by Andrej Mitrovic, 2011.
  4. +/
  5. module Emf3;
  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 = "EMF3";
  26. string description = "Enhanced Metafile Demo #3";
  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. LOGBRUSH lb;
  92. HDC hdc, hdcEMF;
  93. HENHMETAFILE hemf;
  94. PAINTSTRUCT ps;
  95. RECT rect;
  96. switch (message)
  97. {
  98. case WM_CREATE:
  99. hdcEMF = CreateEnhMetaFile(NULL, "emf3.emf", NULL,
  100. "EMF3\0EMF Demo #3\0");
  101. SelectObject(hdcEMF, CreateSolidBrush(RGB(0, 0, 255)));
  102. lb.lbStyle = BS_SOLID;
  103. lb.lbColor = RGB(255, 0, 0);
  104. lb.lbHatch = 0;
  105. SelectObject(hdcEMF,
  106. ExtCreatePen(PS_SOLID | PS_GEOMETRIC, 5, &lb, 0, NULL));
  107. if (GetVersion() & 0x80000000) // Windows 98
  108. Rectangle(hdcEMF, 100, 100, 201, 201);
  109. else // Windows NT
  110. Rectangle(hdcEMF, 101, 101, 202, 202);
  111. MoveToEx(hdcEMF, 100, 100, NULL);
  112. LineTo(hdcEMF, 200, 200);
  113. MoveToEx(hdcEMF, 200, 100, NULL);
  114. LineTo(hdcEMF, 100, 200);
  115. DeleteObject(SelectObject(hdcEMF, GetStockObject(BLACK_PEN)));
  116. DeleteObject(SelectObject(hdcEMF, GetStockObject(WHITE_BRUSH)));
  117. hemf = CloseEnhMetaFile(hdcEMF);
  118. DeleteEnhMetaFile(hemf);
  119. return 0;
  120. case WM_PAINT:
  121. hdc = BeginPaint(hwnd, &ps);
  122. GetClientRect(hwnd, &rect);
  123. rect.left = rect.right / 4;
  124. rect.right = 3 * rect.right / 4;
  125. rect.top = rect.bottom / 4;
  126. rect.bottom = 3 * rect.bottom / 4;
  127. hemf = GetEnhMetaFile("emf3.emf");
  128. PlayEnhMetaFile(hdc, hemf, &rect);
  129. DeleteEnhMetaFile(hemf);
  130. EndPaint(hwnd, &ps);
  131. return 0;
  132. case WM_DESTROY:
  133. PostQuitMessage(0);
  134. return 0;
  135. default:
  136. }
  137. return DefWindowProc(hwnd, message, wParam, lParam);
  138. }