| | |
GUI colouring
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Aug 2005
Posts: 188
Reputation:
Solved Threads: 3
How would you colour a control, for example colour the background of a button control to Black and the text of the button control to white...
I've tried looking into API's like SetTextColor() and SetBkColor() but I can't get anything working as such.
Thanks.
C++ Syntax (Toggle Plain Text)
HWND hButton = CreateWindowEx(0,"BUTTON","&Send",WS_VISIBLE | WS_CHILD,230,100,70,20,hwnd,(HMENU)cmdBUTTON,0,0); if ( hButton == NULL ) { MessageBox(hwnd,"Error: button could not be created","Error",0); } /*Change the colour of the created button here..*/
I've tried looking into API's like SetTextColor() and SetBkColor() but I can't get anything working as such.
Thanks.
Last edited by bops; Jun 6th, 2007 at 11:01 am.
•
•
Join Date: Dec 2006
Posts: 1,089
Reputation:
Solved Threads: 164
for controls other than buttons, you could handle the WM_CTLCOLORXXX notification; eg. WM_CTLCOLOREDIT for an edit control.
for buttons, the button needs to be an owner-drawn button; for such a button the system sends the parent window a WM_DRAWITEM message.
see: http://msdn2.microsoft.com/en-us/library/ms673346.aspx
for buttons, the button needs to be an owner-drawn button; for such a button the system sends the parent window a WM_DRAWITEM message.
see: http://msdn2.microsoft.com/en-us/library/ms673346.aspx
Last edited by vijayan121; Jun 6th, 2007 at 11:24 am.
•
•
Join Date: Aug 2005
Posts: 188
Reputation:
Solved Threads: 3
Thanks, that seemed to work except that for example the edit control background was set to the correct colour BUT the text in the edit control had a different background colour. for example a text box had a white line where there is any text in it and the rest was the correct color..
also, can you specify which controls you wish to colour etc using those messages and by using..
Thanks.
also, can you specify which controls you wish to colour etc using those messages and by using..
C++ Syntax (Toggle Plain Text)
case WM_CTLCOLOREDIT: { switch(LOWORD(wParam)) { case CONTROLNAME: { } break; default: break; } break;
Thanks.
•
•
Join Date: Aug 2005
Posts: 188
Reputation:
Solved Threads: 3
•
•
•
•
The wparam of the message is a handle to the device context; you can use SetTextColor, SetBkMode, SetTextAlign etc. ion it.
like..
C++ Syntax (Toggle Plain Text)
case WM_CTLCOLOREDIT: { switch (LOWORD(wParam)) { case (txtMessage): { SetTextColor((HDC)wParam,(COLORREF)CreateSolidBrush(RGB(255,255,255))); SetBkColor((HDC)wParam,(COLORREF)CreateSolidBrush(RGB(0,0,0))); } } }
C++ Syntax (Toggle Plain Text)
or maybe could i do this.. case WM_CTLCOLOREDIT: { HDC hDC = getDC(txtMessage); SetTextColor(hDC,(COLORREF)CreateSolidBrush(RGB(255,255,255))); SetBkColor(hDC,(COLORREF)CreateSolidBrush(RGB(0,0,0))); }
Thanks.
Last edited by bops; Jun 7th, 2007 at 10:18 am.
•
•
Join Date: Dec 2006
Posts: 1,089
Reputation:
Solved Threads: 164
for the WM_CTLCLREDIT message,
wparam - handle to the device context for the edit control window (HDC)
lparam - handle to the edit control (HWND)
if you want to make the decision based on the HWND, this is all that is required; if you want to do the same based on control id:
wparam - handle to the device context for the edit control window (HDC)
lparam - handle to the edit control (HWND)
if you want to make the decision based on the HWND, this is all that is required; if you want to do the same based on control id:
C++ Syntax (Toggle Plain Text)
// ... case WM_CTLCOLOREDIT: { HDC dc = HDC(wparam) ; HWND edit_window = HWND(lparam) ; int edit_control_id = GetDlgCtrlID(edit_window) ; switch(edit_control_id) { // ...
Last edited by vijayan121; Jun 7th, 2007 at 10:51 am.
•
•
Join Date: Aug 2005
Posts: 188
Reputation:
Solved Threads: 3
C++ Syntax (Toggle Plain Text)
case WM_CTLCOLOREDIT: { HWND hControl = (HWND) lParam; HDC hDC = (HDC) wParam; int iControl = GetDlgCtrlID(hControl); int itxtMessage = GetDlgCtrlID(txtMessage); if ( iControl == itxtMessage ) { SetBkMode((HDC)wParam,OPAQUE); SetTextColor((HDC)wParam,(COLORREF)RGB(255,255,255)); SetBkColor((HDC)wParam,(COLORREF)RGB(0,0,0)); } }
http://img159.imageshack.us/img159/5295/79814659hr4.png
Changing the opaque keyword to transparent I get the following output..
http://img257.imageshack.us/img257/8644/22815276qx2.png
- I need the full background of the test control to be black and the text of the edit control to be white, how could I do this?
Thanks for all your help.
•
•
Join Date: Dec 2006
Posts: 1,089
Reputation:
Solved Threads: 164
•
•
•
•
...I need the full background of the test control to be black and the text of the edit control to be white, how could I do this?
return LRESULT( GetStockObject( BLACK_BRUSH ) ) ;
Last edited by vijayan121; Jun 8th, 2007 at 11:10 am.
•
•
Join Date: Aug 2005
Posts: 188
Reputation:
Solved Threads: 3
Thanks, that seems to work fine except one problem when i write some text "test" and then press the backspace key.. the caret moves back as it should but the last character is still displayed for example if i wrote "test" and then pressed backspace, the box would display "test" but in fact only contain "tes".
Is there any way to stop this from happening?
Thanks.
Also.. are there any message handlers for special controls such as WC_IPADDRESS or RICHEDIT etc
Is there any way to stop this from happening?
Thanks.
Also.. are there any message handlers for special controls such as WC_IPADDRESS or RICHEDIT etc
Last edited by bops; Jun 8th, 2007 at 11:44 am.
•
•
Join Date: Dec 2006
Posts: 1,089
Reputation:
Solved Threads: 164
you could write a handler for the EN_UPDATE notification sent by the edit control and force a repaint by InvalidateRect/UpdateWindow.
rich edit controls do not send CTLCOLOR messages; however you can set its background colour by sending it a EM_SETBKGNDCOLOR message.
i've not used WC_IPADDRESS (i've not written any gui code for several years now), but i think it is just an edit control with special formatting/validation. so my guess is that you should be able to treat it like an edit control.
rich edit controls do not send CTLCOLOR messages; however you can set its background colour by sending it a EM_SETBKGNDCOLOR message.
i've not used WC_IPADDRESS (i've not written any gui code for several years now), but i think it is just an edit control with special formatting/validation. so my guess is that you should be able to treat it like an edit control.
![]() |
Other Threads in the C++ Forum
- Previous Thread: Simulate Mouse Move
- Next Thread: What's the meaning of the "return" here?
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion count database delete deploy desktop developer directshow dll dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph homeworkhelp homeworkhelper iamthwee ifstream input int integer lib linkedlist linux list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






