c++ - 如何为所有者绘制对话框启用阴影

标签 c++ mfc shadow cdialog

我正在使用所有者绘制的对话框。我喜欢给我的子对话框加上阴影。是否可以?

提前致谢。

最佳答案

当然,这是可能的。您可以使用 OnEraseBkgnd() 调整对话框背景。

例如,我在对话框 (CDialogControlShadowDlg) 的确定和取消按钮上放置了阴影 ...

首先,对话框类头文件中的一些声明:

// Implementation
protected:
    HICON m_hIcon;

    CRect ComputeDrawingRect(int control_id);   // <-- !!!
    void DrawShadow(CDC* pDC, CRect &r);        // <-- !!!

    // Generated message map functions
    virtual BOOL OnInitDialog();
    afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
    afx_msg void OnPaint();
    afx_msg HCURSOR OnQueryDragIcon();
    afx_msg BOOL OnEraseBkgnd(CDC* pDC);        // <-- !!!
    DECLARE_MESSAGE_MAP()
};

然后将 OnEraseBkgnd 添加到 .cpp 文件中的消息映射:

BEGIN_MESSAGE_MAP(CDialogControlShadowDlg, CDialog)
    ON_WM_SYSCOMMAND()
    ON_WM_PAINT()
    ON_WM_QUERYDRAGICON()
    ON_WM_ERASEBKGND()          // <-- !!!
    //}}AFX_MSG_MAP
END_MESSAGE_MAP()

最后但同样重要的是,成员函数定义:

// gets the actual drawing location of a control relative to the dialog frame
CRect CDialogControlShadowDlg::ComputeDrawingRect(int control_id)
{
    CRect r;
    GetDlgItem(control_id)->GetWindowRect(&r);
    ScreenToClient(&r);

    return r;
}

#define SHADOW_WIDTH 6  
// draws the actual shadow
void CDialogControlShadowDlg::DrawShadow(CDC* pDC, CRect &r)
{
    DWORD dwBackgroundColor = GetSysColor(COLOR_BTNFACE);
    DWORD dwDarkestColor = RGB(GetRValue(dwBackgroundColor)/2, 
                            GetGValue(dwBackgroundColor)/2,
                            GetBValue(dwBackgroundColor)/2); // dialog background halftone as base color for shadow
    int nOffset;
    for (nOffset = SHADOW_WIDTH; nOffset > 0; nOffset--)
    {
        DWORD dwCurrentColorR = (GetRValue(dwDarkestColor)*(SHADOW_WIDTH-nOffset)
                                 + GetRValue(dwBackgroundColor)*nOffset) / SHADOW_WIDTH;
        DWORD dwCurrentColorG = (GetGValue(dwDarkestColor)*(SHADOW_WIDTH-nOffset)
                                 + GetGValue(dwBackgroundColor)*nOffset) / SHADOW_WIDTH;
        DWORD dwCurrentColorB = (GetBValue(dwDarkestColor)*(SHADOW_WIDTH-nOffset) 
                                + GetBValue(dwBackgroundColor)*nOffset) / SHADOW_WIDTH;
        pDC->FillSolidRect(r + CPoint(nOffset, nOffset), RGB(dwCurrentColorR, dwCurrentColorG, dwCurrentColorB));
    }
}

BOOL CDialogControlShadowDlg::OnEraseBkgnd( CDC* pDC )
{
    // draw dialog background
    CRect rdlg;
    GetClientRect(&rdlg);
    DWORD dwBackgroundColor = GetSysColor(COLOR_BTNFACE);
    pDC->FillSolidRect(rdlg, dwBackgroundColor); 

    // draw shadows
    CRect r1, r2;
    r1 = ComputeDrawingRect(IDOK);
    r2 = ComputeDrawingRect(IDCANCEL);
    DrawShadow(pDC, r1);
    DrawShadow(pDC, r2);

    return TRUE;
}

应用这些修改后,对话框应如下所示:

screenshot
(来源:easyct.de)

关于c++ - 如何为所有者绘制对话框启用阴影,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21979505/

相关文章:

c++ - c++中的位运算

c++ - QTcpSocket 不发出错误信号

winapi - 具有 alpha 值的 VC++ COLORREF

text - 使用 CSS 3 的浅色文本阴影

android - ListView Item阴影+自定义选择器

c++ - 如何从 native win32 C++ 应用程序获取当前实例的可执行文件名?

c++ - 没有函数模板 "std::make_pair"的实例与参数列表匹配

c++ - C++ 应用程序的框架

mfc - 我在 VS-2019 中缺少添加新项目 Add "MFC Class From Typelib"

opengl - 阴影贴图和深度值混淆