如何按照线型画粗线

类别:VC语言 点击:0 评论:0 推荐:
 

WINNT下,可以用以下方式创建画笔

       LOGBRUSH brush;

       brush.lbColor=DrawColor;

       brush.lbStyle=BS_SOLID;

mPen.CreatePen(PS_GEOMETRIC|PS_USERSTYLE|PS_ENDCAP_FLAT,(int)LineWide,&brush,i,PenStyle);

 

Win98,给DC设置合适的笔宽,选择实线,下面函数可以根据PenStyle数组给定长度值画直线。如果画曲线或者折线,需要把曲线离散成直线段,根据上次画线返回值确定本次画线的起始值,就可以了

#include "math.h"

DWORD   PenStyle[16] = {0,0,0,0,0,0,0,0,

0,0,0,0,0,0,0,0};

typedef struct xyz {

       double x;

       double y;

       double z;

} XYZ,*PXYZ,**PPXYZ;

 

//按照PenStyle[]画一条从p1点到p2点的直线

//例如PenStyle[0]=5,PenStyle[1]=2表示按照长度为5的实线、长度为2的间隔形成的线型画线

//_____  _____  _____  _

//并且指定从线型的何处画,例如iStart=6从Penstyle[1]处的第二个空格开始画

//iStart取值范围为0到一个线型的总长度-1

//返回值:线型总长度与剩余最后一段线长度之差,表示下一次画线应该指定的从何处开始画

int DrawLineA(CDC* pDC,POINT p1,POINT p2,int iStart)

{

       int i=0;

       int iEnd=0;//返回值,表示最后一段不完全的线段的长度

       //计算直线长度

       double dDistance=sqrt((p2.x-p1.x)*(p2.x-p1.x)+(p2.y-p1.y)*(p2.y-p1.y));

       int iLineType=0;//线型的长度

       if(dDistance<2)

       {

              for(i=0;i<16;i++)

              {

                     if(PenStyle[i]==0)break;

                     iLineType+=PenStyle[i];

              }

              iEnd=(int(iStart-dDistance))%iLineType;

              if(iEnd<0)iEnd+=iLineType;

              return iEnd;

       }

       double ddltx[16]={0,0,0,0,//每一段线型的x距离

              0,0,0,0,

              0,0,0,0,

              0,0,0,0};

       double ddlty[16]={0,0,0,0,//每一段线型的y距离

              0,0,0,0,

              0,0,0,0,

              0,0,0,0};

       double dXScale=(p2.x-p1.x)/dDistance;//x方向长度和总长度比例

       double dYScale=(p2.y-p1.y)/dDistance;//y方向长度和总长度比例

       //计算每一段线型的x、y距离及线型的长度

       for(i=0;i<16;i++)

       {

              if(PenStyle[i]==0)break;

              ddltx[i]=PenStyle[i]*dXScale;

              ddlty[i]=PenStyle[i]*dYScale;

              iLineType+=PenStyle[i];

       }

       iEnd=(int(dDistance+iStart))%iLineType;

       if(iEnd<0)iEnd+=iLineType;

      

       XYZ pp={0,0,0};//第一段之前的完整线型开始点

       pDC->MoveTo(p1);

       int iLength=-iStart;//已经画的线段的长度

       pp.x=iLength*dXScale+p1.x;

       pp.y=iLength*dYScale+p1.y;

       for(;;)

       {

              for(i=0;i<16;i++)

              {

                     if(PenStyle[i]==0)break;

                     pp.x+=ddltx[i];

                     pp.y+=ddlty[i];

                     iLength+=PenStyle[i];

                     if(i%2==0)

                     {

                            if(iLength>0)

                            {

                                   if(iLength<dDistance)

                                   {

                                          pDC->LineTo(pp.x,pp.y);

                                   }

                                   else

                                   {

                                          pDC->LineTo(p2);

                                          return iEnd;

                                   }

                            }

                     }else

                     {

                            if(iLength>0)

                            {

                                   if(iLength<dDistance)

                                   {

                                          pDC->MoveTo(pp.x,pp.y);

                                   }

                                   else

                                   {

                                          pDC->MoveTo(p2);

                                          return iEnd;

                                   }

                            }

                     }

              }

       }

       return iEnd;

}

 

本文地址:http://com.8s8s.com/it/it1366.htm