PageRenderTime 56ms CodeModel.GetById 33ms RepoModel.GetById 0ms app.codeStats 0ms

/Samples/Chap06/KeyView1/KeyView1.d

http://github.com/AndrejMitrovic/DWinProgramming
D | 240 lines | 193 code | 39 blank | 8 comment | 9 complexity | 17e700d362ff752a3808aa79aa247e16 MD5 | raw file
  1. /+
  2. + Copyright (c) Charles Petzold, 1998.
  3. + Ported to the D Programming Language by Andrej Mitrovic, 2011.
  4. +/
  5. module KeyView1;
  6. import core.runtime;
  7. import core.thread;
  8. import std.algorithm : max, min;
  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. extern (Windows)
  23. int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int iCmdShow)
  24. {
  25. int result;
  26. try
  27. {
  28. Runtime.initialize();
  29. result = myWinMain(hInstance, hPrevInstance, lpCmdLine, iCmdShow);
  30. Runtime.terminate();
  31. }
  32. catch (Throwable o)
  33. {
  34. MessageBox(null, o.toString().toUTF16z, "Error", MB_OK | MB_ICONEXCLAMATION);
  35. result = 0;
  36. }
  37. return result;
  38. }
  39. int myWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int iCmdShow)
  40. {
  41. string appName = "KeyView1";
  42. HWND hwnd;
  43. MSG msg;
  44. WNDCLASS wndclass;
  45. wndclass.style = CS_HREDRAW | CS_VREDRAW;
  46. wndclass.lpfnWndProc = &WndProc;
  47. wndclass.cbClsExtra = 0;
  48. wndclass.cbWndExtra = 0;
  49. wndclass.hInstance = hInstance;
  50. wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  51. wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
  52. wndclass.hbrBackground = cast(HBRUSH) GetStockObject(WHITE_BRUSH);
  53. wndclass.lpszMenuName = NULL;
  54. wndclass.lpszClassName = appName.toUTF16z;
  55. if (!RegisterClass(&wndclass))
  56. {
  57. MessageBox(NULL, "This program requires Windows NT!", appName.toUTF16z, MB_ICONERROR);
  58. return 0;
  59. }
  60. hwnd = CreateWindow(appName.toUTF16z, // window class name
  61. "Keyboard Message Viewer #1", // window caption
  62. WS_OVERLAPPEDWINDOW, // window style
  63. CW_USEDEFAULT, // initial x position
  64. CW_USEDEFAULT, // initial y position
  65. CW_USEDEFAULT, // initial x size
  66. CW_USEDEFAULT, // initial y size
  67. NULL, // parent window handle
  68. NULL, // window menu handle
  69. hInstance, // program instance handle
  70. NULL); // creation parameters
  71. ShowWindow(hwnd, iCmdShow);
  72. UpdateWindow(hwnd);
  73. while (GetMessage(&msg, NULL, 0, 0))
  74. {
  75. TranslateMessage(&msg);
  76. DispatchMessage(&msg);
  77. }
  78. return msg.wParam;
  79. }
  80. extern(Windows)
  81. LRESULT WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) nothrow
  82. {
  83. scope (failure) assert(0);
  84. static int cLinesMax, cLines;
  85. static int cxClientMax, cyClientMax, cxClient, cyClient, cxChar, cyChar;
  86. static MSG[] msgArr;
  87. static int msgCount;
  88. static RECT rectScroll;
  89. enum szTop = "Message Key Char Repeat Scan Ext ALT Prev Tran";
  90. enum szUnd = "_______ ___ ____ ______ ____ ___ ___ ____ ____";
  91. enum szFormat = ["%-13s %3s %-15s%1s%6s %4s %3s %3s %4s %4s",
  92. "%-13s 0x%04X%1s%s %6s %4s %3s %3s %4s %4s"];
  93. enum szYes = "Yes";
  94. enum szNo = "No";
  95. enum szDown = "Down";
  96. enum szUp = "Up";
  97. enum szMessage =
  98. [
  99. "WM_KEYDOWN", "WM_KEYUP",
  100. "WM_CHAR", "WM_DEADCHAR",
  101. "WM_SYSKEYDOWN", "WM_SYSKEYUP",
  102. "WM_SYSCHAR", "WM_SYSDEADCHAR"
  103. ];
  104. HDC hdc;
  105. int iType;
  106. PAINTSTRUCT ps;
  107. string szBuffer;
  108. wchar[32] szKeyName;
  109. wchar[] keyName;
  110. int keyLength;
  111. TEXTMETRIC tm;
  112. switch (message)
  113. {
  114. case WM_CREATE:
  115. case WM_DISPLAYCHANGE:
  116. {
  117. // Get maximum size of client area
  118. cxClientMax = GetSystemMetrics(SM_CXMAXIMIZED);
  119. cyClientMax = GetSystemMetrics(SM_CYMAXIMIZED);
  120. hdc = GetDC(hwnd);
  121. scope(exit) ReleaseDC(hwnd, hdc);
  122. // Get character size for fixed-pitch font
  123. SelectObject(hdc, GetStockObject(SYSTEM_FIXED_FONT));
  124. GetTextMetrics(hdc, &tm);
  125. cxChar = tm.tmAveCharWidth;
  126. cyChar = tm.tmHeight;
  127. cLinesMax = cyClientMax / cyChar;
  128. cLines = 0;
  129. goto case WM_SIZE;
  130. }
  131. case WM_SIZE:
  132. {
  133. if (message == WM_SIZE)
  134. {
  135. cxClient = LOWORD(lParam);
  136. cyClient = HIWORD(lParam);
  137. }
  138. // Calculate scrolling rectangle
  139. rectScroll.left = 0;
  140. rectScroll.right = cxClient;
  141. rectScroll.top = cyChar;
  142. rectScroll.bottom = cyChar * (cyClient / cyChar);
  143. InvalidateRect(hwnd, NULL, TRUE);
  144. return 0;
  145. }
  146. case WM_KEYDOWN:
  147. case WM_KEYUP:
  148. case WM_CHAR:
  149. case WM_DEADCHAR:
  150. case WM_SYSKEYDOWN:
  151. case WM_SYSKEYUP:
  152. case WM_SYSCHAR:
  153. case WM_SYSDEADCHAR:
  154. {
  155. msgArr ~= MSG(hwnd, message, wParam, lParam);
  156. cLines = min(cLines + 1, cLinesMax);
  157. // Scroll up
  158. ScrollWindow(hwnd, 0, -cyChar, &rectScroll, &rectScroll);
  159. break;
  160. }
  161. case WM_PAINT:
  162. {
  163. hdc = BeginPaint(hwnd, &ps);
  164. scope(exit) EndPaint(hwnd, &ps);
  165. SelectObject(hdc, GetStockObject(SYSTEM_FIXED_FONT));
  166. SetBkMode(hdc, TRANSPARENT);
  167. TextOut(hdc, 0, 0, szTop.toUTF16z, szTop.count);
  168. TextOut(hdc, 0, 0, szUnd.toUTF16z, szUnd.count);
  169. foreach (index, myMsg; lockstep(iota(0, min(cLines, cyClient / cyChar - 1)), retro(msgArr)))
  170. {
  171. iType = myMsg.message == WM_CHAR ||
  172. myMsg.message == WM_SYSCHAR ||
  173. myMsg.message == WM_DEADCHAR ||
  174. myMsg.message == WM_SYSDEADCHAR;
  175. keyLength = GetKeyNameText(myMsg.lParam, szKeyName.ptr, szKeyName.length);
  176. keyName = szKeyName[0..keyLength];
  177. szBuffer = format(szFormat[iType],
  178. szMessage[myMsg.message - WM_KEYFIRST],
  179. myMsg.wParam,
  180. (iType ? "" : keyName.dup),
  181. (iType ? to!string(cast(char*)&myMsg.wParam) : ""),
  182. LOWORD(myMsg.lParam),
  183. HIWORD(myMsg.lParam) & 0xFF,
  184. (0x01000000 & myMsg.lParam ? szYes : szNo),
  185. (0x20000000 & myMsg.lParam ? szYes : szNo),
  186. (0x40000000 & myMsg.lParam ? szDown : szUp),
  187. (0x80000000 & myMsg.lParam ? szUp : szDown)
  188. );
  189. TextOut(hdc, 0, (cyClient / cyChar - 1 - index) * cyChar, szBuffer.toUTF16z, szBuffer.count);
  190. }
  191. return 0;
  192. }
  193. case WM_DESTROY:
  194. {
  195. PostQuitMessage(0);
  196. return 0;
  197. }
  198. default:
  199. }
  200. return DefWindowProc(hwnd, message, wParam, lParam);
  201. }