WinCE 下实现Listctrl自画

源代码在线查看: skinhead.cpp

软件大小: 208 K
上传用户: cq745
关键词: Listctrl WinCE
下载地址: 免注册下载 普通下载 VIP

相关代码

				// Head.cpp : implementation file
				//
				
				#include "stdafx.h"
				#include "SkinHead.h"
				#include "ListView.h"
				
				#ifdef _DEBUG
				#define new DEBUG_NEW
				#undef THIS_FILE
				static char THIS_FILE[] = __FILE__;
				#endif
				
				/////////////////////////////////////////////////////////////////////////////
				// CHead
				
				CHead::CHead()
				{
					m_bDoubleBuffer = TRUE;
					m_cr3DFace=::GetSysColor(COLOR_3DFACE);
					m_cr3DHighLight=::GetSysColor(COLOR_3DHIGHLIGHT);
					m_cr3DShadow=::GetSysColor(COLOR_3DSHADOW);
					m_crBtnText=::GetSysColor(COLOR_BTNTEXT);
				}
				
				CHead::~CHead()
				{
				}
				
				
				BEGIN_MESSAGE_MAP(CHead, CHeaderCtrl)
					//{{AFX_MSG_MAP(CHead)
					ON_WM_ERASEBKGND()
					ON_WM_PAINT()
					ON_MESSAGE(HDM_LAYOUT, OnLayout)
					//}}AFX_MSG_MAP
				END_MESSAGE_MAP()
				
				/////////////////////////////////////////////////////////////////////////////
				// CHead message handlers
				void CHead::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
				{
					ASSERT(FALSE); 
				}
				void CHead::DrawItem(CDC* pDC, CRect rect, LPHDITEM lphdi)
				{
					int x=rect.left;
					ASSERT(lphdi->mask & HDI_FORMAT);
					CBitmap bmp;
					bmp.LoadBitmap(IDB_BMP_HEAD);
					BITMAP  bmpInfo;
					bmp.GetBitmap(&bmpInfo);
					CDC dcMem;
					dcMem.CreateCompatibleDC(pDC);
					CBitmap *pOldBitmap=dcMem.SelectObject(&bmp);
					pDC->StretchBlt(x,0,rect.Width()-1,rect.Height(),&dcMem,0,0,bmpInfo.bmWidth,bmpInfo.bmHeight,SRCCOPY);
					dcMem.SelectObject(pOldBitmap);
					DrawText(pDC, rect, lphdi);
				}
				void CHead::DrawCtrl(CDC *pDC)
				{
					CRect RectClip;
					if(pDC->GetClipBox(RectClip)==ERROR)
						return;
					CRect RectClient,RectItem;
					GetClientRect(&RectClient);
				
				
					pDC->FillSolidRect(RectClip,m_cr3DFace);
					int iItems=GetItemCount();
					ASSERT(iItems>=0);
					CPen penHighLight(PS_SOLID,1,m_cr3DHighLight);
					CPen penShadow(PS_SOLID,1,m_cr3DShadow);
					CPen *pPen=pDC->GetCurrentPen();
				    CFont font;
					font.CreatePointFont(10,_T("宋体"));
					pDC->SetBkColor(m_cr3DFace);
					pDC->SetTextColor(m_crBtnText);
					int iWidth=0;
					for(int i=0;i					{
						int iItem=OrderToIndex(i);
						TCHAR	szText[FLATHEADER_TEXT_MAX];
						HDITEM hditem;
						hditem.mask=HDI_WIDTH|HDI_FORMAT|HDI_TEXT|HDI_IMAGE|HDI_BITMAP;
						hditem.pszText=szText;
						hditem.cchTextMax=sizeof(szText);
						VERIFY(GetItem(iItem, &hditem));
						VERIFY(GetItemRect(iItem, RectItem));
						if(RectItem.right>=RectItem.left||RectItem.left						{
							if(hditem.fmt & HDF_OWNERDRAW)
							{
								DRAWITEMSTRUCT disItem;
								disItem.CtlType = ODT_BUTTON;
								disItem.CtlID = GetDlgCtrlID();
								disItem.itemID = iItem;
								disItem.itemAction = ODA_DRAWENTIRE;
								disItem.itemState = 0;
								disItem.hwndItem = m_hWnd;
								disItem.hDC = pDC->m_hDC;
								disItem.rcItem = RectItem;
								disItem.itemData = 0;
							}
							else
							{
								CRect rect;
								this->GetItemRect(i,&rect);
								DrawItem(pDC, rect, &hditem);
							}
						}
						iWidth += hditem.cxy;
					}
					if (iWidth > 0)
					{
						RectClient.right = RectClient.left + iWidth;
						pDC->Draw3dRect(RectClient, m_cr3DHighLight, m_cr3DShadow);
					}
					pDC->SelectObject(font);
					pDC->SelectObject(pPen);
					penHighLight.DeleteObject();
					penShadow.DeleteObject();
				}
				int CHead::DrawText(CDC* pDC, CRect rect, LPHDITEM lphdi)
				{
					CSize size;
					pDC->SetTextColor(RGB(255,0,255));
					pDC->SetBkMode(TRANSPARENT);
					CFont font;
					font.CreatePointFont(90,_T("宋体"),pDC);
					if (rect.Width() > 0 && lphdi->mask & HDI_TEXT && lphdi->fmt & HDF_STRING)
					{
				
						size = pDC->GetTextExtent(lphdi->pszText);
				        CFont *pFont=pDC->SelectObject(&font);
						pDC->DrawText(lphdi->pszText, -1, rect, DT_CENTER|DT_END_ELLIPSIS|DT_SINGLELINE|DT_VCENTER);
					}
					size.cx = rect.Width()>size.cx ? size.cx:rect.Width();
					return size.cx>0 ? size.cx:0;
				}
				LRESULT CHead::OnLayout(WPARAM, LPARAM lParam)
				{
					LPHDLAYOUT lphdlayout = (LPHDLAYOUT)lParam;
				
					if (m_bStaticBorder)
						lphdlayout->prc->right += GetSystemMetrics(SM_CXBORDER)*2;
				
					return CHeaderCtrl::DefWindowProc(HDM_LAYOUT, 0, lParam);
				}
				
				BOOL CHead::OnEraseBkgnd(CDC* pDC) 
				{
					UNUSED_ALWAYS(pDC);
					return CHeaderCtrl::OnEraseBkgnd(pDC);
				}
				
				void CHead::OnPaint() 
				{
					CPaintDC dc(this);
					if (m_bDoubleBuffer)
				    {
				        CMemDC MemDC(&dc);
				        DrawCtrl(&MemDC);
				    }
				    else
				        DrawCtrl(&dc);
				}
							

相关资源