Solved

Cannot get Static Picture Control (Rectangle Mode) to change color

Posted on 2004-08-20
4
1,864 Views
Last Modified: 2013-11-20
Hey Everybody;

I am currently trying to get Static Picture Control in Rectangle Mode to Change colors from the default black, grey, or white. Here is my code to do so,

HBRUSH Cdev_discDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
      HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
      switch (nCtlColor) {
            case CTLCOLOR_STATIC:
                   if (pWnd->GetDlgCtrlID() == IDC_myRectangle)
                   {
                         pDC->SetBkMode(OPAQUE);
                         pDC->SetBkColor(RGB(150, 150, 150));
                         return m_pBkBrush;
                   }
                   else
                   {
                        pDC->SetTextColor(RGB(54, 51, 127));
                        pDC->SetBkColor(RGB(209, 208, 226));
                   }
                  // Drop through to return the background brush.
            case CTRLCOLOR_DLG:
                  return m_pBkBrush;

            default:
                   return m_pBkBrush;
       }
   }

Now the function works correctly for all other controls. I can get it to change the dialogue and other static controls but not the rectangle. Any help would be greatly appreciated
0
Comment
Question by:komeisa
4 Comments
 
LVL 48

Expert Comment

by:AlexFM
Comment Utility
OnCtlColor function may be used to change dialog controls color and text parameters. To change control reclangle you need MoveWindow function. To get current dialog control rectangle, use GetWindowRect function. OnCtlColor is not a good place to move the controls. You may do this in any other place of the program. For example, after pressing of some button you want to move some dialog control:

void CMyDialog::OnSomeButton()
{
    CRect rect;
    CWnd* pWnd = GetDlgItem(IDC_CONTROL_ID);
    pWnd->GetWindowRect(&rect);
    ScreenToClient(&rect);
    rect.left += 10;
    rect.right += 10;
    pWnd->MoveWindow(&rect);
}
0
 
LVL 9

Accepted Solution

by:
Priyesh earned 200 total points
Comment Utility
SS_xxxRECT style does not seem to work with CtlColor. Not sure why that is.

Tried making a derived class from CStatic and handling the OnPaint like this. Seems to work.
Has the added advantage of having the color as a member in this class.

void CMyStatic::OnPaint()
{
      CPaintDC dc(this) ;
      CRect rc ;
      GetClientRect(rc) ;
      dc.FillSolidRect(rc, RGB(0, 0, 255)) ;
}

HTH
0
 

Expert Comment

by:AnashPO
Comment Utility
Hi,
   My guess is that SS_xxxRECT style won't work with CtlColor. The following link may explain why.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnwui/html/msdn_statictl.asp

SS_BLACKRECT
The SS_BLACKRECT static control displays a frame around the control and fills the control with a solid color that is the same color as the Window Frame (COLOR_WINDOWFRAME). The default Windows color is black. The user can change the color by changing the window frame color in the Control Panel, or an application can call SetSysColors.

Obviously, a static with SS_BLACKRECT seems not relying on the brush passed from CtlColor, instead, it fills the rect with COLOR_WINDOWFRAME. I would also suggest the solution as given by Priyesh.

Any info which shows how this can be done as per given in the question would be appreciated.

AnashPO
0
 
LVL 6

Expert Comment

by:nabehs
Comment Utility
Handle WM_ERASEBKGND and paint the background

BOOL CMyStatic::OnEraseBkgnd(CDC* pDC)
{

    CRect rcBounds;
    GetClientRect(&rcBounds);
    pDC->FillSolidRect(rcBounds, RGB(255, 0, 0)); // FILL WITH RED

    return TRUE;
}
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now