PageRenderTime 37ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/Samples/Chap14/Bricks2/Bricks2.d

http://github.com/AndrejMitrovic/DWinProgramming
D | 159 lines | 126 code | 28 blank | 5 comment | 5 complexity | 87d9566e18d7734f2e8b4dc63393aade MD5 | raw file
  1. /+
  2. + Copyright (c) Charles Petzold, 1998.
  3. + Ported to the D Programming Language by Andrej Mitrovic, 2011.
  4. +/
  5. module Bricks2;
  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 = "Bricks2";
  25. string description = "CreateBitmap 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. HACCEL hAccel;
  48. HWND hwnd;
  49. MSG msg;
  50. WNDCLASS wndclass;
  51. wndclass.style = CS_HREDRAW | CS_VREDRAW;
  52. wndclass.lpfnWndProc = &WndProc;
  53. wndclass.cbClsExtra = 0;
  54. wndclass.cbWndExtra = 0;
  55. wndclass.hInstance = hInstance;
  56. wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  57. wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
  58. wndclass.hbrBackground = cast(HBRUSH) GetStockObject(WHITE_BRUSH);
  59. wndclass.lpszMenuName = appName.toUTF16z;
  60. wndclass.lpszClassName = appName.toUTF16z;
  61. if (!RegisterClass(&wndclass))
  62. {
  63. MessageBox(NULL, "This program requires Windows NT!", appName.toUTF16z, MB_ICONERROR);
  64. return 0;
  65. }
  66. hwnd = CreateWindow(appName.toUTF16z, // window class name
  67. description.toUTF16z, // window caption
  68. WS_OVERLAPPEDWINDOW, // window style
  69. CW_USEDEFAULT, // initial x position
  70. CW_USEDEFAULT, // initial y position
  71. CW_USEDEFAULT, // initial x size
  72. CW_USEDEFAULT, // initial y size
  73. NULL, // parent window handle
  74. NULL, // window menu handle
  75. hInstance, // program instance handle
  76. NULL); // creation parameters
  77. ShowWindow(hwnd, iCmdShow);
  78. UpdateWindow(hwnd);
  79. while (GetMessage(&msg, NULL, 0, 0))
  80. {
  81. TranslateMessage(&msg);
  82. DispatchMessage(&msg);
  83. }
  84. return msg.wParam;
  85. }
  86. extern (Windows)
  87. LRESULT WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) nothrow
  88. {
  89. scope (failure) assert(0);
  90. static BITMAP bitmap = BITMAP(0, 8, 8, 2, 1, 1);
  91. static ubyte[] bits = [0xFF, 0, 0x0C, 0, 0x0C, 0, 0x0C, 0,
  92. 0xFF, 0, 0xC0, 0, 0xC0, 0, 0xC0, 0];
  93. static HBITMAP hBitmap;
  94. static int cxClient, cyClient, cxSource, cySource;
  95. HDC hdc, hdcMem;
  96. int x, y;
  97. PAINTSTRUCT ps;
  98. switch (message)
  99. {
  100. case WM_CREATE:
  101. bitmap.bmBits = bits.ptr;
  102. hBitmap = CreateBitmapIndirect(&bitmap);
  103. cxSource = bitmap.bmWidth;
  104. cySource = bitmap.bmHeight;
  105. return 0;
  106. case WM_SIZE:
  107. cxClient = LOWORD(lParam);
  108. cyClient = HIWORD(lParam);
  109. return 0;
  110. case WM_PAINT:
  111. hdc = BeginPaint(hwnd, &ps);
  112. hdcMem = CreateCompatibleDC(hdc);
  113. SelectObject(hdcMem, hBitmap);
  114. for (y = 0; y < cyClient; y += cySource)
  115. {
  116. for (x = 0; x < cxClient; x += cxSource)
  117. {
  118. BitBlt(hdc, x, y, cxSource, cySource, hdcMem, 0, 0, SRCCOPY);
  119. }
  120. }
  121. DeleteDC(hdcMem);
  122. EndPaint(hwnd, &ps);
  123. return 0;
  124. case WM_DESTROY:
  125. DeleteObject(hBitmap);
  126. PostQuitMessage(0);
  127. return 0;
  128. default:
  129. }
  130. return DefWindowProc(hwnd, message, wParam, lParam);
  131. }