ReactOS  0.4.14-dev-98-gb0d4763
dimmedwindow.cpp
Go to the documentation of this file.
1 /*
2  * COPYRIGHT: See COPYING in the top level directory
3  * PROJECT: ReactOS msgina.dll
4  * FILE: dll/win32/msgina/dimmedwindow.cpp
5  * PURPOSE: Implementation of ShellDimScreen
6  * PROGRAMMER: Mark Jansen
7  */
8 
9 #define COM_NO_WINDOWS_H
10 #include "msgina.h"
11 #include <wingdi.h>
12 #include <atlbase.h>
13 #include <atlcom.h>
14 #include <pseh/pseh2.h>
15 
16 CComModule gModule;
17 
18 // Please note: The INIT_TIMER is a workaround because ReactOS does not redraw the desktop in time,
19 // so the start menu is still visible on the dimmed screen.
20 #define INIT_TIMER_ID 0x112233
21 #define FADE_TIMER_ID 0x12345
22 
24  public CComObjectRootEx<CComMultiThreadModelNoCS>,
25  IUnknown
26 {
27 private:
36  int m_step;
37 
39 
40 public:
42  : m_hwnd(NULL)
43  , m_hdc(NULL)
44  , m_hbitmap(NULL)
45  , m_oldbitmap(NULL)
46  , m_width(0)
47  , m_height(0)
48  , m_bytes(NULL)
49  , m_step(0)
50  {
51  WNDCLASSEXW wndclass = {sizeof(wndclass)};
52  wndclass.lpfnWndProc = WndProc;
53  wndclass.hInstance = hDllInstance;
54  wndclass.hCursor = LoadCursor(0, IDC_ARROW);
55  wndclass.lpszClassName = L"DimmedWindowClass";
56 
57  if (!RegisterClassExW(&wndclass))
58  return;
59 
62 
63  memset(&m_bi, 0, sizeof(m_bi));
64  m_bi.bmiHeader.biSize = sizeof(m_bi);
71  m_bytes = new UCHAR[m_width * 4 * m_height];
72 
75 
77  L"DimmedWindowClass",
78  NULL,
79  WS_POPUP,
80  x, y,
82  NULL, NULL,
84  (LPVOID)this);
85  }
86 
88  {
89  if (m_hwnd)
91  UnregisterClassW(L"DimmedWindowClass", hDllInstance);
92  if (m_oldbitmap)
94  if (m_hbitmap)
96  if (m_hdc)
98  if (m_bytes)
99  delete[] m_bytes;
100  }
101 
102  // This is needed so that we do not capture the start menu while it's closing.
103  void WaitForInit()
104  {
105  MSG msg;
106 
107  while (IsWindow(m_hwnd) && !IsWindowVisible(m_hwnd))
108  {
109  while (::PeekMessage(&msg, m_hwnd, 0, 0, PM_REMOVE))
110  {
113 
114  if (IsWindowVisible(m_hwnd))
115  break;
116  }
117  }
118  }
119 
120  void Init()
121  {
122  Capture();
123 
127 
129  }
130 
131  void Capture()
132  {
133  HWND desktopWnd = GetDesktopWindow();
134  HDC desktopDC = GetDC(desktopWnd);
135 
136  m_hdc = CreateCompatibleDC(desktopDC);
137 
140  BitBlt(m_hdc, 0, 0, m_width, m_height, desktopDC, 0, 0, SRCCOPY);
141 
142  ReleaseDC(desktopWnd, desktopDC);
143  }
144 
145  bool Step()
146  {
147  // Stop after 10 steps
148  if (m_step++ > 10 || !m_bytes)
149  return false;
150 
152  if (lines)
153  {
154  for (int xh = 0; xh < m_height; ++xh)
155  {
156  int h = m_width * 4 * xh;
157  for (int w = 0; w < m_width; ++w)
158  {
159  UCHAR b = m_bytes[(h + w * 4) + 0];
160  UCHAR g = m_bytes[(h + w * 4) + 1];
161  UCHAR r = m_bytes[(h + w * 4) + 2];
162 
163  // Standard formula to convert a color.
164  int gray = (r * 30 + g * 59 + b * 11) / 100;
165  if (gray < 0)
166  gray = 0;
167 
168  // Do not fade too fast.
169  r = (r*2 + gray) / 3;
170  g = (g*2 + gray) / 3;
171  b = (b*2 + gray) / 3;
172 
173  m_bytes[(h + w * 4) + 0] = b;
174  m_bytes[(h + w * 4) + 1] = g;
175  m_bytes[(h + w * 4) + 2] = r;
176  }
177  }
179  }
180  return true;
181  }
182 
183  void Blt(HDC hdc)
184  {
185  BitBlt(hdc, 0, 0, m_width, m_height, m_hdc, 0, 0, SRCCOPY);
186  }
187 
189  {
190  return m_hwnd;
191  }
192 
193 
196  END_COM_MAP()
197 
198 };
199 
200 
202 {
203  switch (uMsg)
204  {
205  case WM_NCCREATE:
206  {
207  LPCREATESTRUCT lpcs = reinterpret_cast<LPCREATESTRUCT>(lParam);
208  CDimmedWindow* info = static_cast<CDimmedWindow*>(lpcs->lpCreateParams);
211  break;
212  }
213 
214  case WM_PAINT:
215  {
216  CDimmedWindow* info = reinterpret_cast<CDimmedWindow*>(GetWindowLongPtrW(hWnd, GWLP_USERDATA));
217  if (info)
218  {
219  PAINTSTRUCT ps;
220  BeginPaint(hWnd, &ps);
221  info->Blt(ps.hdc);
222  EndPaint(hWnd, &ps);
223  }
224  return 0;
225  }
226 
227  case WM_TIMER:
228  {
229  if (wParam == INIT_TIMER_ID)
230  {
231  CDimmedWindow* info = reinterpret_cast<CDimmedWindow*>(GetWindowLongPtrW(hWnd, GWLP_USERDATA));
233  info->Init();
234  }
235  else if (wParam == FADE_TIMER_ID)
236  {
237  CDimmedWindow* info = reinterpret_cast<CDimmedWindow*>(GetWindowLongPtrW(hWnd, GWLP_USERDATA));
238  if (info && info->Step())
240  else
242  }
243  return 0;
244  }
245 
246  default:
247  break;
248  }
249 
250  return DefWindowProc(hWnd, uMsg, wParam, lParam);
251 }
252 
253 
254 extern "C"
256 ShellDimScreen(void** pUnknown, HWND* hWindow)
257 {
258  CComObject<CDimmedWindow> *pWindow;
259  HRESULT hr = CComObject<CDimmedWindow>::CreateInstance(&pWindow);
260  ULONG refcount;
261 
262  pWindow->WaitForInit();
263 
264  if (!IsWindow(pWindow->Wnd()))
265  {
266  refcount = pWindow->AddRef();
267  while (refcount)
268  refcount = pWindow->Release();
269 
270  return E_FAIL;
271  }
272 
273  _SEH2_TRY
274  {
275  hr = pWindow->QueryInterface(IID_IUnknown, pUnknown);
276  *hWindow = pWindow->Wnd();
277  hr = S_OK;
278  }
280  {
281  hr = E_INVALIDARG;
282  refcount = pWindow->AddRef();
283  while (refcount)
284  refcount = pWindow->Release();
285  }
286  _SEH2_END
287 
288  return hr;
289 }
#define DispatchMessage
Definition: winuser.h:5666
BOOL WINAPI TranslateMessage(_In_ const MSG *)
#define PeekMessage
Definition: winuser.h:5731
#define TRUE
Definition: types.h:120
GLubyte GLubyte GLubyte GLubyte w
Definition: glext.h:6102
BITMAPINFOHEADER bmiHeader
Definition: wingdi.h:1471
#define SM_XVIRTUALSCREEN
Definition: winuser.h:1026
HRESULT hr
Definition: shlfolder.c:183
BOOL WINAPI UnregisterClassW(_In_ LPCWSTR, HINSTANCE)
BOOL WINAPI IsWindow(_In_opt_ HWND)
#define SM_CXVIRTUALSCREEN
Definition: winuser.h:1028
HDC WINAPI GetDC(_In_opt_ HWND)
HBITMAP WINAPI CreateCompatibleBitmap(_In_ HDC hdc, _In_ INT cx, _In_ INT cy)
GLdouble GLdouble GLdouble r
Definition: gl.h:2055
void Blt(HDC hdc)
TW_UINT32 TW_UINT16 TW_UINT16 MSG
Definition: twain.h:1827
static HDC
Definition: imagelist.c:92
HWND hWnd
Definition: settings.c:17
HGDIOBJ WINAPI SelectObject(_In_ HDC, _In_ HGDIOBJ)
Definition: dc.c:1497
BOOL WINAPI DeleteObject(_In_ HGDIOBJ)
GLint GLint GLint GLint GLint x
Definition: gl.h:1548
HDC WINAPI CreateCompatibleDC(_In_opt_ HDC hdc)
UINT_PTR WPARAM
Definition: windef.h:207
CComModule gModule
#define GetWindowLongPtrW
Definition: winuser.h:4730
HGDIOBJ m_oldbitmap
BOOL WINAPI ShowWindow(_In_ HWND, _In_ int)
BOOL WINAPI EndPaint(_In_ HWND, _In_ const PAINTSTRUCT *)
#define E_FAIL
Definition: ddrawi.h:102
#define WM_NCCREATE
Definition: winuser.h:1665
BOOL WINAPI DestroyWindow(_In_ HWND)
static VOID NTAPI BitBlt(IN ULONG Left, IN ULONG Top, IN ULONG Width, IN ULONG Height, IN PUCHAR Buffer, IN ULONG BitsPerPixel, IN ULONG Delta)
Definition: vga.c:410
WPARAM wParam
Definition: combotst.c:138
GLfloat GLfloat GLfloat GLfloat h
Definition: glext.h:7723
_SEH2_TRY
Definition: create.c:4250
ATOM WINAPI RegisterClassExW(_In_ CONST WNDCLASSEXW *)
long LONG
Definition: pedump.c:60
HDC WINAPI BeginPaint(_In_ HWND, _Out_ LPPAINTSTRUCT)
int WINAPI ReleaseDC(_In_opt_ HWND, _In_ HDC)
DWORD biCompression
Definition: amvideo.idl:35
#define GWLP_USERDATA
Definition: treelist.c:63
#define EXCEPTION_EXECUTE_HANDLER
Definition: excpt.h:85
#define E_INVALIDARG
Definition: ddrawi.h:101
smooth NULL
Definition: ftsmooth.c:416
HWND WINAPI GetDesktopWindow(void)
Definition: window.c:652
static LRESULT WINAPI WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
LONG_PTR LPARAM
Definition: windef.h:208
#define SW_SHOW
Definition: winuser.h:769
UINT_PTR WINAPI SetTimer(_In_opt_ HWND, _In_ UINT_PTR, _In_ UINT, _In_opt_ TIMERPROC)
LPCWSTR lpszClassName
Definition: winuser.h:3200
#define SM_CYVIRTUALSCREEN
Definition: winuser.h:1029
#define DefWindowProc
Definition: ros2win.h:31
static HINSTANCE hDllInstance
Definition: clb.c:30
_In_ PUNKNOWN pUnknown
Definition: drmk.h:76
eMaj lines
Definition: tritemp.h:206
#define BEGIN_COM_MAP(x)
Definition: atlcom.h:541
GLboolean GLboolean g
Definition: glext.h:6204
GLboolean GLboolean GLboolean b
Definition: glext.h:6204
HRESULT WINAPI ShellDimScreen(void **pUnknown, HWND *hWindow)
LONG HRESULT
Definition: typedefs.h:77
const GUID IID_IUnknown
#define WM_TIMER
Definition: winuser.h:1724
#define WINAPI
Definition: msvc.h:8
BOOL WINAPI InvalidateRect(_In_opt_ HWND, _In_opt_ LPCRECT, _In_ BOOL)
int WINAPI GetSystemMetrics(_In_ int)
DWORD biSizeImage
Definition: amvideo.idl:36
HINSTANCE hInstance
Definition: winuser.h:3195
#define WM_PAINT
Definition: winuser.h:1602
#define FADE_TIMER_ID
unsigned char UCHAR
Definition: xmlstorage.h:181
BOOL WINAPI KillTimer(_In_opt_ HWND, _In_ UINT_PTR)
static const WCHAR L[]
Definition: oid.c:1250
HWND WINAPI CreateWindowExW(_In_ DWORD dwExStyle, _In_opt_ LPCWSTR lpClassName, _In_opt_ LPCWSTR lpWindowName, _In_ DWORD dwStyle, _In_ int X, _In_ int Y, _In_ int nWidth, _In_ int nHeight, _In_opt_ HWND hWndParent, _In_opt_ HMENU hMenu, _In_opt_ HINSTANCE hInstance, _In_opt_ LPVOID lpParam)
HDC hdc
Definition: main.c:9
#define WS_EX_TOPMOST
Definition: pedump.c:647
HCURSOR hCursor
Definition: winuser.h:3197
HBITMAP m_hbitmap
#define LoadCursor
Definition: winuser.h:5713
WNDPROC lpfnWndProc
Definition: winuser.h:3192
#define SM_YVIRTUALSCREEN
Definition: winuser.h:1027
BOOL WINAPI EnableWindow(_In_ HWND, _In_ BOOL)
#define S_OK
Definition: intsafe.h:59
_SEH2_END
Definition: create.c:4424
BITMAPINFO m_bi
LPVOID lpCreateParams
Definition: winuser.h:2914
#define COM_INTERFACE_ENTRY_IID(iid, x)
Definition: atlcom.h:561
__int3264 LONG_PTR
Definition: mstsclib_h.h:276
#define WS_POPUP
Definition: pedump.c:616
unsigned int UINT
Definition: ndis.h:50
GLint GLint GLint GLint GLint GLint y
Definition: gl.h:1548
#define msg(x)
Definition: auth_time.c:54
#define IDC_ARROW
Definition: winuser.h:682
int WINAPI GetDIBits(_In_ HDC hdc, _In_ HBITMAP hbm, _In_ UINT start, _In_ UINT cLines, _Out_opt_ LPVOID lpvBits, _At_((LPBITMAPINFOHEADER) lpbmi, _Inout_) LPBITMAPINFO lpbmi, _In_ UINT usage)
unsigned int ULONG
Definition: retypes.h:1
BOOL WINAPI IsWindowVisible(_In_ HWND)
BOOL WINAPI SetForegroundWindow(_In_ HWND)
#define SetWindowLongPtrW
Definition: winuser.h:5247
static HBITMAP
Definition: button.c:44
#define _SEH2_EXCEPT(...)
Definition: pseh2_64.h:6
#define PM_REMOVE
Definition: winuser.h:1182
#define END_COM_MAP()
Definition: atlcom.h:552
LONG_PTR LRESULT
Definition: windef.h:209
#define DIB_RGB_COLORS
Definition: wingdi.h:366
#define memset(x, y, z)
Definition: compat.h:39
#define INIT_TIMER_ID
int WINAPI SetDIBits(_In_opt_ HDC, _In_ HBITMAP, _In_ UINT, _In_ UINT, _In_ CONST VOID *, _In_ CONST BITMAPINFO *, _In_ UINT)
#define BI_RGB
Definition: precomp.h:34
LPARAM lParam
Definition: combotst.c:139
#define SRCCOPY
Definition: wingdi.h:332