PageRenderTime 49ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/Samples/Chap18/Emf4/Emf4.d

http://github.com/AndrejMitrovic/DWinProgramming
D | 165 lines | 127 code | 34 blank | 4 comment | 3 complexity | 64792bc43cddda0b8e5a9454bdcf6bfe MD5 | raw file
  1. /+
  2. + Copyright (c) Charles Petzold, 1998.
  3. + Ported to the D Programming Language by Andrej Mitrovic, 2011.
  4. +/
  5. module Emf4;
  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 = "EMF4";
  26. string description = "Enhanced Metafile Demo #4";
  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. BITMAP bm;
  92. HBITMAP hbm;
  93. HDC hdc, hdcEMF, hdcMem;
  94. HENHMETAFILE hemf;
  95. PAINTSTRUCT ps;
  96. RECT rect;
  97. switch (message)
  98. {
  99. case WM_CREATE:
  100. hdcEMF = CreateEnhMetaFile(NULL, "emf4.emf", NULL,
  101. "EMF4\0EMF Demo #4\0");
  102. hbm = LoadBitmap(NULL, MAKEINTRESOURCE(OBM_CLOSE));
  103. GetObject(hbm, BITMAP.sizeof, &bm);
  104. hdcMem = CreateCompatibleDC(hdcEMF);
  105. SelectObject(hdcMem, hbm);
  106. StretchBlt(hdcEMF, 100, 100, 100, 100,
  107. hdcMem, 0, 0, bm.bmWidth, bm.bmHeight, SRCCOPY);
  108. DeleteDC(hdcMem);
  109. DeleteObject(hbm);
  110. hemf = CloseEnhMetaFile(hdcEMF);
  111. DeleteEnhMetaFile(hemf);
  112. return 0;
  113. case WM_PAINT:
  114. hdc = BeginPaint(hwnd, &ps);
  115. GetClientRect(hwnd, &rect);
  116. rect.left = rect.right / 4;
  117. rect.right = 3 * rect.right / 4;
  118. rect.top = rect.bottom / 4;
  119. rect.bottom = 3 * rect.bottom / 4;
  120. hemf = GetEnhMetaFile("emf4.emf");
  121. PlayEnhMetaFile(hdc, hemf, &rect);
  122. DeleteEnhMetaFile(hemf);
  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. }