Solved

OnCtlColor and multiline edit - odd behaviour

Posted on 2010-08-25
4
852 Views
Last Modified: 2013-11-20
I have a multi line edit control on a dialog (no subclassing of the edit control).  In the dialog I have the following code

HBRUSH CMyDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
      HBRUSH hbr = CMyDlg::OnCtlColor(pDC, pWnd, nCtlColor);

      // TODO:  Change any attributes of the DC here
      if(nCtlColor == CTLCOLOR_STATIC)
      {.....
      }
      else if(nCtlColor == CTLCOLOR_EDIT)
      {
            UINT nID = pWnd->GetDlgCtrlID();
            switch(nID)
            {
            case IDC_EDIT_BENEFICIARY:
                  pDC->SetBkMode(TRANSPARENT);
                  return (HBRUSH)m_brBeneficiary;
            }
      }

where m_brBeneficiary is a CBrush aand set as follows in the c'tor
      m_brBeneficiary.CreateSolidBrush(RGB(255, 0, 0));


When I type into the edit control I get text as in picture 1 (sort of bold font - WRONG), now if I select some text in the control the font appears as I want it to appear see picture 2.  <Picture 3 shows what happens without the OnCtlColor>

Any ideas what is going on and more important how to get it to work correctly.

VS 2005 on Windows 7


0010.jpg
0011.jpg
0012.jpg
0
Comment
Question by:AndyAinscow
  • 2
  • 2
4 Comments
 
LVL 31

Accepted Solution

by:
Zoppo earned 500 total points
ID: 33520927
Hi Andy,

I think the problem is the 'pDC->SetBkMode(TRANSPARENT);' because this causes the control to draw the entered text just over the existing (and I guess for some reason the font mapping makes the font getting bold after some entered text) - you can simply verify this by entering some text and then deleting using backspace ...

So I would suggest to not use 'pDC->SetBkMode(TRANSPARENT);' - instead use 'SetBkColor' to set the text's background color to the same as the brush's color, i.e. 'pDC->SetBkColor(RGB( 255, 0, 0 ));' for a red filled edit control.

Hope that helps,

ZOPPO
0
 
LVL 44

Author Comment

by:AndyAinscow
ID: 33521171
And I thought I had tried all possibles

this doesn't work
            pDC->SetBkColor(RGB(255, 0, 0));
//            pDC->SetBkMode(TRANSPARENT);
//            return (HBRUSH)m_brBeneficiary;


but this does
            pDC->SetBkColor(RGB(255, 0, 0));
//            pDC->SetBkMode(TRANSPARENT);
            return (HBRUSH)m_brBeneficiary;



Set the bkcolor AND supply the brush is required.


0
 
LVL 44

Author Closing Comment

by:AndyAinscow
ID: 33521177
Thanks
0
 
LVL 31

Expert Comment

by:Zoppo
ID: 33521221
You're welcome - I'm glad I could help you ...

have a nice day,

best regards,

ZOPPO
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Get filename and folder into excel 7 84
Expand to include initial dialog with two choices. 9 85
Smart Camera scanning and reading information 3 121
sum67 challenge 35 116
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.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

740 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