Set edit box text colour
-
any idea, how to set the colour of text for an edit box. Say from black to blue. Kind Regards Caoimh
You'll need to override the WM_CTLCOLOR message via CWnd::OnCtlColor()'s handler function. You can then set the colour of the edit (or static) controls of your program.
HBRUSH CMyDialog::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) { switch(nCtlColor) { case CTLCOLOR_EDIT: **// colour edit controls** { pDC->SetTextColor(RGB(0, 0, 255)); **// set text to red** pDC->SetBkMode(TRANSPARENT); } break; default: break; } return CDialog::OnCtlColor(pDC, pWnd, nCtlColor); // call the base functon }
I Dream of Absolute Zero
-
You'll need to override the WM_CTLCOLOR message via CWnd::OnCtlColor()'s handler function. You can then set the colour of the edit (or static) controls of your program.
HBRUSH CMyDialog::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) { switch(nCtlColor) { case CTLCOLOR_EDIT: **// colour edit controls** { pDC->SetTextColor(RGB(0, 0, 255)); **// set text to red** pDC->SetBkMode(TRANSPARENT); } break; default: break; } return CDialog::OnCtlColor(pDC, pWnd, nCtlColor); // call the base functon }
I Dream of Absolute Zero
I have a question in regard to your post. I'm sure there is a way I just can't figure it out. How do you set an individual edit box's background color instead of CTLCOLOR_EDIT where it changes all the edit controls? Secondly I can't get the code you posted to actually work. I changed a few things to get it to work. HBRUSH CMyDialog::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) { if (nCtlColor == CTLCOLOR_EDIT || nCtlColor == CTLCOLOR_STATIC) { pDC->SetBkMode(TRANSPARENT); pDC->SetTextColor(RGB(100,190,199)); HBRUSH B = CreateSolidBrush(RGB(2,2,255)); return (HBRUSH) B; } HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor); return hbr; } Win32newb "Making windows programs worse than they already are"
-
I have a question in regard to your post. I'm sure there is a way I just can't figure it out. How do you set an individual edit box's background color instead of CTLCOLOR_EDIT where it changes all the edit controls? Secondly I can't get the code you posted to actually work. I changed a few things to get it to work. HBRUSH CMyDialog::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) { if (nCtlColor == CTLCOLOR_EDIT || nCtlColor == CTLCOLOR_STATIC) { pDC->SetBkMode(TRANSPARENT); pDC->SetTextColor(RGB(100,190,199)); HBRUSH B = CreateSolidBrush(RGB(2,2,255)); return (HBRUSH) B; } HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor); return hbr; } Win32newb "Making windows programs worse than they already are"
Firstly, looking at the code that you've posted, I suspect the reason why it now suddenly works is because your control is a regular static window (used for read-only text disply etc) and not an edit control, as was assumed. I tend to use a switch statement to distinguish between the control types (there are other types such as CTLCOLOR_DLG and CTLCOLOR_LISTBOX)
Secondly, you can target a specific control by quering its control id. For Example:if(pWnd->GetDlgCtrlID() == IDC_EDIT_INPUT) { pDC->SetTextColor(RGB(0,190,0)); pDC->SetBkMode(TRANSPARENT); }
I Dream of Absolute Zero