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

/Samples/Chap11/Colors2/Colors2.d

http://github.com/AndrejMitrovic/DWinProgramming
D | 205 lines | 160 code | 41 blank | 4 comment | 9 complexity | 21fe4d1bad108c901f53fcea82b39880 MD5 | raw file
  1. /+
  2. + Copyright (c) Charles Petzold, 1998.
  3. + Ported to the D Programming Language by Andrej Mitrovic, 2011.
  4. +/
  5. module Colors2;
  6. import core.runtime;
  7. import core.thread;
  8. import std.algorithm : max, min;
  9. import std.conv;
  10. import std.math;
  11. import std.range;
  12. import std.string;
  13. import std.utf;
  14. auto toUTF16z(S)(S s)
  15. {
  16. return toUTFz!(const(wchar)*)(s);
  17. }
  18. pragma(lib, "gdi32.lib");
  19. import core.sys.windows.windef;
  20. import core.sys.windows.winuser;
  21. import core.sys.windows.wingdi;
  22. import core.sys.windows.winbase;
  23. import resource;
  24. string appName = "Colors2";
  25. string description = "Color Scroll";
  26. enum ID_TIMER = 1;
  27. HINSTANCE hinst;
  28. HWND hDlgModeless;
  29. extern (Windows)
  30. int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int iCmdShow)
  31. {
  32. int result;
  33. try
  34. {
  35. Runtime.initialize();
  36. result = myWinMain(hInstance, hPrevInstance, lpCmdLine, iCmdShow);
  37. Runtime.terminate();
  38. }
  39. catch (Throwable o)
  40. {
  41. MessageBox(null, o.toString().toUTF16z, "Error", MB_OK | MB_ICONEXCLAMATION);
  42. result = 0;
  43. }
  44. return result;
  45. }
  46. int myWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int iCmdShow)
  47. {
  48. hinst = hInstance;
  49. HWND hwnd;
  50. MSG msg;
  51. WNDCLASS wndclass;
  52. wndclass.style = CS_HREDRAW | CS_VREDRAW;
  53. wndclass.lpfnWndProc = &WndProc;
  54. wndclass.cbClsExtra = 0;
  55. wndclass.cbWndExtra = 0;
  56. wndclass.hInstance = hInstance;
  57. wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  58. wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
  59. wndclass.hbrBackground = cast(HBRUSH) GetStockObject(WHITE_BRUSH);
  60. wndclass.lpszMenuName = NULL;
  61. wndclass.lpszClassName = appName.toUTF16z;
  62. if (!RegisterClass(&wndclass))
  63. {
  64. MessageBox(NULL, "This program requires Windows NT!", appName.toUTF16z, MB_ICONERROR);
  65. return 0;
  66. }
  67. hwnd = CreateWindow(appName.toUTF16z, // window class name
  68. description.toUTF16z, // window caption
  69. WS_OVERLAPPEDWINDOW, // window style
  70. CW_USEDEFAULT, // initial x position
  71. CW_USEDEFAULT, // initial y position
  72. CW_USEDEFAULT, // initial x size
  73. CW_USEDEFAULT, // initial y size
  74. NULL, // parent window handle
  75. NULL, // window menu handle
  76. hInstance, // program instance handle
  77. NULL); // creation parameters
  78. ShowWindow(hwnd, iCmdShow);
  79. UpdateWindow(hwnd);
  80. hDlgModeless = CreateDialog(hInstance, "ColorScrDlg", hwnd, &ColorScrDlg);
  81. while (GetMessage(&msg, NULL, 0, 0))
  82. {
  83. if (hDlgModeless == null || !IsDialogMessage(hDlgModeless, &msg))
  84. {
  85. TranslateMessage(&msg);
  86. DispatchMessage(&msg);
  87. }
  88. }
  89. return msg.wParam;
  90. }
  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_DESTROY:
  98. DeleteObject(cast(HGDIOBJ)SetClassLong(hwnd, GCL_HBRBACKGROUND,
  99. cast(LONG)GetStockObject(WHITE_BRUSH)));
  100. PostQuitMessage(0);
  101. return 0;
  102. default:
  103. }
  104. return DefWindowProc(hwnd, message, wParam, lParam);
  105. }
  106. extern (Windows)
  107. BOOL ColorScrDlg(HWND hDlg, UINT message,
  108. WPARAM wParam, LPARAM lParam)
  109. {
  110. static ubyte[3] iColor;
  111. HWND hwndParent, hCtrl;
  112. int iCtrlID, iIndex;
  113. switch (message)
  114. {
  115. case WM_INITDIALOG:
  116. for (iCtrlID = 10; iCtrlID < 13; iCtrlID++)
  117. {
  118. hCtrl = GetDlgItem(hDlg, iCtrlID);
  119. SetScrollRange(hCtrl, SB_CTL, 0, 255, FALSE);
  120. SetScrollPos(hCtrl, SB_CTL, 0, FALSE);
  121. }
  122. return TRUE;
  123. case WM_VSCROLL:
  124. hCtrl = cast(HWND)lParam;
  125. iCtrlID = GetWindowLongPtr(hCtrl, GWL_ID);
  126. iIndex = iCtrlID - 10;
  127. hwndParent = GetParent(hDlg);
  128. switch (LOWORD(wParam))
  129. {
  130. case SB_PAGEDOWN:
  131. iColor[iIndex] += 15; // fall through
  132. goto case;
  133. case SB_LINEDOWN:
  134. iColor[iIndex] = cast(ubyte)min(255, iColor[iIndex] + 1);
  135. break;
  136. case SB_PAGEUP:
  137. iColor[iIndex] -= 15; // fall through
  138. goto case;
  139. case SB_LINEUP:
  140. iColor[iIndex] = cast(ubyte)max(0, iColor[iIndex] - 1);
  141. break;
  142. case SB_TOP:
  143. iColor[iIndex] = 0;
  144. break;
  145. case SB_BOTTOM:
  146. iColor[iIndex] = 255;
  147. break;
  148. case SB_THUMBPOSITION:
  149. case SB_THUMBTRACK:
  150. iColor[iIndex] = cast(ubyte)HIWORD(wParam);
  151. break;
  152. default:
  153. return FALSE;
  154. }
  155. SetScrollPos(hCtrl, SB_CTL, iColor[iIndex], TRUE);
  156. SetDlgItemInt(hDlg, iCtrlID + 3, iColor[iIndex], FALSE);
  157. DeleteObject(cast(HGDIOBJ)SetClassLong(hwndParent, GCL_HBRBACKGROUND,
  158. cast(LONG)CreateSolidBrush(RGB(iColor[0], iColor[1], iColor[2]))));
  159. InvalidateRect(hwndParent, NULL, TRUE);
  160. return TRUE;
  161. default:
  162. }
  163. return FALSE;
  164. }