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

/Samples/Chap10/PopMenu/PopMenu.d

http://github.com/AndrejMitrovic/DWinProgramming
D | 187 lines | 147 code | 36 blank | 4 comment | 4 complexity | fbf4d6623fd1542fcbe543f35085a7ff MD5 | raw file
  1. /+
  2. + Copyright (c) Charles Petzold, 1998.
  3. + Ported to the D Programming Language by Andrej Mitrovic, 2011.
  4. +/
  5. module name;
  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;
  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 = "PopMenu";
  24. string description = "Popup Menu Demonstration";
  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. hinst = hInstance;
  47. HWND hwnd;
  48. MSG msg;
  49. WNDCLASS wndclass;
  50. wndclass.style = CS_HREDRAW | CS_VREDRAW;
  51. wndclass.lpfnWndProc = &WndProc;
  52. wndclass.cbClsExtra = 0;
  53. wndclass.cbWndExtra = 0;
  54. wndclass.hInstance = hInstance;
  55. wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  56. wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
  57. wndclass.hbrBackground = cast(HBRUSH)GetStockObject(WHITE_BRUSH);
  58. wndclass.lpszMenuName = appName.toUTF16z;
  59. wndclass.lpszClassName = appName.toUTF16z;
  60. if (!RegisterClass(&wndclass))
  61. {
  62. MessageBox(NULL, "This program requires Windows NT!", appName.toUTF16z, MB_ICONERROR);
  63. return 0;
  64. }
  65. hwnd = CreateWindow(appName.toUTF16z, // window class name
  66. description.toUTF16z, // window caption
  67. WS_OVERLAPPEDWINDOW, // window style
  68. CW_USEDEFAULT, // initial x position
  69. CW_USEDEFAULT, // initial y position
  70. CW_USEDEFAULT, // initial x size
  71. CW_USEDEFAULT, // initial y size
  72. NULL, // parent window handle
  73. NULL, // window menu handle
  74. hInstance, // program instance handle
  75. NULL); // creation parameters
  76. ShowWindow(hwnd, iCmdShow);
  77. UpdateWindow(hwnd);
  78. while (GetMessage(&msg, NULL, 0, 0))
  79. {
  80. TranslateMessage(&msg);
  81. DispatchMessage(&msg);
  82. }
  83. return msg.wParam;
  84. }
  85. extern (Windows)
  86. LRESULT WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) nothrow
  87. {
  88. scope (failure) assert(0);
  89. static HMENU hMenu;
  90. enum idColor = [WHITE_BRUSH, LTGRAY_BRUSH, GRAY_BRUSH, DKGRAY_BRUSH, BLACK_BRUSH];
  91. static int iSelection = IDM_BKGND_WHITE;
  92. POINT point;
  93. switch (message)
  94. {
  95. case WM_CREATE:
  96. hMenu = LoadMenu(hinst, appName.toUTF16z);
  97. hMenu = GetSubMenu(hMenu, 0);
  98. return 0;
  99. case WM_RBUTTONUP:
  100. point.x = LOWORD(lParam);
  101. point.y = HIWORD(lParam);
  102. ClientToScreen(hwnd, &point);
  103. TrackPopupMenu(hMenu, TPM_RIGHTBUTTON, point.x, point.y, 0, hwnd, NULL);
  104. return 0;
  105. case WM_COMMAND:
  106. switch (LOWORD(wParam))
  107. {
  108. case IDM_FILE_NEW:
  109. case IDM_FILE_OPEN:
  110. case IDM_FILE_SAVE:
  111. case IDM_FILE_SAVE_AS:
  112. case IDM_EDIT_UNDO:
  113. case IDM_EDIT_CUT:
  114. case IDM_EDIT_COPY:
  115. case IDM_EDIT_PASTE:
  116. case IDM_EDIT_CLEAR:
  117. MessageBeep(0);
  118. return 0;
  119. case IDM_BKGND_WHITE: // Note: Logic below
  120. case IDM_BKGND_LTGRAY: // assumes that IDM_WHITE
  121. case IDM_BKGND_GRAY: // through IDM_BLACK are
  122. case IDM_BKGND_DKGRAY: // consecutive numbers in
  123. case IDM_BKGND_BLACK: // the order shown here.
  124. CheckMenuItem(hMenu, iSelection, MF_UNCHECKED);
  125. iSelection = LOWORD(wParam);
  126. CheckMenuItem(hMenu, iSelection, MF_CHECKED);
  127. SetClassLong(hwnd, GCL_HBRBACKGROUND,
  128. cast(LONG)GetStockObject(idColor [LOWORD(wParam) - IDM_BKGND_WHITE]));
  129. InvalidateRect(hwnd, NULL, TRUE);
  130. return 0;
  131. case IDM_APP_ABOUT:
  132. MessageBox(hwnd, "Popup Menu Demonstration Program\n(c) Charles Petzold, 1998",
  133. appName.toUTF16z, MB_ICONINFORMATION | MB_OK);
  134. return 0;
  135. case IDM_APP_EXIT:
  136. SendMessage(hwnd, WM_CLOSE, 0, 0);
  137. return 0;
  138. case IDM_APP_HELP:
  139. MessageBox(hwnd, "Help not yet implemented!",
  140. appName.toUTF16z, MB_ICONEXCLAMATION | MB_OK);
  141. return 0;
  142. default:
  143. }
  144. break;
  145. case WM_DESTROY:
  146. PostQuitMessage(0);
  147. return 0;
  148. default:
  149. }
  150. return DefWindowProc(hwnd, message, wParam, lParam);
  151. }