• MFC Button控件自绘


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

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

       比如说

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

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

      DrawItem函数 进行控件的绘制   所以说 自绘制 就2个步骤   

    •   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(); //是局部无效引起重画 
    }
  • 相关阅读:
    【权限维持】window几种隐藏技术
    Flash XSS 漏洞实例
    nginx_lua_waf 部署、测试记录
    WAF Bypass数据库特性(Access探索篇)
    WAF Bypass数据库特性(MSsql探索篇)
    WAF Bypass数据库特性(Mysql探索篇)
    WAF Bypass数据库特性(Oracle探索篇)
    WAF Bypass 笔记(SQL注入篇)
    如何关闭Struts2的webconsole.html
    Windows Server 2008 R2 WSUS服务器的详细配置和部署
  • 原文地址:https://www.cnblogs.com/wuyuan2011woaini/p/4286461.html
Copyright © 2020-2023  润新知