Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I couldn't resist doing it. </p> <p>It's your code with added timers and implemented a fade in / fade out. Also I changed to use mid grey rather than black for the obscuring block. </p> <p>You can tweak the constants that control the fade to make it smoother by increasing the duration or the increasing the rate. Experiment shows me that a rate of 10hz is smooth for me, but YMMV</p> <pre><code>// DimWnd.h : header file #pragma once class CDimWnd : public CFrameWnd { public: CDimWnd(class CWnd * pParent); virtual ~CDimWnd(); BOOL OnEraseBkgnd(CDC* pDC); int opacity, opacity_increment; protected: DECLARE_MESSAGE_MAP() public: afx_msg void OnTimer(UINT_PTR nIDEvent); void fadeOut(); }; </code></pre> <hr> <pre><code>// DimWnd.cpp : implementation file // #include "stdafx.h" #include "dimmer.h" #include "DimWnd.h" #include "MainFrm.h" #include &lt;math.h&gt; const int TIMER_ID = 111; // For preventing two dimmer windows ever appearing bool is_dimmer_active = false; // constants to control the fade. int ticks_per_second = 1000; // ms int start_opacity = 44; // 20% int max_opacity = 220; // 0-&gt;255 double fade_in_duration = 4; // seconds to fade in (appear) double fade_out_duration = 0.2; // seconds to fade out (disappear) int rate = 100; // Timer rate (ms CDimWnd::CDimWnd(CWnd * pParent) { // Don't do anything if the main frame doesn't appear to be there, // or if there is already dimming happening. if (pParent != NULL &amp;&amp; !is_dimmer_active) { // Get the client area of the window to dim. CRect rc; pParent-&gt;GetClientRect(&amp;rc); pParent-&gt;ClientToScreen(&amp;rc); // convert to screen coordinates // Do some fudging to fit the client area exactly. // Other applications may not need this if the above client area fits already. rc.top += GetSystemMetrics(SM_CYFRAME); rc.top += GetSystemMetrics(SM_CYCAPTION); // MFC feature pack seems to include caption in client area rc.left -= GetSystemMetrics(SM_CXBORDER); rc.right += GetSystemMetrics(SM_CXBORDER) + 1; rc.bottom += GetSystemMetrics(SM_CYBORDER) + 1; // Create a layered window for transparency, with no caption/border. CreateEx(WS_EX_LAYERED | WS_EX_TRANSPARENT | WS_EX_TOOLWINDOW, NULL, TEXT(""), WS_POPUP, rc.left, rc.top, rc.Width(), rc.Height(), pParent-&gt;GetSafeHwnd(), NULL); // Bring in front of main window. BringWindowToTop(); // Show the dimmer window ShowWindow(SW_SHOW); double increment_per_second = ((max_opacity - start_opacity) / fade_in_duration); opacity_increment = ceil( increment_per_second / (ticks_per_second / rate) ) ; is_dimmer_active = true; opacity = start_opacity; SetLayeredWindowAttributes(RGB(0,0,0), opacity, LWA_ALPHA); SetTimer(TIMER_ID, rate,NULL); } } CDimWnd::~CDimWnd() { fadeOut(); // fade the window out rather than just disappearing. is_dimmer_active = false; } void CDimWnd::fadeOut() { // can't use timers as may be in the process of being destroyed so make it quick.. double increment_per_second = ((opacity - start_opacity) / fade_out_duration); opacity_increment = ceil( increment_per_second / (ticks_per_second / rate) ) ; while(opacity &gt; start_opacity) { opacity -= opacity_increment; SetLayeredWindowAttributes(RGB(0,0,0), opacity, LWA_ALPHA); Sleep(100); } } BOOL CDimWnd::OnEraseBkgnd(CDC* pDC) { // Fill with midgray CBrush backBrush(RGB(128,128,128)); CBrush* pOldBrush = pDC-&gt;SelectObject(&amp;backBrush); CRect rect; pDC-&gt;GetClipBox(&amp;rect); // Erase the area needed pDC-&gt;PatBlt(rect.left, rect.top, rect.Width(), rect.Height(), PATCOPY); pDC-&gt;SelectObject(pOldBrush); return TRUE; } BEGIN_MESSAGE_MAP(CDimWnd, CFrameWnd) ON_WM_ERASEBKGND() ON_WM_TIMER() END_MESSAGE_MAP() void CDimWnd::OnTimer(UINT_PTR nIDEvent) { if (opacity &gt;= max_opacity) { // stop the timer when fade in finished. KillTimer(TIMER_ID); return; } opacity += opacity_increment; SetLayeredWindowAttributes(RGB(0,0,0), opacity, LWA_ALPHA); CFrameWnd::OnTimer(nIDEvent); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload