VC++ 6.0 MFC List Control

1、List Control配置

BOOL CDialog1::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	// TODO: Add extra initialization here

	LONG lStyle; 
        lStyle = GetWindowLong(m_list.m_hWnd, GWL_STYLE);// 获取当前窗口style 
        lStyle &= ~LVS_TYPEMASK; // 清除显示方式位 
        lStyle |= LVS_REPORT; // 设置style 
        SetWindowLong(m_list.m_hWnd, GWL_STYLE, lStyle);// 设置style 
        DWORD dwStyle = m_list.GetExtendedStyle(); 
        dwStyle |= LVS_EX_FULLROWSELECT;// 选中某行使整行高亮(只适用与report 风格的listctrl ) 
	dwStyle |= LVS_EX_GRIDLINES;// 网格线(只适用与report 风格的listctrl ) 
	//dwStyle |= LVS_EX_CHECKBOXES;//item 前生成checkbox 控件 
	dwStyle |= LVS_SHOWSELALWAYS;
	m_list.SetExtendedStyle(dwStyle); // 设置扩展风格 

	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

2、List Control 添加数据

	m_list.InsertColumn(0,"编号",LVCFMT_RIGHT,50,0);
	m_list.InsertColumn(1,"地址",LVCFMT_LEFT,200,0);
	m_list.InsertColumn(2,"端口",LVCFMT_LEFT,50,0);
	m_list.InsertColumn(3,"状态",LVCFMT_LEFT,200,0);


	int		i = 0;
	for(i=0; i<100; i++)
	{
		m_list.InsertItem(i,"");
		m_list.SetItemText(i,0,"file");
		m_list.SetItemText(i,1,"file");
		m_list.SetItemText(i,2,"file");
		m_list.SetItemText(i,3,"file");
	}
3、List Control 人性化处理

重载函数OnCustomdrawList(),分三步走。

(1)添加响应事件

BEGIN_MESSAGE_MAP(CDialog1, CDialog)
	//{{AFX_MSG_MAP(CDialog1)
	ON_NOTIFY(NM_CUSTOMDRAW, IDC_LIST, OnCustomdrawList)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

(2)添加函数声明

// Implementation
protected:

	// Generated message map functions
	//{{AFX_MSG(CDialog1)
	virtual BOOL OnInitDialog();
	afx_msg void OnCustomdrawList(NMHDR*, LRESULT*);
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()


(3)添加重载函数

void CDialog1::OnCustomdrawList(NMHDR *pNMHDR, LRESULT *pResult)
{
    //////////////////////////////////////////////////////
    NMLVCUSTOMDRAW* pLVCD = reinterpret_cast<NMLVCUSTOMDRAW*>( pNMHDR );

    // Take the default processing unless we 

	// set this to something else below.

    *pResult = CDRF_DODEFAULT;

    // First thing - check the draw stage. If it's the control's prepaint

	// stage, then tell Windows we want messages for every item.


    if ( CDDS_PREPAINT == pLVCD->nmcd.dwDrawStage )
    {
        *pResult = CDRF_NOTIFYITEMDRAW;
    }
    else if ( CDDS_ITEMPREPAINT == pLVCD->nmcd.dwDrawStage )
    {
        COLORREF crText,crBk;//奇偶判断
        if ( (pLVCD->nmcd.dwItemSpec % 2) == 0 ){
            crText = RGB(0,0,0);//RGB(32,32,255);
            crBk = RGB(229,232,239);
        }
        else if ( (pLVCD->nmcd.dwItemSpec % 2) == 1 ){
            crText = RGB(0,0,0);
            crBk = RGB(240,247,249);
        }
        else{
            crText = RGB(0,0,0);
            crBk = RGB(0,0,126);
        }
        
        // Store the color back in the NMLVCUSTOMDRAW struct.

        pLVCD->clrText = crText;
        pLVCD->clrTextBk = crBk;
        //设置选择项的颜色
        if( this->m_list.GetItemState(pLVCD->nmcd.dwItemSpec, CDIS_SELECTED) ){
            crBk =RGB(75, 149, 229);//itunes//RGB(10, 36, 106);//RGB(0, 0, 64);
            crText = RGB(255,255,255);
            pLVCD->clrText = crText;
            pLVCD->clrTextBk = crBk;
            *pResult = CDRF_NEWFONT;
        }
        if(LVIS_SELECTED == m_list.GetItemState(pLVCD->nmcd.dwItemSpec,LVIS_SELECTED))
        {
            //清除选择状态,如果不清除的话,还是会显示出蓝色的高亮条
            BOOL b = m_list.SetItemState(pLVCD->nmcd.dwItemSpec,0,LVIS_SELECTED); 
            pLVCD->clrText = crText;
            pLVCD->clrTextBk = crBk;

            *pResult = CDRF_NEWFONT;
            return;
        }
        *pResult = CDRF_NEWFONT;
        //*pResult = CDRF_DODEFAULT;
    }
}

OK,This ALL!



你可能感兴趣的:(mfc)