PageRenderTime 63ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/Samples/Chap10/MenuDemo/MenuDemo.d

http://github.com/AndrejMitrovic/DWinProgramming
D | 204 lines | 161 code | 39 blank | 4 comment | 5 complexity | 5ac23c234584a6dd4fced27c9bc5ca15 MD5 | raw file
  1. /+
  2. + Copyright (c) Charles Petzold, 1998.
  3. + Ported to the D Programming Language by Andrej Mitrovic, 2011.
  4. +/
  5. module MenuDemo;
  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.stdio;
  13. import std.utf;
  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. HINSTANCE hInst;
  25. enum ID_TIMER = 1;
  26. string appName = "MenuDemo";
  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(NULL, IDI_APPLICATION);
  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. hwnd = CreateWindow(appName.toUTF16z, // window class name
  65. "Menu Demo", // window caption
  66. WS_OVERLAPPEDWINDOW, // window style
  67. CW_USEDEFAULT, // initial x position
  68. CW_USEDEFAULT, // initial y position
  69. CW_USEDEFAULT, // initial x size
  70. CW_USEDEFAULT, // initial y size
  71. NULL, // parent window handle
  72. NULL, // window menu handle
  73. hInstance, // program instance handle
  74. NULL); // creation parameters
  75. ShowWindow(hwnd, iCmdShow);
  76. UpdateWindow(hwnd);
  77. while (GetMessage(&msg, NULL, 0, 0))
  78. {
  79. TranslateMessage(&msg);
  80. DispatchMessage(&msg);
  81. }
  82. return msg.wParam;
  83. }
  84. extern (Windows)
  85. LRESULT WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) nothrow
  86. {
  87. scope (failure) assert(0);
  88. static HMENU hMenu;
  89. enum idColor = [WHITE_BRUSH, LTGRAY_BRUSH, GRAY_BRUSH, DKGRAY_BRUSH, BLACK_BRUSH];
  90. static int iSelection = IDM_BKGND_WHITE;
  91. POINT point;
  92. switch (message)
  93. {
  94. case WM_CREATE:
  95. hMenu = LoadMenu(hInst, appName.toUTF16z);
  96. hMenu = GetSubMenu(hMenu, 0);
  97. return 0;
  98. case WM_RBUTTONUP:
  99. point.x = LOWORD(lParam);
  100. point.y = HIWORD(lParam);
  101. ClientToScreen(hwnd, &point);
  102. TrackPopupMenu(hMenu, TPM_RIGHTBUTTON, point.x, point.y, 0, hwnd, NULL);
  103. return 0;
  104. case WM_COMMAND:
  105. {
  106. hMenu = GetMenu(hwnd);
  107. switch (LOWORD(wParam))
  108. {
  109. case IDM_FILE_NEW:
  110. case IDM_FILE_OPEN:
  111. case IDM_FILE_SAVE:
  112. case IDM_FILE_SAVE_AS:
  113. case IDM_EDIT_UNDO:
  114. case IDM_EDIT_CUT:
  115. case IDM_EDIT_COPY:
  116. case IDM_EDIT_PASTE:
  117. case IDM_EDIT_CLEAR:
  118. MessageBeep(0);
  119. return 0;
  120. case IDM_APP_EXIT:
  121. SendMessage(hwnd, WM_CLOSE, 0, 0);
  122. return 0;
  123. case IDM_BKGND_WHITE: // Note: Logic below assumes that IDM_WHITE
  124. case IDM_BKGND_LTGRAY: // through IDM_BLACK are consecutive numbers in
  125. case IDM_BKGND_GRAY: // the order shown here.
  126. case IDM_BKGND_DKGRAY:
  127. case IDM_BKGND_BLACK:
  128. CheckMenuItem(hMenu, iSelection, MF_UNCHECKED);
  129. iSelection = LOWORD(wParam);
  130. CheckMenuItem(hMenu, iSelection, MF_CHECKED);
  131. SetClassLong(hwnd, GCL_HBRBACKGROUND,
  132. cast(LONG)GetStockObject(idColor [LOWORD(wParam) - IDM_BKGND_WHITE]));
  133. InvalidateRect(hwnd, NULL, TRUE);
  134. return 0;
  135. case IDM_TIMER_START:
  136. if (SetTimer(hwnd, ID_TIMER, 1000, NULL))
  137. {
  138. EnableMenuItem(hMenu, IDM_TIMER_START, MF_GRAYED);
  139. EnableMenuItem(hMenu, IDM_TIMER_STOP, MF_ENABLED);
  140. }
  141. return 0;
  142. case IDM_TIMER_STOP:
  143. KillTimer(hwnd, ID_TIMER);
  144. EnableMenuItem(hMenu, IDM_TIMER_START, MF_ENABLED);
  145. EnableMenuItem(hMenu, IDM_TIMER_STOP, MF_GRAYED);
  146. return 0;
  147. case IDM_APP_HELP:
  148. MessageBox(hwnd, "Help not yet implemented!",
  149. appName.toUTF16z, MB_ICONEXCLAMATION | MB_OK);
  150. return 0;
  151. case IDM_APP_ABOUT:
  152. MessageBox(hwnd, "Menu Demonstration Program\n (c) Charles Petzold, 1998",
  153. appName.toUTF16z, MB_ICONINFORMATION | MB_OK);
  154. return 0;
  155. default:
  156. }
  157. break;
  158. }
  159. case WM_DESTROY:
  160. PostQuitMessage(0);
  161. return 0;
  162. default:
  163. }
  164. return DefWindowProc(hwnd, message, wParam, lParam);
  165. }