Solved

CEdit bmp background

Posted on 2003-10-27
6
1,459 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
  • 3
  • 2
6 Comments
 
LVL 3

Accepted Solution

by:
freewell earned 500 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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

ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
zeroFront challenge 7 99
matchUp  challenge 9 108
Fibonacci challenge 11 122
how to split multiple lines delimiter : 8 99
Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

772 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