Windows/MFC

CEdit CBrush background image 에디트에 배경 이미지 깔기

aucd29 2013. 10. 2. 18:05
Link : http://www.codeguru.com/forum/showthread.php?t=173087&highlight=CEdit+background+image

One way : in the dialog class ...

1) add member variable
[code]
HBRUSH m_hBrush;[/code]

2) in the constructor
[code]m_hBrush = CreatePatternBrush((HBITMAP)LoadImage(NULL,"c:\\background.bmp",IMAGE_BITMAP,0,0,
LR_LOADFROMFILE | LR_CREATEDIBSECTION | LR_DEFAULTSIZE));[/code]

3) process WM_CTLCOLOR for dialog class and code it :
[code]HBRUSH dialog1::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
    HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);

    if (pWnd->GetDlgCtrlID() == IDC_EDIT1)
    {
        pDC->SetBkMode(TRANSPARENT);
        hbr = m_hBrush;
    }

    // TODO: Return a different brush if the default is not desired
    return hbr;
}[/code]

You could also derive a class, CMyEdit, based on CEdit and make
the following changes to it:
1 & 2) steps 1 and 2 same as above, except for the CMyEDit class, not the
dialog class

3) process ON_WM_CTLCOLOR_REFLECT() , NOT WM_CTLCOLOR, add code it :
[code]HBRUSH CMyEdit::CtlColor(CDC* pDC, UINT nCtlColor)
{
    pDC->SetBkMode(TRANSPARENT); // probably want to do this
    return m_hBrush;
}[/code]