944,184 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 4375
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Jun 6th, 2007
0

GUI colouring

Expand Post »
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...

C++ Syntax (Toggle Plain Text)
  1. HWND hButton = CreateWindowEx(0,"BUTTON","&Send",WS_VISIBLE | WS_CHILD,230,100,70,20,hwnd,(HMENU)cmdBUTTON,0,0);
  2.  
  3. if ( hButton == NULL )
  4. {
  5. MessageBox(hwnd,"Error: button could not be created","Error",0);
  6. }
  7.  
  8. /*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.
Reputation Points: 23
Solved Threads: 5
Posting Whiz in Training
bops is offline Offline
214 posts
since Aug 2005
Jun 6th, 2007
0

Re: GUI colouring

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
Last edited by vijayan121; Jun 6th, 2007 at 11:24 am.
Reputation Points: 1159
Solved Threads: 285
Posting Virtuoso
vijayan121 is offline Offline
1,606 posts
since Dec 2006
Jun 6th, 2007
0

Re: GUI colouring

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..
C++ Syntax (Toggle Plain Text)
  1. case WM_CTLCOLOREDIT:
  2. {
  3. switch(LOWORD(wParam))
  4. {
  5. case CONTROLNAME:
  6. {
  7.  
  8. }
  9. break;
  10. default:
  11. break;
  12. }
  13. break;

Thanks.
Reputation Points: 23
Solved Threads: 5
Posting Whiz in Training
bops is offline Offline
214 posts
since Aug 2005
Jun 7th, 2007
0

Re: GUI colouring

Click to Expand / Collapse  Quote originally posted by bops ...
BUT the text in the edit control had a different background colour.
The wparam of the message is a handle to the device context; you can use SetTextColor, SetBkMode, SetTextAlign etc. ion it.
Reputation Points: 1159
Solved Threads: 285
Posting Virtuoso
vijayan121 is offline Offline
1,606 posts
since Dec 2006
Jun 7th, 2007
0

Re: GUI colouring

Click to Expand / Collapse  Quote originally posted by vijayan121 ...
The wparam of the message is a handle to the device context; you can use SetTextColor, SetBkMode, SetTextAlign etc. ion it.
Yes but how would one set each of these on one specific control?
like..
C++ Syntax (Toggle Plain Text)
  1. case WM_CTLCOLOREDIT:
  2. {
  3. switch (LOWORD(wParam))
  4. {
  5. case (txtMessage):
  6. {
  7. SetTextColor((HDC)wParam,(COLORREF)CreateSolidBrush(RGB(255,255,255)));
  8. SetBkColor((HDC)wParam,(COLORREF)CreateSolidBrush(RGB(0,0,0)));
  9. }
  10. }
  11. }

C++ Syntax (Toggle Plain Text)
  1. or maybe could i do this..
  2.  
  3. case WM_CTLCOLOREDIT:
  4. {
  5. HDC hDC = getDC(txtMessage);
  6.  
  7. SetTextColor(hDC,(COLORREF)CreateSolidBrush(RGB(255,255,255)));
  8. SetBkColor(hDC,(COLORREF)CreateSolidBrush(RGB(0,0,0)));
  9. }

Thanks.
Last edited by bops; Jun 7th, 2007 at 10:18 am.
Reputation Points: 23
Solved Threads: 5
Posting Whiz in Training
bops is offline Offline
214 posts
since Aug 2005
Jun 7th, 2007
0

Re: GUI colouring

Click to Expand / Collapse  Quote originally posted by bops ...
Yes but how would one set each of these on one specific control?
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:
C++ Syntax (Toggle Plain Text)
  1. // ...
  2. case WM_CTLCOLOREDIT:
  3. {
  4. HDC dc = HDC(wparam) ;
  5. HWND edit_window = HWND(lparam) ;
  6. int edit_control_id = GetDlgCtrlID(edit_window) ;
  7. switch(edit_control_id)
  8. {
  9. // ...
Last edited by vijayan121; Jun 7th, 2007 at 10:51 am.
Reputation Points: 1159
Solved Threads: 285
Posting Virtuoso
vijayan121 is offline Offline
1,606 posts
since Dec 2006
Jun 8th, 2007
0

Re: GUI colouring

C++ Syntax (Toggle Plain Text)
  1. case WM_CTLCOLOREDIT:
  2. {
  3. HWND hControl = (HWND) lParam;
  4. HDC hDC = (HDC) wParam;
  5. int iControl = GetDlgCtrlID(hControl);
  6. int itxtMessage = GetDlgCtrlID(txtMessage);
  7.  
  8. if ( iControl == itxtMessage )
  9. {
  10. SetBkMode((HDC)wParam,OPAQUE);
  11. SetTextColor((HDC)wParam,(COLORREF)RGB(255,255,255));
  12. SetBkColor((HDC)wParam,(COLORREF)RGB(0,0,0));
  13. }
  14.  
  15. }
This is the code I have up to now.. I couldnt get the switch case to work like that but the above seems to work fine.. this is the output..

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.
Reputation Points: 23
Solved Threads: 5
Posting Whiz in Training
bops is offline Offline
214 posts
since Aug 2005
Jun 8th, 2007
0

Re: GUI colouring

Click to Expand / Collapse  Quote originally posted by bops ...
...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?
what are are you returning? you need to return a handle to the brush with which the control background would be painted. eg. for a black background:
return LRESULT( GetStockObject( BLACK_BRUSH ) ) ;
Last edited by vijayan121; Jun 8th, 2007 at 11:10 am.
Reputation Points: 1159
Solved Threads: 285
Posting Virtuoso
vijayan121 is offline Offline
1,606 posts
since Dec 2006
Jun 8th, 2007
0

Re: GUI colouring

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
Last edited by bops; Jun 8th, 2007 at 11:44 am.
Reputation Points: 23
Solved Threads: 5
Posting Whiz in Training
bops is offline Offline
214 posts
since Aug 2005
Jun 8th, 2007
0

Re: GUI colouring

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.
Reputation Points: 1159
Solved Threads: 285
Posting Virtuoso
vijayan121 is offline Offline
1,606 posts
since Dec 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Simulate Mouse Move
Next Thread in C++ Forum Timeline: What's the meaning of the "return" here?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC