草庐IT

c++ - MFC - 显示模态对话框时主窗口变暗

coder 2024-02-06 原文

我有一个相当标准的 MFC 应用程序,它包含一个主窗口,偶尔会弹出模式对话框。众所周知,在模式对话框关闭之前,在模式对话框之外什么都做不了。

因此,一个不错的 UI 功能是使对话框后面主窗口的其余部分“变暗”,以直观地指示您在完成模式对话框之前不能使用它。一些网络应用程序和 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 的回答的解决方案,但我开始悬赏以防任何人可以改进它 - 特别是平滑的淡入/淡出。

最佳答案

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

编辑:我编写了一个示例 - 但请注意,我不是 MFC 开发人员,我通常直接使用 Windows API。不过,它似乎工作正常。 Here是一个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;

更新:

我为您编写了更多代码来处理褪色。我仍然不是 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 中使用计时器的一般概念。不过,我绝对不是考虑任何事情的合适人选:)

关于c++ - MFC - 显示模态对话框时主窗口变暗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4063547/

有关c++ - MFC - 显示模态对话框时主窗口变暗的更多相关文章

  1. ruby-on-rails - Rails 编辑表单不显示嵌套项 - 2

    我得到了一个包含嵌套链接的表单。编辑时链接字段为空的问题。这是我的表格:Editingkategori{:action=>'update',:id=>@konkurrancer.id})do|f|%>'Trackingurl',:style=>'width:500;'%>'Editkonkurrence'%>|我的konkurrencer模型:has_one:link我的链接模型:classLink我的konkurrancer编辑操作:defedit@konkurrancer=Konkurrancer.find(params[:id])@konkurrancer.link_attrib

  2. ruby - 解析 RDFa、微数据等的最佳方式是什么,使用统一的模式/词汇(例如 schema.org)存储和显示信息 - 2

    我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i

  3. ruby-on-rails - 如何优雅地重启 thin + nginx? - 2

    我的瘦服务器配置了nginx,我的ROR应用程序正在它们上运行。在我发布代码更新时运行thinrestart会给我的应用程序带来一些停机时间。我试图弄清楚如何优雅地重启正在运行的Thin实例,但找不到好的解决方案。有没有人能做到这一点? 最佳答案 #Restartjustthethinserverdescribedbythatconfigsudothin-C/etc/thin/mysite.ymlrestartNginx将继续运行并代理请求。如果您将Nginx设置为使用多个上游服务器,例如server{listen80;server

  4. ruby-on-rails - 使用 Sublime Text 3 突出显示 HTML 背景语法中的 ERB? - 2

    所以我在关注Railscast,我注意到在html.erb文件中,ruby代码有一个微弱的背景高亮效果,以区别于其他代码HTML文档。我知道Ryan使用TextMate。我正在使用SublimeText3。我怎样才能达到同样的效果?谢谢! 最佳答案 为SublimeText安装ERB包。假设您安装了SublimeText包管理器*,只需点击cmd+shift+P即可获得命令菜单,然后键入installpackage并选择PackageControl:InstallPackage获取包管理器菜单。在该菜单中,键入ERB并在看到包时选择

  5. ruby-on-rails - link_to 不显示任何 rails - 2

    我试图在索引页中创建一个超链接,但它没有显示,也没有给出任何错误。这是我的index.html.erb代码。ListingarticlesTitleTextssss我检查了我的路线,我认为它们也没有问题。PrefixVerbURIPatternController#Actionwelcome_indexGET/welcome/index(.:format)welcome#indexarticlesGET/articles(.:format)articles#indexPOST/articles(.:format)articles#createnew_articleGET/article

  6. ruby-on-rails - 如何在 Rails View 上显示错误消息? - 2

    我是rails的新手,想在form字段上应用验证。myviewsnew.html.erb.....模拟.rbclassSimulation{:in=>1..25,:message=>'Therowmustbebetween1and25'}end模拟Controller.rbclassSimulationsController我想检查模型类中row字段的整数范围,如果不在范围内则返回错误信息。我可以检查上面代码的范围,但无法返回错误消息提前致谢 最佳答案 关键是您使用的是模型表单,一种显示ActiveRecord模型实例属性的表单。c

  7. ruby-on-rails - 复数 for fields_for has_many 关联未显示在 View 中 - 2

    目前,Itembelongs_toCompany和has_manyItemVariants。我正在尝试使用嵌套的fields_for通过Item表单添加ItemVariant字段,但是使用:item_variants不显示该表单。只有当我使用单数时才会显示。我检查了我的关联,它们似乎是正确的,这可能与嵌套在公司下的项目有关,还是我遗漏了其他东西?提前致谢。注意:下面的代码片段中省略了不相关的代码。编辑:不知道这是否相关,但我正在使用CanCan进行身份验证。routes.rbresources:companiesdoresources:itemsenditem.rbclassItemi

  8. ruby - 使用 `+=` 和 `send` 方法 - 2

    如何将send与+=一起使用?a=20;a.send"+=",10undefinedmethod`+='for20:Fixnuma=20;a+=10=>30 最佳答案 恐怕你不能。+=不是方法,而是语法糖。参见http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_expressions.html它说Incommonwithmanyotherlanguages,Rubyhasasyntacticshortcut:a=a+2maybewrittenasa+=2.你能做的最好的事情是:

  9. ruby-on-rails - 在 Flash 警报 Rails 3 中显示错误消息 - 2

    如果我在模型中设置验证消息validates:name,:presence=>{:message=>'Thenamecantbeblank.'}我如何让该消息显示在闪光警报中,这是我迄今为止尝试过的方法defcreate@message=Message.new(params[:message])if@message.valid?ContactMailer.send_mail(@message).deliverredirect_to(root_path,:notice=>"Thanksforyourmessage,Iwillbeintouchsoon")elseflash[:error]

  10. ruby-on-rails - Rails 4 WYSIWYG Bootsy 不显示格式 - 2

    我刚刚按照thebootsygempage上的安装说明进行操作在我保存并查看帖子内容之前,一切看起来都不错。这是输出在View中的样子:HeaderSubhead:似乎没有呈现任何html格式,因为它被引号或类似的东西转义了-其他人有这个问题吗?我没有在github页面或SO上看到任何问题来指出我正确的方向。除了遵循gem安装说明之外,我还没有做任何事情,但也许我错过了什么或者只是犯了一个愚蠢的错误。如果你还有什么想知道的,请尽管问。干杯 最佳答案 你需要有这样的东西,转义html: 关

随机推荐