大侠们给我讲讲过程吧

解决方案 »

  1.   

    //摘自http://sanjianxia.myrice.com/vc/vc37.htm第一步:派生出自己的按纽类。//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));…….}
      

  2.   

    COLORREF SetBkColor(
      HDC hdc,           // handle of device context
      COLORREF crColor   // background color value
    );
      

  3.   

    看看这个网址里面有详细的介绍:
    http://www.cetinet.com/t_article/list.asp?indexid=1662
      

  4.   

    要不要这么复杂呀,只是改背景,那就用WM_CTLCOLOR吧。要改DC的东西,也可以在参数里改。
    比如文字(背景,颜色等,字型)。
      

  5.   

    SDK下一般的方法无法实现,必须采用挂钩的方式从底层重画
    http://www.vccode.com/file_show.php?id=2565
      

  6.   

    to  "要不要这么复杂呀,只是改背景,那就用WM_CTLCOLOR吧"
    一般的控件都可以通过WM_CTLCOLOR进行背景颜色,字体颜色等改变,但是按钮是比较特殊的一个,它只能够进行重绘才能够实现的。没有简单的方法的。
    你想简单的话可以调用网上别人写好的按钮类直接用来使用,网上有很多的。
      

  7.   

    晕,说过多少回了,WM_CTLCOLOR不能改变按钮的背景
      

  8.   

    最简单的,在主窗口得wm_paint里边画就行
      

  9.   

    wm_paint里面怎么画?能事例一下吗?
    //
    按钮字体我用sendmessage就实现了
    颜色就这么费尽阿
      

  10.   

    假设你的button的HWND为hButton;
    wm_paint:
    {
      HDC hdcButton = GetDC( hButton );
      RECT rt;
      GetClientRect( hButton, &rt );
      SetBkMode( hdcButton,OPAQUE )//  如果只是单色
      SetBkColor( hdcButton,RGB(...) );
     DrawText( hcButton,..,&rt,. );
     DeleteDC( hdcButton );
    }如果背景是图片,将
    SetBkMode( hdcButton,OPAQUE )//  如果只是单色
      SetBkColor( hdcButton,RGB(...) );
    替换为
    BitBlt( hdcButton, 0, 0, rt.right - rt.left, rt.botton-rt.top,
      hdcMem, 0, 0, SRCCOPY );
    SetBkMode( hdcButton,TRANSPARENT );
      

  11.   

    建议使用自画的风格,
    这样做起来比较简单,
    如果想要同时输出文字的话,
    也可以在直接修改图片(在生成好的图片中写字)来达到最后的效果
    自画的方式msdn中有详细的介绍
    如果实在实现不了或者不清楚,给我短消息
      

  12.   

    to MikeChen2003(asdghgdhf) 
    我按你的方法做了好像不行
    to
    lygfqy(风清扬) 
    你说的自画是不是创建按钮的时候用BS_OWNERDRAW属性
    我现在用bmp文件可以实现
    但是要在bmp上输出文件效果显示好不是很方便
    能不能直接设置颜色,然后显示文字用按钮的caption?
      

  13.   

    sorry,真的不行
    可能还是得自画,然后在WM_DRAWITEM里DrawText,应该没问题了吧??
      

  14.   

    DrawText,这个我做过
    但是背景颜色只是文字覆盖范围的背景颜色,也就是背景颜色没有设置整个按钮的颜色
      

  15.   

    WM_CTLCOLORBTN
    if( (HWND)lParam != GetDlgItem( hWnd, ID_STATUSBAR ) )
    {
    hdc = (HDC) wParam;
    SetBkColor(hdc,CR_STATUSBAR);
    SetTextColor(hdc,CR_TEXTCOLOR);//此处设置字体的颜色
    return (LONG)CreateSolidBrush( CR_BUTTONCOLOR );
    }
      

  16.   

    靠,没写完就发了上一个不算
    今天由于工作需要,所以试验了一下:
    case WM_CTLCOLORBTN:if( (HWND)lParam != GetDlgItem( hWnd, ID_STATUSBAR ) )
    {
    hdc = (HDC) wParam;
    SetBkColor(hdc,CR_STATUSBAR);
    SetTextColor(hdc,CR_TEXTCOLOR);//此处设置字体的颜色
    return (LONG)CreateSolidBrush( CR_BUTTONCOLOR );
    }这样事可以的,CR_BUTTONCOLOR 就是你的背景色,但是有两个阴影边改变不了如果用WM_DRAWITEM,你可以用getwindowrect获取矩形,再drewtext,或者先fillrect,也是可以得,但是不知道为什么这样用过了一段时间就获取不了 hdc了,怪得很