c++ - 如何在 CPropertySheet 中定义新颜色?

标签 c++ mfc cpropertysheet cmfcpropertypage

我正在尝试在 CPropertySheet(mfc 库)的某些区域中定义新颜色。我尝试的是重载 OnCtlColor 并定义新的背景颜色。这种方法效果很好,但它不会为我想要的区域着色。

在下一张图片中,您可以看到我用我的方法得到了什么。

Image of the control

在此图像中,您可以看到 4 个彩色区域:

  1. 红色:我可以使用 OnCtlColor
  2. 着色的区域
  3. 深灰色和黑色:我可以使用 OnCtlColor 着色的区域 对象 CPropertyPage
  4. 浅灰色(用蓝色箭头表示):我要着色的区域
  5. 白边:我也想着色的区域。

我不知道如何使用此库或使用任何可自定义对象为所有区域着色。任何帮助将不胜感激。

谢谢!

更新 1

在 Adrian 的回答之后它看起来像 this

但是,仍然有一个区域我们无法着色。

回答

在尝试很多组合之前,我已经完成了接下来的两个对象,它们允许我定义我需要的颜色。您可以在后面找到所有源代码。这段代码的结果可以在 this 中查看图片

属性页

标题

class CustomPropertyPage : public CPropertyPage
{   
    public:
        static const COLORREF PROPERTYPAGE_BACKGROUND = RGB(68, 74, 80);
        DECLARE_MESSAGE_MAP()

    public:
        CustomPropertyPage(UINT nIDTemplate);
        afx_msg HBRUSH OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor);
};

cpp

CustomPropertyPage::CustomPropertyPage(UINT nIDTemplate) : CPropertyPage(nIDTemplate)
{
}

BEGIN_MESSAGE_MAP(CustomPropertyPage, CPropertyPage)
    ON_WM_CTLCOLOR()
END_MESSAGE_MAP()

HBRUSH CustomPropertyPage::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
    if (pWnd->GetDlgCtrlID() != 0)
        return CPropertyPage::OnCtlColor(pDC, pWnd, nCtlColor);

    HBRUSH hbr = CreateSolidBrush(PROPERTYPAGE_BACKGROUND_COLOR);
    return hbr;
}

属性表

标题

class CustomPropertySheet : public CPropertySheet
{
    DECLARE_MESSAGE_MAP()

    public:
        CustomPropertySheet(UINT nIDCaption, CWnd* pParentWnd, UINT iSelectPage);
        virtual BOOL OnInitDialog();
        afx_msg void OnDrawItem(int nIDCtl, LPDRAWITEMSTRUCT lpDrawItemStruct);
        afx_msg HBRUSH OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor);

    private:
        void Draw_Background(CDC *pDC);
};

cpp

CustomPropertySheet::CustomPropertySheet(UINT nIDCaption, CWnd* pParentWnd, UINT iSelectPage) : CPropertySheet(nIDCaption, pParentWnd, iSelectPage)
{
}

BEGIN_MESSAGE_MAP(CustomPropertySheet, CPropertySheet)
    ON_WM_CTLCOLOR()
    ON_WM_DRAWITEM()
END_MESSAGE_MAP()

BOOL CustomPropertySheet::OnInitDialog()
{
    BOOL answer = CPropertySheet::OnInitDialog();

    CWnd* pTab = GetDlgItem(AFX_IDC_TAB_CONTROL);
    SetWindowLongPtr(pTab->m_hWnd, GWL_STYLE, GetWindowLongPtr(pTab->m_hWnd, GWL_STYLE) | TCS_OWNERDRAWFIXED);
    pTab->RedrawWindow();

    return answer;
}

void CustomPropertySheet::OnDrawItem(int nIDCtl, LPDRAWITEMSTRUCT lpDrawItemStruct)
{
    if (nIDCtl == AFX_IDC_TAB_CONTROL)
    {
        CDC* pDC = CDC::FromHandle(lpDrawItemStruct->hDC);
        Draw_Background(pDC);

        CRect rc(lpDrawItemStruct->rcItem);
        rc.bottom += 1;
        pDC->FillSolidRect(rc, CEasyPropertyPage::PROPERTYPAGE_BACKGROUND);
        pDC->SetTextColor(GENERIC_TEXT_COLOR);
        pDC->SetBkMode(TRANSPARENT);

        char  text[256];
        TCITEM tci = { TCIF_TEXT | TCIF_STATE, 0, 0, text, 255, -1, 0 };
        HWND tcw = ::GetDlgItem(m_hWnd, nIDCtl);
        int i, tic = int(::SendMessage(tcw, TCM_GETITEMCOUNT, 0, 0));
        for (i = 0; i < tic; ++i) 
        {
            if (lpDrawItemStruct->itemState & ODS_SELECTED)
            {
                CRect tir;
                ::SendMessage(tcw, TCM_GETITEM, WPARAM(i), LPARAM(&tci));
                ::SendMessage(tcw, TCM_GETITEMRECT, WPARAM(i), LPARAM(&tir));
                pDC->DrawText(text, tir, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
            }
        }
    }

    else CPropertySheet::OnDrawItem(nIDCtl, lpDrawItemStruct);
}

HBRUSH CustomPropertySheet::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
    if (pWnd->GetDlgCtrlID() != 0)
        return CPropertySheet::OnCtlColor(pDC, pWnd, nCtlColor);

    HBRUSH hbr = CreateSolidBrush(GENERIC_BACKGROUND_COLOR);
    return hbr;
}

