المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : Setting a controls background to transparent



C++ Programming
09-15-2009, 07:42 AM
Hi
I want to set the background of a static control to transparent. How to do this?

I tried the methode described in MSDN ( using the ON_WM_CTLCOLOR() and ON_WM_ERASEBKGND() ) but not working.

My intention is to set the background to a blue gradient or just a solid fill. I have lot of static controls and check boxes in my dialog. I mangaed to set the background of my dialog to solid blue with the help of MSDN documentation. But static control background becomes white whatever i do according to the documentation. Compailer complainted that m_brush is not declaired. I declaired it as CBrush m_brush; in .h file. I have not initialized and not sure how to initialise it.

Am i doing something wrong? Please help. Below is what I got from the MSDN (FYR)


HBRUSH CTestDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
// Call the base class implementation first! Otherwise, it may
// undo what we're trying to accomplish here.
HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);

// Are we painting the IDC_MYSTATIC control? We can use
// CWnd::GetDlgCtrlID() to perform the most efficient test.
if (pWnd->GetDlgCtrlID() == IDC_MYSTATIC)
{
// Set the text color to red
pDC->SetTextColor(RGB(255, 0, 0));

// Set the background mode for text to transparent
// so background will show thru.
pDC->SetBkMode(TRANSPARENT);

// Return handle to our CBrush object
hbr = m_brush;
}
return hbr;
}

BOOL CTestDlg::OnEraseBkgnd(CDC* pDC)
{
// Set brush to desired background color
CBrush backBrush(RGB(255, 128, 128));

// 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;
}