GUI colouring

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Aug 2005
Posts: 188
Reputation: bops is an unknown quantity at this point 
Solved Threads: 3
bops bops is offline Offline
Junior Poster

GUI colouring

 
0
  #1
Jun 6th, 2007
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...

  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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,089
Reputation: vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all 
Solved Threads: 164
vijayan121 vijayan121 is offline Offline
Veteran Poster

Re: GUI colouring

 
0
  #2
Jun 6th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 188
Reputation: bops is an unknown quantity at this point 
Solved Threads: 3
bops bops is offline Offline
Junior Poster

Re: GUI colouring

 
0
  #3
Jun 6th, 2007
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..
  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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,089
Reputation: vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all 
Solved Threads: 164
vijayan121 vijayan121 is offline Offline
Veteran Poster

Re: GUI colouring

 
0
  #4
Jun 7th, 2007
Originally Posted by bops View Post
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 188
Reputation: bops is an unknown quantity at this point 
Solved Threads: 3
bops bops is offline Offline
Junior Poster

Re: GUI colouring

 
0
  #5
Jun 7th, 2007
Originally Posted by vijayan121 View Post
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..
  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. }

  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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,089
Reputation: vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all 
Solved Threads: 164
vijayan121 vijayan121 is offline Offline
Veteran Poster

Re: GUI colouring

 
0
  #6
Jun 7th, 2007
Originally Posted by bops View Post
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:
  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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 188
Reputation: bops is an unknown quantity at this point 
Solved Threads: 3
bops bops is offline Offline
Junior Poster

Re: GUI colouring

 
0
  #7
Jun 8th, 2007
  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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,089
Reputation: vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all 
Solved Threads: 164
vijayan121 vijayan121 is offline Offline
Veteran Poster

Re: GUI colouring

 
0
  #8
Jun 8th, 2007
Originally Posted by bops View Post
...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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 188
Reputation: bops is an unknown quantity at this point 
Solved Threads: 3
bops bops is offline Offline
Junior Poster

Re: GUI colouring

 
0
  #9
Jun 8th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,089
Reputation: vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all 
Solved Threads: 164
vijayan121 vijayan121 is offline Offline
Veteran Poster

Re: GUI colouring

 
0
  #10
Jun 8th, 2007
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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC