有一个单文档的程序,可以弹出一个对话框,对话框里有一个文本框,我想实现文本框的背景色随着时间改变,不知道怎么做。请哪位大侠帮帮忙,最好能提供源代码。 
 
 

解决方案 »

  1.   

    自绘吧 或者在OnctlColor消息中  获得你要改变控件的ID 自绘
      

  2.   

    响应对话框的WM_CTLCOLOR消息
    判断nCtlColor=CTLCOLOR_EDIT,然后调用pDC->SetBkColor设置颜色即可
    如果存在多个文本编辑框,则需要通过pWnd->GetDlgCtrlID获得控件ID,然后比较是不是你要变色的控件,然后再调用pDC->SetBkColor
    就是这样……
      

  3.   

    全局变量  
    int m_icolor;
    初始化中
    m_icolor = rand()%256;void CTest::OnButton1()   
    {
    // TODO: Add your control notification handler code here
    SetTimer(2,1000,NULL);}void CTest::OnTimer(UINT nIDEvent)   
    {
    // TODO: Add your message handler code here and/or call default
    if (nIDEvent==2)
    {  
    m_icolor = rand()%256;//产生随机颜色
    }
    CDialog::OnTimer(nIDEvent);
    }
    HBRUSH CTest::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)   
    {
    HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
    // TODO: Change any attributes of the DC hereif(nCtlColor==CTLCOLOR_STATIC) //修改静态文本颜色
    {
     pDC->SetTextColor(m_icolor ,rand()%256,rand()%256));
     pDC->SetBkColor(m_icolor ,rand()%256,rand()%256));
     HBRUSH b=CreateSolidBrush(RGB(m_icolor ,rand()%256,rand()%256));
     return b;
    }
    return hbr;
    }
    }
      

  4.   

    我想用Settimer 做定时器,然后接受消息,改变文本框的背景色,不知道怎么自绘?这是我的代码
    void CTest::OnButton1() 
    {
    // TODO: Add your control notification handler code here
    SetTimer(2,1000,NULL);}void CTest::OnTimer(UINT nIDEvent) 
    {
    // TODO: Add your message handler code here and/or call default
    if (nIDEvent==2)
    { i=i+9;}
    CDialog::OnTimer(nIDEvent);
    }
    HBRUSH CTest::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) 
    {
    HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
    if(nCtlColor==CTLCOLOR_STATIC) //修改静态文本颜色
    {
     pDC->SetTextColor(RGB(rand()%256,rand()%256,rand()%256));
     pDC->SetBkColor(RGB(rand()%256,rand()%256,rand()%256));
     HBRUSH b=CreateSolidBrush(RGB(rand()%256,rand()%256,rand()%256));
     return b;
    } // TODO: Return a different brush if the default is not desired
     return hbr;

    // TODO: Change any attributes of the DC here  if ((pWnd->GetDlgCtrlID() == IDC_EDIT1) && (nCtlColor == CTLCOLOR_EDIT))
      {
         
        COLORREF clr = RGB(0,i,0);
          pDC->SetBkColor(clr);     //设置黑色的背景
          m_brMine = ::CreateSolidBrush(clr);
          return m_brMine;  //作为约定,返回背景色对应的刷子句柄
      }
      else
      {
          HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor); // TODO: Return a different brush if the default is not desired
    return hbr;
    }
    }