如题,网上找不到完整的例子,东一块西一块的,听说好像在DrawItem里面重画没有在onpaint里面重画好?我主要就是想改改表头的背景颜色
解决方案 »
- 怎么向另一个程序(写字板)发送一个消息?
- 请教:vs2005环境下,在dll工程中的使用模板产生的警告是什么意思?
- List ConTrol 控件的如何实现读取文件目录 在线等待.......
- ~~~VC求助~~~
- 我的SDI程序中视图中SetTimer()没有起作用?为什么?怎么办?
- 如何在boxlist中显示排文件名
- ======200分求助两个比较恼头的问题=====
- wchar_t wc=L'\xAA';这句话错在那里?
- 求类似监控系统cpu、内存利用率程序的源代码。谢谢
- IADsUser接口如何使用?
- 这个怎样做到,请高手帮忙!
- 求:ADO接口_RecordsetPtr::GetCollect()提取字符串时到底给你隐式转换了什么?
{
CPaintDC dc(this); // device context for painting
CRect rect, rectItem, clientRect;
GetClientRect(&rect);
GetClientRect(&clientRect);
CMemDC memDC(&dc, rect);
CDC bitmapDC;
bitmapDC.CreateCompatibleDC(&dc);
memDC.FillSolidRect(&rect, RGB(64,109,176));你的背景颜色
int nItems = GetItemCount();
for(int i = 0; i <nItems; i++)
{
TCHAR buf1[256];
HD_ITEM hditem1;
hditem1.mask = HDI_TEXT | HDI_FORMAT | HDI_ORDER;
hditem1.pszText = buf1;
hditem1.cchTextMax = 255;
GetItem( i, &hditem1 );
GetItemRect(i, &rect);
DRAWITEMSTRUCT DrawItemStruct;
GetItemRect(i, &rectItem);
DrawItemStruct.CtlType = 100;
DrawItemStruct.hDC = dc.GetSafeHdc();
DrawItemStruct.itemAction = ODA_DRAWENTIRE;
DrawItemStruct.hwndItem = GetSafeHwnd();
DrawItemStruct.rcItem = rectItem;
DrawItemStruct.itemID = i;
DrawItem(&DrawItemStruct);
UINT uFormat = DT_SINGLELINE | DT_NOPREFIX | DT_TOP |DT_CENTER | DT_END_ELLIPSIS ;
CFont font;
LOGFONT lf;
memset(&lf, 0, sizeof(LOGFONT));
lf.lfHeight = 12;
strcpy(lf.lfFaceName, "宋体");
font.CreateFontIndirect(&lf);
CFont* def_font = memDC.SelectObject(&font);
memDC.SetTextColor(RGB(255,255,255));
memDC.SetBkMode(TRANSPARENT);
rectItem.DeflateRect(2,2,2,2);
TCHAR buf[256];
HD_ITEM hditem;
hditem.mask = HDI_TEXT | HDI_FORMAT | HDI_ORDER;
hditem.pszText = buf;
hditem.cchTextMax = 255;
GetItem( DrawItemStruct.itemID, &hditem ); memDC.DrawText(buf, &rectItem, uFormat);
memDC.SelectObject(def_font);
font.DeleteObject();
} }为了防止闪烁最好也处理一下WM_ERASEBKGND这样出来的效果仅仅是背景色填充纯色,显示不了网格线,很难重绘得好,我一般是填充背景图来实现网格线效果,底图做得好可以有金属效果
这么大的CHeaderCtrlEx啊,晕死!!!!!!!!
// CHeaderCtrlEx
class CMemDC : public CDC
{
private:
CBitmap* m_bitmap;
CBitmap* m_oldBitmap;
CDC* m_pDC;
CRect m_rcBounds;
public:
CMemDC(CDC* pDC, const CRect& rcBounds) : CDC()
{
CreateCompatibleDC(pDC);
m_bitmap = new CBitmap;
m_bitmap->CreateCompatibleBitmap(pDC, rcBounds.Width(), rcBounds.Height());
m_oldBitmap = SelectObject(m_bitmap);
m_pDC = pDC;
m_rcBounds = rcBounds;
}
~CMemDC()
{
m_pDC->BitBlt(m_rcBounds.left, m_rcBounds.top, m_rcBounds.Width(), m_rcBounds.Height(),
this, m_rcBounds.left, m_rcBounds.top, SRCCOPY);
SelectObject(m_oldBitmap);
if (m_bitmap != NULL)
delete m_bitmap;
}
CMemDC* operator->()
{
return this;
}
}; class CHeaderCtrlEx : public CHeaderCtrl
{
public:
CHeaderCtrlEx();
public:
virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct);
virtual ~CHeaderCtrlEx();
protected:
//{{AFX_MSG(CHeaderCtrlEx)
afx_msg void OnPaint();
afx_msg BOOL OnEraseBkgnd(CDC* pDC);
//}}AFX_MSG DECLARE_MESSAGE_MAP()
};
//.cpp File
// HeaderCtrlEx.cpp : 实现文件
//#include "stdafx.h"
#include "HeaderCtrlEx.h"
#include "resource.h"CHeaderCtrlEx::CHeaderCtrlEx()
{
}
CHeaderCtrlEx::~CHeaderCtrlEx()
{
}
BEGIN_MESSAGE_MAP(CHeaderCtrlEx, CHeaderCtrl)
//{{AFX_MSG_MAP(CHeaderCtrlEx)
ON_WM_PAINT()
ON_WM_ERASEBKGND()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
void CHeaderCtrlEx::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{} void CHeaderCtrlEx::OnPaint()
{
CPaintDC dc(this); // device context for painting
CRect rect, rectItem, clientRect;
GetClientRect(&rect);
GetClientRect(&clientRect);
CMemDC memDC(&dc, rect);
CDC bitmapDC;
bitmapDC.CreateCompatibleDC(&dc);
memDC.FillSolidRect(&rect, RGB(64,109,176));
/*
CBitmap bitmapSpan;
bitmapSpan.LoadBitmap(IDB_COLUMNHEADER_SPAN);
CBitmap* pOldBitmapSpan = bitmapDC.SelectObject(&bitmapSpan);
for(int v=0; v<rect.Width(); v++)
{
memDC.BitBlt((rect.left+2)+(v*1), 0, 1, 23, &bitmapDC,0,0,SRCCOPY);
} bitmapDC.SelectObject(pOldBitmapSpan);
bitmapSpan.DeleteObject();
*/
int nItems = GetItemCount(); CBitmap bitmap;
CBitmap bitmap2;
CBitmap bitmap3;
bitmap.LoadBitmap(IDB_COLUMNHEADER_START);
bitmap2.LoadBitmap(IDB_COLUMNHEADER_SPAN);
bitmap3.LoadBitmap(IDB_COLUMNHEADER_END); for(int i = 0; i <nItems; i++)
{
TCHAR buf1[256];
HD_ITEM hditem1;
hditem1.mask = HDI_TEXT | HDI_FORMAT | HDI_ORDER;
hditem1.pszText = buf1;
hditem1.cchTextMax = 255;
GetItem( i, &hditem1 );
GetItemRect(i, &rect);
/*
CBitmap* pOldBitmap = NULL;
//make sure we draw the start piece
//on the first item so it has a left border //For the following items we will just use the
//right border of the previous items as the left
//border
if(hditem1.iOrder==0)
{
pOldBitmap = bitmapDC.SelectObject(&bitmap);
memDC.BitBlt(rect.left,rect.top,2,23,&bitmapDC,0,0,SRCCOPY);
}
else
{
memDC.BitBlt(rect.left-1,rect.top,2,23,&bitmapDC,0,0,SRCCOPY);
pOldBitmap = bitmapDC.SelectObject(&bitmap2);
memDC.BitBlt(rect.left+1,rect.top,1,23,&bitmapDC,0,0,SRCCOPY);
} bitmapDC.SelectObject(pOldBitmap);
//span the bitmap for the width of the column header item
int nWidth = rect.Width() - 4;
CBitmap* pOldBitmap2 = bitmapDC.SelectObject(&bitmap2);
for(int v=0; v<nWidth; v++)
{
memDC.BitBlt((rect.left+2)+(v*1), 0, 1, 23, &bitmapDC,0,0,SRCCOPY);
} bitmapDC.SelectObject(pOldBitmap2);
//draw the end piece of the column header
CBitmap* pOldBitmap3 = bitmapDC.SelectObject(&bitmap3);
memDC.BitBlt((rect.right-2), 0, 2, 23, &bitmapDC,0,0,SRCCOPY);
bitmapDC.SelectObject(pOldBitmap3);
*/
//Get all the info for the current
//item so we can draw the text to it
//in the desired font and style
DRAWITEMSTRUCT DrawItemStruct;
GetItemRect(i, &rectItem);
DrawItemStruct.CtlType = 100;
DrawItemStruct.hDC = dc.GetSafeHdc();
DrawItemStruct.itemAction = ODA_DRAWENTIRE;
DrawItemStruct.hwndItem = GetSafeHwnd();
DrawItemStruct.rcItem = rectItem;
DrawItemStruct.itemID = i;
DrawItem(&DrawItemStruct);
UINT uFormat = DT_SINGLELINE | DT_NOPREFIX | DT_TOP |DT_CENTER | DT_END_ELLIPSIS ;
CFont font;
LOGFONT lf;
memset(&lf, 0, sizeof(LOGFONT));
lf.lfHeight = 12;
strcpy(lf.lfFaceName, "宋体");
font.CreateFontIndirect(&lf);
CFont* def_font = memDC.SelectObject(&font);
memDC.SetTextColor(RGB(255,255,255));
memDC.SetBkMode(TRANSPARENT);
rectItem.DeflateRect(2,2,2,2);
TCHAR buf[256];
HD_ITEM hditem;
hditem.mask = HDI_TEXT | HDI_FORMAT | HDI_ORDER;
hditem.pszText = buf;
hditem.cchTextMax = 255;
GetItem( DrawItemStruct.itemID, &hditem ); memDC.DrawText(buf, &rectItem, uFormat);
memDC.SelectObject(def_font);
font.DeleteObject();
}
}
BOOL CHeaderCtrlEx::OnEraseBkgnd(CDC* pDC)
{
return false;
}