void CustomPropertySheet::Draw_Background(CDC* pDC)
{
    CRect rect; this->GetClientRect(rect);
    pDC->FillSolidRect(rect, GENERIC_BACKGROUND_COLOR);

    rect.DeflateRect(0, 20, 0, 0);
    pDC->FillSolidRect(rect, GENERIC_BORDER_COLOR);
}

最佳答案

要自定义浅灰色区域(这是嵌入式选项卡控件),您需要重写派生自CPropertySheet< 的类中的OnDrawItem 方法 并使用 AFX_IDC_TAB_CONTROL 标识符对控件进行自定义绘制。像这样:

void MyPropertySheet::OnDrawItem(int nID, LPDRAWITEMSTRUCT pDIS)
{   
    if (nID == AFX_IDC_TAB_CONTROL) {
        CDC* pDC = CDC::FromHandle(pDIS->hDC);
        CRect rc(pDIS->rcItem); rc.bottom += 1;
        pDC->FillSolidRect(rc, RGB(255, 0, 0)); // Or whatever b/g/ colour you want
        pDC->SetTextColor(RGB(0,0,0)); // Or whatever text colour you want
        pDC->SetBkMode(TRANSPARENT);
        char  text[256];
        TCITEM tci = { TCIF_TEXT | TCIF_STATE, 0, 0, text, 255, -1, 0 };
        CRect tir;
        HWND tcw = ::GetDlgItem(m_hWnd, nID);
        int i, tic = int(::SendMessage(tcw, TCM_GETITEMCOUNT, 0, 0));
        for (i = 0; i < tic; ++i) {
            ::SendMessage(tcw, TCM_GETITEM, WPARAM(i), LPARAM(&tci));
            ::SendMessage(tcw, TCM_GETITEMRECT, WPARAM(i), LPARAM(&tir));
            if (pDIS->itemState & ODS_SELECTED)
                pDC->DrawText(text, tir, DT_CENTER |DT_VCENTER | DT_SINGLELINE);
        }
        pDC->Detach();
    }
    else { // Pass other stuff to the base class
        CPropertySheet::OnDrawItem(nID, pDIS);
    }
    return;
}

当然,消息映射一定要加上ON_WM_DRAWITEM()!

编辑:您还必须显式设置嵌入式选项卡控件的样式以包含TCS_OWNERDRAWFIXED。您可以在类的 OnInitDialog 覆盖中执行此操作。

编辑 2:现在,我有一个更好的方法来获取指向选项卡控件的指针!此外,我添加了几行代码作为“作弊”来解决需要着色的剩余区域 - 通过扩展选项卡以适应底层控件的宽度 ...

BOOL MyPropertySheet::OnInitDialog()
{
    BOOL answer = CPropertySheet::OnInitDialog(); // Call base class first!
    // ... whatever other stuff you may wish to do
//  CWnd* pTab = GetDlgItem(AFX_IDC_TAB_CONTROL);
    CTabCtrl* pTab = GetTabControl(); // This is a bit clearer than above line!
    // The following 4 lines comprise a 'first stab' at fixing the remaining issue:
    CRect rcTab; pTab->GetWindowRect(&rcTab);
    int nItems = pTab->GetItemCount();
    int border = GetSystemMetrics(SM_CXEDGE) * 2;
    pTab->SetMinTabWidth((rcTab.Width() - border) / nItems);
    // ...
    SetWindowLongPtr(pTab->m_hWnd, GWL_STYLE, GetWindowLongPtr(pTab->m_hWnd, GWL_STYLE) | TCS_OWNERDRAWFIXED);
    pTab->RedrawWindow();
    return answer;
}

随时要求进一步澄清和/或解释。

关于c++ - 如何在 CPropertySheet 中定义新颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58540250/

相关文章:

c++ - Windows MFC 对话框在无效时闪烁

mfc - 如何删除 MFC 中 CPropertySheet 中的默认帮助按钮?

c++ - 如何对 CPropertySheet 内的 CPropertyPage 重新排序

c++ - 引用与设置局部变量

c++ - 为什么我必须为文件末尾的 seekg 提供负两个偏移量

c++ - 为什么 `clang-format`和 `git clang-format`的结果不同

c++ - STL std::map 的 MFC 等价物

c++ - OnVScroll:是从CSpinButtonCtrl还是Vertical ScrollBar调用?

c++ - 如何在基于 CMake 的项目中使用 Boost.Test?