I am using owner drawn dialog. I like to give the shadow for my sub-dialog. Is it possible?
Thank in advance.
Sure, it's possible. You could tweak your dialog background using OnEraseBkgnd().
As an example, I have put shadows on the OK and Cancel buttons of a dialog (CDialogControlShadowDlg) ...
First, some declarations in the header file of your dialog class:
// 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()
};
Then add OnEraseBkgnd to your message map in the .cpp file:
BEGIN_MESSAGE_MAP(CDialogControlShadowDlg, CDialog)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_WM_ERASEBKGND() // <-- !!!
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
Last, but not least, the member function definitions:
// 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;
}
After applying these modifications, the dialog should look like this:
(source: easyct.de)