资源描述:
《clistctrl设置行高》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、CListCtrl设置行高CListCtrl改变行高:1.表的修改 新建一个MFC类CMyListCtrl,其基类为CListCtrl,属性OwnerDrawFixed设置为TRUE;添加变量CMyListCtrlm_List;MyListCtrl.h文件里,添加变量intm_nRowHeight;2、重载DrawItem()实现自绘,实现请参照以下代码。 手动添加消息宏ON_WM_MEASUREITEM_REFLECT(),添加下面的函数,实现修改行高。voidCMyListCtrl::MeasureIt
2、em(LPMEASUREITEMSTRUCTlpMeasureItemStruct){ if(m_nRowHeight>0) { lpMeasureItemStruct->itemHeight=m_nRowHeight; }}3、添加ON_WM_MEASUREITEM,响应消息处理OnMeasureItem()voidCMyListCtrl::OnMeasureItem(intnIDCtl,LPMEASUREITEMSTRUCTlpMeasureItemStruct){ //TODO:在此添加消息处理程序代码
3、和/或调用默认值 CListCtrl::OnMeasureItem(nIDCtl,lpMeasureItemStruct);}4、voidCMyListCtrl::SetRowHeigt(intnHeight)//高置行高(别忘了在.h里添加voidSetRowHeigt(intnHeight);){ m_nRowHeight=nHeight; CRectrcWin; GetWindowRect(&rcWin); WINDOWPOSwp; wp.hwnd=m_hWnd; wp.cx=rcWin.Width();
4、 wp.cy=rcWin.Height(); wp.flags=SWP_NOACTIVATE
5、SWP_NOMOVE
6、SWP_NOOWNERZORDER
7、SWP_NOZORDER; SendMessage(WM_WINDOWPOSCHANGED,0,(LPARAM)&wp);}5、重画列表控件,重载DrawItem,添加ON_WM_DRAWITEM();(别忘了在.h里添加voidDrawItem(LPDRAWITEMSTRUCTlpMeasureItemStruct);)voidCMyListCtrl::Dra
8、wItem(LPDRAWITEMSTRUCTlpMeasureItemStruct){ CDC*pDC=CDC::FromHandle(lpMeasureItemStruct->hDC); LVITEMlvi={0}; lvi.mask=LVIF_STATE;//
9、LVIF_IMAGE; lvi.stateMask=LVIS_FOCUSED
10、LVIS_SELECTED; lvi.iItem=lpMeasureItemStruct->itemID; BOOLbGet=GetItem
11、(&lvi); //高亮显示BOOLbHighlight=((lvi.state&LVIS_DROPHILITED)
12、
13、((lvi.state&LVIS_SELECTED)&& ((GetFocus()==this)
14、
15、(GetStyle()&LVS_SHOWSELALWAYS)))); //画文本背景 CRectrcBack=lpMeasureItemStruct->rcItem; pDC->SetBkMode(TRANSPARENT); if(bHighlight)//如果被选中 { pDC->SetT
16、extColor(RGB(255,255,255));//文本为白色 pDC->FillRect(rcBack,&CBrush(RGB(90,162,0))); } else { pDC->SetTextColor(RGB(0,0,0)); //文本为黑色 pDC->FillRect(rcBack,&CBrush(RGB(255,255,255))); } if(lpMeasureItemStruct->itemAction&ODA_DRAWENTIRE) { //写文本
17、 CStringszText; intnCollumn=GetHeaderCtrl()->GetItemCount();//列数 for(inti=0;iGetItemCount();i++) {//循环得到文本 CRectrcItem; if(!GetSubIte