⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 05o012.htm

📁 VC知识库5_chm_decompile_20040520_210715
💻 HTM
字号:
<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta name="GENERATOR" content="Microsoft FrontPage 4.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<title></title>
<link rel="stylesheet" type="text/css" href="../../vckbase.css">
</head>

<body>

<div align="justify">
  <table border="0" width="100%" class="font" height="57">
    <tr>
      <td width="27%" height="6" class="bigfont" bgcolor="#B8CFE7" align="center" bordercolor="#800080">
      <font color="#800080">VC知识库(五)</font>
      </td>
      <td width="73%" height="6" class="bigfont" bgcolor="#B8CFE7" align="center" bordercolor="#800080">
      <font color="#800080">www.vckbase.com</font>
      </td>
    </tr>
    <tr>
      <td width="100%" height="4" class="header" valign="top" align="center" colspan="2">
      <hr>
      </td>
    </tr>
    <tr>
      <td width="100%" height="17" class="header" valign="top" align="center" colspan="2">
<small>
Win32中新增GDI对象路径及其在文字特显方面的应用</small>
      </td>
    </tr>
    <tr>
      <td width="100%" height="17" class="info" align="center" colspan="2">
      <small>张俊锋</small>
      </td>  
    </tr>  
    <tr> 
      <td width="100%" height="22" class="font" colspan="2">
        <hr>
      </td>  
    </tr> 
    <tr> 
      <td width="100%" height="5" class="font" colspan="2"> 
  <p><font size="3">
  1 路径的概念<br> 
  在Windows 95/NT 这样的Win32操作系统中,除了已有的位图,画笔,画刷,字体,调色板和区域之外,还增加了一个新的GDI对象:路径。路径是可以被填充,画出轮廓或同时被画出轮廓并填充的一个或多个图形。路径的引入,大大地丰富了Windows的图形功能,使得应用程序可以方便地建立复杂区域,绘制和填充不规则图形。这里说的不规则图形是指由直线和贝塞尔曲线组成的图形(相对于矩形,多边形,椭圆等规则图形)。<br> 
  <br> 
  2 路径的使用<br> 
  与其它原有的GDI对象不同的是,MFC类库没有专门用一个C++类来封装路径对象(或许在以后的版本中会得到支持)。有关路径的定义和使用等各种操作都必须通过调用API函数(或CDC类中对应的成员函数)来实现。<br> 
  路径的使用过程大致如下:<br> 
  (1)调用BeginPath()函数开始路径定义;<br> 
  (2)调用GDI绘图函数来定义路径;<br> 
  在Win32中,可以用于定义路径的GDI绘图函数包括:<br> 
  AngleArc Arc ArcTo Chord *CloseFigure<br> 
  Ellipse *ExtTextOut *LineTo *MoveToEx Pie<br> 
  *PolyBezier *PolyBezierTo PolyDraw *Polygon *Polyline<br> 
  *PolyLineTo *PolyPolygon *PolyPolylin Rectangl RoundRect<br> 
  *TextOut<br> 
  其中,在Windows 95中只能使用上述带*的GDI函数。<br> 
  (3)调用EndPath()函数结束路径定义;<br> 
  完成路径定义后,所定义的路径即被同时选进设备描述表,设备描述表中原有的路径对象在调用BeginPath()函数开始路径定义时即被废弃。<br> 
  (4)使用路径对象。<br> 
  完成路径定义工作之后,应用程序便可以利用有关GDI函数来使用路径,这些函数包括绘制路径轮廓StrokePath(),填充路径FillPath(),绘制轮廓并填充StrokeAndFillPath(),把路径转换成区域PathToRegion(),把路径直线化FlattenPath(),提取路径数据GetPath(),加宽路径WidenPath()和设置裁剪路径SelectClipPath()等。这些函数的具体使用方法可参阅有关的SDK文档。<br> 
  <br> 
  3 应用举例<br> 
  路径的引入为我们在应用程序中定义复杂区域提供了极大的方便,而不再局限于直线和椭圆弧这两种线形,这一点是很容易理解的。<br> 
  另外,注意到在定义路径时可以使用TextOut()和ExtTextOut()函数,我们便可以在文字特色显示方面巧妙地使用路径,克服以往文字特显对位图操作的倚赖,从而方便快捷地制作出堪与WPS和Word等文字处理软件相媲美的“艺术汉字”来。<br> 
  本文下面所提供的这个示例程序执行后,在窗口中显示出按正弦曲线起伏排列的“龙腾虎跃”五个楷体大字。窗口背景为灰色,文字前景则为一幅256色位图,就好象是把彩图剪成文字粘贴在窗口上一样(见下图)。下面具体说明该示例程序的创建方法。<br> 
  <br> 
  (1)启动VC++,创建一个单文档应用,项目名取为Path,其它选项保留原缺省设置。<br> 
  (2)在CPathView类中增加一个成员变量:<br> 
  // PathView.h : interface of the CPathView class<br> 
  ……<br> 
  class CPathView : public CView<br> 
  {<br> 
  ……<br> 
  // Implementation<br> 
  public:<br> 
  CFont m_fontKaiTi;<br> 
  ……<br> 
  并在CPathView类的构造函数中创建该Cfont对象,在CPathView类的析构函数中撤消该Cfont对象:<br> 
  // PathView.cpp : implementation of the CPathView class<br> 
  ……<br> 
  CPathView::CPathView()<br> 
  {<br> 
  // TODO: add construction code here<br> 
  m_fontKaiTi.CreateFont(200 , 0 , 0 , 0 , FW_BLACK , <br> 
  FALSE , FALSE , FALSE ,<br> 
  GB2312_CHARSET , <br> 
  OUT_DEFAULT_PRECIS ,<br> 
  CLIP_DEFAULT_PRECIS , <br> 
  DEFAULT_QUALITY ,<br> 
  FIXED_PITCH | FF_MODERN,<br> 
  &quot;楷体_GB2312&quot;);<br> 
  }<br> 
  CPathView::~CPathView()<br> 
  {<br> 
  m_fontKaiTi.DeleteObject();<br> 
  }<br> 
  (3)在CPathView::OnDraw()函数中添加如下代码:<br> 
  void CPathView::OnDraw(CDC* pDC)<br> 
  {<br> 
  ……<br> 
  // TODO: add draw code for native data here<br> 
  RECT rect;<br> 
  GetClientRect(&amp;rect);<br> 
  <br> 
  CFont* pOldFont=(CFont*)pDC-&gt;SelectObject(&amp;m_fontKaiTi);<br> 
  pDC-&gt;SetBkMode(TRANSPARENT);<br> 
  <br> 
  //定义路径<br> 
  pDC-&gt;BeginPath();{<br> 
  pDC-&gt;TextOut(0,10,&quot;龙&quot;,2);<br> 
  pDC-&gt;TextOut(200,10,&quot;腾&quot;,2);<br> 
  pDC-&gt;TextOut(400,10,&quot;虎&quot;,2);<br> 
  pDC-&gt;TextOut(600,10,&quot;跃&quot;,2); }<br> 
  pDC-&gt;EndPath();<br> 
  <br> 
  pDC-&gt;SelectObject(pOldFont);<br> 
  <br> 
  //检取路径数据<br> 
  int nCount=pDC-&gt;GetPath(NULL,NULL,0);<br> 
  CPoint* points=new CPoint[nCount];<br> 
  BYTE* bytes=new BYTE[nCount];<br> 
  pDC-&gt;GetPath(points,bytes,nCount);<br> 
  <br> 
  //对路径定义点按正弦曲线进行变换<br> 
  int i;<br> 
  for(i=0;i&lt;nCount;i++)<br> 
  points[i].y=points[i].y+(int)(80*sin(points[i].x<br> 
  /300.*3.1415926)+100);<br> 
  <br> 
  //重建一个新的路径<br> 
  CPoint ptStart;<br> 
  pDC-&gt;BeginPath();{<br> 
  for(i=0;i&lt;nCount;i++){<br> 
  switch(bytes[i]){<br> 
  //移动当前点位置<br> 
  case PT_MOVETO:<br> 
  pDC-&gt;MoveTo(points[i]);<br> 
  ptStart=points[i];<br> 
  break;<br> 
  //画直线<br> 
  case PT_LINETO:<br> 
  pDC-&gt;LineTo(points[i]);<br> 
  break;<br> 
  //画贝塞尔曲线<br> 
  case PT_BEZIERTO:<br> 
  pDC-&gt;PolyBezierTo(points+i,3);<br> 
  i=i+2;<br> 
  break;<br> 
  //画贝塞尔曲线并封闭图形<br> 
  case PT_BEZIERTO|PT_CLOSEFIGURE:<br> 
  points[i+2]=ptStart;<br> 
  pDC-&gt;PolyBezierTo(points+i,3);<br> 
  i=i+2;<br> 
  break;<br> 
  //画直线并封闭图形<br> 
  case PT_LINETO|PT_CLOSEFIGURE:<br> 
  pDC-&gt;LineTo(ptStart);<br> 
  break;<br> 
  }<br> 
  }<br> 
  pDC-&gt;CloseFigure();<br> 
  }<br> 
  pDC-&gt;EndPath();<br> 
  <br> 
  //绘制窗口灰色背景<br> 
  CBrush* pOldBrush=(CBrush*)(pDC-&gt;SelectStockObject(GRAY_BRUSH));<br> 
  pDC-&gt;Rectangle(&amp;rect);<br> 
  pDC-&gt;SelectObject(pOldBrush);<br> 
  <br> 
  //设置裁剪路径<br> 
  pDC-&gt;SetPolyFillMode(WINDING);<br> 
  pDC-&gt;SelectClipPath(RGN_COPY);<br> 
  <br> 
  //用位图填充裁剪区域<br> 
  CBitmap bmp;<br> 
  CBitmap* pBmpOld;<br> 
  bmp.LoadBitmap(IDB_BMP);<br> 
  <br> 
  CDC dcMem;<br> 
  dcMem.CreateCompatibleDC(pDC);<br> 
  pBmpOld=dcMem.SelectObject(&amp;bmp);<br> 
  <br> 
  pDC-&gt;StretchBlt(0,0,rect.right,rect.bottom,<br> 
  &amp;dcMem,0,0,600,100,SRCCOPY);<br> 
  <br> 
  dcMem.SelectObject(pBmpOld);<br> 
  dcMem.DeleteDC();<br> 
  bmp.DeleteObject();<br> 
  }<br> 
  (4)在资源中添加文字前景位图,其ID为IDB_BMP。<br> 
  (5)编译,连接,运行该应用程序。</font></p> 
      </td>     
    </tr>    
    <tr> 
      <td width="100%" height="12" class="font" colspan="2">  
      </td>     
    </tr> 
    <tr> 
      <td width="100%" height="6" class="font" colspan="2">  
      </td>     
    </tr> 
    <tr> 
      <td width="100%" height="8" class="font" colspan="2">  
      </td>     
    </tr> 
    <tr>    
      <td width="100%" height="17" class="font" colspan="2"></td>     
    </tr>    
  </table>     
</div>     
     
</body>     
     
</html>     

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -