MFC - 显示模态对话框时的暗淡主窗口

Ash*_*ain 5 c++ user-interface mfc modal-dialog visual-studio-2010

我有一个相当标准的MFC应用程序,它包含一个主窗口,偶尔会显示模态对话框.众所周知,在关闭模式对话框之前无法完成任何操作.

因此,一个不错的UI功能是"调暗"对话框后面的主窗口的其余部分,直观地表明在完成模态对话框之前不能使用它.一些Web应用程序和java/mac应用程序执行此操作,但我从未在传统的C++/MFC应用程序中看到它.我想尝试一下,即使这个平台很不寻常.

如何才能做到这一点?我在应用程序中有几个模态对话框,在此模式中使用:

// pMainFrame is available as a pointer to the CWnd of the main window
CMyDialog dialog;
dialog.DoModal(); // invoke modal dialog; returns after dialog closed
Run Code Online (Sandbox Code Playgroud)

是否有一种简单的方法可以在任何DoModal()之前使窗口变暗并在之后恢复?我正在使用Visual Studio 2010,以防更新的MFC具有任何可能有用的功能.

编辑:我已经发布了一个基于oystein答案的解决方案,但是我开始获得赏金以防万一有人可以改进它 - 尤其是顺利淡入/淡出.

Oys*_*ein 14

您可以在要调暗的窗口顶部创建另一个完全黑色的窗口,并使用SetLayeredWindowAttributes设置黑色窗口的不透明度.当然,它不一定是黑色,但我猜这是最好的调光颜色.

编辑:我一起破解了一个例子 - 但请注意我不是MFC开发人员,我通常直接使用Windows API.但它似乎工作正常. 是一个pastebin.随意添加淡入等自己.另请注意,这会使整个屏幕变暗,如果您不想要这种行为,则必须调整调光窗口的大小.查看代码注释.

/**********************************************************************************************

    MFC screen dim test
        :: oystein          :: November 2010

    Creates a simple window - click it to toggle whether a translucent black "dimmer" window 
    is shown. The dimmer-window covers the entire screen, but the taskbar ("superbar" in 
    Windows 7) will jump on top of it if clicked - it seems. Simple suggestions to fix that
    are welcome.

    Should work on Windows 2000 and later. 

    Disclaimer: This is my first MFC program ever, so if anything seems wrong, it probably is.
    I have previously only coded with pure Win32 API, and hacked this together using online
    tutorials. Code provided "as-is" with no guarantees - I can not be held responsible for 
    anything bad that happens if you run this program.

***********************************************************************************************/

#include "stdafx.h"

#undef WINVER
#define WINVER 0x500 // Windows 2000 & above, because of layered windows


// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
//
//                       Black window used to dim everything else 
//
class CDimWnd : public CFrameWnd
{               
public: 
    CDimWnd()
    {
        // Get screen res into rect
        RECT rc;
        GetDesktopWindow()->GetWindowRect(&rc);

        CreateEx(WS_EX_LAYERED |        // Layered window for translucency
                 WS_EX_TRANSPARENT |    // Click through
                 WS_EX_TOPMOST |        // Always on top
                 WS_EX_TOOLWINDOW,      // Do not appear in taskbar & similar
                 NULL, TEXT(""), 
                 WS_POPUP,              // No frame/borders - though there is 
                                        // still some border left - we'll remove 
                                        // it with regions

                 0, 0, rc.right + 10, rc.bottom + 10, // Make the window 10px larger 
                                                      // than screen resolution in both 
                                                      // directions - it is still positioned 
                                                      // at 0,0
                 NULL, NULL);

        // Grab a part of the window the size of the desktop - but 5px into it  
        // Because the window is larger than the desktop res, the borders are removed 
        CRgn rgn;                         
        rgn.CreateRectRgn(rc.left + 5, rc.top + 5, rc.right + 5, rc.bottom + 5);
        SetWindowRgn((HRGN)rgn, FALSE);
        rgn.Detach();                               

        // We have to reposition window - (0,0) of window has not changed
        SetWindowPos(NULL, -5, -5, 0, 0, SWP_NOSIZE | SWP_NOZORDER);        

        // This is where we set the opacity of the window: 0-255
        SetLayeredWindowAttributes(RGB(0,0,0), 150, LWA_ALPHA);                     
    }
    void Close()
    {
        CFrameWnd::OnClose();
    }
    BOOL CDimWnd::OnEraseBkgnd(CDC* pDC); // Set BKG color
    DECLARE_MESSAGE_MAP()
};

