PageRenderTime 47ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/Samples/Chap18/Emf2/Emf2.d

http://github.com/AndrejMitrovic/DWinProgramming
D | 163 lines | 127 code | 32 blank | 4 comment | 5 complexity | 09d32f02ec8df3b6c17e965f331844d0 MD5 | raw file
  1. /+
  2. + Copyright (c) Charles Petzold, 1998.
  3. + Ported to the D Programming Language by Andrej Mitrovic, 2011.
  4. +/
  5. module Emf2;
  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 = "EMF2";
  26. string description = "Enhanced Metafile Demo #2";
  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. HDC hdc, hdcEMF;
  92. HENHMETAFILE hemf;
  93. PAINTSTRUCT ps;
  94. RECT rect;
  95. switch (message)
  96. {
  97. case WM_CREATE:
  98. hdcEMF = CreateEnhMetaFile(NULL, "emf2.emf", NULL,
  99. "EMF2\0EMF Demo #2\0");
  100. if (!hdcEMF)
  101. return 0;
  102. Rectangle(hdcEMF, 100, 100, 200, 200);
  103. MoveToEx(hdcEMF, 100, 100, NULL);
  104. LineTo(hdcEMF, 200, 200);
  105. MoveToEx(hdcEMF, 200, 100, NULL);
  106. LineTo(hdcEMF, 100, 200);
  107. hemf = CloseEnhMetaFile(hdcEMF);
  108. DeleteEnhMetaFile(hemf);
  109. return 0;
  110. case WM_PAINT:
  111. hdc = BeginPaint(hwnd, &ps);
  112. GetClientRect(hwnd, &rect);
  113. rect.left = rect.right / 4;
  114. rect.right = 3 * rect.right / 4;
  115. rect.top = rect.bottom / 4;
  116. rect.bottom = 3 * rect.bottom / 4;
  117. hemf = GetEnhMetaFile("emf2.emf");
  118. if (hemf !is null)
  119. {
  120. PlayEnhMetaFile(hdc, hemf, &rect);
  121. DeleteEnhMetaFile(hemf);
  122. }
  123. EndPaint(hwnd, &ps);
  124. return 0;
  125. case WM_DESTROY:
  126. PostQuitMessage(0);
  127. return 0;
  128. default:
  129. }
  130. return DefWindowProc(hwnd, message, wParam, lParam);
  131. }