CEdit bmp background

I have tiled a bmp image to create a background for my CEdit object.  This works fine, except when text is selected.  The problem is that when the text is unselected the background of where the text selection was is now white instead of my bmp.  I need a solution to this problem.  I have thought of two possible ways to get around this:

Make it so text selection doesnt highlight the background (it only changes the actual text color).

Have the background redraw when the text selection is gone.

The problem is I'm not sure how to implement either of these solutions.

Here is the code I have to place the background bmp.

            CRect rect;
            CDC* pMemDC = new CDC;
            CBitmap*     pBitmap = new CBitmap;

            HBITMAP hBMPDefault = CreateCompatibleBitmap(pDC->m_hDC, rect.Width(), rect.Height());
        SelectObject(pMemDC->m_hDC, hBMPDefault);
            CBitmap*     pOldBitmap = (CBitmap*)pMemDC->SelectObject(pBitmap);

            int cx = 0;
            int cy = 0;
            int nWidth = rect.Width();
            int nHeight = rect.Height();
            BITMAP bm;
            for (int y=0;y < nHeight; y += bm.bmHeight )
                  for (int x=0;x < 225; x += bm.bmWidth )
                        if((x+bm.bmWidth) > nWidth)
                              cx = nWidth;
                              cx = bm.bmWidth;
                        if((y+bm.bmHeight) > nHeight)
                              cy = nHeight;
                              cy = bm.bmHeight;
                        pDC->BitBlt(x, y, cx, cy, pMemDC, 0, 0, SRCCOPY);
            delete pBitmap;
            delete pMemDC;
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

The CEdit required a brush object when painting its DC, create a bitmapped brush and return the brush handler in your dialog's OnCtlColor.

class CAboutDlg : public CDialog
      CBrush m_BrushEdit;

BOOL CAboutDlg::OnInitDialog()
      // TODO: Add extra initialization here
      CBitmap BitmapEdit;

      return TRUE;  // return TRUE unless you set the focus to a control
                    // EXCEPTION: OCX Property Pages should return FALSE

HBRUSH CAboutDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
      HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
      // TODO: Change any attributes of the DC here

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Roshan DavisCommented:
Draw the bitmap by mapping WM_ERASEBKGND.

You can map that message to any CWnd derived class using class wizard.
ON_WM_ERASEBKGND() in message map

and declare
afx_msg BOOL OnEraseBkgnd( CDC* pDC );

Rosh :)
lwinkenbAuthor Commented:
Roshmon >>
All the code I posted above is already in my OnEraseBackground() method of my CEdit derived class.

I also forgot to mention that the dialog the CEdit control is on is a CDialogBar (not sure if that makes a difference here).

I will give your idea a try.
Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

Roshan DavisCommented:
Map EN_UPDATE in the control class itself, here is some code sample


HBRUSH CEditTrans::CtlColor(CDC* pDC, UINT nCtlColor)

    if (m_BackColor == TRANS_BACK) {
    else {


    return (HBRUSH)m_Brush;

void CEditTrans::OnUpdate()
void CEditTrans::UpdateCtrl()
    CWnd* pParent = GetParent();
    CRect   rect;

    rect.DeflateRect(2, 2);

    pParent->InvalidateRect(rect, FALSE);

Rosh :)
Roshan DavisCommented:
And try this link

http://www.codeproject.com/editctrl/ctrltrans.asp - CEdit & CStatic Transparency Control

Good Luck
lwinkenbAuthor Commented:
thanks for the comments.

Freewell's code turned out to work perfectly, the highlighting no longer removes the background.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.