Solved

CEdit bmp background

Posted on 2003-10-27
6
1,449 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 23

Expert Comment

by:Roshan Davis
Comment Utility
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
Comment Utility
And try this link

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

Good Luck
0
 
LVL 5

Author Comment

by:lwinkenb
Comment Utility
thanks for the comments.

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

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
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.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

728 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now