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

C++ 如何为所有者绘制的对话框启用阴影,c++,mfc,shadow,cdialog,C++,Mfc,Shadow,Cdialog,我正在使用所有者绘制的对话框。我喜欢给我的子对话框添加阴影。可能吗 先谢谢你 当然,这是可能的。您可以使用OnEraseBkgnd()调整对话框背景 例如,我在对话框(CDialogControlShadowDlg)的“确定”和“取消”按钮上放置了阴影 首先,对话框类的头文件中的一些声明: // Implementation protected: HICON m_hIcon; CRect ComputeDrawingRect(int control_id); // <

我正在使用所有者绘制的对话框。我喜欢给我的子对话框添加阴影。可能吗


先谢谢你

当然,这是可能的。您可以使用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()
};
应用这些修改后,对话框应如下所示:



(来源:)

不错,你用了DrawShadow作为按钮。我可以用它来做一个CDDialog吗?如果我没弄错的话,你可以在对话框中动态创建你的子窗口。当调用GetWindowRect()时,它们的行为与按钮几乎相同——除非您将它们修改为三角形。:)您可能还希望在创建时尝试设置子对话框的CS_DROPSHADOW窗口样式。但阴影是否可见取决于操作系统和当前使用的主题。我已经尝试过CS_DROPSHADOW,但在这种情况下,当我尝试使用ShowWindow()隐藏对话框时,它将隐藏对话框,只有阴影未隐藏。那么此所有者绘制的阴影是适用于所有者绘制的对话框的正确解决方案。:)
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;
}