摘 要 该文介绍了一种建立在dos界面下生成图素文件的通用图形编辑程序的设计方法。 目前用作ddc的pc总线工控机(ipc)大部分工作在dos界面上,而dos不具有像windows那样美观方便的图形用户接口(gui)。生成工艺流程图等复杂图形若用程序设计语言直接编程需花费大量精力和代码,且不易修改。设计出数据文件小,占用内存少的图形编辑软件是控制界的一个研究课题。这里介绍一种生成图素数据文件的通用图形编辑软件的设计方法。 一、数据结构与数据文件格式 由于所有的操作都基本建立在图素的基础之上,故数据结构也以图素为中心。以下以圆、直线、矩形、字符串为例,其它图素类似。 1.定义所需图素 struct circle /*定义圆 */ { int x,y,r; /* 圆心,半径 */ char linecolor,linestyle; /* 圆外围线的颜色,线型 */ char fillcolor,fillstyle; /* 填充颜色,模式 */ }; struct line /* 定义直线 */ { int x1,y1; int x2,y2; char linecolor,linestyle,linethick; /* 线颜色,模式,粗细 */ }; struct box /* 定义矩形 */ { int x1,y1; int x2,y2; char linecolor,linestyle; char fillcolor,fillstyle; }; struct string /* 定义字符串 */ { int x,y; char str[10] char backcolor,dir; char str-color,str-style; }; . . /* 定义其它图素 */ . 2.将各图素置于一条链表之中 typedef struct tagelementlist { char elementtype; /* 标识元素类别 */ int elementid; /* 元素标识符,在接口中用来控制其属性 */ union tagelement { struct circle circle; struct box box; struct string string; struct line line; . . /* 可在此说明其它元素 */ . }element; struct tagelementlist *next; }elementlist; 利用这种数据结构可在内存中形成一个图素链表,所有操作都可以此链表为基础。loCalHost 3.定义几个指针,以备各种操作 elementlist *list-head. *list-end,*list-temp, *list-here; 4.定义一个全局变量,记录图素个数 static int elementcount=0; 图形文件格式为:第一字节(char),表示整个图形的背景颜色;接下来一个字(word),对应于elementcount,表示图素个数;后面是内存链表中每个图素的属性值。 二、图形编辑功能的实现 本软件包含的图形编辑功能主要有:作图、修改、移动、删除、复制,下面仅举几例说明实现的方法。 1.作图 以圆为例,其它图形类似。 drawcircle() { int i; char s[20],c; int cx,cy,cr; int cls,clc,cfc,cfs; movecursor(); /* 移动光标,确定圆心 */ cx=cursor-x; cy=cursor-y; movecursor(); /* 确定半径 */ cr=(int)sqrt((cursor-x-cx)*(cursor-x-cx)+(cursor-y-cy)*(cursor-y-cy); setcolor(white); circle (cx,cy,cr); /* 画圆 */ cls=selectlinestyle(); clc=selectcolor (select-line-color); setcolor(clc); for(i=0;i<=cls;i++) circle(cx,cy,cr-i); cfs=selectfillstyle(); cfc=selectcolor(set-fill-color); setfillstyle(cfs.cfc); floodfill(cx,cy,clc); /* 填充 */ temp(elementlist *) malloc(sizeof(elementlist)); temp->elementtype= c; temp->element.circle.x=cx; temp->element.circle.y=cy; temp->element.circle.r=cr; temp->element.circle.lcolor=clc; temp->element.circle.lstyle=cls; temp->element.circle.fcolor=cfc; temp->element.circle.fstyle=cfs; addtolist(temp); /* 将图素加入图素链表 */ } 其中 addtolist ()可以如下实现: addtolist (elementlist *etemp) { if(list-head==null) { list-head=etemp; list-end=etemp; } else { list-end->next=etemp; list-end=etemp; etemp->next=null; } elementcount++; } 2.图形的移动、删除、复制功能 以移动为例,首先用箭头键或鼠标框取要移动的区域,区域矩形的左上,右下坐标分别为(block-x1,block-y1),(block-x2,block-y
[1] [2] 下一页