PageRenderTime 40ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/Samples/Chap14/BitBlt/BitBlt.d

http://github.com/AndrejMitrovic/DWinProgramming
D | 150 lines | 118 code | 27 blank | 5 comment | 5 complexity | 779549c3c8cbb4ffeb91262770022838 MD5 | raw file
  1. /+
  2. + Copyright (c) Charles Petzold, 1998.
  3. + Ported to the D Programming Language by Andrej Mitrovic, 2011.
  4. +/
  5. module name;
  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. import core.sys.windows.windef;
  20. import core.sys.windows.winuser;
  21. import core.sys.windows.wingdi;
  22. import core.sys.windows.winbase;
  23. //~ import resource;
  24. string appName = "BitBlt";
  25. string description = "BitBlt Demo";
  26. HINSTANCE hinst;
  27. extern (Windows)
  28. int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int iCmdShow)
  29. {
  30. int result;
  31. try
  32. {
  33. Runtime.initialize();
  34. result = myWinMain(hInstance, hPrevInstance, lpCmdLine, iCmdShow);
  35. Runtime.terminate();
  36. }
  37. catch (Throwable o)
  38. {
  39. MessageBox(null, o.toString().toUTF16z, "Error", MB_OK | MB_ICONEXCLAMATION);
  40. result = 0;
  41. }
  42. return result;
  43. }
  44. int myWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int iCmdShow)
  45. {
  46. hinst = hInstance;
  47. HWND hwnd;
  48. MSG msg;
  49. WNDCLASS wndclass;
  50. wndclass.style = CS_HREDRAW | CS_VREDRAW;
  51. wndclass.lpfnWndProc = &WndProc;
  52. wndclass.cbClsExtra = 0;
  53. wndclass.cbWndExtra = 0;
  54. wndclass.hInstance = hInstance;
  55. wndclass.hIcon = LoadIcon(NULL, IDI_INFORMATION);
  56. wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
  57. wndclass.hbrBackground = cast(HBRUSH) GetStockObject(WHITE_BRUSH);
  58. wndclass.lpszMenuName = null;
  59. wndclass.lpszClassName = appName.toUTF16z;
  60. if (!RegisterClass(&wndclass))
  61. {
  62. MessageBox(NULL, "This program requires Windows NT!", appName.toUTF16z, MB_ICONERROR);
  63. return 0;
  64. }
  65. hwnd = CreateWindow(appName.toUTF16z, // window class name
  66. description.toUTF16z, // window caption
  67. WS_OVERLAPPEDWINDOW, // window style
  68. CW_USEDEFAULT, // initial x position
  69. CW_USEDEFAULT, // initial y position
  70. CW_USEDEFAULT, // initial x size
  71. CW_USEDEFAULT, // initial y size
  72. NULL, // parent window handle
  73. NULL, // window menu handle
  74. hInstance, // program instance handle
  75. NULL); // creation parameters
  76. ShowWindow(hwnd, iCmdShow);
  77. UpdateWindow(hwnd);
  78. while (GetMessage(&msg, NULL, 0, 0))
  79. {
  80. TranslateMessage(&msg);
  81. DispatchMessage(&msg);
  82. }
  83. return msg.wParam;
  84. }
  85. extern (Windows)
  86. LRESULT WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) nothrow
  87. {
  88. scope (failure) assert(0);
  89. static int cxClient, cyClient, cxSource, cySource;
  90. HDC hdcClient, hdcWindow;
  91. int x, y;
  92. PAINTSTRUCT ps;
  93. switch (message)
  94. {
  95. case WM_CREATE:
  96. cxSource = GetSystemMetrics(SM_CXSIZEFRAME) +
  97. GetSystemMetrics(SM_CXSIZE);
  98. cySource = GetSystemMetrics(SM_CYSIZEFRAME) +
  99. GetSystemMetrics(SM_CYCAPTION);
  100. return 0;
  101. case WM_SIZE:
  102. cxClient = LOWORD(lParam);
  103. cyClient = HIWORD(lParam);
  104. return 0;
  105. case WM_PAINT:
  106. hdcClient = BeginPaint(hwnd, &ps);
  107. hdcWindow = GetWindowDC(hwnd);
  108. for (y = 0; y < cyClient; y += cySource)
  109. for (x = 0; x < cxClient; x += cxSource)
  110. {
  111. BitBlt(hdcClient, x, y, cxSource, cySource,
  112. hdcWindow, 0, 0, SRCCOPY);
  113. }
  114. ReleaseDC(hwnd, hdcWindow);
  115. EndPaint(hwnd, &ps);
  116. return 0;
  117. case WM_DESTROY:
  118. PostQuitMessage(0);
  119. return 0;
  120. default:
  121. }
  122. return DefWindowProc(hwnd, message, wParam, lParam);
  123. }