PageRenderTime 45ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/Samples/Chap09/BtnLook/BtnLook.d

http://github.com/AndrejMitrovic/DWinProgramming
D | 213 lines | 164 code | 40 blank | 9 comment | 5 complexity | 541167139a3aa33d6b97e79e4793f5a6 MD5 | raw file
  1. /+
  2. + Copyright (c) Charles Petzold, 1998.
  3. + Ported to the D Programming Language by Andrej Mitrovic, 2011.
  4. +/
  5. module BtnLook;
  6. import core.runtime;
  7. import core.thread;
  8. import std.conv;
  9. import std.math;
  10. import std.range;
  11. import std.string;
  12. import std.utf : count, toUTFz;
  13. auto toUTF16z(S)(S s)
  14. {
  15. return toUTFz!(const(wchar)*)(s);
  16. }
  17. pragma(lib, "gdi32.lib");
  18. import core.sys.windows.windef;
  19. import core.sys.windows.winuser;
  20. import core.sys.windows.wingdi;
  21. import core.sys.windows.winbase;
  22. struct Button
  23. {
  24. int iStyle;
  25. immutable(wchar)* szText;
  26. }
  27. Button[] buttons =
  28. [
  29. Button(BS_PUSHBUTTON, "PUSHBUTTON" ),
  30. Button(BS_DEFPUSHBUTTON, "DEFPUSHBUTTON"),
  31. Button(BS_CHECKBOX, "CHECKBOX" ),
  32. Button(BS_AUTOCHECKBOX, "AUTOCHECKBOX" ),
  33. Button(BS_RADIOBUTTON, "RADIOBUTTON" ),
  34. Button(BS_3STATE, "3STATE" ),
  35. Button(BS_AUTO3STATE, "AUTO3STATE" ),
  36. Button(BS_GROUPBOX, "GROUPBOX" ),
  37. Button(BS_AUTORADIOBUTTON, "AUTORADIO" ),
  38. Button(BS_OWNERDRAW, "OWNERDRAW" ),
  39. ];
  40. extern (Windows)
  41. int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int iCmdShow)
  42. {
  43. int result;
  44. try
  45. {
  46. Runtime.initialize();
  47. result = myWinMain(hInstance, hPrevInstance, lpCmdLine, iCmdShow);
  48. Runtime.terminate();
  49. }
  50. catch (Throwable o)
  51. {
  52. MessageBox(null, o.toString().toUTF16z, "Error", MB_OK | MB_ICONEXCLAMATION);
  53. result = 0;
  54. }
  55. return result;
  56. }
  57. int myWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int iCmdShow)
  58. {
  59. string appName = "BtnLook";
  60. HWND hwnd;
  61. MSG msg;
  62. WNDCLASS wndclass;
  63. wndclass.style = CS_HREDRAW | CS_VREDRAW;
  64. wndclass.lpfnWndProc = &WndProc;
  65. wndclass.cbClsExtra = 0;
  66. wndclass.cbWndExtra = 0;
  67. wndclass.hInstance = hInstance;
  68. wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  69. wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
  70. wndclass.hbrBackground = cast(HBRUSH) GetStockObject(WHITE_BRUSH);
  71. wndclass.lpszMenuName = NULL;
  72. wndclass.lpszClassName = appName.toUTF16z;
  73. if (!RegisterClass(&wndclass))
  74. {
  75. MessageBox(NULL, "This program requires Windows NT!", appName.toUTF16z, MB_ICONERROR);
  76. return 0;
  77. }
  78. hwnd = CreateWindow(appName.toUTF16z, // window class name
  79. "Button Look", // window caption
  80. WS_OVERLAPPEDWINDOW, // window style
  81. CW_USEDEFAULT, // initial x position
  82. CW_USEDEFAULT, // initial y position
  83. CW_USEDEFAULT, // initial x size
  84. CW_USEDEFAULT, // initial y size
  85. NULL, // parent window handle
  86. NULL, // window menu handle
  87. hInstance, // program instance handle
  88. NULL); // creation parameters
  89. ShowWindow(hwnd, iCmdShow);
  90. UpdateWindow(hwnd);
  91. while (GetMessage(&msg, NULL, 0, 0))
  92. {
  93. TranslateMessage(&msg);
  94. DispatchMessage(&msg);
  95. }
  96. return msg.wParam;
  97. }
  98. extern (Windows)
  99. LRESULT WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) nothrow
  100. {
  101. scope (failure) assert(0);
  102. static HWND[] hwndButtons;
  103. if (hwndButtons is null)
  104. {
  105. hwndButtons.length = buttons.length;
  106. }
  107. static RECT rect;
  108. static szTop = "message wParam lParam";
  109. static szUnd = "_______ ______ ______";
  110. static szFormat = "%-16s%04X-%04X %04X-%04X";
  111. static char[] szBuffer;
  112. static int cxChar, cyChar;
  113. HDC hdc;
  114. PAINTSTRUCT ps;
  115. int i;
  116. switch (message)
  117. {
  118. case WM_CREATE:
  119. cxChar = LOWORD(GetDialogBaseUnits());
  120. cyChar = HIWORD(GetDialogBaseUnits());
  121. foreach (index, button, ref hwndButton; lockstep(buttons, hwndButtons))
  122. {
  123. hwndButton = CreateWindow("button",
  124. button.szText,
  125. WS_CHILD | WS_VISIBLE | button.iStyle,
  126. cxChar, cyChar * (1 + 2 * index),
  127. 20 * cxChar, 7 * cyChar / 4,
  128. hwnd, cast(HMENU)index,
  129. (cast(LPCREATESTRUCT)lParam).hInstance, // lparam is createstruct,
  130. // but could use global window handle
  131. NULL);
  132. }
  133. return 0;
  134. case WM_SIZE:
  135. rect.left = 24 * cxChar;
  136. rect.top = 2 * cyChar;
  137. rect.right = LOWORD(lParam);
  138. rect.bottom = HIWORD(lParam);
  139. return 0;
  140. // ~ case WM_KILLFOCUS:
  141. // ~ if (hwnd == GetParent(cast(HWND)wParam))
  142. // ~ SetFocus(hwnd);
  143. // ~ return 0;
  144. case WM_PAINT:
  145. InvalidateRect(hwnd, &rect, TRUE);
  146. hdc = BeginPaint(hwnd, &ps);
  147. SelectObject(hdc, GetStockObject(SYSTEM_FIXED_FONT));
  148. SetBkMode(hdc, TRANSPARENT);
  149. TextOut(hdc, 24 * cxChar, cyChar, szTop.toUTF16z, szTop.count);
  150. TextOut(hdc, 24 * cxChar, cyChar, szUnd.toUTF16z, szUnd.count);
  151. EndPaint(hwnd, &ps);
  152. return 0;
  153. case WM_DRAWITEM:
  154. case WM_COMMAND:
  155. ScrollWindow(hwnd, 0, -cyChar, &rect, &rect);
  156. hdc = GetDC(hwnd);
  157. SelectObject(hdc, GetStockObject(SYSTEM_FIXED_FONT));
  158. szBuffer = format(szFormat,
  159. message == WM_DRAWITEM ? "WM_DRAWITEM" : "WM_COMMAND",
  160. HIWORD(wParam), LOWORD(wParam),
  161. HIWORD(lParam), LOWORD(lParam)).dup;
  162. TextOut(hdc, 24 * cxChar, cyChar * (rect.bottom / cyChar - 1),
  163. szBuffer.toUTF16z, szBuffer.count);
  164. ReleaseDC(hwnd, hdc);
  165. ValidateRect(hwnd, &rect);
  166. break;
  167. case WM_DESTROY:
  168. PostQuitMessage(0);
  169. return 0;
  170. default:
  171. }
  172. return DefWindowProc(hwnd, message, wParam, lParam);
  173. }