BOOL CDimWnd::OnEraseBkgnd(CDC* pDC)
{
    // Set brush to desired background color
    CBrush backBrush(RGB(0, 0, 0));

    // Save old brush
    CBrush* pOldBrush = pDC->SelectObject(&backBrush);

    CRect rect;
    pDC->GetClipBox(&rect);     // Erase the area needed

    pDC->PatBlt(rect.left, rect.top, rect.Width(), rect.Height(), PATCOPY);
    pDC->SelectObject(pOldBrush);   
    return TRUE;
}

BEGIN_MESSAGE_MAP(CDimWnd, CFrameWnd)
    ON_WM_ERASEBKGND()
END_MESSAGE_MAP()

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -


// Global variable - is screen dimmed?
bool g_bIsDimmed = false;


// The main window
class CMainWnd : public CFrameWnd
{     
    // Contains a CDimWnd - I'm not sure if this is the "MFC way" of doing things
    CDimWnd dimmer; 

public: 
    CMainWnd()
    {
        Create(NULL, TEXT("Screen dimmer - Press left mouse button on window to toggle"), 
            WS_OVERLAPPEDWINDOW, CRect(50, 50, 400, 250));
    }
    // Left mouse button toggles dimming
    afx_msg void OnLButtonDown(UINT Flags, CPoint Point)
    {
        if(!g_bIsDimmed)
        {
            dimmer.ShowWindow(SW_SHOW);
            dimmer.BringWindowToTop();          
            g_bIsDimmed = true;
        }
        else
        {           
            dimmer.ShowWindow(SW_HIDE);     
            g_bIsDimmed = false;
        }
    }
    DECLARE_MESSAGE_MAP()
};

BEGIN_MESSAGE_MAP(CMainWnd, CFrameWnd)
    ON_WM_LBUTTONDOWN()
END_MESSAGE_MAP()


// The app
class CApp : public CWinApp
{
public:         
    virtual BOOL InitInstance();
};

BOOL CApp::InitInstance()
{               
    m_pMainWnd = new CMainWnd();              
    m_pMainWnd->ShowWindow(m_nCmdShow);           
    m_pMainWnd->UpdateWindow();        
    return TRUE;
}

CApp HelloApp;
Run Code Online (Sandbox Code Playgroud)

更新:

我为你处理了一些代码,以处理褪色.我仍然没有MFC开发,我将代码保持在"粗略"状态(小错误处理,不是非常强大)给你一些事情.:)无论如何,这是一种方法,我觉得相当干净:

要使用它,请使主窗口包含调光窗口

class CMainFrm : public CFrameWnd
{     
    CDimWnd* dimmer; 

public: 
    CMainFrm()
    {
        // constructor code here ...
        dimmer = new CDimWnd();         
    }

// rest of class ...

};  
Run Code Online (Sandbox Code Playgroud)

然后可以使用它,例如:

dimmer->Show();
MessageBox(TEXT("Hello world"));
dimmer->Hide();
Run Code Online (Sandbox Code Playgroud)

或者我猜你可以将这些代码(Show()/ Hide()calls)放在模态对话框的构造函数和析构函数中,如果你想保留代码那么.如果你想要一个"范围"-dim,就像你发布的例子一样,这段代码必须放在CDimWnd类的构造函数和析构函数中,你需要像静态成员变量一样来确保只有一个调光器是一次运行(除非你想使用全局变量).

对于调光窗口 - 我这样做了:

CDimWnd.h

#define TARGET_OPACITY 70   // Target opacity 0-255 for dimmed window
#define FADE_TIME 20        // Time between each fade step in milliseconds
#define FADE_STEP 5      // How much to add to/remove from opacity each fade step
#define ID_FADE_TIMER 1

