c++mfcmfc-feature-pack

How to change the color of a property in CMFCPropertyGridCtrl


How can I change color of a property(single property among all other properties) in CMFCPropertyGridCtrl?


Solution

  • You need to derive a class from the CMFCPropertyGridCtrl class, and override the CMFCPropertyGridCtrl::OnDrawProperty method. This allows you to change the device context to your liking prior to calling the default implementation:

    class CMFCMyPropertyGridCtrl : public CMFCPropertyGridCtrl {
    
    public:
        virtual int OnDrawProperty( CDC * pDC, CMFCPropertyGridProperty* pProp ) const {
            // Implement check to see, if this is the property we are looking for
            // If it is, set an appropriate text color
            pDC->SetTextColor( RGB( 0xff, 0x0, 0xff ) );
    
            // Call the default implementation to perform rendering
            return CMFCPropertyGridCtrl::OnDrawProperty( pDC, pProp );
        }
    };