我有一个相当标准的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答案的解决方案,但是我开始获得赏金,万一有人可以改进它 - 特别是顺利淡入/淡出。
答案 0 :(得分:14)
您可以在要调暗的窗口顶部创建另一个完全黑色的窗口,并使用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,就像你发布的例子一样,这段代码必须放在构造函数和&amp; 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中如何(我认为)使用定时器的一般概念。我绝对不是一个对此有所思考的人,但是:)
答案 1 :(得分:3)
我已经接受了奥斯坦的答案,因为它引导我找到解决方案,但我想我会发布我的修改。我不得不修改它以使其适用于我,所以它可能对其他人有用。
根据记录,调光效果很好,但看起来并不像我希望的那样自然。在经常出现对话框的应用程序中,调光变得分散注意力地打开和关闭主窗口的规律性。为了妥协,我已经使调光相当微妙(大约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(...);
}
答案 2 :(得分:2)
我无法抗拒。
这是你的代码添加了定时器并实现了淡入/淡出。此外,我改为使用中灰色而不是黑色来遮挡遮挡。
您可以通过增加持续时间或增加速率来调整控制淡入淡出的常量,使其更平滑。实验表明,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);
}