Solved

OnCtlColor and multiline edit - odd behaviour

Posted on 2010-08-25
4
839 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 30

Accepted Solution

by:
Zoppo earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks
0
 
LVL 30

Expert Comment

by:Zoppo
Comment Utility
You're welcome - I'm glad I could help you ...

have a nice day,

best regards,

ZOPPO
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

Suggested Solutions

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 …
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
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.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

744 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

16 Experts available now in Live!

Get 1:1 Help Now