CMFCPropertyGridCtrl更改时如何更改颜色?

时间:2018-05-17 05:52:42

标签: c++ winapi mfc

我想在更改项目时更改文本颜色。我使用的是CMFCPropertyGridCtrlCMFCPropertyGridProperty

像我想要的this image更改时应该更改属性颜色。

这是CMFCPropertyGridCtrl类:

class CResizablePropertyGridCtrl : public CMFCPropertyGridCtrl
{
public:

    CResizablePropertyGridCtrl()
        : m_main_dialog(nullptr)
    {}
    bool flag = false;
    void setMainDialog(SelectPlugins* main_dialog)
    {
        m_main_dialog = main_dialog;
    }

    void SetLeftColumnWidth(int cx)
    {
        m_nLeftColumnWidth = cx;
        AdjustLayout();
    }

virtual void OnPropertyChanged(CMFCPropertyGridProperty* pProp) const;

private:
    SelectPlugins* m_main_dialog;
};

这是CDialogEx类:

class SelectPlugins : public CDialogEx
{

    DECLARE_DYNAMIC(SelectPlugins)

public:
    SelectPlugins(bool upgrade = false);  
    virtual ~SelectPlugins();

    void setInstallConfiguration(CInstallConfiguration*);

    void setDatabase(CDatabaseAppW*);

    void setPluginConfiguration(const PluginConfiguration&);

    const PluginConfiguration& getPluginsToInstall() const;

    const AppDataManager::Plugin& _setSelection(const std::string& id, const std::string& value);
    void _clearSelection(const std::string& id);

    CMFCPropertyGridProperty* getOption(const std::string& id);

    enum { IDD = IDD_SELECTPLUGINS };

protected:
    virtual void DoDataExchange(CDataExchange* pDX);    
     HBRUSH OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor);
    DECLARE_MESSAGE_MAP()
public:
    virtual BOOL OnInitDialog();

    CResizablePropertyGridCtrl m_grid;

private:

    std::map<std::string,Plugin> m_available_plugins;

    bool m_upgrade;
    PluginConfiguration m_plugin_already_installed;
    bool isPluginAlreadyInstalled(const std::string& id);
    std::string getAlreadyInstalledVersion(const std::string& id);

    void refreshAutomaticDependencySelection();

    std::map<std::string, CMFCPropertyGridProperty*> m_id_to_option;

    std::set<std::string> m_mandatory_plugins;

public:

    CEdit m_status_edit;
    PluginConfiguration m_plugin_to_install;
};

这是CMFCPropertyGridProperty类:

class ToolTipPropertyGridProperty : public CMFCPropertyGridProperty
{
public:

    ToolTipPropertyGridProperty(const std::string& name)
        : CMFCPropertyGridProperty(name.c_str())
    {}


    ToolTipPropertyGridProperty(const CString& strName, const COleVariant& varValue, LPCTSTR lpszDescr = NULL, DWORD_PTR dwData = 0,
        LPCTSTR lpszEditMask = NULL, LPCTSTR lpszEditTemplate = NULL, LPCTSTR lpszValidChars = NULL)
        : CMFCPropertyGridProperty(strName, varValue, lpszDescr, dwData, lpszEditMask, lpszEditTemplate, lpszValidChars)
    {}


    virtual CString GetNameTooltip()
    {
        return m_name_tooltip;
    }

    virtual CString GetValueTooltip()
    {
        return m_value_tooltip;
    }

    void setNameTooltip(const std::string& tooltip)
    {
        m_name_tooltip = tooltip.c_str();
    }

    void setValueTooltip(const std::string& tooltip)
    {
        m_value_tooltip = tooltip.c_str();
    }

private:

    CString m_name_tooltip;
    CString m_value_tooltip;
};

我想在选择其他项目时更改属性的颜色。已尝试OnDrawColor()OnCtlColor()。但是没有工作。

class ToolTipPropertyGridProperty:public CMFCPropertyGridProperty { 公共:

ToolTipPropertyGridProperty(const std::string& name)
    : CMFCPropertyGridProperty(name.c_str())
{}


ToolTipPropertyGridProperty(const CString& strName, const COleVariant& varValue, LPCTSTR lpszDescr = NULL, DWORD_PTR dwData = 0,
    LPCTSTR lpszEditMask = NULL, LPCTSTR lpszEditTemplate = NULL, LPCTSTR lpszValidChars = NULL)
    : CMFCPropertyGridProperty(strName, varValue, lpszDescr, dwData, lpszEditMask, lpszEditTemplate, lpszValidChars)
{ }

// override
virtual CString GetNameTooltip()
{
    return m_name_tooltip;
}

virtual CString GetValueTooltip()
{
    return m_value_tooltip;
}
 HBRUSH OnCtlColor(CDC* pDC, UINT nCtlColor)
{

    HBRUSH br;

    if (CTLCOLOR_STATIC == nCtlColor)
    {
        pDC->SetTextColor(RGB(0xff, 0x0, 0xff));
        br = CreateSolidBrush(RGB(0, 255, 0));
        return br;
    }


    return CMFCPropertyGridProperty::OnCtlColor(pDC, nCtlColor);
}

void setNameTooltip(const std::string& tooltip)
{
    m_name_tooltip = tooltip.c_str();
}

void setValueTooltip(const std::string& tooltip)
{
    m_value_tooltip = tooltip.c_str();
}

私人:

CString m_name_tooltip;
CString m_value_tooltip;

};尝试!!但是当点击另一个属性时,最后选择的属性将变为默认颜色。

1 个答案:

答案 0 :(得分:0)

您可以派生自己的CMFCPropertyGridProperty课程。

您只需实施CMFCPropertyGridProperty::OnCtlColor并选择背景画笔和所需文字的颜色。