PageRenderTime 49ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/Samples/Chap08/Beeper2/Beeper2.d

http://github.com/AndrejMitrovic/DWinProgramming
D | 140 lines | 108 code | 28 blank | 4 comment | 3 complexity | 459068f0e96b820ee562384742d677a6 MD5 | raw file
  1. /+
  2. + Copyright (c) Charles Petzold, 1998.
  3. + Ported to the D Programming Language by Andrej Mitrovic, 2011.
  4. +/
  5. module Beeper2;
  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. enum ID_TIMER = 1;
  22. extern (Windows)
  23. int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int iCmdShow)
  24. {
  25. int result;
  26. try
  27. {
  28. Runtime.initialize();
  29. result = myWinMain(hInstance, hPrevInstance, lpCmdLine, iCmdShow);
  30. Runtime.terminate();
  31. }
  32. catch (Throwable o)
  33. {
  34. MessageBox(null, o.toString().toUTF16z, "Error", MB_OK | MB_ICONEXCLAMATION);
  35. result = 0;
  36. }
  37. return result;
  38. }
  39. int myWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int iCmdShow)
  40. {
  41. string appName = "Beeper2";
  42. HWND hwnd;
  43. MSG msg;
  44. WNDCLASS wndclass;
  45. wndclass.style = CS_HREDRAW | CS_VREDRAW;
  46. wndclass.lpfnWndProc = &WndProc;
  47. wndclass.cbClsExtra = 0;
  48. wndclass.cbWndExtra = 0;
  49. wndclass.hInstance = hInstance;
  50. wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  51. wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
  52. wndclass.hbrBackground = cast(HBRUSH) GetStockObject(WHITE_BRUSH);
  53. wndclass.lpszMenuName = NULL;
  54. wndclass.lpszClassName = appName.toUTF16z;
  55. if (!RegisterClass(&wndclass))
  56. {
  57. MessageBox(NULL, "This program requires Windows NT!", appName.toUTF16z, MB_ICONERROR);
  58. return 0;
  59. }
  60. hwnd = CreateWindow(appName.toUTF16z, // window class name
  61. "Beeper2 Timer Demo", // window caption
  62. WS_OVERLAPPEDWINDOW, // window style
  63. CW_USEDEFAULT, // initial x position
  64. CW_USEDEFAULT, // initial y position
  65. CW_USEDEFAULT, // initial x size
  66. CW_USEDEFAULT, // initial y size
  67. NULL, // parent window handle
  68. NULL, // window menu handle
  69. hInstance, // program instance handle
  70. NULL); // creation parameters
  71. ShowWindow(hwnd, iCmdShow);
  72. UpdateWindow(hwnd);
  73. while (GetMessage(&msg, NULL, 0, 0))
  74. {
  75. TranslateMessage(&msg);
  76. DispatchMessage(&msg);
  77. }
  78. return msg.wParam;
  79. }
  80. extern (Windows)
  81. LRESULT WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) nothrow
  82. {
  83. scope (failure) assert(0);
  84. switch (message)
  85. {
  86. case WM_CREATE:
  87. SetTimer (hwnd, ID_TIMER, 1000, &TimerProc);
  88. return 0;
  89. case WM_DESTROY:
  90. KillTimer (hwnd, ID_TIMER);
  91. PostQuitMessage (0);
  92. return 0;
  93. default:
  94. }
  95. return DefWindowProc (hwnd, message, wParam, lParam);
  96. }
  97. extern (Windows)
  98. void TimerProc(HWND hwnd, UINT message, UINT iTimerID, DWORD dwTime)
  99. {
  100. static BOOL fFlipFlop = FALSE;
  101. HBRUSH hBrush;
  102. HDC hdc;
  103. RECT rc;
  104. MessageBeep (-1);
  105. fFlipFlop = !fFlipFlop;
  106. GetClientRect (hwnd, &rc);
  107. hdc = GetDC (hwnd);
  108. hBrush = CreateSolidBrush (fFlipFlop ? RGB(255, 0, 0) : RGB(0, 0, 255));
  109. FillRect (hdc, &rc, hBrush);
  110. ReleaseDC (hwnd, hdc);
  111. DeleteObject (hBrush);
  112. }