CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Sep 2010
    Posts
    5

    Arrow Color for static text control

    I have a dialog with a static text control
    I erase the dialog background (OnearaseBkgrn()) with some color
    But the text control's background is not erased.
    I'd like to make it the same as the dialog's background color

    How can I do this ?

    Thanks

  2. #2
    Join Date
    May 2009
    Location
    Bengaluru, India
    Posts
    460

    Re: Color for static text control

    try to get the handle of the static control explicitly and then change the background color...

  3. #3
    Join Date
    Sep 2010
    Posts
    5

    Re: Color for static text control

    Thanks,
    I have tried
    ::setbkColor(Getdlgitem(IDC_TXT)->getdc()->m_hdc,RGB(140,141,142));

    but I don't see any change at all

  4. #4
    Join Date
    Oct 2009
    Posts
    577

    Smile Re: Color for static text control

    Overload the OnCtlColor for your dialog. It will be called when a child window will be drawn. You may return the brush that has the required background color after you identified your static control.

  5. #5
    Join Date
    May 2005
    Location
    Oregon
    Posts
    3,725

    Re: Color for static text control

    Handle WM_CTLCOLOR inside your application .For more detail have a look in MSDN.

    Thanks

  6. #6
    Join Date
    Jun 2004
    Posts
    1,352

    Re: Color for static text control

    Try this:



    Code:
    HBRUSH MyDialog::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) 
    {
    	HBRUSH hbr = CFormView::OnCtlColor(pDC, pWnd, nCtlColor);
    	
    	// TODO: Change any attributes of the DC here
    	
       //If you just want one static control
       if(pWnd->GetDlgCtrlID() == IDC_STATIC_MY_STATIC_CONTROL)
       {
          // yes, change text color
          pDC->SetTextColor(RGB(0,0,255));
       }
    
       //or
    
       //if you want all the static controls. 
    
       if(nCtlColor == CTLCOLOR_STATIC){
    		pDC->SetTextColor(RGB(0,0,255));
    
       }
    
    
    	// TODO: Return a different brush if the default is not desired
    	return hbr;
    }

    Select one or the other if statements depending on your situation.

    I think you mentioned that you wanted only the background color, if that is the case then use the following command.

    Code:
    pDC->SetBkColor(RGB(0,0,255));
    Last edited by ADSOFT; September 16th, 2010 at 10:59 AM.
    Rate this post if it helped you.

  7. #7
    Join Date
    Oct 2009
    Posts
    577

    Smile Re: Color for static text control

    Quote Originally Posted by ADSOFT View Post
    Try this:

    Code:
    HBRUSH MyDialog::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) 
    {
    	HBRUSH hbr = CFormView::OnCtlColor(pDC, pWnd, nCtlColor);
    	
    	// TODO: Change any attributes of the DC here
    	
       //If you just want one static control
       if(pWnd->GetDlgCtrlID() == IDC_STATIC_MY_STATIC_CONTROL)
       {
          // yes, change text color
          pDC->SetTextColor(RGB(0,0,255));
       }
    
       //or
    
       //if you want all the static controls. 
    
       if(nCtlColor == CTLCOLOR_STATIC){
    		pDC->SetTextColor(RGB(0,0,255));
    
       }
    
    
    	// TODO: Return a different brush if the default is not desired
    	return hbr;
    }
    Select one or the other if statements depending on your situation.
    Don't think the HBRUSH should be retrieved by calling CFormView::OnCtlColor. Even if the MyDialog was derived from CFormView (what is doubtful) the On... functions are handler functions for messages and rarely should be misused for other purposes. Instead you can use a brush where you hold the handle as member in your dialog class and which was created at first use like that:


    Code:
    HBRUSH MyDialog::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) 
    {
         if (pWnd == GetDlgItem(IDC_MY_STATIC))
         {
            if (m_hbrStatic == NULL)
            {
               m_hbrStatic = ::CreateSolidBrush(RGB(100, 120, 140));
            }
            return m_hbrStatic; 	
        }
        return (HBRUSH)(*pDC->GetCurrentBrush());
    }
    The m_hbrStatic can be a private member of type HBRUSH in your dialog class. You can free the m_hbrStatic by calling DeleteObject in the EndDialog member of your dialog or in the destructor.

  8. #8
    Join Date
    Jun 2004
    Posts
    1,352

    Re: Color for static text control

    HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
    I tested it on my FormView but the statement should be as above

    This is the edited version

    Code:
    HBRUSH MyDialog::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) 
    {
    	HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
    	
    	// TODO: Change any attributes of the DC here
    	
       //If you just want one static control
       if(pWnd->GetDlgCtrlID() == IDC_STATIC_MY_STATIC_CONTROL)
       {
          // yes, change text color
          pDC->SetTextColor(RGB(0,0,255));
       }
    
       //or
    
       //if you want all the static controls. 
    
       if(nCtlColor == CTLCOLOR_STATIC){
    		pDC->SetTextColor(RGB(0,0,255));
    
       }
    
    
    	// TODO: Return a different brush if the default is not desired
    	return hbr;
    }

    The first on is if you want to work with a FormView, change MyDialog to MyFormview.
    Rate this post if it helped you.

  9. #9
    Join Date
    Jun 2004
    Posts
    1,352

    Re: Color for static text control

    Quote Originally Posted by itsmeandnobodyelse View Post


    Code:
    HBRUSH MyDialog::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) 
    {
         if (pWnd == GetDlgItem(IDC_MY_STATIC))
         {
            if (m_hbrStatic == NULL)
            {
               m_hbrStatic = ::CreateSolidBrush(RGB(100, 120, 140));
            }
            return m_hbrStatic; 	
        }
        return (HBRUSH)(*pDC->GetCurrentBrush());
    }
    The m_hbrStatic can be a private member of type HBRUSH in your dialog class. You can free the m_hbrStatic by calling DeleteObject in the EndDialog member of your dialog or in the destructor.

    To much work, have to create member variables and deleting objects.

    I prefer to just look up the Static's ID and let the FRAMEWORK handle the rest. Thats how OnCtlColor was intened to be used. The framework handles all the details.
    Rate this post if it helped you.

  10. #10
    Join Date
    May 2005
    Location
    Oregon
    Posts
    3,725

    Re: Color for static text control

    Quote Originally Posted by ADSOFT View Post
    To much work, have to create member variables and deleting objects.

    I prefer to just look up the Static's ID and let the FRAMEWORK handle the rest. Thats how OnCtlColor was intened to be used. The framework handles all the details.
    It's always good to create object in constructor and delete it in destructor .try to avoid creating brush under your WM_CTLCOLOR it will just keep creating new brush every time without deleting them .And you can see performance issue or if application is big may be it can hang your application too.You don't have to do lots of work.

    Thanks
    Last edited by humptydumpty; September 16th, 2010 at 03:03 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured