MFC Button控件自绘

文章参考地址:  http://blog.csdn.net/yue7603835/article/details/6649458框架

   VC下的界面着实难看 有时候咱们不得不本身进行控件的绘制 之前 一直不理解最近再次看了学了一遍终于明白了一点
  与你们分享下...       须要源代码的Q我 寻找一块儿学VC的朋友 函数

   好比说spa

  咱们要改变一个编辑框的背景 咱们响应WM_CTLCOLOR函数 进行OnCtlColor进行修改可是对与 Button控件就不行了 ..
  这时候咱们要进行自绘制    相关函数   virtual void DrawItem( LPDRAWITEMSTRUCT lpDrawItemStruct );.net

  要覆盖掉这个虚函数  而且类型要设置为 BS_OWNERDRAW 这时候 放 应用程序进行初始化界面的时候 会进入咱们的设计

  DrawItem函数 进行控件的绘制   因此说 自绘制 就2个步骤    3d

  •   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 
    );   
   //函数功能: 画一个焦点矩形。这个矩形是在标志焦点的样式中经过异或运算完成的(焦点一般用一个点线表示)。 
   //如用一样的参数再次调用这个函数,就表示删除焦点矩形

下面是程序代码: code

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(); //是局部无效引发重画 
}