我继承CStatic类,写了一个CColorStatic类,.cpp如下:// ColorStatic.cpp : implementation file
//
#include "stdafx.h"
#include "CTI_Server.h"
#include "ColorStatic.h"#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif/////////////////////////////////////////////////////////////////////////////
// CColorStaticCColorStatic::CColorStatic()
{
    m_TextColor = RGB(0, 0, 0);
}CColorStatic::~CColorStatic()
{
}
BEGIN_MESSAGE_MAP(CColorStatic, CStatic)
//{{AFX_MSG_MAP(CColorStatic)
ON_WM_CREATE()
ON_WM_PAINT()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////
// CColorStatic message handlersint CColorStatic::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
if (CStatic::OnCreate(lpCreateStruct) == -1)
return -1;

// TODO: Add your specialized creation code here return 0;
}void CColorStatic::OnPaint() // 重画Static
{
CPaintDC dc(this); // device context for painting

CRect rect;
GetClientRect(&rect); dc.SetBkColor(m_BackColor);
dc.SetBkMode(TRANSPARENT);
CFont *pFont = GetParent()->GetFont();      // 得到父窗体的字体
CFont *pOldFont;
pOldFont = dc.SelectObject(pFont);          // 选用父窗体的字体
dc.SetTextColor(m_TextColor);             // 设置文本颜色
dc.DrawText(m_strCaption, &rect, DT_LEFT);  // 将文本画在Static的左侧
dc.SelectObject(pOldFont); // Do not call CStatic::OnPaint() for painting messages
}void CColorStatic::Create(CString strCaption, COLORREF BackColor)
{

}void CColorStatic::SetCaption(CString strCaption)
{
m_strCaption = strCaption; // 设置Static文本
}void CColorStatic::SetBackColor(COLORREF BackColor)
{
m_BackColor = BackColor; // 设置背景颜色
}void CColorStatic::SetTextColor(COLORREF TextColor)
{
m_TextColor = TextColor; // 设置文字颜色
}就是这样简单,然后我在对话框的OnInitDialog()中
m_Static5.SubclassDlgItem(IDC_Agent, this); //在.h中已经定义CColorStatic m_Static5;
最后,我在OnTimer()中
CString strRelay2;
strRelay2.Format("%d/%d/0", m_iNX, m_iActAgent);

m_Static5.SetCaption(strRelay2);
m_Static5.SetBackColor(RGB(222,223,222));

CWnd* pWnd=GetDlgItem(IDC_Agent); // 得到控件指针
CDC* pControlDC = pWnd->GetDC();
pWnd->Invalidate();
目的就是按照一定的时间动态改变m_iNX, m_iActAgent中的之,然后显示在IDC_Agent静态控件上。
通过以上代码,虽然实现了动态改变,但是每次改变的值仍然留在IDC_Agent静态控件上,以致和新改变的值重叠了。比如以前IDC_Agent静态控件上显示1,经过1秒后,变为2,这样1和2就重叠显示了,如果再过几秒改变为别的值,便会继续重叠。
那么如何解决这个问题呢,请指教。
pWnd->UpdateWindow();

解决方案 »

  1.   

    问题在这段
    void CColorStatic::OnPaint() // 重画Static
    {
    CPaintDC dc(this); // device context for painting

    CRect rect;
    GetClientRect(&rect); dc.SetBkColor(m_BackColor);
    dc.SetBkMode(TRANSPARENT);
    CFont *pFont = GetParent()->GetFont();      // 得到父窗体的字体
    CFont *pOldFont;
    pOldFont = dc.SelectObject(pFont);          // 选用父窗体的字体
    dc.SetTextColor(m_TextColor);             // 设置文本颜色
    dc.DrawText(m_strCaption, &rect, DT_LEFT);  // 将文本画在Static的左侧
    dc.SelectObject(pOldFont); // Do not call CStatic::OnPaint() for painting messages
    }
    OnPaint里面并没有做前次颜色的擦除,就事论事的讲,需要前面加一句:
    dc.FillSolidRect(&rect,RGB(100,220,255)); 颜色随便写的。
    还有,方法有些问题,但偶的确不知道你这CStatic 子类用来干嘛,与原来的CStaic有什么不同,有哪些功能差别,所以还是不多说了。
      

  2.   

    void CColorStatic::SetTextColor(COLORREF TextColor)
    {
    m_TextColor = TextColor; // 设置文字颜色
    //      Redrawthis
    }