PageRenderTime 47ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/Samples/Chap08/WhatClr/WhatClr.d

http://github.com/AndrejMitrovic/DWinProgramming
D | 174 lines | 132 code | 38 blank | 4 comment | 5 complexity | f215cb868808d490a9a9369604c21f7e MD5 | raw file
  1. /+
  2. + Copyright (c) Charles Petzold, 1998.
  3. + Ported to the D Programming Language by Andrej Mitrovic, 2011.
  4. +/
  5. module WhatClr;
  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. enum ID_TIMER = 1;
  23. extern (Windows)
  24. int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int iCmdShow)
  25. {
  26. int result;
  27. try
  28. {
  29. Runtime.initialize();
  30. result = myWinMain(hInstance, hPrevInstance, lpCmdLine, iCmdShow);
  31. Runtime.terminate();
  32. }
  33. catch (Throwable o)
  34. {
  35. MessageBox(null, o.toString().toUTF16z, "Error", MB_OK | MB_ICONEXCLAMATION);
  36. result = 0;
  37. }
  38. return result;
  39. }
  40. int myWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int iCmdShow)
  41. {
  42. string appName = "WhatClr";
  43. HWND hwnd;
  44. MSG msg;
  45. WNDCLASS wndclass;
  46. wndclass.style = CS_HREDRAW | CS_VREDRAW;
  47. wndclass.lpfnWndProc = &WndProc;
  48. wndclass.cbClsExtra = 0;
  49. wndclass.cbWndExtra = 0;
  50. wndclass.hInstance = hInstance;
  51. wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  52. wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
  53. wndclass.hbrBackground = cast(HBRUSH)GetStockObject(WHITE_BRUSH);
  54. wndclass.lpszMenuName = NULL;
  55. wndclass.lpszClassName = appName.toUTF16z;
  56. if (!RegisterClass(&wndclass))
  57. {
  58. MessageBox(NULL, "This program requires Windows NT!", appName.toUTF16z, MB_ICONERROR);
  59. return 0;
  60. }
  61. int cxWindow, cyWindow;
  62. FindWindowSize(cxWindow, cyWindow);
  63. hwnd = CreateWindow(appName.toUTF16z, // window class name
  64. "What Color", // window caption
  65. WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_BORDER, // window style
  66. CW_USEDEFAULT, // initial x position
  67. CW_USEDEFAULT, // initial y position
  68. cxWindow, // initial x size
  69. cyWindow, // 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. void FindWindowSize(ref int pcxWindow, ref int pcyWindow)
  84. {
  85. HDC hdcScreen;
  86. TEXTMETRIC tm;
  87. hdcScreen = CreateIC("DISPLAY", NULL, NULL, NULL);
  88. GetTextMetrics(hdcScreen, &tm);
  89. DeleteDC(hdcScreen);
  90. pcxWindow = 2 * GetSystemMetrics(SM_CXBORDER) + 12 * tm.tmAveCharWidth;
  91. pcyWindow = 2 * GetSystemMetrics(SM_CYBORDER) + GetSystemMetrics(SM_CYCAPTION) +
  92. 2 * tm.tmHeight;
  93. }
  94. extern (Windows)
  95. LRESULT WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) nothrow
  96. {
  97. scope (failure) assert(0);
  98. static COLORREF cr;
  99. static COLORREF crLast;
  100. static HDC hdcScreen;
  101. HDC hdc;
  102. PAINTSTRUCT ps;
  103. POINT pt;
  104. RECT rc;
  105. string szBuffer;
  106. switch (message)
  107. {
  108. case WM_CREATE:
  109. hdcScreen = CreateDC("DISPLAY", NULL, NULL, NULL);
  110. SetTimer(hwnd, ID_TIMER, 100, NULL);
  111. return 0;
  112. case WM_TIMER:
  113. GetCursorPos(&pt);
  114. cr = GetPixel(hdcScreen, pt.x, pt.y);
  115. if (cr != crLast)
  116. {
  117. crLast = cr;
  118. InvalidateRect(hwnd, NULL, FALSE);
  119. }
  120. return 0;
  121. case WM_PAINT:
  122. hdc = BeginPaint(hwnd, &ps);
  123. GetClientRect(hwnd, &rc);
  124. szBuffer = format(" %02X %02X %02X ", GetRValue(cr), GetGValue(cr), GetBValue(cr));
  125. DrawText(hdc, szBuffer.toUTF16z, -1, &rc, DT_SINGLELINE | DT_CENTER | DT_VCENTER);
  126. EndPaint(hwnd, &ps);
  127. return 0;
  128. case WM_DESTROY:
  129. DeleteDC(hdcScreen);
  130. KillTimer(hwnd, ID_TIMER);
  131. PostQuitMessage(0);
  132. return 0;
  133. default:
  134. }
  135. return DefWindowProc(hwnd, message, wParam, lParam);
  136. }