想美化对话框界面,怎么调背景及控件如按钮,编辑框的背景色

解决方案 »

  1.   

    按钮的背景色调不了...除非自画
    其它的可以如下:HBRUSH CZilchDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) 
    {
       HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
       if (nCtlColor==CTLCOLOR_DLG){
        //设置对话框的画刷...
        hbr=CreateSolidBrush(RGB(255,0,0))
       }   return hbr;
    }
    具体请看MSDN
      

  2.   

    能具体点吗?我的MSDN上没有呀
      

  3.   

    CWnd::OnCtlColor  
    afx_msg HBRUSH OnCtlColor( CDC* pDC, CWnd* pWnd, UINT nCtlColor );Return ValueOnCtlColor must return a handle to the brush that is to be used for painting the control background.ParameterspDCContains a pointer to the display context for the child window. May be temporary.pWndContains a pointer to the control asking for the color. May be temporary.nCtlColorContains one of the following values, specifying the type of control: CTLCOLOR_BTN   Button control
    CTLCOLOR_DLG   Dialog box
    CTLCOLOR_EDIT   Edit control
    CTLCOLOR_LISTBOX   List-box control
    CTLCOLOR_MSGBOX   Message box
    CTLCOLOR_SCROLLBAR   Scroll-bar control
    CTLCOLOR_STATIC   Static control 
    ResThe framework calls this member function when a child control is about to be drawn. Most controls send this message to their parent (usually a dialog box) to prepare the pDC for drawing the control using the correct colors.To change the text color, call the SetTextColor member function with the desired red, green, and blue (RGB) values. To change the background color of a single-line edit control, set the brush handle in both the CTLCOLOR_EDIT and CTLCOLOR_MSGBOX message codes, and call the CDC::SetBkColor function in response to the CTLCOLOR_EDIT code.OnCtlColor will not be called for the list box of a drop-down combo box because the drop-down list box is actually a child of the combo box and not a child of the window. To change the color of the drop-down list box, create a CComboBox with an override of OnCtlColor that checks for CTLCOLOR_LISTBOX in the nCtlColor parameter. In this handler, the SetBkColor member function must be used to set the background color for the text.Note   This member function is called by the framework to allow your application to handle a Windows message. The parameters passed to your function reflect the parameters received by the framework when the message was received. If you call the base-class implementation of this function, that implementation will use the parameters originally passed with the message and not the parameters you supply to the function.Example// This OnCtlColor handler will change the color of a static control
    // with the ID of IDC_MYSTATIC. The code assumes that the CMyDialog
    // class has an initialized and created CBrush member named m_brush.
    // The control will be painted with red text and a background
    // color of m_brush.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;
    }CWnd Overview |  Class Members |  Hierarchy ChartSee Also   CDC::SetBkColor
      

  4.   

    编辑框的用上面的方法:
    按钮的重载OnDrawItem,传入的结构DRAWITEMSTRUCT 里有个HDC,可以用随便画:
    void CMyButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct) 
    {   // This code only works with buttons.
       ASSERT(lpDrawItemStruct->CtlType == ODT_BUTTON);
    ...
       ::SetBkColor(lpDrawItemStruct->hDC,RGB(0,0,255));
    ....}
      

  5.   

    phoenix96_2000(Jack Of All Trades) 
    你能否说具体点呀,CMyButton是否是给"按钮"的加的类,还是别的?
    // This code only works with buttons.
       ASSERT(lpDrawItemStruct->CtlType == ODT_BUTTON);
    ...
       ::SetBkColor(lpDrawItemStruct->hDC,RGB(0,0,255));
    ....
    上面省略号应该填写什么?
      

  6.   

    调用CWinApp : : SetDialogBkColor可以改变所有应用程序的背景颜色。第一个参数指定了背景颜色,第二个参数指定了文本颜色。下例将应用程序对话设置为蓝色背景和黄色文本。 
    BOOL CSampleApp : : InitInstance ( ) 

    … 
     
    //use blue dialog with yellow text . 
    SetDialogBkColor (RGB (0, 0, 255 ), RGB ( 255 ,255 , 0 ) ) 
     
    … 

     
    需要重画对话(或对话的子控件)时,Windows向对话发送消息WM_CTLCOLOR,通常用户可以让Windows选择绘画背景的刷子,也可重置该消息指定刷子。下例说明了创建一个红色背景对话的步骤。 
     
    首先,给对话基类增加一人成员变量 
    CBursh :class CMyFormView : public CFormView 

    … 
     
    private : 
    CBrush m_ brush // background brush 
     
    … 

     
    其次, 在类的构造函数中将刷子初始化为所需要的背景颜色。 
    CMyFormView : : CMyFormView ( ) 

    // Initialize background brush . 
    m_brush .CreateSolidBrush (RGB ( 0, 0, 255) ) 

     
    最后,使用ClassWizard处理WM_CTLCOLOR消息并返回一个用来绘画对话背景的刷子句柄。注意:由于当重画对话控件时也要调用该函数,所以要检测nCtlColor参量。 
    HBRUSH CMyFormView : : OnCtlColor (CDC* pDC , CWnd*pWnd , UINT nCtlColor 

     

    // Determine if drawing a dialog box . If we are, return +handle to 
    //our own background brush . Otherwise let windows handle it . 
    if (nCtlColor = = CTLCOLOR _ DLG ) 
    return (HBRUSH) m_brush.GetSafeHandle ( ) 
    return CFormView : : OnCtlColor (pDC, pWnd , nCtlColor 


      

  7.   

    有两种方法。其一,可以在父类中指定控件的颜色,或者利用MFC4.0新的消息反射在控件类中指定颜色。 当控件需要重新着色时,工作框调用父窗口(通常是对话框)的CWnd: : OnCrtlColor,可以在父窗口类中重置该函数并指定控件的新的绘画属性。例如,下述代码将对话中的所有编辑控件文本颜色改为红色: 
    HBRUSH CAboutDig : : OnCtlColor (CDC * pDCM , CWnd * pWnd , UINT nCtlColor) 
     

    HBRUSH hbr = CDialog : : OnCtlColor (pDC, pWnd , nCtlColor ) 
     
    //Draw red text for all edit controls . 
    if (nCtlColor= = CTLCOLOR_EDIT ) 
    pDC —> SetTextColor (RGB (255, 0 , 0 , ) ) 
     
    return hbr 

     
    然而,由于每个父窗口必须处理通知消息并指定每个控件的绘画属性,所以,这种方法不是完全的面向对象的方法。控件处理该消息并指定绘画属性更合情合理。消息反射允许用户这样做。通知消息首先发送给父窗口,如果父窗口没有处理则发送给控件。创建一个定制彩色列表框控件必须遵循下述步骤。 
     
    首先,使用ClassWizard 创建一个CListBox 的派生类并为该类添加下述数据成员。 
    class CMyListBox publilc CListBox 

    … 
    private 
    COLORREF m_clrFor // foreground color 
    COLORREF m_clrBack //background color 
    Cbrush m_brush //background brush 
    … 

    其次,在类的构造函数中,初始化数据中。 
    CMyListBox : : CMyListBox () 

    //Initialize data members . 
    m_clrFore =RGB (255 , 255 , 0) //yellow text 
    m_clrBack=RGB (0 , 0 , 255) // blue background 
    m_brush . CreateSolidBrush (m _clrBack ) 

     
    最后,使用ClassWizard处理反射的WM_CTLCOLOR(=WM_CTLCOLOR)消息并指定新的绘画属性。 
    HBRUSH CMyListBox : : CtlColor (CDC* pDC, UINT nCtlColor ) 

    pDC—>SetTextColor (m_clrFore) 
    pDC—>SetBkColor (m_clrBack) 
     
    return (HBRUSH) m_brush.GetSafeHandle () 

    现在,控件可以自己决定如何绘画,与父窗口无关。 
      

  8.   

    二、通过定制来实现不同颜色按纽。以下通过定制方形彩色按纽来说明: 第一步:派生出自己的按纽类。//CcolorButton.h class CColorButton : public CButton{DECLARE_DYNAMIC(CColorButton)public:CColorButton(); virtual ~CColorButton(); 
    BOOL Attach(const UINT nID, CWnd* pParent, const COLORREF BGColor = RGB(192, 123, 192), // 按纽的背景色const COLORREF FGColor = RGB(1, 1, 1), // 文本颜色);protected:virtual void DrawItem(LPDRAWITEMSTRUCT lpDIS); //重定义虚拟函数DrawItemvoid DrawFrame(CDC *DC, CRect R); //绘制按纽框void DrawFilledRect(CDC *DC, CRect R, COLORREF color); //填充按纽框void DrawLine(CDC *DC, CRect EndPoints, COLORREF color); void DrawLine(CDC *DC, long left, long top, long right, long bottom, COLORREF color);void DrawButtonText(CDC *DC, CRect R, const char *Buf, COLORREF TextColor);//绘制按纽上的文本
    COLORREF GetFGColor() { return m_fg; } COLORREF GetBGColor() { return m_bg; }private:COLORREF m_fg, m_bg;};#endif
    第二步:定义各函数//CcolorButton.cpp……// CColorButtonIMPLEMENT_DYNAMIC(CColorButton, CButton)CColorButton::CColorButton() { } CColorButton::~CColorButton(){} 
    //定义Attach()函数BOOL CColorButton::Attach(const UINT nID, CWnd* pParent, const COLORREF BGColor, const COLORREF FGColor){if (!SubclassDlgItem(nID, pParent))return FALSE;m_fg = FGColor;m_bg = BGColor; return TRUE;}
    //重载DrawItem()void CColorButton::DrawItem(LPDRAWITEMSTRUCT lpDIS){CDC* pDC = CDC::FromHandle(lpDIS->hDC);UINT state = lpDIS->itemState; CRect focusRect, btnRect;focusRect.CopyRect(&lpDIS->rcItem); //按纽的选中虚线框btnRect.CopyRect(&lpDIS->rcItem); 
    // 设置表示按纽被选中的虚线框focusRect.left += 4;focusRect.right -= 4;focusRect.top += 4;focusRect.bottom -= 4;
    // 按纽标题const int bufSize = 512;TCHAR buffer[bufSize];GetWindowText(buffer, bufSize);
    // 绘制并标志按纽 DrawFilledRect(pDC, btnRect, GetBGColor()); DrawFrame(pDC, btnRect);DrawButtonText(pDC, btnRect, buffer, GetFGColor());
    // 如果按纽处于选中状态则在其上绘制选中虚线框if (state & ODS_FOCUS) {DrawFocusRect(lpDIS->hDC, (LPRECT)&focusRect);}}void CColorButton::DrawFrame(CDC *DC, CRect R){ //绘制按纽,用户通过定制该函数可实现不同形状的按纽。DrawLine(DC, R.left, R.top, R.right, R.top, RGB(255, 255, 255)); DrawLine(DC, R.left, R.top, R.left, R.bottom, RGB(255, 255, 255)); //以下绘制按纽的外围框线以使按纽有立体感DrawLine(DC, R.left + 1, R.bottom - 1, R.right, R.bottom - 1, RGB(1, 1, 1));//绘制按纽左框线和上框线DrawLine(DC, R.right - 1, R.top + 1, R.right - 1, R.bottom, RGB(1, 1, 1));//绘制按纽右框线和下框线 }//用色彩填充按纽框void CColorButton::DrawFilledRect(CDC *DC, CRect R, COLORREF color){ CBrush B;B.CreateSolidBrush(color);DC->FillRect(R, &B);}// DrawLine用于绘制按纽,其为多态函数void CColorButton::DrawLine(CDC *DC, CRect EndPoints, COLORREF color){ ……}void CColorButton::DrawLine(CDC *DC, long left, long top, long right, long bottom, COLORREF color){ ……}//绘制按纽文本void CColorButton::DrawButtonText(CDC *DC, CRect R, const char *Buf, COLORREF TextColor){COLORREF prevColor = DC->SetTextColor(TextColor);DC->SetBkMode(TRANSPARENT);DC->DrawText(Buf, strlen(Buf), R, DT_CENTER|DT_VCENTER|DT_SINGLELINE);DC->SetTextColor(prevColor);}
    第三步:引用定制类定制任意对话框CColorDlg,在其上画一按键控件。ID为IDOK。//CColorDlg.hclass CColorDlg : public CDialog{…..// Implementationprotected:CColorButton m_btnOK;}
    //CColorDlg.cpp…….BOOL CColorBtnSampleDlg::OnInitDialog(){CDialog::OnInitDialog();…….VERIFY(m_btnOK.Attach(IDOK, this, RED, BLUE, YELLOW));…….}
      

  9.   

    只能对动态生成的按钮进行颜色设置吗?
    对话框上已经放置的按钮怎么设置?
    还有STATIC控件怎么设置
      

  10.   

    可以用CWinApp::SetDialogBkColor()来改变对话框背景色,
    如果要改变按钮颜色你可能要自定义类了,不过推荐CBustonST类。
      

  11.   

    谁能详细介绍一下怎样改变按钮,静态标签(static)的颜色?