当前所在位置:珠峰网资料 >> 计算机 >> 计算机等级考试 >> 正文
MFC中实现的画箭头算法(ArrowinMFC)
发布时间:2010/5/18 11:47:10 来源:城市学习网 编辑:ziteng
  在codeproject中寻找到一个这样的算法,在这里介绍一下
  可以改变三角形大小,顶点角度,是否填充和填充颜色等
  但是画出的箭头还是不够美观....呵呵,还好吧
  其中填充是代表箭头内是否填充颜色
  先来看声明和实现
  //使用一个结构体来存储相关的信息
  //Defines the attributes of an arrow.
  typedef struct tARROWSTRUCT {
  int nWidth;     // width (in pixels) of the full base of the arrowhead
  float fTheta;   // angle (in radians) at the arrow tip between the two
  //  sides of the arrowhead
  bool bFill;     // flag indicating whether or not the arrowhead should be
  //  filled
  } ARROWSTRUCT;
  ///////////////////////
  //函数声明
  // Draws an arrow, using the current pen and brush, from the current position
  //  to the passed point using the attributes defined in the ARROWSTRUCT.
  void ArrowTo(HDC hDC, int x, int y, ARROWSTRUCT *pArrow);
  void ArrowTo(HDC hDC, const POINT *lpTo, ARROWSTRUCT *pArrow);
  ///////////////////////
  //画箭头函数实现
  void CMyDialog::ArrowTo(HDC hDC, int x, int y, ARROWSTRUCT *pA) {
  POINT ptTo = {x, y};
  ArrowTo(hDC, &ptTo, pA);
  }
  void CMyDialog::ArrowTo(HDC hDC, const POINT *lpTo, ARROWSTRUCT *pA) {
  POINT pFrom;
  POINT pBase;
  POINT aptPoly[3];
  float vecLine[2];
  float vecLeft[2];
  float fLength;
  float th;
  float ta; [NextPage]  [NextPage]   // get from point
  MoveToEx(hDC, 0, 0, &pFrom);
  // set to point
  aptPoly[0].x = lpTo->x;
  aptPoly[0].y = lpTo->y;
  // build the line vector
  vecLine[0] = (float) aptPoly[0].x - pFrom.x;
  vecLine[1] = (float) aptPoly[0].y - pFrom.y;
  // build the arrow base vector - normal to the line
  vecLeft[0] = -vecLine[1];
  vecLeft[1] = vecLine[0];
  // setup length parameters
  fLength = (float) sqrt(vecLine[0] * vecLine[0] + vecLine[1] * vecLine[1]);
  th = pA->nWidth / (2.0f * fLength);
  ta = pA->nWidth / (2.0f * (tanf(pA->fTheta) / 2.0f) * fLength);
  // find the base of the arrow
  pBase.x = (int) (aptPoly[0].x + -ta * vecLine[0]);
  pBase.y = (int) (aptPoly[0].y + -ta * vecLine[1]);
  // build the points on the sides of the arrow
  aptPoly[1].x = (int) (pBase.x + th * vecLeft[0]);
  aptPoly[1].y = (int) (pBase.y + th * vecLeft[1]);
  aptPoly[2].x = (int) (pBase.x + -th * vecLeft[0]);
  aptPoly[2].y = (int) (pBase.y + -th * vecLeft[1]);
  MoveToEx(hDC, pFrom.x, pFrom.y, NULL);
  // draw we're fillin'...
  if(pA->bFill) {
  LineTo(hDC, aptPoly[0].x, aptPoly[0].y);
  Polygon(hDC, aptPoly, 3);
广告合作:400-664-0084 全国热线:400-664-0084
Copyright 2010 - 2017 www.my8848.com 珠峰网 粤ICP备15066211号
珠峰网 版权所有 All Rights Reserved