PageRenderTime 47ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/Samples/Chap11/About1/About1.d

http://github.com/AndrejMitrovic/DWinProgramming
D | 164 lines | 126 code | 34 blank | 4 comment | 6 complexity | c72261921fbcb8fc720e049f1867f527 MD5 | raw file
  1. /+
  2. + Copyright (c) Charles Petzold, 1998.
  3. + Ported to the D Programming Language by Andrej Mitrovic, 2011.
  4. +/
  5. module About1;
  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 = "About1";
  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. hwnd = CreateWindow(appName.toUTF16z, // window class name
  65. description.toUTF16z, // 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 HINSTANCE hInstance;
  89. switch (message)
  90. {
  91. case WM_CREATE:
  92. hInstance = (cast(LPCREATESTRUCT)lParam).hInstance;
  93. return 0;
  94. case WM_COMMAND:
  95. switch (LOWORD(wParam))
  96. {
  97. case IDM_APP_ABOUT:
  98. DialogBox(hInstance, "AboutBox", hwnd, &AboutDlgProc);
  99. break;
  100. default:
  101. }
  102. return 0;
  103. case WM_DESTROY:
  104. PostQuitMessage(0);
  105. return 0;
  106. default:
  107. }
  108. return DefWindowProc(hwnd, message, wParam, lParam);
  109. }
  110. extern (Windows)
  111. BOOL AboutDlgProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
  112. {
  113. switch (message)
  114. {
  115. case WM_INITDIALOG:
  116. return TRUE;
  117. case WM_COMMAND:
  118. switch (LOWORD(wParam))
  119. {
  120. case IDOK:
  121. case IDCANCEL:
  122. EndDialog(hDlg, 0);
  123. return TRUE;
  124. default:
  125. }
  126. break;
  127. default:
  128. }
  129. return FALSE;
  130. }