ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

MFC CListCtrl LVS_ICON风格的自绘

2021-09-15 21:36:29  阅读:306  来源: 互联网

标签:bEnable MFC 自绘 CListCtrl CListCtrlEx item rItem pDC rcItem


转载:https://bbs.csdn.net/topics/100120508

1.对CListCtrl的自绘,就要在NM_CUSTOMDRAW自绘

 

 

CListCtrlEx.h

class CListCtrlEx : public CListCtrl
{
    DECLARE_DYNAMIC(CListCtrlEx)

public:
    CListCtrlEx();
    virtual ~CListCtrlEx();

protected:
    DECLARE_MESSAGE_MAP()
public:
    afx_msg void OnNMCustomdraw(NMHDR *pNMHDR, LRESULT *pResult);
    afx_msg void OnEnable(BOOL bEnable);

private:
    BOOL m_bEnable;
};

CListCtrlEx.cpp

// CListCtrlEx

IMPLEMENT_DYNAMIC(CListCtrlEx, CListCtrl)

CListCtrlEx::CListCtrlEx()
    :m_bEnable(TRUE)
{

}

CListCtrlEx::~CListCtrlEx()
{
}


BEGIN_MESSAGE_MAP(CListCtrlEx, CListCtrl)
    ON_NOTIFY_REFLECT(NM_CUSTOMDRAW, &CListCtrlEx::OnNMCustomdraw)
    ON_WM_ENABLE()
END_MESSAGE_MAP()



// CListCtrlEx 消息处理程序




void CListCtrlEx::OnNMCustomdraw(NMHDR *pNMHDR, LRESULT *pResult)
{
    NMLVCUSTOMDRAW* pLVCD = reinterpret_cast<NMLVCUSTOMDRAW*>(pNMHDR);
    // TODO: 在此添加控件通知处理程序代码
    *pResult = 0;

    if (CDDS_PREPAINT == pLVCD->nmcd.dwDrawStage)
    {
        *pResult = CDRF_NOTIFYITEMDRAW;
    }
    else if (CDDS_ITEMPREPAINT == pLVCD->nmcd.dwDrawStage)
    {
        // This is the beginning of an item's paint cycle.
        LVITEM rItem;
        int nItem = static_cast<int>(pLVCD->nmcd.dwItemSpec);//当前项
        CDC* pDC = CDC::FromHandle(pLVCD->nmcd.hdc);
        COLORREF crBkgnd;
        BOOL bListHasFocus;
        CRect rcItem;
        CRect rcText;
        CString sText;
        UINT uFormat;

        POSITION pos = GetFirstSelectedItemPosition();
        int index = GetNextSelectedItem(pos);

        bListHasFocus = nItem == index;//有item选中

        // Get the image index and selected/focused state of the
        // item being drawn.
        ZeroMemory(&rItem, sizeof(LVITEM));
        rItem.mask = LVIF_IMAGE | LVIF_STATE;
        rItem.iItem = nItem;
        rItem.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
        GetItem(&rItem);

        // Get the rect that holds the item's icon.
        GetItemRect(nItem, &rcItem, LVIR_ICON);

        // Draw the icon.
        uFormat = ILD_TRANSPARENT;

        if ((rItem.state & LVIS_SELECTED) && bListHasFocus)
            uFormat |= ILD_FOCUS;//item图片有焦点,图片是选中状态

        GetImageList(LVSIL_NORMAL)->Draw(pDC, rItem.iImage, rcItem.TopLeft(), uFormat);//绘制图片


        // Get the rect that bounds the text label.
        GetItemRect(nItem, rcItem, LVIR_LABEL); //获取文字区域


        // Draw the background of the list item. Colors are selected
        // according to the item's state.
        if (m_bEnable)//控件可用
        {
            if (bListHasFocus)
            {
                crBkgnd = GetSysColor(COLOR_HIGHLIGHT);
                pDC->SetTextColor(GetSysColor(COLOR_HIGHLIGHTTEXT));
            }
            else
            {
                crBkgnd = GetSysColor(COLOR_BTNFACE);
                pDC->SetTextColor(GetSysColor(COLOR_BTNTEXT));
            }

            // Draw a focus rect around the item if necessary.
            if (bListHasFocus)
            {
                pDC->DrawFocusRect(rcItem);
            }
        }
        else//控件禁用
        {
            crBkgnd = GetSysColor(COLOR_WINDOW);
            pDC->SetTextColor(GetSysColor(COLOR_GRAYTEXT));
        }

        // Draw the background & prep the DC for the text drawing. Note
        // that the entire item RECT is filled in, so this emulates the full-
        // row selection style of normal lists.
        pDC->FillSolidRect(rcItem, crBkgnd);
        pDC->SetBkMode(TRANSPARENT);


        // Tweak the rect a bit for nicer-looking text alignment.
        rcText = rcItem;
        // Draw the text.
        sText = GetItemText(nItem, 0);

        pDC->DrawText(sText, CRect::CRect(rcText.left , rcText.top, rcText.right, rcText.bottom ), DT_CENTER);//绘制文字

    

        *pResult = CDRF_SKIPDEFAULT; // We've painted everything. 完全自绘,屏蔽系统
    }
}


void CListCtrlEx::OnEnable(BOOL bEnable)
{
    CListCtrl::OnEnable(bEnable);

    // TODO: 在此处添加消息处理程序代码
    m_bEnable = bEnable;//控件禁用False;可用True
}

 

标签:bEnable,MFC,自绘,CListCtrl,CListCtrlEx,item,rItem,pDC,rcItem
来源: https://www.cnblogs.com/chechen/p/15290952.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有