PageRenderTime 53ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/Samples/Chap14/HelloBit/HelloBit.d

http://github.com/AndrejMitrovic/DWinProgramming
D | 195 lines | 155 code | 36 blank | 4 comment | 7 complexity | dfa97527b34a890a8c9a7590e60d971d MD5 | raw file
  1. /+
  2. + Copyright (c) Charles Petzold, 1998.
  3. + Ported to the D Programming Language by Andrej Mitrovic, 2011.
  4. +/
  5. module HelloBlit;
  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 : count, toUTFz;
  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 = "HelloBit";
  25. string description = "HelloBit";
  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 HBITMAP hBitmap;
  91. static HDC hdcMem;
  92. static int cxBitmap, cyBitmap, cxClient, cyClient, iSize = IDM_BIG;
  93. string szText = " Hello, world! ";
  94. HDC hdc;
  95. HMENU hMenu;
  96. int x, y;
  97. PAINTSTRUCT ps;
  98. SIZE size;
  99. switch (message)
  100. {
  101. case WM_CREATE:
  102. hdc = GetDC(hwnd);
  103. hdcMem = CreateCompatibleDC(hdc);
  104. GetTextExtentPoint32(hdc, szText.toUTF16z, szText.count, &size);
  105. cxBitmap = size.cx;
  106. cyBitmap = size.cy;
  107. hBitmap = CreateCompatibleBitmap(hdc, cxBitmap, cyBitmap);
  108. ReleaseDC(hwnd, hdc);
  109. SelectObject(hdcMem, hBitmap);
  110. TextOut(hdcMem, 0, 0, szText.toUTF16z, szText.count);
  111. return 0;
  112. case WM_SIZE:
  113. cxClient = LOWORD(lParam);
  114. cyClient = HIWORD(lParam);
  115. return 0;
  116. case WM_COMMAND:
  117. hMenu = GetMenu(hwnd);
  118. switch (LOWORD(wParam))
  119. {
  120. case IDM_BIG:
  121. case IDM_SMALL:
  122. CheckMenuItem(hMenu, iSize, MF_UNCHECKED);
  123. iSize = LOWORD(wParam);
  124. CheckMenuItem(hMenu, iSize, MF_CHECKED);
  125. InvalidateRect(hwnd, NULL, TRUE);
  126. break;
  127. default:
  128. }
  129. return 0;
  130. case WM_PAINT:
  131. hdc = BeginPaint(hwnd, &ps);
  132. switch (iSize)
  133. {
  134. case IDM_BIG:
  135. StretchBlt(hdc, 0, 0, cxClient, cyClient,
  136. hdcMem, 0, 0, cxBitmap, cyBitmap, SRCCOPY);
  137. break;
  138. case IDM_SMALL:
  139. for (y = 0; y < cyClient; y += cyBitmap)
  140. {
  141. for (x = 0; x < cxClient; x += cxBitmap)
  142. {
  143. BitBlt(hdc, x, y, cxBitmap, cyBitmap, hdcMem, 0, 0, SRCCOPY);
  144. }
  145. }
  146. break;
  147. default:
  148. }
  149. EndPaint(hwnd, &ps);
  150. return 0;
  151. case WM_DESTROY:
  152. DeleteDC(hdcMem);
  153. DeleteObject(hBitmap);
  154. PostQuitMessage(0);
  155. return 0;
  156. default:
  157. }
  158. return DefWindowProc(hwnd, message, wParam, lParam);
  159. }