zoukankan      html  css  js  c++  java
  • 打造自己的专业图像工具-Visual C++ 2005图像编程系列【五】(下)

    至此,所有我们需要的信息都获取到了,并进行了必要的清除工作。在CFontComboBox头文件里还有两个函数没有介绍:MeasureItemDrawItem.。熟悉MFC控件类的朋友一看就知道是自绘控件。接下来我们就看看自绘是如何进行的。在自绘CListCtrlCListBoxCComboBox时,都需要重载MeasureItemDrawItem函数,前者计算每个Item需要的高度,后者完成绘图功能。

    void CFontComboBox::MeasureItem(LPMEASUREITEMSTRUCT lpMeasureItemStruct)

    {

           int height;

           CWindowDC dc(NULL);

           TEXTMETRIC tm;

           dc.GetTextMetrics(&tm);

           height = tm.tmHeight - tm.tmInternalLeading;

           lpMeasureItemStruct->itemHeight = height - 1;

    }

           DrawItem完成绘图时,需要根据字体类型和图像类型画上不同的图像。函数的参数DRAWITEMSTRUCT结构里有我们需要的信息:

    CtlType       控件的类型

    itemID        当前绘图Item索引ID

    hDC           当前绘图Item的画图DC

    itemAction  当前绘图Item的状态(焦点、选择)

    rcItem        当前绘图Item的矩形大小

    void CFontComboBox::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)

    {

           ASSERT(lpDrawItemStruct->CtlType == ODT_COMBOBOX);

           CString strText;

           //判断当前索引的字体名称是否为空

           int nIndex = lpDrawItemStruct->itemID;

           if (GetLBTextLen(nIndex) < 0)

                  return ;

           GetLBText(nIndex, strText);

           ASSERT(!strText.IsEmpty());

           int nImage = 0;

           //根据索引值从m_pFontVec得到字体图像类型

           if (!m_pFontVec.empty())

                  nImage = m_pFontVec[nIndex]->GetImage();

           CDC dc;

           dc.Attach(lpDrawItemStruct->hDC);

           COLORREF crOldTextColor = dc.GetTextColor();

           COLORREF crOldBkColor = dc.GetBkColor();

           //如果Item处于选择焦点、状态下,用系统高亮色改变文本和背景色

           if ((lpDrawItemStruct->itemAction | ODA_SELECT) &&

                  (lpDrawItemStruct->itemState & ODS_SELECTED))

           {

                  dc.SetTextColor(::GetSysColor(COLOR_HIGHLIGHTTEXT));

                  dc.SetBkColor(::GetSysColor(COLOR_HIGHLIGHT));

                  dc.FillSolidRect(&lpDrawItemStruct->rcItem, ::GetSysColor(COLOR_HIGHLIGHT));

           }

           else

                  dc.FillSolidRect(&lpDrawItemStruct->rcItem, crOldBkColor);

           CRect     rItem(lpDrawItemStruct->rcItem);

           CRect     rText(rItem);

           CRect     rBmp(&rItem);

           if(m_FontBmp)

           {

                  //计算Item中字体预览图的贴图位置

                  rBmp.top +=  (rBmp.Height() - FNTIMG_Y) / 2;

                  rBmp.bottom = rBmp.top + FNTIMG_Y + 1;

                  rText.left += FNTIMG_X;

                  if (nImage != (int)0XFF)

                  {

                         int x,y;

                         x = nImage * FNTIMG_X;   //根据预览图类型计算源图的矩形大小

                         y = FNTIMG_Y;

                         CDC mdc;

                         mdc.CreateCompatibleDC(&dc);

                         CBitmap* pOldBmp = mdc.SelectObject(CBitmap::FromHandle(m_FontBmp));

                         COLORREF clrTransparent = mdc.GetPixel(0, y);

                         //调用TransparentBlt进行透明贴图,此函数需要加入MsImg32.Lib文件

                         ::TransparentBlt (dc.GetSafeHdc(),rBmp.left, rBmp.top, FNTIMG_X, FNTIMG_Y, 

                                                     mdc, x, y, FNTIMG_X, FNTIMG_Y, clrTransparent);

                  }

           }

           else

                  rText.left += 10;

           //rText所代表的字体名称填充位置需要右移图片的宽度

           dc.DrawText(strText, rText, DT_LEFT|DT_SINGLELINE|DT_VCENTER);

           dc.SetTextColor(crOldTextColor);

           dc.SetBkColor(crOldBkColor);

           dc.Detach();

    } 
  • 相关阅读:
    一本通1269 有限背包
    python3 threading.Lock() 多线程锁的使用
    Sqlite3错误:Recursive use of cursors not allowed 的解决方案
    linux 常用命令
    90%的人说Python程序慢,5大神招让你的代码像赛车一样跑起来
    python3 使用flask连接数据库出现“ModuleNotFoundError: No module named 'MySQLdb'”
    Navicat Premium12远程连接MySQL数据库
    pymysql pymysql.err.OperationalError 1045 Access denied最简单解决办法
    CentOS7 安装MySQL8修改密码
    CentOS7 升级Openssl的办法
  • 原文地址:https://www.cnblogs.com/hehe520/p/6330113.html
Copyright © 2011-2022 走看看