CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 13 of 13

Threaded View

  1. #6
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,244

    Re: Resource Exception

    Just an aside remark and advice.
    No offense, your CXRayFIDlg::OnCtlColor looks like a mess, then it's no wonder that you can easily make mistakes in it. Too much code, too many conditions, too many hardcoded color values. What about if you have tens of dialogs with tens of colored controls? And what about if in other places you need different colors and/or you have to change colors at run-time? Normally, will result a triple mess...

    First to note: MFC "reflects" WM_CTLCOLOR notification messages to the child controls. That allows handling them in a more object-oriented manner.
    So, much more reliable, flexible and easy to maintain is to derive your own control class (ex. CColoredStatic derived from CStatic) an handle reflected WM_CTLCOLOR messages.

    Here is an example of "colored" static control class:
    Code:
    // ColoredStatic.h
    
    class CColoredStatic : public CStatic
    {
       DECLARE_DYNAMIC(CColoredStatic)
    
    // Construction
    public:
       CColoredStatic();
    
    // Attributes
    private:
       int m_nBkMode;
       COLORREF m_crTextColor;
       COLORREF m_crBkColor;
       CBrush m_brush;
    
    // Operations
    public:
       void SetBkMode(int nValue);
       void SetTextColor(COLORREF crValue);
       void SetBkColor(COLORREF crValue);
    
    // Message handlers
    protected:
       DECLARE_MESSAGE_MAP()
       afx_msg HBRUSH CtlColor(CDC* pDC, UINT nCtlColor);
    };
    Code:
    // ColoredStatic,cpp
    
    #include "stdafx.h"
    #include "ColoredStatic.h"
    
    // CColoredStatic
    IMPLEMENT_DYNAMIC(CColoredStatic, CStatic)
    
    CColoredStatic::CColoredStatic() 
    : m_nBkMode(OPAQUE),
      m_crTextColor(::GetSysColor(COLOR_WINDOWTEXT)),
      m_crBkColor(::GetSysColor(COLOR_BTNFACE))
    {
    }
    
    void CColoredStatic::SetBkMode(int nValue)
    {
       ASSERT((OPAQUE == nValue) || (TRANSPARENT == nValue));
       m_nBkMode = nValue;
       Invalidate();
    }
    
    void CColoredStatic::SetTextColor(COLORREF crValue)
    {
       m_crTextColor = crValue;
       Invalidate();
    }
    
    void CColoredStatic::SetBkColor(COLORREF crValue)
    {
       m_crBkColor = crValue;
       Invalidate();
    }
    
    BEGIN_MESSAGE_MAP(CColoredStatic, CStatic)
       ON_WM_CTLCOLOR_REFLECT()
    END_MESSAGE_MAP()
    
    // CColoredStatic message handlers
    
    HBRUSH CColoredStatic::CtlColor(CDC* pDC, UINT nCtlColor)
    {
       if(NULL != m_brush.GetSafeHandle())
          m_brush.DeleteObject();
    
       m_brush.CreateSolidBrush(m_crBkColor);
    
       pDC->SetBkMode(m_nBkMode);
       pDC->SetTextColor(m_crTextColor);
       pDC->SetBkColor(m_crBkColor);
    
       return m_brush;
    }
    All you have now to do in the parent dialog class is to include "ColoredStatic.h" and replace CStatic with CColoredStatic then call the methods for setting the color atributes whenever is necessary.
    Example:
    Code:
    #include "ColoredStatic.h"
    
    class CWhateverDialog : public CDialog
    {
       CColoredStatic m_static1;
       CColoredStatic m_static2;
       // ...
    };
    
    BOOL CWhateverDialog ::OnInitDialog()
    {
       CDialog::OnInitDialog();
    
       m_static1.SetBkMode(TRANSPARENT);
       m_static1.SetTextColor(RGB(0,0,255));
       
       m_static2.SetTextColor(RGB(255,255,255));
       m_static2.SetBkColor(RGB(255,0,0));
       
       // ...
    
       return TRUE;
    }
    In the same way you can proceed for other control types (edit and so on).
    It takes no much time to implement and test such class, once and forever, then easily use it anywhere is needed.
    Last edited by ovidiucucu; January 10th, 2013 at 05:39 AM.
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

Tags for this Thread

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