PageRenderTime 60ms CodeModel.GetById 34ms RepoModel.GetById 0ms app.codeStats 0ms

/Samples/Chap11/About3/About3.d

http://github.com/AndrejMitrovic/DWinProgramming
D | 225 lines | 172 code | 48 blank | 5 comment | 9 complexity | 1622e60c0598ae2090aaa961b0f79d18 MD5 | raw file
  1. /+
  2. + Copyright (c) Charles Petzold, 1998.
  3. + Ported to the D Programming Language by Andrej Mitrovic, 2011.
  4. +/
  5. module About3;
  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. import resource;
  23. string appName = "About3";
  24. string description = "About Box Demo Program";
  25. enum ID_TIMER = 1;
  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. HWND hwnd;
  47. MSG msg;
  48. WNDCLASS wndclass;
  49. wndclass.style = CS_HREDRAW | CS_VREDRAW;
  50. wndclass.lpfnWndProc = &WndProc;
  51. wndclass.cbClsExtra = 0;
  52. wndclass.cbWndExtra = 0;
  53. wndclass.hInstance = hInstance;
  54. wndclass.hIcon = LoadIcon(hInstance, appName.toUTF16z);
  55. wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
  56. wndclass.hbrBackground = cast(HBRUSH)GetStockObject(WHITE_BRUSH);
  57. wndclass.lpszMenuName = appName.toUTF16z;
  58. wndclass.lpszClassName = appName.toUTF16z;
  59. if (!RegisterClass(&wndclass))
  60. {
  61. MessageBox(NULL, "This program requires Windows NT!", appName.toUTF16z, MB_ICONERROR);
  62. return 0;
  63. }
  64. wndclass.style = CS_HREDRAW | CS_VREDRAW;
  65. wndclass.lpfnWndProc = &EllipPushWndProc;
  66. wndclass.cbClsExtra = 0;
  67. wndclass.cbWndExtra = 0;
  68. wndclass.hInstance = hInstance;
  69. wndclass.hIcon = NULL;
  70. wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
  71. wndclass.hbrBackground = cast(HBRUSH)(COLOR_BTNFACE + 1);
  72. // ~ wndclass.hbrBackground = cast(HBRUSH)(COLOR_WINDOW + 1);
  73. wndclass.lpszMenuName = NULL;
  74. wndclass.lpszClassName = "EllipPush";
  75. RegisterClass(&wndclass);
  76. hwnd = CreateWindow(appName.toUTF16z, // window class name
  77. description.toUTF16z, // window caption
  78. WS_OVERLAPPEDWINDOW, // window style
  79. CW_USEDEFAULT, // initial x position
  80. CW_USEDEFAULT, // initial y position
  81. CW_USEDEFAULT, // initial x size
  82. CW_USEDEFAULT, // initial y size
  83. NULL, // parent window handle
  84. NULL, // window menu handle
  85. hInstance, // program instance handle
  86. NULL); // creation parameters
  87. ShowWindow(hwnd, iCmdShow);
  88. UpdateWindow(hwnd);
  89. while (GetMessage(&msg, NULL, 0, 0))
  90. {
  91. TranslateMessage(&msg);
  92. DispatchMessage(&msg);
  93. }
  94. return msg.wParam;
  95. }
  96. extern (Windows)
  97. LRESULT WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) nothrow
  98. {
  99. scope (failure) assert(0);
  100. static HINSTANCE hInstance;
  101. switch (message)
  102. {
  103. case WM_CREATE:
  104. hInstance = (cast(LPCREATESTRUCT)lParam).hInstance;
  105. return 0;
  106. case WM_COMMAND:
  107. switch (LOWORD(wParam))
  108. {
  109. case IDM_APP_ABOUT:
  110. DialogBox(hInstance, "AboutBox", hwnd, &AboutDlgProc);
  111. break;
  112. default:
  113. }
  114. return 0;
  115. case WM_DESTROY:
  116. PostQuitMessage(0);
  117. return 0;
  118. default:
  119. }
  120. return DefWindowProc(hwnd, message, wParam, lParam);
  121. }
  122. extern (Windows)
  123. BOOL AboutDlgProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
  124. {
  125. switch (message)
  126. {
  127. case WM_INITDIALOG:
  128. return true;
  129. case WM_COMMAND:
  130. {
  131. switch (LOWORD(wParam))
  132. {
  133. case IDOK:
  134. EndDialog(hDlg, 0);
  135. return true;
  136. default:
  137. }
  138. break;
  139. }
  140. default:
  141. }
  142. return false;
  143. }
  144. extern (Windows)
  145. LRESULT EllipPushWndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  146. {
  147. TCHAR[40] szText;
  148. HBRUSH hBrush;
  149. HDC hdc;
  150. PAINTSTRUCT ps;
  151. RECT rect;
  152. switch (message)
  153. {
  154. case WM_PAINT:
  155. GetClientRect(hwnd, &rect);
  156. GetWindowText(hwnd, szText.ptr, szText.count);
  157. hdc = BeginPaint(hwnd, &ps);
  158. hBrush = CreateSolidBrush(GetSysColor(COLOR_WINDOW));
  159. hBrush = cast(HBRUSH)SelectObject(hdc, hBrush);
  160. SetBkColor(hdc, GetSysColor(COLOR_WINDOW));
  161. SetTextColor(hdc, GetSysColor(COLOR_WINDOWTEXT));
  162. Ellipse(hdc, rect.left, rect.top, rect.right, rect.bottom);
  163. DrawText(hdc, szText.ptr, -1, &rect, DT_SINGLELINE | DT_CENTER | DT_VCENTER);
  164. DeleteObject(SelectObject(hdc, hBrush));
  165. EndPaint(hwnd, &ps);
  166. return 0;
  167. case WM_KEYUP:
  168. if (wParam != VK_SPACE)
  169. break;
  170. goto case WM_LBUTTONUP;
  171. case WM_LBUTTONUP:
  172. SendMessage(GetParent(hwnd), WM_COMMAND, GetWindowLongPtr(hwnd, GWL_ID), cast(LPARAM)hwnd);
  173. return 0;
  174. default:
  175. }
  176. return DefWindowProc(hwnd, message, wParam, lParam);
  177. }