?
Solved

CEdit bmp background

Posted on 2003-10-27
6
Medium Priority
?
1,490 Views
Last Modified: 2013-11-20
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:

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

2)
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.

pDC->SetBkMode(TRANSPARENT);
            CRect rect;
            this->GetWindowRect(&rect);
   
            CDC* pMemDC = new CDC;
            CBitmap*     pBitmap = new CBitmap;


            pBitmap->LoadBitmap(IDB_BITMAP_TEXT);
            pMemDC->CreateCompatibleDC(pDC);
            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;
            pBitmap->GetBitmap(&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;
                        else
                              cx = bm.bmWidth;
                        if((y+bm.bmHeight) > nHeight)
                              cy = nHeight;
                        else
                              cy = bm.bmHeight;
                        pDC->BitBlt(x, y, cx, cy, pMemDC, 0, 0, SRCCOPY);
                  }
            }
            pMemDC->SelectObject(pOldBitmap);
            delete pBitmap;
            pMemDC->DeleteDC();
            delete pMemDC;
0
Comment
Question by:lwinkenb
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 3

Accepted Solution

by:
freewell earned 2000 total points
ID: 9631188
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
{
public:
      CAboutDlg();
      CBrush m_BrushEdit;
...
}

BOOL CAboutDlg::OnInitDialog()
{
      CDialog::OnInitDialog();
      
      // TODO: Add extra initialization here
      CBitmap BitmapEdit;
      BitmapEdit.LoadBitmap(IDB_IMAGE);
      m_BrushEdit.CreatePatternBrush(&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
      if(pWnd->GetDlgCtrlID()==IDC_EDIT1)
      {
            pDC->SetBkMode(TRANSPARENT);
            pDC->SetTextColor(RGB(255,0,0));
            pDC->SelectObject(&m_BrushEdit);
            hbr=(HBRUSH)m_BrushEdit;
      }

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

Expert Comment

by:Roshan Davis
ID: 9631453
Draw the bitmap by mapping WM_ERASEBKGND.

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

and declare
afx_msg BOOL OnEraseBkgnd( CDC* pDC );


Rosh :)
0
 
LVL 5

Author Comment

by:lwinkenb
ID: 9631470
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).

freewell>>
I will give your idea a try.
0
Get proactive database performance tuning online

At Percona’s web store you can order full Percona Database Performance Audit in minutes. Find out the health of your database, and how to improve it. Pay online with a credit card. Improve your database performance now!

 
LVL 23

Expert Comment

by:Roshan Davis
ID: 9631486
Map EN_UPDATE in the control class itself, here is some code sample


BEGIN_MESSAGE_MAP(CEditTrans, CEdit)
    //{{AFX_MSG_MAP(CEditTrans)
    ON_WM_CTLCOLOR_REFLECT()
    ON_CONTROL_REFLECT(EN_UPDATE, OnUpdate)
    ON_WM_LBUTTONDOWN()
    ON_CONTROL_REFLECT(EN_KILLFOCUS, OnKillfocus)
    //}}AFX_MSG_MAP
END_MESSAGE_MAP()

HBRUSH CEditTrans::CtlColor(CDC* pDC, UINT nCtlColor)
{
    m_Brush.DeleteObject();

    if (m_BackColor == TRANS_BACK) {
        m_Brush.CreateStockObject(HOLLOW_BRUSH);
        pDC->SetBkMode(TRANSPARENT);
    }
    else {
        m_Brush.CreateSolidBrush(m_BackColor);
        pDC->SetBkColor(m_BackColor);
    }

    pDC->SetTextColor(m_TextColor);

    return (HBRUSH)m_Brush;
}

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

    GetWindowRect(rect);
    pParent->ScreenToClient(rect);
    rect.DeflateRect(2, 2);

    pParent->InvalidateRect(rect, FALSE);
}


Rosh :)
0
 
LVL 23

Expert Comment

by:Roshan Davis
ID: 9631497
And try this link

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

Good Luck
0
 
LVL 5

Author Comment

by:lwinkenb
ID: 9631548
thanks for the comments.

Freewell's code turned out to work perfectly, the highlighting no longer removes the background.
0

Featured Post

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question