重载listctrl列头列头皮肤Word下载.docx
《重载listctrl列头列头皮肤Word下载.docx》由会员分享,可在线阅读,更多相关《重载listctrl列头列头皮肤Word下载.docx(15页珍藏版)》请在冰豆网上搜索。
.\rslistctrl.h"
IMPLEMENT_DYNAMIC(CRSListCtrl,CListCtrl)
CRSListCtrl:
:
CRSListCtrl()
}
~CRSListCtrl()
BEGIN_MESSAGE_MAP(CRSListCtrl,CListCtrl)
ON_WM_PAINT()
ON_WM_ERASEBKGND()
ON_WM_DRAWITEM_REFLECT()
ON_WM_MEASUREITEM_REFLECT()
ON_WM_CREATE()
ON_WM_DESTROY()
ON_MESSAGE(LVM_INSERTCOLUMN,OnInsertColumn)END_MESSAGE_MAP()
//CRSListCtrl消息处理程序
BOOLCRSListCtrl:
PreCreateWindow(CREATESTRUCT&
cs){
cs.style|=LVS_OWNERDRAWFIXED;
returnCListCtrl:
PreCreateWindow(cs);
voidCRSListCtrl:
RSRectangle(HDChDC,intleft,inttop,intright,intbottom){
POINTptOrg,ptTmp;
right=max(0,right-1);
bottom=max(0,bottom-1);
MoveToEx(hDC,left,top,&
ptOrg);
LineTo(hDC,left,bottom);
MoveToEx(hDC,left,bottom,&
ptTmp);
LineTo(hDC,right,bottom);
MoveToEx(hDC,right,bottom,&
LineTo(hDC,right,top);
MoveToEx(hDC,right,top,&
LineTo(hDC,left,top);
MoveToEx(hDC,ptOrg.x,ptOrg.y,&
OnPaint()
PAINTSTRUCTps;
HDChDC=NULL;
HDChDCMem=NULL;
HBITMAPhBMP=NULL;
HBITMAPhOldBitmap=NULL;
HPENhPen=NULL;
HPENhOldPen=NULL;
CRectrectClient;
HRGNhRgn=NULL;
GetClientRect(&
rectClient);
InitHeader();
//donothing,onlyvalidatetheclipregion.
hDC=:
BeginPaint(m_hWnd,&
ps);
EndPaint(m_hWnd,&
//realdraw
GetDC(m_hWnd);
hDCMem=:
CreateCompatibleDC(hDC);
hBMP=(HBITMAP):
CreateCompatibleBitmap(hDC,rectClient.Width(),rectClient.Height());
hOldBitmap=(HBITMAP):
SelectObject(hDCMem,hBMP);
hPen=:
CreatePen(PS_SOLID,1,RGB(150,198,226));
hOldPen=(HPEN):
SelectObject(hDCMem,hPen);
//clipheaderctrl
CHeaderCtrl*pHeadCtrl=GetHeaderCtrl();
CRectrectHead;
pHeadCtrl->
rectHead);
hRgn=:
CreateRectRgn(rectClient.left,rectClient.top+rectHead.Height(),rectClient.right,rectClient.bottom);
SelectClipRgn(hDC,hRgn);
//DrawBackGround
SetBkColor(hDCMem,RGB(242,250,255));
ExtTextOut(hDCMem,0,0,ETO_OPAQUE,&
rectClient,NULL,0,NULL);
//DrawItems
intnItemCount=GetItemCount();
intfirstvisible=GetTopIndex();
intlastvisible=firstvisible+GetCountPerPage();
if(lastvisible>
nItemCount)
lastvisible=nItemCount;
intindex=firstvisible;
while(index<
lastvisible)
DRAWITEMSTRUCTdis;
dis.CtlID=:
GetWindowLong(m_hWnd,GWL_ID);
dis.CtlType=ODT_LISTVIEW;
dis.hDC=hDCMem;
dis.hwndItem=m_hWnd;
dis.itemAction=ODA_DRAWENTIRE;
dis.itemData=0;
dis.itemID=index;
dis.itemState=GetItemState(index,LVIS_SELECTED)==LVIS_SELECTED?
ODS_SELECTED:
0;
GetItemRect(index,&
dis.rcItem,LVIR_BOUNDS);
DrawItem(&
dis);
index++;
RSRectangle(hDCMem,rectClient.left,rectClient.top,rectClient.right,rectClient.bottom);
BitBlt(hDC,0,0,rectClient.Width(),rectClient.Height(),hDCMem,0,0,SRCCOPY);
if(hOldBitmap)
SelectObject(hDCMem,hOldBitmap);
if(hOldPen)
SelectObject(hDCMem,hOldPen);
DeleteObject(hRgn);
DeleteObject(hBMP);
DeleteObject(hPen);
DeleteDC(hDCMem);
ReleaseDC(m_hWnd,hDC);
OnEraseBkgnd(CDC*pDC)
returnTRUE;
MeasureItem(LPMEASUREITEMSTRUCTlpMeasureItemStruct){
if(lpMeasureItemStruct->
CtlType==ODT_LISTVIEW)
lpMeasureItemStruct->
itemHeight=18;
DrawItem(LPDRAWITEMSTRUCTlpDrawItemStruct)
if(lpDrawItemStruct->
CtlType!
=ODT_LISTVIEW)
return;
CDCdc;
dc.Attach(lpDrawItemStruct->
hDC);
itemAction==ODA_DRAWENTIRE)
itemState&
ODS_SELECTED)
dc.FillSolidRect(&
lpDrawItemStruct->
rcItem,RGB(182,218,243));
else
rcItem,RGB(242,250,255));
CFont*pOldFont=dc.SelectObject(&
m_fontText);
dc.SetBkMode(TRANSPARENT);
dc.SetTextColor(RGB(35,99,133));
//DrawItem
TCHARszItemText[256];
LVITEMlvi;
lvi.mask=LVIF_IMAGE|LVIF_STATE|LVIF_TEXT;
lvi.iImage=0;
lvi.iItem=lpDrawItemStruct->
itemID;
lvi.iSubItem=0;
lvi.cchTextMax=256;
lvi.pszText=szItemText;
GetItem(&
lvi);
CRectheaderItemRect;
GetItemRect(0,&
headerItemRect);
CRectrectItem;
rectItem=lpDrawItemStruct->
rcItem;
rectItem.right=rectItem.left+headerItemRect.Width();
CImageList*pImageList=GetImageList(LVSIL_NORMAL);
if(pImageList)
CPointpt;
pt.x=rectItem.left+4;
pt.y=rectItem.top+2;
pImageList->
Draw(&
dc,lvi.iImage,pt,ILD_TRANSPARENT);
rectItem.left+=24;
dc.DrawText(szItemText,(int)_tcslen(szItemText),&
rectItem,DT_LEFT|DT_VCENTER|DT_SINGLELINE|DT_WORDBREAK|DT_END_ELLIPSIS);
//DrawSubItem
intnCol=pHeadCtrl->
GetItemCount();
for(inti=1;
i<
nCol;
i++)
CRectrectSubItem;
GetSubItemRect(lpDrawItemStruct->
itemID,i,LVIR_BOUNDS,rectSubItem);
CStringstrText=GetItemText(lpDrawItemStruct->
itemID,i);
dc.DrawText(strText,&
rectSubItem,DT_CENTER|DT_VCENTER|DT_SINGLELINE|DT_END_ELLIPSIS|DT_WORDBREAK);
}
if(pOldFont)
dc.SelectObject(pOldFont);
dc.Detach();
intCRSListCtrl:
OnCreate(LPCREATESTRUCTlpCreateStruct)
if(CListCtrl:
OnCreate(lpCreateStruct)==-1)
return-1;
m_fontText.CreateFont(-12,0,0,0,FW_NORMAL,
DEFAULT_CHARSET,OUT_DEFAULT_PRECIS,
ANTIALIASED_QUALITY,FF_DONTCARE,_T("
Arial"
));
return0;
OnDestroy()
CListCtrl:
OnDestroy();
m_fontText.DeleteObject();
InitHeader()
if(m_wndHeaderCtrl.GetSafeHwnd()==NULL)
m_wndHeaderCtrl.SubclassDlgItem(0,this);
m_wndHeaderCtrl.Init();
FALSE,FALSE,FALSE,CLIP_DEFAULT_PRECIS,
LRESULTCRSListCtrl:
OnInsertColumn(WPARAMwParam,LPARAMlParam)
LRESULTlResult=CListCtrl:
DefWindowProc(LVM_INSERTCOLUMN,wParam,lParam);
returnlResult;
//CRSHeaderCtrl
classCRSHeaderCtrl:
publicCHeaderCtrl
DECLARE_DYNAMIC(CRSHeaderCtrl)
CRSHeaderCtrl();
virtual~CRSHeaderCtrl();
voidInit();
HBITMAPm_hBitmap;
CFontm_font;
voidHorzStretchBlt(HDChDC,LPRECTlpDst,HBITMAPhBMP,LPRECTlpSrc,UINTnLeftPart,UINTnRightPart);
afx_msgLRESULTOnLayout(WPARAMwParam,LPARAMlParam);
afx_msgvoidOnNMCustomdraw(NMHDR*pNMHDR,LRESULT*pResult);
};
//RSHeaderCtrl.cpp:
IMPLEMENT_DYNAMIC(CRSHeaderCtrl,CHeaderCtrl)
CRSHeaderCtrl:
CRSHeaderCtrl()
m_hBitmap=NULL;
~CRSHeaderCtrl()
BEGIN_MESSAGE_MAP(CRSHeaderCtrl,CHeaderCtrl)
ON_NOTIFY_REFLECT(NM_CUSTOMDRAW,OnNMCustomdraw)
ON_MESSAGE(HDM_LAYOUT,OnLayout)
END_MESSAGE_MAP()
voidCRSHeaderCtrl:
Init()
if(m_hBitmap==NULL)
CStringstrSkinPath=theApp.m_pWinManager->
duGetSkinPath();
CStringstrImagePath=strSkinPath+"
headerctrl.bmp"
;
m_hBitmap=(HBITMAP):
LoadImage(NULL,strImagePath.GetBuffer(strImagePath.GetLength()),IMAGE_BITMAP,0,0,LR_DEFAULTSIZE|LR_LOADFROMFILE);
m_font.CreateFont(-12,0,0,0,FW_NORMAL,FALSE,FALSE,FALSE,DEFAULT_CHARSET,OUT_DEFAULT_PRECIS,CLIP_DEFAULT_PRECIS,ANTIALIASED_QUALITY,FF_DONTCARE,_T("
HorzStretchBlt(HDChDC,LPRECTlpDst,HBITMAPhBMP,LPRECTlpSrc,UINTnLeftPart,UINTnRightPart)
if(hDC==NULL||lpDst==NULL||hBMP==NULL)
BITMAPbm;
GetObject(hBMP,sizeof(BITMAP),&
bm);
if(nLeftPart>
(UINT)bm.bmWidth||nRightPart>
(UINT)bm.bmWidth)
RECTrSrc;
if(lpSrc==NULL)
rSrc.left=0;
rSrc.top=0;
rSrc.right=bm.bmWidth;
rSrc.bottom=bm.bmHeight;
CopyRect(&
rSrc,lpSrc);
HDChMemDC=CreateCompatibleDC(hDC);
HBITMAPhOldBitmap=(HBITMAP)SelectObject(hMemDC,hBMP);
BitBlt(hDC,lpDst->
left,lpDst->
top,nLeftPart,lpDst->
bottom-lpDst->
top,hMemDC,rSrc.left,rSrc.top,SRCCOPY);
StretchBlt(hDC,
lpDst->
left+nLeftPart,
top,
max(0,lpDst->
right-lpDst->
left-nLeftPart-nRightPart),
hMemDC,
rSrc.left+nLeftPart,
rSrc.top,
max(0,rSrc.right-rSrc.left-nLeftPart-nRightPart),
rSrc.bottom-rSrc.top,
SRCCOPY);
right-nRightPart,lpDst->
top,nRightPart,lpDst->
top,hMemDC,rSrc.right-nRightPart,rSrc.top,SRCCOPY);
SelectObject(hMemDC,hOldBitmap);
DeleteDC(hMemDC);
//CRSHeaderCtrl消息处理程序
LRESULTCRSHeaderCtrl:
OnLayout(WPARAMwParam,LPARAMlParam)
LRESULTlResult=CHeaderCtrl:
DefWindowProc(HDM_LAYOUT,0,lParam);
HD_LAYOUT&
hdl=*(HD_LAYOUT*)lParam;
RECT*prc=hdl.prc;
WINDOWPOS*pwpos=hdl.pwpos;
intnHeight=18;
pwpos->
cy=nHeight;
prc->
top=nHeight;
OnNMCustomdraw(NMHDR*lpNMHDR,LRESULT*pResult){
LPNMCUSTOMDRAWlpNMCD
reinterpret_cast<
LPNMCUSTOMDRAW>
(lpNMHDR);
switch(lpNMCD->
dwDrawStage)
caseCDDS_PREPAINT:
*pResult=CDRF_NOTIFYITEMDRAW|C