PageRenderTime 46ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/Samples/Chap10/PoorMenu/PoorMenu.d

http://github.com/AndrejMitrovic/DWinProgramming
D | 151 lines | 118 code | 29 blank | 4 comment | 4 complexity | 0ab805193690b0fb9e48653b0ec9644c MD5 | raw file
  1. /+
  2. + Copyright (c) Charles Petzold, 1998.
  3. + Ported to the D Programming Language by Andrej Mitrovic, 2011.
  4. +/
  5. module PoorMenu;
  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. string appName = "PoorMenu";
  23. enum ID_TIMER = 1;
  24. HINSTANCE hinst;
  25. extern (Windows)
  26. int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int iCmdShow)
  27. {
  28. int result;
  29. try
  30. {
  31. Runtime.initialize();
  32. result = myWinMain(hInstance, hPrevInstance, lpCmdLine, iCmdShow);
  33. Runtime.terminate();
  34. }
  35. catch (Throwable o)
  36. {
  37. MessageBox(null, o.toString().toUTF16z, "Error", MB_OK | MB_ICONEXCLAMATION);
  38. result = 0;
  39. }
  40. return result;
  41. }
  42. int myWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int iCmdShow)
  43. {
  44. HMENU hMenu;
  45. HWND hwnd;
  46. MSG msg;
  47. WNDCLASS wndclass;
  48. wndclass.style = CS_HREDRAW | CS_VREDRAW;
  49. wndclass.lpfnWndProc = &WndProc;
  50. wndclass.cbClsExtra = 0;
  51. wndclass.cbWndExtra = 0;
  52. wndclass.hInstance = hInstance;
  53. wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  54. wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
  55. wndclass.hbrBackground = cast(HBRUSH) GetStockObject(WHITE_BRUSH);
  56. wndclass.lpszMenuName = NULL;
  57. wndclass.lpszClassName = appName.toUTF16z;
  58. if (!RegisterClass(&wndclass))
  59. {
  60. MessageBox(NULL, "This program requires Windows NT!", appName.toUTF16z, MB_ICONERROR);
  61. return 0;
  62. }
  63. hwnd = CreateWindow(appName.toUTF16z, // window class name
  64. "PoorMenu", // window caption
  65. WS_OVERLAPPEDWINDOW, // window style
  66. CW_USEDEFAULT, // initial x position
  67. CW_USEDEFAULT, // initial y position
  68. CW_USEDEFAULT, // initial x size
  69. CW_USEDEFAULT, // initial y size
  70. NULL, // parent window handle
  71. NULL, // window menu handle
  72. hInstance, // program instance handle
  73. NULL); // creation parameters
  74. hMenu = GetSystemMenu(hwnd, FALSE);
  75. AppendMenu(hMenu, MF_SEPARATOR, 0, NULL);
  76. AppendMenu(hMenu, MF_STRING, IDM_SYS_ABOUT, "About...");
  77. AppendMenu(hMenu, MF_STRING, IDM_SYS_HELP, "Help...");
  78. AppendMenu(hMenu, MF_STRING, IDM_SYS_REMOVE, "Remove Additions");
  79. ShowWindow(hwnd, iCmdShow);
  80. UpdateWindow(hwnd);
  81. while (GetMessage(&msg, NULL, 0, 0))
  82. {
  83. TranslateMessage(&msg);
  84. DispatchMessage(&msg);
  85. }
  86. return msg.wParam;
  87. }
  88. enum IDM_SYS_ABOUT = 1;
  89. enum IDM_SYS_HELP = 2;
  90. enum IDM_SYS_REMOVE = 3;
  91. extern (Windows)
  92. LRESULT WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) nothrow
  93. {
  94. scope (failure) assert(0);
  95. switch (message)
  96. {
  97. case WM_SYSCOMMAND:
  98. {
  99. switch (LOWORD(wParam))
  100. {
  101. case IDM_SYS_ABOUT:
  102. MessageBox(hwnd, "A Poor-Person's Menu Program\n(c) Charles Petzold, 1998",
  103. appName.toUTF16z, MB_OK | MB_ICONINFORMATION);
  104. return 0;
  105. case IDM_SYS_HELP:
  106. MessageBox(hwnd, "Help not yet implemented!",
  107. appName.toUTF16z, MB_OK | MB_ICONEXCLAMATION);
  108. return 0;
  109. case IDM_SYS_REMOVE:
  110. GetSystemMenu(hwnd, TRUE);
  111. return 0;
  112. default:
  113. }
  114. break;
  115. }
  116. case WM_DESTROY:
  117. PostQuitMessage(0);
  118. return 0;
  119. default:
  120. }
  121. return DefWindowProc(hwnd, message, wParam, lParam);
  122. }