我在程序的主界面(基于 DIALOG 写的程序)上,定义了一个 static text 静态文本框。我现在想把那个静态文本框的背景做成蓝色(即那部分显示成蓝屏)。是不是需要用GDI 里的brush 或是 pen自己来画?谢谢!请达人指点一下。。

解决方案 »

  1.   

    写WM_CTLCOLOR事件改变背景颜色
    HBRUSH CTestDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) 
    {
    HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
        // TODO: Change any attributes of the DC here
        if(pWnd->GetDlgCtrlID()==IDC_STATIC) 

            pDC->SetTextColor(RGB(255,0,0)); 
            pDC->SetBkColor(RGB(0,0,255)); 
            return  ::CreateSolidBrush(RGB(0,0,255));// 返回的是你希望的颜色
    } }
      

  2.   

    Details see MSDN, Here's a sample:HBRUSH CZilchDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) 
    {
       // Call the base class implementation first! Otherwise, it may
       // undo what we're trying to accomplish here.
       HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);   // Are we painting the IDC_MYSTATIC control? We can use
       // CWnd::GetDlgCtrlID() to perform the most efficient test.
       if (pWnd->GetDlgCtrlID() == IDC_MYSTATIC)
       {
          // Set the text color to red
          pDC->SetTextColor(RGB(255, 0, 0));      // Set the background mode for text to transparent 
          // so background will show thru.
          pDC->SetBkMode(TRANSPARENT);      // Return handle to our CBrush object
          hbr = m_brush;
       }   return hbr;
    }
      

  3.   

    OK,可以了! 太感谢楼上的几位哥们了:)写了一个响应WM_CTLCOLOR消息的函数。新的问题同时也产生了:我的程序运行后,在static text 静态文本框,确实是显示了蓝色的背景(蓝屏)。但当我点击程序界面上的一个BUTTON(打开一幅图片),在static text 静态文本框显示出了一幅图片,我现在需要再实现,当我点击界面上的另外一个BUTTON(关闭图片显示)后,static text 静态文本框又恢复成最开始的蓝屏(即再执行一次响应WM_CTLCOLOR消息的函数)。 如何实现呢? 我觉得应该比较简单吧不好意思啊!刚学VC,菜得很呢。。 呵呵
    再次感谢楼上的几位VC达人!
      

  4.   

    我是楼主。 按我上面的描叙,是不是只要在我的BUTTON(关闭图片显示)函数里,发一个WM_CTLCOLOR的消息,就可以了? SendMessage(m_hWnd,WM_CTLCOLOR,?,?),后面的这两个参数该怎么填呢?SendMessage(
      (HWND) hwnd, 
      WM_CTLCOLOR ,
      (WPARAM) wParam, 
      (LPARAM) lParam
    );Parameters
    hwnd 
    Handle to destination window. 
    wParam 
    Handle to a display context (DC). 
    lParam 
    Handle to a child window (control). 这是MSDN上,关于发送WM_CTLCOLOR消息的说明。 哪位达人可以告诉我,后面的那两个参数我该怎么写呢??
    thanks....