PageRenderTime 52ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/Samples/Chap10/NoPopups/NoPopups.d

http://github.com/AndrejMitrovic/DWinProgramming
D | 167 lines | 129 code | 34 blank | 4 comment | 4 complexity | 7c914bb19ea4b4a24a7af1415fdc4b63 MD5 | raw file
  1. /+
  2. + Copyright (c) Charles Petzold, 1998.
  3. + Ported to the D Programming Language by Andrej Mitrovic, 2011.
  4. +/
  5. module NoPopups;
  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 = "NoPopups";
  24. enum ID_TIMER = 1;
  25. HINSTANCE hinst;
  26. extern (Windows)
  27. int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int iCmdShow)
  28. {
  29. int result;
  30. try
  31. {
  32. Runtime.initialize();
  33. result = myWinMain(hInstance, hPrevInstance, lpCmdLine, iCmdShow);
  34. Runtime.terminate();
  35. }
  36. catch (Throwable o)
  37. {
  38. MessageBox(null, o.toString().toUTF16z, "Error", MB_OK | MB_ICONEXCLAMATION);
  39. result = 0;
  40. }
  41. return result;
  42. }
  43. int myWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int iCmdShow)
  44. {
  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. "NoPopups", // 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. ShowWindow(hwnd, iCmdShow);
  75. UpdateWindow(hwnd);
  76. while (GetMessage(&msg, NULL, 0, 0))
  77. {
  78. TranslateMessage(&msg);
  79. DispatchMessage(&msg);
  80. }
  81. return msg.wParam;
  82. }
  83. extern (Windows)
  84. LRESULT WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) nothrow
  85. {
  86. scope (failure) assert(0);
  87. static HMENU hMenuMain, hMenuEdit, hMenuFile;
  88. HINSTANCE hInstance;
  89. switch (message)
  90. {
  91. case WM_CREATE:
  92. hInstance = cast(HINSTANCE)GetWindowLongPtr(hwnd, GWL_HINSTANCE);
  93. hMenuMain = LoadMenu(hInstance, "MenuMain");
  94. hMenuFile = LoadMenu(hInstance, "MenuFile");
  95. hMenuEdit = LoadMenu(hInstance, "MenuEdit");
  96. SetMenu(hwnd, hMenuMain);
  97. return 0;
  98. case WM_COMMAND:
  99. switch (LOWORD(wParam))
  100. {
  101. case IDM_MAIN:
  102. SetMenu(hwnd, hMenuMain);
  103. return 0;
  104. case IDM_FILE:
  105. SetMenu(hwnd, hMenuFile);
  106. return 0;
  107. case IDM_EDIT:
  108. SetMenu(hwnd, hMenuEdit);
  109. return 0;
  110. case IDM_FILE_NEW:
  111. case IDM_FILE_OPEN:
  112. case IDM_FILE_SAVE:
  113. case IDM_FILE_SAVE_AS:
  114. case IDM_EDIT_UNDO:
  115. case IDM_EDIT_CUT:
  116. case IDM_EDIT_COPY:
  117. case IDM_EDIT_PASTE:
  118. case IDM_EDIT_CLEAR:
  119. MessageBeep(0);
  120. return 0;
  121. default:
  122. }
  123. break;
  124. case WM_DESTROY:
  125. SetMenu(hwnd, hMenuMain);
  126. DestroyMenu(hMenuFile);
  127. DestroyMenu(hMenuEdit);
  128. PostQuitMessage(0);
  129. return 0;
  130. default:
  131. }
  132. return DefWindowProc(hwnd, message, wParam, lParam);
  133. }