您的位置:首页 > 其它

MFC修改button的颜色、背景、边框

2016-03-10 15:11 381 查看
MFC的button控件是一个不同于其他控件,其CButtton类是CWnd的一个子类,在修改button的背景、颜色和边框的时候必须自己进行编写一个新的类,如CMyButton。下面是进行背景、颜色、边框修改的步骤:

1.在项目->添加类->CMyButton。这样会自动生成两个文件,一个.CPP文件和一个.h文件。例如CMyButton.cpp和CMyButton.h

2.在你的主工程的头文件C**Dlg.h中添加对CMyButton.h的包含,即 #include “CMyButton.h”

3.找到OnInitDialog()函数,在该函数里面添加如下代码:

 

    CMyButtonm_Btn;//定义一个CMybutton的变量,可以在其他地方进行定义,只需要包含 “CMyButton.h”即可

    

    //将按钮修改为BS_OWNERDRAW风格,允许button的采用自绘模式

   GetDlgItem(IDC_BUTTON1)->ModifyStyle(0,BS_OWNERDRAW,0);

   //绑定控件IDC_BUTTON1与类CMyButton,响应重载函数DrawItem()

   m_Btn.Attach(IDC_BUTTON1,this);

 

    //设置ButtonDown的背景色,SetDownColor()和SetUpnColor()是CMyButton类中的析构函数

   m_Btn.SetDownColor(RGB(255,0,0));

    //设置Button Up的背景色

   m_Btn.SetUpColor(RGB(0,0,255));

   注:若控件IDC_BUTTON1在以前进行过消息绑定,那么就会出现bug,只需要在映射函数DoDataExchange()中将消息绑定的语句注释掉就行了。如下:

      //DDX_Control(pDX, IDC_BUTTON1,m_cbBtn);//注释掉就可以了。

4.在新建的CMyButton.cpp文件和CMyButton.h文件中添加下面代码即可:

 

头文件:CMyButton.h如下:

#pragmaonce

#include"afxwin.h"

class CMyButton : publicCButton

{

//DECLARE_DYNAMIC(CMyButton)

   public:

  CMyButton();

   virtual~CMyButton();

   //设置ButtonDown的背景颜色

   voidSetDownColor(COLORREF color);

   //设置ButtonUp的背景颜色

   voidSetUpColor(COLORREF color);

   BOOL Attach(constUINT nID, CWnd* pParent);

  protected:

  //必需重载的函数

  virtual voidDrawItem(LPDRAWITEMSTRUCTlpDrawItemStruct);

  public:

   //三种颜色分别为文字,ButtonDown的背景颜色,Button Up的背景颜色

   COLORREFm_TextColor, m_DownColor,m_UpColor;

};

源文件:CMyButton.cpp

#include"StdAfx.h"

#include"CMyButton.h"

CMyButton::CMyButton(void)

{

  m_DownColor =m_UpColor = RGB(0,0,0);//初始化设为黑色

}

CMyButton::~CMyButton(void)

{

}

 

BOOL CMyButton::Attach(constUINT nID,CWnd* pParent)

{

    if(!SubclassDlgItem(nID, pParent))

   return FALSE;

   return TRUE;

}

voidCMyButton::SetDownColor(COLORREFcolor)

{   //CMyButton类的函数

   m_DownColor = color;

}

voidCMyButton::SetUpColor(COLORREF color)

{

   m_UpColor = color;

}

voidCMyButton::DrawItem(LPDRAWITEMSTRUCTlpDrawItemStruct)

{

   CDCdc;

  dc.Attach(lpDrawItemStruct->hDC);//得到绘制的设备环境CDC

   VERIFY(lpDrawItemStruct->CtlType==ODT_BUTTON);

  //得当Button上文字,这里的步骤是:1,先得到在资源里编辑的按钮的文字,

  //然后将此文字重新绘制到按钮上,

   //同时将此文字的背景色设为透明,这样,按钮上仅会显示文字

   const int bufSize = 512;

   TCHAR buffer[bufSize];

   GetWindowText(buffer,bufSize);

   intsize=strlen(buffer);//得到长度

  DrawText(lpDrawItemStruct->hDC,buffer,size,&lpDrawItemStruct->rcItem,DT_CENTER|DT_VCENTER|DT_SINGLELINE|DT_TABSTOP);//绘制文字

  SetBkMode(lpDrawItemStruct->hDC,TRANSPARENT);//透明

   if(lpDrawItemStruct->itemState&ODS_SELECTED)//当按下按钮时的处理

  {////重绘整个控制

        CBrushbrush(m_DownColor);

        dc.FillRect(&(lpDrawItemStruct->rcItem),&brush);//利用画刷brush,填充矩形框

       //因为这里进行了重绘,所以文字也要重绘

       DrawText(lpDrawItemStruct->hDC,buffer,size,&lpDrawItemStruct->rcItem,DT_CENTER|DT_VCENTER|DT_SINGLELINE|DT_TABSTOP);

        SetBkMode(lpDrawItemStruct->hDC,TRANSPARENT);

   }

  else//当按钮不操作或者弹起时

   {

          CBrushbrush(m_UpColor);

          dc.FillRect(&(lpDrawItemStruct->rcItem),&brush);//

          DrawText(lpDrawItemStruct->hDC,buffer,size,&lpDrawItemStruct->rcItem,DT_CENTER|DT_VCENTER|DT_SINGLELINE|DT_TABSTOP);

          SetBkMode(lpDrawItemStruct->hDC,TRANSPARENT);

    }

    if((lpDrawItemStruct->itemState&ODS_SELECTED)&&(lpDrawItemStruct->itemAction&(ODA_SELECT|ODA_DRAWENTIRE)))

    {//选中了本控件,高亮边框

             COLORREFfc=RGB(255-GetRValue(m_UpColor),255-GetGValue(m_UpColor),255-GetBValue(m_UpColor));

           CBrush brush(fc);

          dc.FrameRect(&(lpDrawItemStruct->rcItem),&brush);//用画刷brush,填充矩形边框

      }

    if (!(lpDrawItemStruct->itemState&ODS_SELECTED)&&(lpDrawItemStruct->itemAction& ODA_SELECT))

        {

         CBrushbrush(m_UpColor); //控制的选中状态结束,去掉边框

       dc.FrameRect(&lpDrawItemStruct->rcItem,&brush);//}

      dc.Detach();

}

关于改变AfxMessageBox对话框标题
   其实这个标题,也就是我们的默认的工程名,我们应该怎么在不改变工程名的基础上改变标题呢?其实这个标题在资源StringTable里就能找到,查找AFX_IDS_APP_TITLE,在这里你就能轻而易举的改变标题了.

转载自:http://blog.sina.com.cn/s/blog_65cab32d01013uad.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: