2 Replies - 8789 Views - Last Post: 19 August 2012 - 05:56 AM

#1 Sublight  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 41
  • Joined: 11-July 08

How to change Edit Control Text Color?

Posted 26 June 2012 - 12:56 PM

For reference, I'm using Visual C++ MFC.

I'm still very new to Win32 development, so when developing my first major Win32 program (a serial communication utility) I found some existing code with the right user interface appearance, gutted everything behind the gui, and added my own logic to populate the fields. It worked very well, but when getting feedback the #1 recommendation was to set the text color of any potentially invalid field to red. :plain:


It was a good recommendation, but one I have no clue how to implement. Is it even possible to selectively change the text color for some Edit Control boxes while leaving others on the Dialog window unchanged? If the text color can't be changed could the background?

I got as far as the control variable CEdit m_EditBox1, but from there none of the available members seem to help.

Code snipit below:
class MainDlg : public CDialog
{
public:
    MainDlg();
    // other functions

    CEdit m_EditBox1;
    CEdit m_EditBox2;
    CFont m_italic;

protected:
    virtual void DoDataExchange(CDataExchange* pDX);

    //lots more code
}


MainDlg::MainDlg()
{
    //Constructor. Code fill

    //My attempt at changing the text to italics as a place holder for color. 
    //I haven't quite gotten font changes working yet either, but I have more optimism here.
    m_italic.CreateFontA(0,0,0,0,FW_REGULAR,        //default height, width, escapement, oraintation, weight
        true,false,false,ANSI_CHARSET,             //italics, underline, strikeout, charset
        OUT_DEFAULT_PRECIS,CLIP_DEFAULT_PRECIS,DEFAULT_QUALITY, //out precision, clip precision, quality
        DEFAULT_PITCH | FF_SWISS, "Arial");         //pitch & family, font name
    m_EditBox1.SetFong(&m_italic);

}

void MainDlg::DoDataExchange(CDataExchange* pDx)
{
    CDialog::DoDataExchange(pDX);
    DDX_Control(pDX, m_EditBox1, IDC_EDIT1);
    DDX_Text(pDX, cStringText1, IDC_EDIT1);
    DDX_Text(pDX, cStringText2, IDC_EDIT2);
    //etc
}


Is This A Good Question/Topic? 0
  • +

Replies To: How to change Edit Control Text Color?

#2 Sublight  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 41
  • Joined: 11-July 08

Re: How to change Edit Control Text Color?

Posted 29 June 2012 - 07:35 AM

Nevermind.
I'm now in the process of migrating the code over from a Windows MFC form application to a Windows CLR form application.
Was This Post Helpful? 0
  • +
  • -

#3 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 762
  • View blog
  • Posts: 2,218
  • Joined: 20-March 10

Re: How to change Edit Control Text Color?

Posted 19 August 2012 - 05:56 AM

Yes it is possible to do this

BEGIN_MESSAGE_MAP(CMyDialog, CDialog)
    //{{AFX_MSG_MAP(CMyDialog)
    ON_WM_CTLCOLOR()
    //}}AFX_MSG_MAP
END_MESSAGE_MAP()

....


HBRUSH CMyDialog::OnCtlColor(CDC* pDC, CWnd *pWnd, UINT nCtlColor)
{
    if(pWnd == whatevercontrolyoulike)
    {

    switch (nCtlColor)
    {
    case CTLCOLOR_STATIC:
        pDC->SetTextColor(RGB(255, 0, 0));// Set color to Red
        return (HBRUSH)GetStockObject(NULL_BRUSH);
    default:
        return CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
    }
    }
 return CDialog::OnCtlColor(pDC, pWnd, nCtlColor);

}




Snoopy.

This post has been edited by snoopy11: 19 August 2012 - 05:58 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1