• ListCtrl控件


    一 CListCtrl类型

      LVS_EDITLABELS   

      LVS_OWNERDRAWFIXED

      LVS_REPORT

      LVS_SHOWSELALWAYS

      LVS_SINGLESEL

      LVS_SMALLICONw

      LVS_SORTASCENDING LVS_SORTDESCENDING

      LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES | LVS_EX_HEADERDRAGDROP

      (1)添加或删除风格nStyle |= 或 nStyle &~

           (2)设置扩展风格GetExtendStyle(); SetExtendStyle(DWORD dwStyle);

    二 动态创建带图标的ListCtrl控件

      (1)Create  

    CListCtrlEx *pListCtrl = new CListCtrlEx();
     pListCtrl->Create(WS_VISIBLE|LVS_REPORT|LVS_OWNERDRAWFIXED, CRect(20,20,300,300), this, IDC_LISTCTRL);

      (2)设置扩展风格GetExtendStyle()和GetExtendStyle()

    DWORD nStyle = pListCtrl->GetExtendedStyle();
     nStyle |= LVS_EX_GRIDLINES|LVS_EX_FULLROWSELECT|LVS_EX_HEADERDRAGDROP;
     pListCtrl->SetExtendedStyle(nStyle); 

      (3)初始化CImageList

    CBitmap bmp1,bmp2;
    bmp1.LoadBitmap(IDB_BITMAP1);
    bmp2.LoadBitmap(IDB_BITMAP2);
    m_ImageList.Create(16,16,ILC_COLOR32|ILC_MASK,4,4);
    m_ImageList.Add(&bmp1, RGB(255,255,255));
    m_ImageList.Add(&bmp2,RGB(255,255,255));
    pListCtrl->SetImageList(&m_ImageList, LVSIL_SMALL);

      (4)插入列,行

    pListCtrl->InsertColumn(0, "学号",LVCFMT_LEFT, 100);
    pListCtrl->InsertColumn(1, "姓名",LVCFMT_LEFT,100);
    pListCtrl->InsertColumn(2, "成绩",LVCFMT_LEFT,100);
    // 方法1
    pListCtrl->InsertItem(0, "1", 0);
    pListCtrl->SetItemText(0,1,"张三");
    pListCtrl->SetItemText(0,2,"100");
    方法2
    {
        LV_ITEM lvItem = {0};
        lvItem.mask = LVIF_TEXT|LVIF_IMAGE|LVIF_PARAM;
        lvItem.iItem = 1;
        lvItem.iSubItem = 0;
        lvItem.iImage = 1;
        lvItem.lParam = (LPARAM)0;
        lvItem.pszText="1";
        pListCtrl->InsertItem(&lvItem);
    }
    {
        LV_ITEM lvItem = {0};
        lvItem.mask = LVIF_TEXT;
        lvItem.iItem = 1;
        lvItem.iSubItem = 1;
        lvItem.lParam = 1;
        lvItem.pszText="张三";
        pListCtrl->SetItem(&lvItem);
    }
    {
        LV_ITEM lvItem = {0};
         lvItem.mask = LVIF_TEXT|LVIF_IMAGE;
        lvItem.iItem = 1;
        lvItem.iSubItem = 2;
        lvItem.iImage = 0;          // 特别测试该位置的iImage无效
        lvItem.pszText="100";
        pListCtrl->SetItem(&lvItem);
    }  

    三 自绘ListCtrl控件
      效果图

      

      代码实现

    #pragma once
    
    
    // CListCtrlEx
    #define IDC_ADDEDIT 1002
    class CListCtrlEx : public CListCtrl
    {
        DECLARE_DYNAMIC(CListCtrlEx)
    
    public:
        CListCtrlEx();
        virtual ~CListCtrlEx();
    
    protected:
        DECLARE_MESSAGE_MAP()
    public:
        afx_msg void OnNMClick(NMHDR *pNMHDR, LRESULT *pResult);
        afx_msg void OnLvnColumnclick(NMHDR *pNMHDR, LRESULT *pResult);
        afx_msg void OnNMDblclk(NMHDR *pNMHDR, LRESULT *pResult);
    public:
        virtual void DrawItem(LPDRAWITEMSTRUCT /*lpDrawItemStruct*/);
        void DrawRowICon(CDC *pDC, CRect &rc, int nRow);
    };
    View Code
    // ListCtrlEx.cpp : 实现文件
    //
    
    #include "stdafx.h"
    #include "mfcctrlstu.h"
    #include "ListCtrlEx.h"
    
    
    // CListCtrlEx
    
    IMPLEMENT_DYNAMIC(CListCtrlEx, CListCtrl)
    
    CListCtrlEx::CListCtrlEx()
    {
    
    }
    
    CListCtrlEx::~CListCtrlEx()
    {
    }
    
    
    BEGIN_MESSAGE_MAP(CListCtrlEx, CListCtrl)
        ON_NOTIFY_REFLECT(NM_CLICK, &CListCtrlEx::OnNMClick)
        ON_NOTIFY_REFLECT(LVN_COLUMNCLICK, &CListCtrlEx::OnLvnColumnclick)
        ON_NOTIFY_REFLECT(NM_DBLCLK, &CListCtrlEx::OnNMDblclk)
    END_MESSAGE_MAP()
    
    void CListCtrlEx::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
    {
        // 背景
        CDC dc;
        CRect rc = lpDrawItemStruct->rcItem;
        dc.Attach(lpDrawItemStruct->hDC);
        if (lpDrawItemStruct->itemState & ODS_SELECTED)
        {
            // 选中状态蓝底白字
            dc.FillSolidRect(&rc, GetSysColor(COLOR_HIGHLIGHT));
            dc.SetTextColor(GetSysColor(COLOR_HIGHLIGHTTEXT));
        }
        else
        {
            // 普通状态白底黑字
            //dc.FillSolidRect(&rc, GetSysColor(COLOR_WINDOW));
            dc.FillSolidRect(&rc, RGB(222,33,33));
            dc.SetTextColor(GetSysColor(COLOR_WINDOWTEXT));
        }
        
        int nRow = lpDrawItemStruct->itemID;
        CHeaderCtrl *pHeadCtrl = GetHeaderCtrl();
        int nColCount = pHeadCtrl->GetItemCount();
        CRect rcCell; // 注意单元格rcCell的处理
        for (int nCol = 0;nCol < nColCount; nCol++)
        {
            pHeadCtrl->GetItemRect(nCol, &rcCell);
            rcCell.top = rc.top;
            rcCell.bottom = rc.bottom;                            
            CString strText = GetItemText(nRow, nCol);
        
            // 图标
            if (!nCol)
            {
                DrawRowICon(&dc, rc, nRow);
                rcCell.left += 18;
            }
            
            // 文字
            dc.SetBkMode(TRANSPARENT);
            dc.DrawText(strText, &rcCell, DT_LEFT|DT_VCENTER|DT_SINGLELINE);
            //TRACE("%d-%d-%d-%d-%d-%d-%s
    ", nRow, nCol, rcCell.left, rcCell.top, rcCell.right,rcCell.bottom,strText);
        }
        
        dc.Detach();
    }
    
    void CListCtrlEx::DrawRowICon(CDC *pDC, CRect &rc, int nRow)
    {
        /*LV_ITEM lvi = {LVIF_IMAGE};
        GetItem(&lvi);*/
        CImageList *pImageList = GetImageList(LVSIL_SMALL);
        pImageList->Draw(pDC, nRow, rc.TopLeft(), ILD_TRANSPARENT);
    }
    
    void CListCtrlEx::OnLvnColumnclick(NMHDR *pNMHDR, LRESULT *pResult)
    {
        LPNMLISTVIEW pNMLV = reinterpret_cast<LPNMLISTVIEW>(pNMHDR);
        int nCol = pNMLV->iSubItem;
        HDITEM hi = {HDI_IMAGE | HDI_FORMAT};
        
        CHeaderCtrl *pHeadCtrl = GetHeaderCtrl();
        pHeadCtrl->GetItem(nCol, &hi);
        static int k = 0;
        hi.iImage = k;
        k = !k;
    
        int nColCount = pHeadCtrl->GetItemCount();
        for (int i = 0; i < nColCount; i ++)
        {
            if (i == nCol)
                hi.fmt |= HDF_IMAGE;
            else
                hi.fmt &= ~HDF_IMAGE;
    
            
            pHeadCtrl->SetItem(i, &hi);
        }
    
        *pResult = 0;
    }
    
    void CListCtrlEx::OnNMClick(NMHDR *pNMHDR, LRESULT *pResult)
    {
        // TODO: 在此添加控件通知处理程序代码
        /*if (!pNMHDR)
            return;
        LPNMITEMACTIVATE pNmItemActive = (LPNMITEMACTIVATE)pNMHDR;
        LV_ITEM lvItem;
        lvItem.iItem = pNmItemActive->iItem;
        lvItem.iSubItem = 1;
        lvItem.mask = LVIF_TEXT;
        GetItem(&lvItem);*/
        *pResult = 0;
    }
    void CListCtrlEx::OnNMDblclk(NMHDR *pNMHDR, LRESULT *pResult)
    {
        // 在1行1列创建个Edit
        LPNMITEMACTIVATE pNmItemActive = (LPNMITEMACTIVATE)pNMHDR;
        int nRow = pNmItemActive->iItem;
        int nCol = pNmItemActive->iSubItem;
        CRect rc;
        GetItemRect(nRow, &rc, LVIR_BOUNDS);
        CRect rcCell;
        CHeaderCtrl *pHeadCtrl = GetHeaderCtrl();
        pHeadCtrl->GetItemRect(nCol, &rcCell);
        rcCell.top = rc.top;
        rcCell.bottom = rc.bottom;
        if (3==nRow&&1==nCol)
        {
            CEdit *pEdit = new CEdit;
            pEdit->Create(WS_CHILD|WS_VISIBLE|WS_TABSTOP|ES_LEFT, rcCell, this, IDC_ADDEDIT);
        }
        
        *pResult = 0;
    }
    View Code

      

      

      

      

  • 相关阅读:
    About learn《The C programming Language,Second Edition》
    Reproduction CVE_2019_0708
    Hello universe!
    WebSessionStore: Could not obtain reference to HttpContext
    oracle 触发器
    IE11,用Forms身份验证保存不了Cookie
    水晶报表
    SAP记账期间变式
    SAP会计年度变式
    SAP OB52会计年度变式
  • 原文地址:https://www.cnblogs.com/xiaobingqianrui/p/8092986.html
Copyright © 2020-2023  润新知