帮忙看看这个按钮类,为个么会闪.该怎么解决
帮忙看看这个按钮类,为个么会闪.
class CButtonEx : public CButton
{
public:
CButtonEx(CString str,int nFontSize);
CButtonEx();
public:
virtual ~CButtonEx();
protected:
DECLARE_MESSAGE_MAP()
private:
CBitmap *m_pBitmap,*m_pBitmapSel;
CString m_str;
int m_nFontSize;
public:
void SetPromptText(CString str,int nFontSize);
virtual void DrawItem( LPDRAWITEMSTRUCT lpDrawItemStruct );
void SetBitmap(CBitmap *pBitmap,CBitmap *pBitmapSel=NULL);
};
//////////////////////////////////////////////////////////
CButtonEx::CButtonEx(CString str,int nFontSize)
{
m_str=str;
m_nFontSize=nFontSize;
}
CButtonEx::~CButtonEx()
{
}
BEGIN_MESSAGE_MAP(CButtonEx, CButton)
//{{AFX_MSG_MAP(CButtonEx)
// NOTE - the ClassWizard will add and remove mapping macros here.
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CButtonEx message handlers
void CButtonEx::DrawItem( LPDRAWITEMSTRUCT lpDIS)
{
// use the main bitmap for up, the selected bitmap for down
CBitmap* pBitmap = m_pBitmap;
UINT state = lpDIS-> itemState;
if ((state & ODS_SELECTED) && m_pBitmapSel-> m_hObject != NULL)
pBitmap = m_pBitmapSel;
// draw the whole button
CDC* pDC = CDC::FromHandle(lpDIS-> hDC);
CDC memDC;
memDC.CreateCompatibleDC(pDC);
CBitmap* pOld = memDC.SelectObject(pBitmap);
if (pOld == NULL)
{
return; // destructors will clean up
}
CRect rect;
rect.CopyRect(&lpDIS-> rcItem);
pDC-> BitBlt(rect.left, rect.top, rect.Width(), rect.Height(),&memDC, 0, 0, SRCCOPY);
CFont font;
font.CreatePointFont(m_nFontSize,TEXT( "Arial "),pDC);
CFont *pOldFont=pDC-> SelectObject(&font);
//pOldFont=pDC-> SelectObject(&font);
pDC-> SetBkMode(TRANSPARENT);
pDC-> DrawText(m_str,rect,DT_CENTER|DT_VCENTER|DT_INTERNAL);
memDC.SelectObject(pOld);
pDC-> SelectObject(pOldFont);
};
void CButtonEx::SetBitmap(CBitmap *pBitmap,CBitmap *pBitmapSel)
{
m_pBitmap = pBitmap;
m_pBitmapSel = pBitmapSel;
if (pBitmapSel==NULL)
{
m_pBitmapSel = pBitmap;
}
Invalidate();
};
CButtonEx::CButtonEx()
{
m_str=TEXT( " ");
m_nFontSize=1;
}
void CButtonEx::SetPromptText(CString str,int nFontSize)
{
m_str=str;
m_nFontSize=nFontSize;
}
------解决方案--------------------
应该是文字会闪吧,
用双缓存绘图
------解决方案--------------------
你把SetBitmap中的Invalidate()注释掉试下,估计是该函数让按钮一直重绘的原因
------解决方案--------------------
class CButtonEx : public CButton
{
public:
CButtonEx(CString str,int nFontSize);
CButtonEx();
public:
virtual ~CButtonEx();
protected:
DECLARE_MESSAGE_MAP()
private:
CBitmap *m_pBitmap,*m_pBitmapSel;
CString m_str;
int m_nFontSize;
public:
void SetPromptText(CString str,int nFontSize);
virtual void DrawItem( LPDRAWITEMSTRUCT lpDrawItemStruct );
void SetBitmap(CBitmap *pBitmap,CBitmap *pBitmapSel=NULL);
};
//////////////////////////////////////////////////////////
CButtonEx::CButtonEx(CString str,int nFontSize)
{
m_str=str;
m_nFontSize=nFontSize;
}
CButtonEx::~CButtonEx()
{
}
BEGIN_MESSAGE_MAP(CButtonEx, CButton)
//{{AFX_MSG_MAP(CButtonEx)
// NOTE - the ClassWizard will add and remove mapping macros here.
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CButtonEx message handlers
void CButtonEx::DrawItem( LPDRAWITEMSTRUCT lpDIS)
{
// use the main bitmap for up, the selected bitmap for down
CBitmap* pBitmap = m_pBitmap;
UINT state = lpDIS-> itemState;
if ((state & ODS_SELECTED) && m_pBitmapSel-> m_hObject != NULL)
pBitmap = m_pBitmapSel;
// draw the whole button
CDC* pDC = CDC::FromHandle(lpDIS-> hDC);
CDC memDC;
memDC.CreateCompatibleDC(pDC);
CBitmap* pOld = memDC.SelectObject(pBitmap);
if (pOld == NULL)
{
return; // destructors will clean up
}
CRect rect;
rect.CopyRect(&lpDIS-> rcItem);
pDC-> BitBlt(rect.left, rect.top, rect.Width(), rect.Height(),&memDC, 0, 0, SRCCOPY);
CFont font;
font.CreatePointFont(m_nFontSize,TEXT( "Arial "),pDC);
CFont *pOldFont=pDC-> SelectObject(&font);
//pOldFont=pDC-> SelectObject(&font);
pDC-> SetBkMode(TRANSPARENT);
pDC-> DrawText(m_str,rect,DT_CENTER|DT_VCENTER|DT_INTERNAL);
memDC.SelectObject(pOld);
pDC-> SelectObject(pOldFont);
};
void CButtonEx::SetBitmap(CBitmap *pBitmap,CBitmap *pBitmapSel)
{
m_pBitmap = pBitmap;
m_pBitmapSel = pBitmapSel;
if (pBitmapSel==NULL)
{
m_pBitmapSel = pBitmap;
}
Invalidate();
};
CButtonEx::CButtonEx()
{
m_str=TEXT( " ");
m_nFontSize=1;
}
void CButtonEx::SetPromptText(CString str,int nFontSize)
{
m_str=str;
m_nFontSize=nFontSize;
}
------解决方案--------------------
应该是文字会闪吧,
用双缓存绘图
------解决方案--------------------
你把SetBitmap中的Invalidate()注释掉试下,估计是该函数让按钮一直重绘的原因
------解决方案--------------------