PageRenderTime 59ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/Samples/Chap06/KeyView2/KeyView2.d

http://github.com/AndrejMitrovic/DWinProgramming
D | 257 lines | 205 code | 44 blank | 8 comment | 11 complexity | 9d14374a4e00e66e9f4807b92bffca20 MD5 | raw file
  1. /+
  2. + Copyright (c) Charles Petzold, 1998.
  3. + Ported to the D Programming Language by Andrej Mitrovic, 2011.
  4. +/
  5. module KeyView2;
  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 = "KeyView2";
  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 #2", // 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 DWORD dwCharSet = DEFAULT_CHARSET;
  85. static int cLinesMax, cLines;
  86. static int cxClientMax, cyClientMax, cxClient, cyClient, cxChar, cyChar;
  87. static MSG[] msgArr;
  88. static int msgCount;
  89. static RECT rectScroll;
  90. enum szTop = "Message Key Char Repeat Scan Ext ALT Prev Tran";
  91. enum szUnd = "_______ ___ ____ ______ ____ ___ ___ ____ ____";
  92. enum szFormat = ["%-13s %3s %-15s%1s%6s %4s %3s %3s %4s %4s",
  93. "%-13s 0x%04X%1s%s %6s %4s %3s %3s %4s %4s"];
  94. enum szYes = "Yes";
  95. enum szNo = "No";
  96. enum szDown = "Down";
  97. enum szUp = "Up";
  98. enum szMessage =
  99. [
  100. "WM_KEYDOWN", "WM_KEYUP",
  101. "WM_CHAR", "WM_DEADCHAR",
  102. "WM_SYSKEYDOWN", "WM_SYSKEYUP",
  103. "WM_SYSCHAR", "WM_SYSDEADCHAR"
  104. ];
  105. HDC hdc;
  106. int iType;
  107. PAINTSTRUCT ps;
  108. string szBuffer;
  109. wchar[32] szKeyName;
  110. wchar[] keyName;
  111. int keyLength;
  112. TEXTMETRIC tm;
  113. switch (message)
  114. {
  115. case WM_INPUTLANGCHANGE:
  116. {
  117. dwCharSet = wParam;
  118. goto case WM_CREATE;
  119. }
  120. case WM_CREATE:
  121. case WM_DISPLAYCHANGE:
  122. {
  123. // Get maximum size of client area
  124. cxClientMax = GetSystemMetrics(SM_CXMAXIMIZED);
  125. cyClientMax = GetSystemMetrics(SM_CYMAXIMIZED);
  126. hdc = GetDC(hwnd);
  127. scope(exit) ReleaseDC(hwnd, hdc);
  128. // Get character size for fixed-pitch font
  129. SelectObject(hdc, CreateFont(0, 0, 0, 0, 0, 0, 0, 0,
  130. dwCharSet, 0, 0, 0, FIXED_PITCH, NULL));
  131. scope(exit) DeleteObject(SelectObject(hdc, GetStockObject(SYSTEM_FONT)));
  132. GetTextMetrics(hdc, &tm);
  133. cxChar = tm.tmAveCharWidth;
  134. cyChar = tm.tmHeight;
  135. cLinesMax = cyClientMax / cyChar;
  136. cLines = 0;
  137. goto case WM_SIZE;
  138. }
  139. case WM_SIZE:
  140. {
  141. if (message == WM_SIZE)
  142. {
  143. cxClient = LOWORD(lParam);
  144. cyClient = HIWORD(lParam);
  145. }
  146. // Calculate scrolling rectangle
  147. rectScroll.left = 0;
  148. rectScroll.right = cxClient;
  149. rectScroll.top = cyChar;
  150. rectScroll.bottom = cyChar * (cyClient / cyChar);
  151. InvalidateRect(hwnd, NULL, TRUE);
  152. if (message == WM_INPUTLANGCHANGE)
  153. return 1;
  154. return 0;
  155. }
  156. case WM_KEYDOWN:
  157. case WM_KEYUP:
  158. case WM_CHAR:
  159. case WM_DEADCHAR:
  160. case WM_SYSKEYDOWN:
  161. case WM_SYSKEYUP:
  162. case WM_SYSCHAR:
  163. case WM_SYSDEADCHAR:
  164. {
  165. msgArr ~= MSG(hwnd, message, wParam, lParam);
  166. cLines = min(cLines + 1, cLinesMax);
  167. // Scroll up
  168. ScrollWindow(hwnd, 0, -cyChar, &rectScroll, &rectScroll);
  169. break;
  170. }
  171. case WM_PAINT:
  172. {
  173. hdc = BeginPaint(hwnd, &ps);
  174. scope(exit) EndPaint(hwnd, &ps);
  175. SelectObject(hdc, CreateFont(0, 0, 0, 0, 0, 0, 0, 0,
  176. dwCharSet, 0, 0, 0, FIXED_PITCH, NULL));
  177. scope(exit) DeleteObject(SelectObject(hdc, GetStockObject(SYSTEM_FONT)));
  178. SetBkMode(hdc, TRANSPARENT);
  179. TextOut(hdc, 0, 0, szTop.toUTF16z, szTop.count);
  180. TextOut(hdc, 0, 0, szUnd.toUTF16z, szUnd.count);
  181. foreach (index, myMsg; lockstep(iota(0, min(cLines, cyClient / cyChar - 1)), retro(msgArr)))
  182. {
  183. iType = myMsg.message == WM_CHAR ||
  184. myMsg.message == WM_SYSCHAR ||
  185. myMsg.message == WM_DEADCHAR ||
  186. myMsg.message == WM_SYSDEADCHAR;
  187. keyLength = GetKeyNameText(myMsg.lParam, szKeyName.ptr, szKeyName.length);
  188. keyName = szKeyName[0..keyLength];
  189. szBuffer = format(szFormat[iType],
  190. szMessage[myMsg.message - WM_KEYFIRST],
  191. myMsg.wParam,
  192. (iType ? "" : keyName.dup),
  193. (iType ? to!string(cast(char*)&myMsg.wParam) : ""),
  194. LOWORD(myMsg.lParam),
  195. HIWORD(myMsg.lParam) & 0xFF,
  196. (0x01000000 & myMsg.lParam ? szYes : szNo),
  197. (0x20000000 & myMsg.lParam ? szYes : szNo),
  198. (0x40000000 & myMsg.lParam ? szDown : szUp),
  199. (0x80000000 & myMsg.lParam ? szUp : szDown)
  200. );
  201. TextOut(hdc, 0, (cyClient / cyChar - 1 - index) * cyChar, szBuffer.toUTF16z, szBuffer.count);
  202. }
  203. return 0;
  204. }
  205. case WM_DESTROY:
  206. {
  207. PostQuitMessage(0);
  208. return 0;
  209. }
  210. default:
  211. }
  212. return DefWindowProc(hwnd, message, wParam, lParam);
  213. }