MFC Button控件自绘制----详细讲解

来源:互联网 发布:娇韵诗淘宝旗舰店真假 编辑:程序博客网 时间:2024/06/06 02:24

VC下的界面着实难看 有时候我们不得不自己进行控件的绘制 以前 一直不理解最近再次看了学了一遍终于明白了一点   与大家分享下...       需要源代码的Q我 寻找一起学VC的朋友 

   比如说

我们要改变一个编辑框的背景 我们响应WM_CTLCOLOR函数 进行OnCtlColor进行修改但是对与 Button控件就不行了 ..     

这时候我们要进行自绘制   

相关函数  

virtual void DrawItem( LPDRAWITEMSTRUCT lpDrawItemStruct );

  要覆盖掉这个虚函数,并且类型要设置为BS_OWNERDRAW,应用程序进行初始化界面的时候,会进入我们的 DrawItem函数进行控件的绘制 .所以说,自绘制就2个步骤    

  ASSERT 宏       Evaluate an expression and generate a debug report when the result is FALSE (debug version only).       计算表达是当结果是false的时候生成调试报告 (仅仅在debug下 )  

1. 类型要设置为 BS_OWNERDRAW

2.重写 virtual void DrawItem( LPDRAWITEMSTRUCT lpDrawItemStruct );函数   代码就我们自己设计了

 

重绘需要的函数  注意 都是SDK中的函数:      

BOOL DrawFrameControl(   //这个函数画一个指定类型控件的框架  

HDC hdc,     // handle to device context  DC  

LPRECT lprc, // bounding rectangle   举行区域   

UINT uType,  // frame-control type   类型  

UINT uState  // frame-control state  状态 具体看MSDN 

);   

 int DrawText(   //在指定的矩形区域 输出文本  

HDC hDC,          // handle to DC  

LPCTSTR lpString, // text to draw  

int nCount,       // text length  

LPRECT lpRect,    // formatting dimensions   UINT uFormat      // text-drawing options

);

COLORREF SetTextColor(  //设置指定DC的文本颜色   

HDC hdc,           // handle to DC  

COLORREF crColor   // text color

);

int FillRect(  // 用给定画刷填充矩形区域   

 HDC hDC,           // handle to DC  

CONST RECT *lprc,  // rectangle   HBRUSH hbr         // handle to brush

);

int SetBkMode(    //设置背景模式   TRANSPARENT透明   

HDC hdc,      // handle to DC   int iBkMode   // background mode

);

typedef struct tagDRAWITEMSTRUCT {    //具体看MSDN   

UINT      CtlType;      //控件类型   

UINT      CtlID;    //id  

UINT      itemID;    //项ID    

UINT      itemAction;  行为      

 UINT      itemState;  //状态   

 HWND      hwndItem;    //控件句柄  

 HDC       hDC;    //dc句柄   

RECT      rcItem;   //举行区域   

ULONG_PTR itemData;    } DRAWITEMSTRUCT  ;

          Draw3dRect       (    

LPCRECT lpRect,       

COLORREF clrTopLeft,    

COLORREF clrBottomRight     

);    此函数用于实现绘制3D矩形的位置大小,其中lpRect是填入整个3D矩形的位置大小,   clrTopLeft和clrBottomRight分别是3D效果中左上方和右下方的颜色RGB的值。

   BOOL DrawFocusRect     (  画一个虚线矩形     

HDC hDC,          // handle to device context   

  CONST RECT* lprc  // logical coordinates     );       数功能: 画一个焦点矩形。这个矩形是在标志焦点的样式中通过异或运算完成的(焦点通常用一个点线表示)。    如用同样的参数再次调用这个函数,就表示删除焦点矩形

 

下面是程序代码: 

   void  CBtnXiaoWei::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)

 {

CString btnCaption;  //保存button标题 

GetWindowText(btnCaption);  //获得button标题 

CRect drawRect; //定义CRect对象

HDC dc= lpDrawItemStruct->hDC;//控件DC

CDC*pDC=CDC::FromHandle(dc);//获得CDC指针 通过 HDC

UINT nStyle=lpDrawItemStruct->CtlType;

drawRect.CopyRect(&(lpDrawItemStruct->rcItem)); //拷贝控件矩形区域到我们的CRect对象   

 DrawFrameControl(dc,&drawRect,DFC_MENU,nStyle); //绘制控件框架 

CBrush pBrush;//创建画刷

static int n=0;

pBrush.CreateSolidBrush(RGB(100+n,130,n)); //创建

pDC->FillRect(drawRect,&pBrush);//画矩形

pDC->SetTextColor(m_clo); //设置文本颜色

CRect textRect;//定义一个CRect用于绘制文本

 textRect.CopyRect(&drawRect); //拷贝矩形区域

CSize sz=pDC->GetTextExtent(btnCaption);//获得字符串尺寸

textRect.top+=(textRect.Height()-sz.cy)/2;//调整文本位置 居中

pDC->SetBkMode(TRANSPARENT);//设置文本背景透明

pDC->DrawText(btnCaption,&textRect,DT_RIGHT|DT_CENTER|DT_BOTTOM);//绘制文本

n+=10;

 }

 

void CBtnXiaoWei::SetTextColer(COLORREF clo) { m_clo=clo; Invalidate(); //是局部无效引起重画  }