Transparent controls on gradient filled view

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Im trying to achieve transparent MFC controls on a gradient filled background.
I have a solution that uses GradientFill() to paint the gradient filled background in the CDialog/CView derived class OnEraseBkgnd event.
I then use the OnCtlColor() event to draw static controls with a HOLLOW_BRUSH in TRANSPARENT mode as follows:
HBRUSH CQuoteDialog::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
COLORREF cr = GetBackgroundColour();
COLORREF cr_white = 0xffffff;

if(!m_BrushColour.m_hObject)
m_BrushColour.CreateSolidBrush(cr);

if(!m_BrushWhite.m_hObject)
m_BrushWhite.CreateSolidBrush(cr_white);

bool bUseWhite = false;

switch (nCtlColor)
{
// process my edit controls by ID.
case CTLCOLOR_EDIT:
if( ES_READONLY & ((CEdit *)pWnd)->GetStyle())
{
if(cr)
pDC->SetBkColor(cr);
break;
}
else
{
bUseWhite = true;
}
break;

case CTLCOLOR_STATIC:
{
CRect rect;
pWnd->GetClientRect(&rect);
DrawThemeParentBackground(pWnd->GetSafeHwnd(), pDC->m_hDC, &rect);
pDC->SetBkMode(TRANSPARENT);

return (HBRUSH)GetStockObject(HOLLOW_BRUSH);
}
break;

case CTLCOLOR_LISTBOX:
bUseWhite = true;
break;

case CTLCOLOR_DLG:
pDC->SetBkColor(cr);
break;

case CTLCOLOR_BTN:
pDC->SetBkColor(cr_white);
bUseWhite = true;
break;

default:
break;
}

if(bUseWhite == true)
return m_BrushWhite;

return m_BrushColour;
}



This solution works fine with CDialog derived objects but it does not work for CView derived objects.
When the views are presented all the controls flash on screen then disappear only partially returning when the mouse is hovered over them.
If I remove the DrawThemeParentBackground () line. All the control are drawn but the display gets corrupted as the mouse traverses the screen – again only in views.
I’m developing on a Win 7, 64-bit machine using the default theme. My app is 32-bit using common controls version 6.

View the full article
 
Back
Top