// Call Show() and Hide() to fade in/fade out dimmer. 
// Creates the dimmer window in constructor.
class CDimWnd : public CFrameWnd
{         
    bool m_isDimming;       

public: 
    CDimWnd();
    void Show();
    void Hide();            

protected:
    BOOL OnEraseBkgnd(CDC* pDC);
    void OnTimer(UINT_PTR nIDEvent);
    DECLARE_MESSAGE_MAP()
};
Run Code Online (Sandbox Code Playgroud)

CDimWnd.cpp

#include "stdafx.h"
#include "CDimWnd.h"
#include "MainFrm.h"

BEGIN_MESSAGE_MAP(CDimWnd, CFrameWnd)
    ON_WM_ERASEBKGND()
END_MESSAGE_MAP()

CDimWnd::CDimWnd()
{
    // Get the main frame of the application which we want to dim.
    CMainFrame* pParent = theApp.pMainFrame;

    // Don't do anything if the main frame doesn't appear to be there
    if (pParent != NULL)
    {
        // Get the client area of the window to dim.
        CRect rc;
        pParent->GetClientRect(&rc);
        pParent->ClientToScreen(&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->GetSafeHwnd(), NULL);
    }
}


void CDimWnd::Show()
{
    // If we are not already dimming, go for it
    if(!m_isDimming)
    {
        // Bring in front of main window.
        BringWindowToTop();

        // Set opacity to 0
        SetLayeredWindowAttributes(RGB(0,0,0), 0, LWA_ALPHA);

        // Show the dimmer window
        ShowWindow(SW_SHOW);

        // Create timer - the rest is handled in OnTimer() function
        SetTimer(ID_FADE_TIMER, FADE_TIME, NULL);
    }
}


void CDimWnd::Hide()
{   
    // If we are dimming, go for it
    if(m_isDimming)
    {
        // Create timer - the rest is handled in OnTimer() function
        SetTimer(ID_FADE_TIMER, FADE_TIME, NULL);
    }
}


void CDimWnd::OnTimer(UINT_PTR nIDEvent)
{
    static int fade = 0;

    if(nIDEvent == ID_FADE_TIMER)
    {
        // We are dimming => we want to fade out
        if(m_isDimming)
        {
            if(fade < 0)
            {
                // Fading finished - hide window completely, update status & destroy timer
                fade = 0;
                ShowWindow(SW_HIDE);
                KillTimer(nIDEvent);
                m_isDimming = false;
            }
            else
            {
                // Set window opacity & update fade counter
                SetLayeredWindowAttributes(RGB(0,0,0), fade, LWA_ALPHA);
                fade -= FADE_STEP;
            }
        }
        else
        // fade in
        {
            if(fade > TARGET_OPACITY)
            {   
                // Fading finished - destroy timer & update status

                fade = TARGET_OPACITY; // but first, let's be accurate.
                SetLayeredWindowAttributes(RGB(0,0,0), fade, LWA_ALPHA);

                KillTimer(nIDEvent);
                m_isDimming = true;             
            }   
            else
            {
                // Set window opacity & update fade counter
                SetLayeredWindowAttributes(RGB(0,0,0), fade, LWA_ALPHA);
                fade += FADE_STEP;
            }
        }
    }
}


BOOL CDimWnd::OnEraseBkgnd(CDC* pDC)
{
    // Fill with black
    CBrush backBrush(RGB(0, 0, 0));
    CBrush* pOldBrush = pDC->SelectObject(&backBrush);

    CRect rect;
    pDC->GetClipBox(&rect);     // Erase the area needed
    pDC->PatBlt(rect.left, rect.top, rect.Width(), rect.Height(), PATCOPY);

    pDC->SelectObject(pOldBrush);   
    return TRUE;
}
Run Code Online (Sandbox Code Playgroud)

好的.正如我所说的那样,它很快就被抛到了一起并处于一个粗糙的状态,但它应该为你提供一些代码,以及我在MFC中如何(我认为)使用定时器的一般概念.我绝对不是一个对此有所思考的人,尽管:)