944,209 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 11010
  • C RSS
Jun 20th, 2005
0

changing color of a pushbutton on a dialog box

Expand Post »
Hi,

I want to change the color of my button during runtime..

i am writing the following code inthe OnCtlColor of my dialog box

  1. HBRUSH CPIVSyncDlg::OnCtlColor(CDC *pdc,CWnd *pwnd,UINT nCtlColor)
  2. {
  3. HBRUSH hbr = CDialog::OnCtlColor(pdc,pwnd,nCtlColor);
  4.  
  5. switch(nCtlColor)
  6. {
  7. case CTLCOLOR_BTN:
  8. if(pwnd->GetDlgCtrlID() == IDC_STARTSTOP)
  9. pdc->SetBkColor(RGB(255,0,0));
  10. return (HBRUSH)m_btbrush.GetSafeHandle();
  11. case CTLCOLOR_DLG:
  12. pdc->SetBkColor(RGB(255,255,255));
  13. pdc->SetBkMode(TRANSPARENT);
  14. return (HBRUSH)m_hbrush.GetSafeHandle();
  15. case CTLCOLOR_EDIT:
  16. pdc->SetBkColor(RGB(0,0,0));
  17. pdc->SetBkMode(TRANSPARENT);
  18. return (HBRUSH)m_hbrush.GetSafeHandle();
  19. default:
  20. return CDialog::OnCtlColor(pdc,pwnd,nCtlColor);
  21. }
  22. return hbr;
  23. //return (HBRUSH)m_hbrush.GetSafeHandle();
  24. }

the button is IDC_STARTSTOP i want to turn it red..but the interesting thing is the whole dialog box turns red except the buttons...

i have searched the msdn library and I am following all the instruction given in example ...all in vein... any suggestion would be helpful..

thanks,
Hardik
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
rangalo is offline Offline
1 posts
since Jun 2005
Jul 19th, 2005
0

Re: changing color of a pushbutton on a dialog box

the method in "How do I change the background color of a control?" will not work for buttons!

If you want to change the color of a dialog button, you have to use an owner-draw button. (You can use bitmap buttons.) Changing the color through OnCtlColor() will not work for buttons. The following Knowledge Base articles may be of help to you: Q32685, "Using the WM_CTLCOLOR Message,"



Quote originally posted by rangalo ...
Hi,

I want to change the color of my button during runtime..

i am writing the following code inthe OnCtlColor of my dialog box

  1. HBRUSH CPIVSyncDlg::OnCtlColor(CDC *pdc,CWnd *pwnd,UINT nCtlColor)
  2. {
  3. HBRUSH hbr = CDialog::OnCtlColor(pdc,pwnd,nCtlColor);
  4.  
  5. switch(nCtlColor)
  6. {
  7. case CTLCOLOR_BTN:
  8. if(pwnd->GetDlgCtrlID() == IDC_STARTSTOP)
  9. pdc->SetBkColor(RGB(255,0,0));
  10. return (HBRUSH)m_btbrush.GetSafeHandle();
  11. case CTLCOLOR_DLG:
  12. pdc->SetBkColor(RGB(255,255,255));
  13. pdc->SetBkMode(TRANSPARENT);
  14. return (HBRUSH)m_hbrush.GetSafeHandle();
  15. case CTLCOLOR_EDIT:
  16. pdc->SetBkColor(RGB(0,0,0));
  17. pdc->SetBkMode(TRANSPARENT);
  18. return (HBRUSH)m_hbrush.GetSafeHandle();
  19. default:
  20. return CDialog::OnCtlColor(pdc,pwnd,nCtlColor);
  21. }
  22. return hbr;
  23. //return (HBRUSH)m_hbrush.GetSafeHandle();
  24. }

the button is IDC_STARTSTOP i want to turn it red..but the interesting thing is the whole dialog box turns red except the buttons...

i have searched the msdn library and I am following all the instruction given in example ...all in vein... any suggestion would be helpful..

thanks,
Hardik
Reputation Points: 10
Solved Threads: 0
Newbie Poster
gobigred is offline Offline
1 posts
since Jul 2005
Jan 3rd, 2009
0

Re: changing color of a pushbutton on a dialog box

What is here m_btbrush and m_hbrush ?

I got an error with this code like this :

error C2228: left of '.GetSafeHandle' must have class/struct/union
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mitulgolakiya is offline Offline
3 posts
since Jun 2008

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: variable length array may not be initialized: error
Next Thread in C Forum Timeline: Controlling Floppy Disk Head using C





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


Follow us on Twitter


© 2011 DaniWeb® LLC