PageRenderTime 43ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/Samples/Chap08/Beeper1/Beeper1.d

http://github.com/AndrejMitrovic/DWinProgramming
D | 141 lines | 109 code | 28 blank | 4 comment | 3 complexity | 8402f4764b4505fe1a2f10de901c3c46 MD5 | raw file
  1. /+
  2. + Copyright (c) Charles Petzold, 1998.
  3. + Ported to the D Programming Language by Andrej Mitrovic, 2011.
  4. +/
  5. module Beeper1;
  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 = "Beeper1";
  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. "Beeper1 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. static BOOL fFlipFlop = FALSE;
  85. HBRUSH hBrush;
  86. HDC hdc;
  87. PAINTSTRUCT ps;
  88. RECT rc;
  89. switch (message)
  90. {
  91. case WM_CREATE:
  92. SetTimer(hwnd, ID_TIMER, 1000, NULL);
  93. return 0;
  94. case WM_TIMER:
  95. fFlipFlop ^= 1;
  96. InvalidateRect(hwnd, NULL, FALSE);
  97. return 0;
  98. case WM_PAINT:
  99. hdc = BeginPaint(hwnd, &ps);
  100. GetClientRect(hwnd, &rc);
  101. hBrush = CreateSolidBrush(fFlipFlop ? RGB(255, 0, 0) : RGB(0, 0, 255));
  102. FillRect(hdc, &rc, hBrush);
  103. EndPaint(hwnd, &ps);
  104. DeleteObject(hBrush);
  105. return 0;
  106. case WM_DESTROY:
  107. KillTimer(hwnd, ID_TIMER);
  108. PostQuitMessage(0);
  109. return 0;
  110. default:
  111. }
  112. return DefWindowProc(hwnd, message, wParam, lParam);
  113. }