【问题标题】:MFC - dim main window when showing modal dialogMFC - 显示模式对话框时使主窗口变暗
【发布时间】:2011-05-03 02:08:18
【问题描述】:

我有一个相当标准的 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

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

编辑:我已经根据 oystein 的回答发布了一个解决方案,但我要开始赏金,以防有人可以改进它 - 特别是平滑淡入/淡出。

【问题讨论】:

  • 首先,什么变暗了?你能提供一张想要的行为的截图吗?
  • 在 Google 图片上找到了这个,这是一个网页,它使对话框周围的其余页面变暗:irritatedvowel.com/pub/blog/… - 就像这样,但在对话框启动时使应用程序的主窗口变暗。

标签: c++ visual-studio-2010 user-interface mfc modal-dialog


【解决方案1】:

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

编辑:我编写了一个示例 - 但请注意,我不是 MFC 开发人员,我通常直接使用 Windows API。不过,它似乎工作正常。 Here 是一个粘贴箱。随意添加淡入等。另请注意,这会使整个屏幕变暗,如果您不想要这种行为,则必须调整我的调光窗口的大小。见代码 cmets。

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

    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;

更新:

我为您编写了更多代码来处理褪色问题。我仍然不是 MFC 开发人员,我将代码置于“粗略”状态(很少有错误处理,不是很健壮),以便您也可以做一些事情。 :) 无论如何,这是一种方法,我认为它相当干净:

要使用它,让你的主窗口包含一个调光窗口

class CMainFrm : public CFrameWnd
{     
    CDimWnd* dimmer; 

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

// rest of class ...

};  

然后可以使用它,例如像这样:

dimmer->Show();
MessageBox(TEXT("Hello world"));
dimmer->Hide();

或者,如果您想将代码保留在那里,我想您可以将此代码(Show()/Hide() 调用)放在模态对话框的构造函数和析构函数中。如果您想要一个“范围”-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()
};

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;
}

好的。正如我所说,这很快就被拼凑在一起并且处于粗略的状态,但它应该为您提供一些可以使用的代码,以及(我认为)如何在 MFC 中使用计时器的一般概念。不过,我绝对不是考虑这个问题的合适人选:)

【讨论】:

  • 我通常直接使用 Windows API - 它显示 ;) 在 MFC 中,窗口创建不进入构造函数而是单独的。无论如何,+1 jsut 咳出一个样本。
  • 对于这种特定情况,将初始化放在构造函数中实际上很有用 - 然后你有一个“作用域窗口调光器”,只需要声明 CDimWnd 的单行就可以在作用域上变暗。
【解决方案2】:

我接受了 oystein 的回答,因为它引导我找到了解决方案,但我想我会发布我的修改。我必须对其进行一些修改以使其适合我,所以它可能对其他人有用。

作为记录,调光效果很好,但看起来并不像我希望的那样自然。在一个经常弹出对话框的应用程序中,调光会因为它看似打开和关闭主窗口的规律性而变得分散注意力。为了妥协,我将调光做得相当微妙(大约 25% 的不透明度),它轻轻地突出了活动对话;瞬间变暗仍然有点让人分心,但我不知道如何让它平滑地淡入或淡出,尤其是在作用域时。

另外,我不是 UI 专家,但调暗给我一种印象,即对话框与其后面的窗口内容的相关性较低。这让它感觉有点脱离了我在应用程序中所做的工作,即使对话框直接操作该内容。这可能是另一种干扰。

反正就是这样:

CDimWnd.h

// Dim the application main window over a scope.  Creates dimmer window in constructor.
class CDimWnd : public CFrameWnd
{               
public: 
    CDimWnd();
    BOOL OnEraseBkgnd(CDC* pDC);

    ~CDimWnd();

protected:
    DECLARE_MESSAGE_MAP()
};

CDimWnd.cpp

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

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

// For preventing two dimmer windows ever appearing
bool is_dimmer_active = false;

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,
    // or if there is already dimming happening.
    if (pParent != NULL && !is_dimmer_active)
    {
        // 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);

        // Bring in front of main window.
        BringWindowToTop();

        // Apply 25% opacity
        SetLayeredWindowAttributes(RGB(0,0,0), 64, LWA_ALPHA);

        // Show the dimmer window
        ShowWindow(SW_SHOW);

        is_dimmer_active = true;
    }
}

CDimWnd::~CDimWnd()
{
    is_dimmer_active = false;
}

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;
}

用法非常简单:因为 CDimWnd 在其 构造函数 中创建自己,您只需将CDimWnd dimmer 添加为对话框类的成员,它会自动使主窗口变暗,不无论您从哪里调用对话框。

您还可以在范围内使用它来使系统模式对话框变暗:

{
    CDimWnd dimmer;
    MessageBox(...);
}

【讨论】:

  • 对于淡入淡出,您可以在构造函数/析构函数中制作一个计时器,逐渐增加/减少不透明度。除非您想要一个单独的线程来执行此操作,否则淡入淡出必须非常快。
  • 就是这样,淡入淡出不应该延迟对话框的出现。是否有可能在模态对话框出现的同时出现它的 Aero 淡入过渡?如果模式对话框正在运行,计时器还会运行吗?
  • 放弃最后的评论。如果您注册一个 Windows 计时器,您将收到一个滴答事件。如果您在事件处理程序中进行淡入淡出,它将独立于其他对话框而淡出,并且不会延迟任何内容的执行。
  • 关于计时器的更多信息:herehereherehere
  • 我用一个例子更新了我的答案,以防你觉得有用
【解决方案3】:

我忍不住这么做了。

这是您的代码,添加了计时器并实现了淡入/淡出。此外,我更改为使用中灰色而不是黑色作为遮蔽块。

您可以通过增加持续时间或增加速率来调整控制淡入淡出的常量以使其更平滑。实验表明10hz的速率对我来说是平滑的,但是YMMV

// 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();
};

// DimWnd.cpp : implementation file
//

#include "stdafx.h"
#include "dimmer.h"
#include "DimWnd.h"
#include "MainFrm.h"
#include <math.h>

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->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 && !is_dimmer_active)
    {
        // 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);

        // 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  > 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->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()
    ON_WM_TIMER()
END_MESSAGE_MAP()

void CDimWnd::OnTimer(UINT_PTR nIDEvent)
{
    if (opacity >= 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);
}

【讨论】:

  • 似乎是一种干净的方法,只要您只需要范围模型,并且不需要长时间的淡出。它肯定比我的版本简单:)
猜你喜欢
  • 2017-06-07
  • 1970-01-01
  • 2012-04-16
  • 2023-04-06
  • 1970-01-01
  • 2010-12-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多