Demo entry 6364114

呜呜呜

   

Submitted by anonymous on May 15, 2017 at 14:35
Language: C++. Code size: 38.3 kB.

#include "graphics.h"
#include "extgraph.h"
#include "genlib.h"
#include "simpio.h"
#include "random.h"
#include "strlib.h"
#include "conio.h"
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <stddef.h>
#include <time.h>

#include <windows.h>
#include <olectl.h>
#include <mmsystem.h>
#include <wingdi.h>
#include <ole2.h>
#include <ocidl.h>
#include <winuser.h>

#include "linkedlist.h"

#define NLIST     4
#define LINE      0
#define RECT      1
#define ELLIPSE   2
#define STRING    3



#define SYSCOLOR "Black"  /*默认绘图颜色*/
#define SYSFONT "Consolas"  /*默认的字体*/

#define CURSOR "_"          /*光标符号*/
#define CURSOR_BLINK  1     /*光标闪烁定时器事件标志号*/
#define MENUREC_BLINK  2        /*菜单点击*/
const int timerseconds = 500; /*光标闪烁周期ms*/
double DelHeight = 0.15625;  /*菜单的高度*/
int SysFontSize;           /*默认字体大小*/
bool isCursorBlink = FALSE;  /*光标是否闪烁*/

void KeyboardEventProcess(int key, int event);/*键盘消息回调函数*/
void CharEventProcess(char c);/*字符消息回调函数*/
void MouseEventProcess(int x, int y, int button, int event);/*鼠标消息回调函数*/
void TimerEventProcess(int timerID);/*定时器消息回调函数*/

typedef struct {/*直线类型*/
	double x1, y1;/*第一个点坐标*/
	double x2, y2;/*第二个点坐标*/
	int PenSize; /*粗细*/
	string color; /*颜色*/
	bool isSelected; /*选中*/
} *LineT, LineTT;

typedef struct {/*矩形类型*/
	double x1, y1;/*左下角坐标*/
	double x2, y2;/*右上角坐标*/
	int PenSize; /*粗细*/
	string color; /*颜色*/
	bool isSelected; /*选中*/
} *RectT, RectTT;

typedef struct {/*椭圆类型*/
	double cx, cy;/*圆心*/
	double rx, ry;/*长短半径*/
	int PenSize; /*粗细*/
	string color; /*颜色*/
	bool isSelected; /*选中*/
} *EllipseT, EllipseTT;

typedef struct {/*文本类型*/
	string text;/*文本指针*/
	double x, y;/*文本显示起始位置坐标*/
	int PointSize; /*文字大小*/
	string color; /*颜色*/
	bool isSelected; /*选中*/
	int curPos; /*光标位置*/
	bool isDisplayed; /*光标是否处于显示状态*/
	string FontType;
} *TextT;

typedef struct {
	double x1, x2;
	bool isClicked;
	string name[20];
} *StateT, StateTT;

typedef struct {
	StateTT Line;
	StateTT Rec;
	StateTT Ellipse;
	StateTT Word;
	StateTT Draw;
	StateTT Help;
	StateTT FontType;
	StateTT Delete;
	bool isDisplayed;
	double my1, my2;
	int FontSize;
} MenuT;

typedef struct {
	StateTT Consolas;
	StateTT Lucida_Fax;
	StateTT Bodoni_MT;
	StateTT YaHei;
	StateTT Times_New_Roman;
	double y1, y2;    /*这里y跟x是反的*/
}FontTT;

linkedlistADT list[NLIST] = { NULL, NULL, NULL, NULL };/*四类图形元素链表指针*/
double minDistance[NLIST] = { 100000000.0, 1000000000.0, 1000000000.0, 1000000000.0 };
int curList = LINE; /*当前链表序号*/
LineT curLine = NULL;/*直线链表的当前对象指针*/
RectT curRect = NULL;/*矩形链表的当前对象指针*/
EllipseT curEllipse = NULL;/*椭圆链表的当前对象指针*/
TextT curText = NULL;/*文本链表的当前对象指针*/
MenuT curMenu;/*菜单变量*/
FontTT Font;/*所有支持字体*/
StateT curFont = NULL;/*字体的当前对象指针*/
StateTT *curMenuRec = NULL;/*当前菜单的函数*/

bool isSelected = FALSE; /*图形元素选中状态*/
bool HelpIsDisplayed = FALSE;/*帮助图形是否显示*/
bool BeginDrawUp = FALSE;/*绘制模式第一个鼠标弹起是无效的*/
bool BeginDrawDown = FALSE;/*绘图模式第一个鼠标按下时无效的*/

#define TEXTLEN  100
static TextT tptr; /*当前编辑文本对象指针*/
static bool inText = FALSE; /*是否处于当前文本编辑状态*/
static char textbuf[TEXTLEN + 1];/*当前文本缓冲区*/

/*菜单处理函数*/
void InitialMenu(void);
void DrawMenu(void);
void DrawMenuRec(StateTT tp, double y1, double y2, string tps);
bool InMenu(double x, double y);
bool InFont(double x, double y);
void ShowFont(void);
void DrawHelp(void);

/*椭圆处理函数*/
void DrawCenteredEllipse(void *ellipse);/*画椭圆*/
bool EllipseEqual(void *ellipse1, void *ellipse2);/*比较两个椭圆相等*/
EllipseT SelectNearestNodeE(linkedlistADT list, double mx, double my);/*选择靠(mx,my)最近的结点*/

/*直线处理函数*/
void DrawLineD(void *line);
bool LineEqual(void *line1, void *line2);
LineT SelectNearestNodeL(linkedlistADT list, double mx, double my);

/*矩形处理函数*/
void DrawRect(void *rect);
void SimpleDrawRect(double x1, double y1, double x2, double y2);
bool RectEqual(void *rect1, void *rect2);
RectT SelectNearestNodeR(linkedlistADT list, double mx, double my);

/*文本处理函数*/
void DrawTextT(void *text); /*显示文本*/
bool TextEqual(void *text1, void *text2);/*比较两个文本是否相同*/
TextT SelectNearestNodeT(linkedlistADT list, double mx, double my);
void InsertCharToString(string str, int pos, char c);/*将字符c插入到字符串str的pos位置*/
void DeleteCharFromString(string str, int pos);/*删除字符串str的pos位置字符*/
void DrawCursor(string str, int curPos, double startx, double starty);/*显示文本光标*/

void PickNearestNode(linkedlistADT list[], double mx, double my);/*选择靠(mx,my)最近结点*/
void TraverseAllList();/*刷新所有图形对象*/
void DrawBackground(void);/*用白色填充背景*/

void Main() /*仅初始化执行一次*/
{
	int i;

	InitGraphics();/*图形初始化*/
	Randomize();/*随机函数初始化*/
	registerKeyboardEvent(KeyboardEventProcess);/*注册键盘消息回调函数*/
	registerCharEvent(CharEventProcess);/*注册字符消息回调函数*/
	registerMouseEvent(MouseEventProcess);/*注册鼠标消息回调函数*/
	registerTimerEvent(TimerEventProcess);/*注册定时器消息回调函数*/

	SetPenColor(SYSCOLOR);
	SetPenSize(1);
	SetFont(SYSFONT);  /*设置当前字体*/
	SetWindowTitle("CAD_09");  /*设置窗体名字*/
	SysFontSize = GetPointSize();  /*读取当前默认的字体大小*/
	InitialMenu();  /*初始化菜单*/
	DrawMenu();   /*绘制菜单*/

	for (i = 0; i < NLIST; i++) list[i] = NewLinkedList();
}

void KeyboardEventProcess(int key, int event)/*每当产生键盘消息,都要执行*/
{
	switch (event) {
	case KEY_DOWN:
		switch (key) {
		case VK_F1:/*F1: 编辑已有文本行*/
			if (!isSelected || curList != STRING) break;/*当前不处于文本行编辑状态*/
			if (inText) break;/*已处于文本行编辑状态*/
			inText = TRUE;/*设置文本编辑状态*/
			tptr = curText;/*取当前选中的文本对象*/
			strcpy(textbuf, tptr->text);/*将当前文本字符串拷贝到临时文本缓冲区以备编辑*/
			DeleteNode(list[STRING], curText, TextEqual);/*从当前文本链表中删除该文本对象*/
			SetPenColor(tptr->color); /*设置选择文本的颜色*/
			DrawCursor(textbuf, tptr->curPos, tptr->x, tptr->y);/*显示当前文本光标*/
			tptr->isDisplayed = TRUE; /*设置当前文本光标闪烁标志*/
			startTimer(CURSOR_BLINK, timerseconds);/*光标闪烁定时器触发*/
			isCursorBlink = TRUE; /*设置光标闪烁状态*/
			break;
		case VK_F6:/*显示或隐藏菜单*/
			curMenu.isDisplayed = !curMenu.isDisplayed;
			if (curMenu.isDisplayed == TRUE)
				DrawMenu();
			else {
				SetEraseMode(TRUE);
				DrawMenu();
				SetEraseMode(FALSE);
				TraverseAllList();
			}
			break;
		case VK_LEFT: /*左移键*/
			if (!inText) break;/*不处于文本编辑状态*/
			SetEraseMode(TRUE);
			MovePen(tptr->x, tptr->y);
			DrawTextString(textbuf);/*擦除当前文本*/
			DrawCursor(textbuf, tptr->curPos, tptr->x, tptr->y); /*擦除光标*/
			if (tptr->curPos > 0) tptr->curPos--;/*左移光标一个字符位置*/
			SetEraseMode(FALSE);
			MovePen(tptr->x, tptr->y);
			DrawTextString(textbuf);/*重新显示当前文本*/
			DrawCursor(textbuf, tptr->curPos, tptr->x, tptr->y); /*显示光标*/
			break;
		case VK_RIGHT: /*右移键*/
			if (!inText) break;/*不处于文本编辑状态*/
			SetEraseMode(TRUE);
			MovePen(tptr->x, tptr->y);
			DrawTextString(textbuf);/*擦除当前文本*/
			DrawCursor(textbuf, tptr->curPos, tptr->x, tptr->y); /*擦除光标*/
			if (tptr->curPos < strlen(textbuf)) tptr->curPos++;/*右移光标一个字符位置*/
			SetEraseMode(FALSE);
			MovePen(tptr->x, tptr->y);
			DrawTextString(textbuf);/*重新显示当前文本*/
			DrawCursor(textbuf, tptr->curPos, tptr->x, tptr->y); /*显示光标*/
			break;
		case VK_DELETE:/*DELETE*/
			if (inText) { /*若处于文本编辑状态,则删除当前字符*/
				SetEraseMode(TRUE);
				MovePen(tptr->x, tptr->y);
				DrawTextString(textbuf);
				DrawCursor(textbuf, tptr->curPos, tptr->x, tptr->y);/*擦除当前光标*/
				DeleteCharFromString(textbuf, tptr->curPos);/*删除当前位置的字符*/
				SetEraseMode(FALSE);
				TraverseAllList();/*刷新所有图形元素*/
				MovePen(tptr->x, tptr->y);
				DrawTextString(textbuf);
				DrawCursor(textbuf, tptr->curPos, tptr->x, tptr->y);/*显示当前光标*/
				break;
			}

		case VK_ESCAPE:/*退出图形元素选中状态*/
			if (!isSelected) break;/*不处于选中状态则不处理*/
			switch (curList) {
			case LINE:
				curLine->isSelected = FALSE;
				curLine->color = GetPenColor();
				break;
			case RECT:
				curRect->isSelected = FALSE;
				curRect->color = GetPenColor();
				break;
			case ELLIPSE:
				curEllipse->isSelected = FALSE;
				curEllipse->color = GetPenColor();
				break;
			case STRING:
				curText->isSelected = FALSE;
				curText->color = GetPenColor();
				break;
			}
			isSelected = FALSE;
			TraverseAllList();
			break;
		}
		break;
	case KEY_UP:
		break;
	}
}

void CharEventProcess(char c)
{
	int len;

	if (!inText) return;
	switch (c) {
	case 27: /*ESC*/
	case '\r':  /* 注意:回车在这里返回的字符是'\r',不是'\n'*/
		inText = FALSE;/*退出当前文本输入*/
		SetEraseMode(TRUE);
		DrawBackground();/*把整个窗口画成白色*/
		tptr->isDisplayed = FALSE;
		tptr->PointSize = GetPointSize();/*获取当前字体大小*/
		tptr->FontType = GetFont();/*获取当前字体*/
		tptr->text = CopyString(textbuf);/*申请字符串空间*/
		InsertNode(list[STRING], NULL, tptr);
		SetEraseMode(FALSE);
		TraverseAllList();
		cancelTimer(CURSOR_BLINK);/*注销光标闪烁定时器*/
		isCursorBlink = FALSE;
		SetPenColor(SYSCOLOR);
		curMenu.Word.isClicked = FALSE;
		DrawMenuRec(curMenu.Word, curMenu.my1, curMenu.my2, curMenu.Word.name);/*重绘菜单块*/
		curMenuRec = NULL;
		break;
	case '\b':/*BACKSPACE*/
		if ((len = strlen(textbuf)) == 0) break;
		SetEraseMode(TRUE);
		MovePen(tptr->x, tptr->y);
		DrawTextString(textbuf);
		DrawCursor(textbuf, tptr->curPos, tptr->x, tptr->y);/*擦除当前光标*/
		DeleteCharFromString(textbuf, tptr->curPos - 1);
		SetEraseMode(FALSE);
		TraverseAllList();
		MovePen(tptr->x, tptr->y);
		DrawTextString(textbuf);
		if (tptr->curPos > 0) tptr->curPos--;
		DrawCursor(textbuf, tptr->curPos, tptr->x, tptr->y);/*显示当前光标*/
		break;
	default:
		if ((len = strlen(textbuf)) >= TEXTLEN) break;
		SetEraseMode(TRUE);
		MovePen(tptr->x, tptr->y);
		DrawTextString(textbuf);
		DrawCursor(textbuf, tptr->curPos, tptr->x, tptr->y);/*擦除当前光标*/
		InsertCharToString(textbuf, tptr->curPos, c);/*将当前字符插入到光标位置*/
		SetEraseMode(FALSE);
		MovePen(tptr->x, tptr->y);
		DrawTextString(textbuf);
		tptr->curPos++;
		DrawCursor(textbuf, tptr->curPos, tptr->x, tptr->y);/*显示当前光标*/
		break;
	}
}

void MouseEventProcess(int x, int y, int button, int event)
{
	static bool isMove = FALSE; /*移动标志*/
	static bool isChangeSize = FALSE; /*缩放标志*/
	static bool isFigure = FALSE;  /*界面上是否有图形*/
	static double omx = 0.0, omy = 0.0;/*前一鼠标坐标*/
	double mx, my;/*当前鼠标坐标*/
	double x1, y1, x2, y2, dx, dy;
	static LineT lptr;
	static RectT rptr;
	static EllipseT eptr;
	int fontsize = GetPointSize();

	mx = ScaleXInches(x);/*pixels --> inches*/
	my = ScaleYInches(y);/*pixels --> inches*/

	/*判断当前是否有图形对象*/
	if (isFigure == FALSE)
		for (int i = 0; i < 4; ++i)
			if (ExamineList(list[i]) == FALSE) {
				isFigure = TRUE;
				break;
			}

	switch (event) {
	case BUTTON_DOWN:
		SetPointSize(SysFontSize);/*设置默认的字体大小*/
		/*当前菜单是显示状态且鼠标在菜单内点击*/
		if (curMenu.isDisplayed == TRUE&&InMenu(mx, my) == TRUE) {
			/*点击LINE*/
			if (mx >= curMenu.Line.x1&&mx < curMenu.Line.x2) {
				/*如果未进入DRAW模式*/
				if (curMenu.Draw.isClicked == FALSE) {
					curMenuRec = &curMenu.Line;
					lptr = (LineT)malloc(sizeof(LineTT));
					lptr->x1 = RandomReal(0.0, GetWindowWidth());
					lptr->y1 = RandomReal(0.0, GetWindowHeight());
					lptr->x2 = RandomReal(0.0, GetWindowWidth());
					lptr->y2 = RandomReal(0.0, GetWindowHeight());
					lptr->PenSize = GetPenSize();
					lptr->color = GetPenColor();
					lptr->isSelected = FALSE;
					InsertNode(list[LINE], NULL, lptr);
					DrawLineD(lptr);
				}
				curMenu.Line.isClicked = TRUE;
				DrawMenuRec(curMenu.Line, curMenu.my1, curMenu.my2, curMenu.Line.name);
			}
			/*点击REC*/
			else if (mx >= curMenu.Rec.x1&&mx < curMenu.Rec.x2) {
				if (curMenu.Draw.isClicked == FALSE) {
					curMenuRec = &curMenu.Rec;
					rptr = (RectT)malloc(sizeof(RectTT));
					x1 = RandomReal(0.0, GetWindowWidth());
					y1 = RandomReal(0.0, GetWindowHeight());
					x2 = RandomReal(0.0, GetWindowWidth());
					y2 = RandomReal(0.0, GetWindowHeight());
					if (x1 < x2) {
						rptr->x1 = x1;
						rptr->x2 = x2;
					}
					else {
						rptr->x1 = x2;
						rptr->x2 = x1;
					}
					if (y1 < y2) {
						rptr->y1 = y1;
						rptr->y2 = y2;
					}
					else {
						rptr->y1 = y2;
						rptr->y2 = y1;
					}
					rptr->PenSize = GetPenSize();
					rptr->color = GetPenColor();
					rptr->isSelected = FALSE;
					InsertNode(list[RECT], NULL, rptr);
					DrawRect(rptr);
				}
				curMenu.Rec.isClicked = TRUE;
				DrawMenuRec(curMenu.Rec, curMenu.my1, curMenu.my2, curMenu.Rec.name);
			}
			/*点击ELLIPSE*/
			else if (mx >= curMenu.Ellipse.x1&&mx < curMenu.Ellipse.x2) {
				if (curMenu.Draw.isClicked == FALSE) {
					curMenuRec = &curMenu.Ellipse;
					eptr = (EllipseT)malloc(sizeof(EllipseTT));
					eptr->cx = RandomReal(1.0, GetWindowWidth() - 1.0);
					eptr->cy = RandomReal(1.0, GetWindowHeight() - 1.0);
					eptr->rx = RandomReal(0.5, 3.0);
					eptr->ry = RandomReal(0.5, 2.0);
					eptr->PenSize = GetPenSize();
					eptr->color = GetPenColor();
					eptr->isSelected = FALSE;
					InsertNode(list[ELLIPSE], NULL, eptr);
					DrawCenteredEllipse(eptr);
				}
				curMenu.Ellipse.isClicked = TRUE;
				DrawMenuRec(curMenu.Ellipse, curMenu.my1, curMenu.my2, curMenu.Ellipse.name);
			}
			/*点击WORD*/
			else if (mx >= curMenu.Word.x1&&mx < curMenu.Word.x2) {
				curMenuRec = &curMenu.Word;
				curMenu.Word.isClicked = TRUE;
				DrawMenuRec(curMenu.Word, curMenu.my1, curMenu.my2, curMenu.Word.name);
			}
			/*点击DRAW*/
			else if (mx >= curMenu.Draw.x1&&mx < curMenu.Draw.x2) {
				/*如果未进入DRAW模式则进入*/
				if (curMenu.Draw.isClicked == FALSE) {
					curMenuRec = &curMenu.Draw;
					curMenu.Draw.isClicked = TRUE;
					DrawMenuRec(curMenu.Draw, curMenu.my1, curMenu.my2, curMenu.Draw.name);
				}
				else {
					curMenuRec = NULL;
					curMenu.Draw.isClicked = FALSE;
					DrawMenuRec(curMenu.Draw, curMenu.my1, curMenu.my2, curMenu.Draw.name);

				}
			}
			/*点击FONTTYPE*/
			else if (mx >= curMenu.FontType.x1&&mx < curMenu.FontType.x2) {
				if (curMenu.FontType.isClicked == FALSE) {
					curMenuRec = &curMenu.FontType;
					curMenu.FontType.isClicked = TRUE;
					DrawMenuRec(curMenu.FontType, curMenu.my1, curMenu.my2, curMenu.FontType.name);
					ShowFont();
				}
				else {
					curMenu.FontType.isClicked = FALSE;
					DrawMenuRec(curMenu.FontType, curMenu.my1, curMenu.my2, curMenu.FontType.name);
					SetEraseMode(TRUE);
					ShowFont();
					SetEraseMode(FALSE);
					TraverseAllList();
				}

			}
			/*点击DELETE*/
			else if (mx >= curMenu.Delete.x1&&mx < curMenu.Delete.x2) {
				curMenuRec = &curMenu.Delete;
				curMenu.Delete.isClicked = TRUE;
				DrawMenuRec(curMenu.Delete, curMenu.my1, curMenu.my2, curMenu.Delete.name);
				if (!isSelected) break;
				/*下面删除选中的图形元素*/
				SetEraseMode(TRUE);
				TraverseAllList();
				switch (curList) {
				case LINE:
					DeleteNode(list[LINE], curLine, LineEqual);
					break;
				case RECT:
					DeleteNode(list[RECT], curRect, RectEqual);
					break;
				case ELLIPSE:
					DeleteNode(list[ELLIPSE], curEllipse, EllipseEqual);
					break;
				case STRING:
					DeleteNode(list[STRING], curText, TextEqual);
					break;
				}
				SetEraseMode(FALSE);
				TraverseAllList();
				isSelected = FALSE;
				break;
			}
			/*点击HELP*/
			else if (mx >= curMenu.Help.x1&&mx < curMenu.Help.x2) {
				if (curMenu.Help.isClicked == FALSE) {
					HelpIsDisplayed = TRUE;
					curMenuRec = &curMenu.Help;
					curMenu.Help.isClicked = TRUE;
					DrawHelp();
					DrawMenuRec(curMenu.Help, curMenu.my1, curMenu.my2, curMenu.Help.name);
				}
				else {
					HelpIsDisplayed = FALSE;
					curMenuRec = NULL;
					curMenu.Help.isClicked = FALSE;
					SetEraseMode(TRUE);
					DrawHelp();
					SetEraseMode(FALSE);
					TraverseAllList();
					DrawMenuRec(curMenu.Help, curMenu.my1, curMenu.my2, curMenu.Help.name);
				}
			}
		}
		/*如果点击WORD后再在非菜单区点击*/
		else if (curMenuRec == &curMenu.Word) {
			if (inText) break;/*若已处于文本编辑状态,则不生成新的空文本*/
			inText = TRUE; /*进入新的文本行输入状态*/
			tptr = GetBlock(sizeof(*tptr));
			/*获取当前的位置进行绘制*/
			tptr->x = mx;
			tptr->y = my;
			MovePen(tptr->x, tptr->y);/*文本显示起始位置*/
			DrawTextString(CURSOR);/*显示光标*/
			textbuf[0] = '\0'; /*形成字符串*/
			tptr->PointSize = GetPointSize();
			tptr->color = GetPenColor();
			tptr->isSelected = FALSE;
			tptr->curPos = 0; /*设置当前光标位置*/
			tptr->isDisplayed = TRUE; /*设置光标闪烁标志*/
			startTimer(CURSOR_BLINK, timerseconds);/*光标闪烁定时器触发*/
			isCursorBlink = TRUE;/*设置光标闪烁标志*/

		}
		/*选择字体*/
		else if (curMenuRec == &curMenu.FontType&&InFont(mx, my)) {
			/*判定选择的字体*/
			if (my <= Font.Bodoni_MT.x2&&my > Font.Bodoni_MT.x1) {
				curFont = &Font.Bodoni_MT;
			}
			else if (my <= Font.Consolas.x2&&my > Font.Consolas.x1) {
				curFont = &Font.Consolas;
			}
			else if (my <= Font.Lucida_Fax.x2&&my > Font.Lucida_Fax.x1) {
				curFont = &Font.Lucida_Fax;
			}
			else if (my <= Font.Times_New_Roman.x2&&my > Font.Times_New_Roman.x1) {
				curFont = &Font.Times_New_Roman;
			}
			else if (my <= Font.YaHei.x2&&my > Font.YaHei.x1) {
				curFont = &Font.YaHei;
			}
			curFont->isClicked = TRUE;
			curMenuRec = &curMenu.Line;
			curMenu.FontType.isClicked = FALSE;
			DrawMenuRec(curMenu.FontType, curMenu.my1, curMenu.my2, curMenu.FontType.name);
			/*如果当前已选择一段文字*/
			if (isSelected) {
				/*设置当前文字的字体*/
				memcpy(curText->FontType, curFont->name, strlen(curFont->name));
				DrawBackground();
				TraverseAllList();
			}
			SetFont(curFont->name);
		}
		/*已是选中状态*/
		else if (isSelected) { 
			omx = mx;
			omy = my;
			/*左键按下,进入移动状态*/
			if (button == LEFT_BUTTON) { 
				isMove = TRUE;
			}
			/*右键键按下,进入缩放状态*/
			else if (button == RIGHT_BUTTON) {
				isChangeSize = TRUE;
			}
		}
		/*进入DRAW模式后的点击*/
		else if (curMenu.Draw.isClicked == TRUE) {
			/*直线*/
			if (curMenu.Line.isClicked == TRUE) {
				lptr = (LineT)malloc(sizeof(LineTT));
				/*直线起点*/
				lptr->x1 = mx;
				lptr->y1 = my;
				MovePen(mx, my);
				BeginDrawDown = TRUE;
			}
			/*矩形*/
			else if (curMenu.Rec.isClicked == TRUE) {
				rptr = (RectT)malloc(sizeof(RectTT));
				/*矩形起点*/
				rptr->x1 = mx;
				rptr->y1 = my;
				BeginDrawDown = TRUE;
			}
			/*椭圆*/
			else if (curMenu.Ellipse.isClicked == TRUE) {
				eptr = (EllipseT)malloc(sizeof(EllipseTT));
				/*椭圆中心*/
				eptr->cx = mx;
				eptr->cy = my;
				BeginDrawDown = TRUE;
			}
		}
		/*当前有图形并点击*/
		else if (isFigure == TRUE) {
			PickNearestNode(list, mx, my);/*选择对象*/
			switch (curList) {
			case LINE:
				curLine->isSelected = TRUE;
				curLine->color = CopyString("GREEN");
				break;
			case RECT:
				curRect->isSelected = TRUE;
				curRect->color = CopyString("GREEN");
				break;
			case ELLIPSE:
				curEllipse->isSelected = TRUE;
				curEllipse->color = CopyString("GREEN");
				break;
			case STRING:
				curText->isSelected = TRUE;
				curText->color = CopyString("GREEN");
				break;
			}
			isSelected = TRUE;
			TraverseAllList();
		}
		SetPointSize(fontsize);/*重设字体*/
		break;
	case BUTTON_DOUBLECLICK:
		break;
	case BUTTON_UP:
		/*鼠标在菜单栏弹起*/
		if (InMenu(mx, my) == TRUE) {
			/*不是DRAW模式且当前菜单指针不指向MENU、HELP、WORD、FONTTYPE*/
			if (curMenu.Draw.isClicked == FALSE&&curMenuRec != NULL&&
				curMenuRec != &curMenu.Help&&curMenuRec != &curMenu.Word&&
				curMenuRec != &curMenu.FontType) {
				curMenuRec->isClicked = FALSE;
				DrawMenuRec(*curMenuRec, curMenu.my1, curMenu.my2, curMenuRec->name);
				curMenuRec = NULL;
			}
		}
		/*在选择字体处弹起*/
		else if (curMenuRec == &curMenu.FontType&&InFont(mx, my)) {
			SetEraseMode(TRUE);
			ShowFont();
			SetEraseMode(FALSE);
			curFont->isClicked = FALSE;
			curMenuRec = NULL;
			TraverseAllList();
		}
		/*DRAW模式绘制图形结束*/
		else if (curMenu.Draw.isClicked == TRUE&&BeginDrawDown == TRUE) {
			/*直线*/
			if (curMenu.Line.isClicked == TRUE) {
				lptr->x2 = mx;
				lptr->y2 = my;
				lptr->PenSize = GetPenSize();
				lptr->color = GetPenColor();
				lptr->isSelected = FALSE;
				InsertNode(list[LINE], NULL, lptr);

				curMenu.Line.isClicked = FALSE;
				DrawMenuRec(curMenu.Line, curMenu.my1, curMenu.my2, curMenu.Line.name);
			}
			/*矩形*/
			else if (curMenu.Rec.isClicked == TRUE) {
				rptr->x2 = mx;
				rptr->y2 = my;
				rptr->PenSize = GetPenSize();
				rptr->color = GetPenColor();
				rptr->isSelected = FALSE;
				InsertNode(list[RECT], NULL, rptr);

				curMenu.Rec.isClicked = FALSE;
				DrawMenuRec(curMenu.Rec, curMenu.my1, curMenu.my2, curMenu.Rec.name);
			}
			/*椭圆*/
			else if (curMenu.Ellipse.isClicked == TRUE) {
				eptr->rx = fabs(mx - eptr->cx);
				eptr->ry = fabs(my - eptr->cy);
				eptr->PenSize = GetPenSize();
				eptr->color = GetPenColor();
				eptr->isSelected = FALSE;
				InsertNode(list[ELLIPSE], NULL, eptr);

				curMenu.Ellipse.isClicked = FALSE;
				DrawMenuRec(curMenu.Ellipse, curMenu.my1, curMenu.my2, curMenu.Ellipse.name);
			}
			BeginDrawDown = FALSE;/*重归虚无*/
		}
		else {
			isMove = FALSE; /*退出移动状态*/
			isChangeSize = FALSE;/*退出缩放状态*/
		}
		break;
	case MOUSEMOVE:
		/*DRAW模式绘制图形的移动*/
		if (curMenu.Draw.isClicked == TRUE&&BeginDrawDown == TRUE) {
			if (curMenu.Line.isClicked == TRUE) {
				SetEraseMode(TRUE);/*擦除上一个图形*/
				MovePen(lptr->x1, lptr->y1);
				DrawLine(lptr->x2 - lptr->x1, lptr->y2 - lptr->y1);
				SetEraseMode(FALSE);
				TraverseAllList();
				lptr->x2 = mx;
				lptr->y2 = my;
				MovePen(lptr->x1, lptr->y1);
				DrawLine(lptr->x2 - lptr->x1, lptr->y2 - lptr->y1);/*绘制当前图形*/
			}
			else if (curMenu.Rec.isClicked == TRUE) {
				SetEraseMode(TRUE);
				SimpleDrawRect(rptr->x1, rptr->y1, rptr->x2, rptr->y2);
				SetEraseMode(FALSE);
				TraverseAllList();
				rptr->x2 = mx;
				rptr->y2 = my;
				SimpleDrawRect(rptr->x1, rptr->y1, rptr->x2, rptr->y2);
			}
			else if (curMenu.Ellipse.isClicked = TRUE) {
				SetEraseMode(TRUE);
				MovePen(eptr->cx + eptr->rx, eptr->cy);
				DrawEllipticalArc(eptr->rx, eptr->ry, 0.0, 360.0);
				SetEraseMode(FALSE);
				TraverseAllList();
				eptr->rx = fabs(mx - eptr->cx);
				eptr->ry = fabs(my - eptr->cy);
				MovePen(eptr->cx + eptr->rx, eptr->cy);
				DrawEllipticalArc(eptr->rx, eptr->ry, 0.0, 360.0);
			}
		}
		/*选中状态下,移动或缩放*/
		else if (isSelected && (isMove || isChangeSize)) { 
			SetEraseMode(TRUE);/*分类别擦除*/
			switch (curList) {
			case LINE:
				DrawLineD(curLine);
				break;
			case RECT:
				DrawRect(curRect);
				break;
			case ELLIPSE:
				DrawCenteredEllipse(curEllipse);
				break;
			case STRING:
				DrawBackground();/*该图形库自身导致其它字体不能擦除成功只能重绘白色背景*/
				break;
			}
			SetEraseMode(FALSE);
			dx = mx - omx;
			dy = my - omy;
			if (isMove) { /*移动*/
				switch (curList) {
				case LINE:
					curLine->x1 += dx;
					curLine->y1 += dy;
					curLine->x2 += dx;
					curLine->y2 += dy;
					break;
				case RECT:
					x1 = curRect->x1 + dx;
					y1 = curRect->y1 + dy;
					x2 = curRect->x2 + dx;
					y2 = curRect->y2 + dy;
					if (x1 < x2) {
						curRect->x1 = x1;
						curRect->x2 = x2;
					}
					else {
						curRect->x1 = x2;
						curRect->x2 = x1;
					}
					if (y1 < y2) {
						curRect->y1 = y1;
						curRect->y2 = y2;
					}
					else {
						curRect->y1 = y2;
						curRect->y2 = y1;
					}
					break;
				case ELLIPSE:
					curEllipse->cx += dx;
					curEllipse->cy += dy;
					break;
				case STRING:
					curText->x += dx;
					curText->y += dy;
					break;
				}
			}
			else if (isChangeSize) {/*缩放*/
				switch (curList) {
				case LINE:
					if (fabs(omx - curLine->x1) + fabs(omy - curLine->y1) <
						fabs(omx - curLine->x2) + fabs(omy - curLine->y2)) {
						curLine->x1 += dx;
						curLine->y1 += dy;
					}
					else {
						curLine->x2 += dx;
						curLine->y2 += dy;
					}
					break;
				case RECT:
					if (fabs(omx - curRect->x1) < fabs(omx - curRect->x2)) {
						curRect->x1 += dx;
					}
					else {
						curRect->x2 += dx;
					}
					if (fabs(omy - curRect->y1) < fabs(omy - curRect->y2)) {
						curRect->y1 += dy;
					}
					else {
						curRect->y2 += dy;
					}
					break;
				case ELLIPSE:
					curEllipse->rx += dx;
					curEllipse->ry += dy;
					break;
				case STRING:
					curText->PointSize += (int)((mx - curText->x) * 5);
					SetPointSize(curText->PointSize);
					break;
				}
			}
			omx = mx;
			omy = my;
			//SetEraseMode(FALSE);
			TraverseAllList();
		}
		break;
	}
}

void TimerEventProcess(int timerID)
{
	switch (timerID) {
	case CURSOR_BLINK: /*100ms光标闪烁定时器*/
		SetEraseMode(!tptr->isDisplayed);
		DrawCursor(textbuf, tptr->curPos, tptr->x, tptr->y);
		SetEraseMode(FALSE);
		MovePen(tptr->x, tptr->y);
		DrawTextString(textbuf);
		tptr->isDisplayed = !tptr->isDisplayed;
		break;
	default:
		break;
	}
}

/*for Menu*/
void InitialMenu(void) {
	/*都未被点击*/
	curMenu.Draw.isClicked = curMenu.Ellipse.isClicked =
		curMenu.Line.isClicked = curMenu.Rec.isClicked =
		curMenu.Word.isClicked = curMenu.Help.isClicked =
		Font.Bodoni_MT.isClicked = Font.Consolas.isClicked =
		Font.Lucida_Fax.isClicked = Font.Times_New_Roman.isClicked =
		Font.YaHei.isClicked = curMenu.Delete.isClicked = FALSE;
	/*设置名字*/
	memcpy(curMenu.Line.name, "Line", strlen("Line"));
	memcpy(curMenu.Ellipse.name, "Ellipse", strlen("Ellipse"));
	memcpy(curMenu.Rec.name, "Rec", strlen("Rec"));
	memcpy(curMenu.Word.name, "Word", strlen("Word"));
	memcpy(curMenu.Help.name, "Help", strlen("Help"));
	memcpy(curMenu.Draw.name, "Draw", strlen("Draw"));
	memcpy(curMenu.Delete.name, "Delete", strlen("Delete"));
	memcpy(curMenu.FontType.name, "FontType", strlen("FontType"));
	memcpy(Font.Bodoni_MT.name, "Bodoni MT", strlen("Bodoni MT"));
	memcpy(Font.Consolas.name, "Consolas", strlen("Consolas"));
	memcpy(Font.Lucida_Fax.name, "Lucida Fax", strlen("Lucida Fax"));
	memcpy(Font.Times_New_Roman.name, "Times New Roman", strlen("Times New Roman"));
	memcpy(Font.YaHei.name, "微软雅黑", strlen("微软雅黑"));
	double delx = GetWindowWidth() / 9;/*设置单位宽度*/
	curMenu.my1 = GetWindowHeight() - 2 * DelHeight;
	curMenu.my2 = GetWindowHeight();
	curMenu.Line.x1 = 0;
	curMenu.Line.x2 = curMenu.Rec.x1 = delx;
	curMenu.Rec.x2 = curMenu.Ellipse.x1 = delx * 2;
	curMenu.Ellipse.x2 = curMenu.Word.x1 = delx * 3;
	curMenu.Word.x2 = curMenu.Draw.x1 = delx * 4;
	curMenu.Draw.x2 = curMenu.Help.x1 = delx * 5;
	curMenu.Help.x2 = Font.y1 = curMenu.FontType.x1 = delx * 6;
	Font.y2 = curMenu.FontType.x2 = curMenu.Delete.x1 = delx * 7;
	curMenu.Delete.x2 = delx * 8;
	Font.Bodoni_MT.x2 = curMenu.my1;
	Font.Consolas.x2 = Font.Bodoni_MT.x1 = curMenu.my1 - 1.5 * DelHeight;
	Font.Lucida_Fax.x2 = Font.Consolas.x1 = curMenu.my1 - 3 * DelHeight;
	Font.Times_New_Roman.x2 = Font.Lucida_Fax.x1 = curMenu.my1 - 4.5 * DelHeight;
	Font.YaHei.x2 = Font.Times_New_Roman.x1 = curMenu.my1 - 6 * DelHeight;
	Font.YaHei.x1 = curMenu.my1 - 7.5 * DelHeight;
	curMenu.isDisplayed = TRUE;
}

void DrawMenuRec(StateTT tp, double y1, double y2, string tps) {
	StartFilledRegion(1);
	/*选择不同颜色*/
	if (tp.isClicked == FALSE)
		SetPenColor("Light Gray");
	else
		SetPenColor("Dark Gray");
	MovePen(tp.x1, y1);
	DrawLine(tp.x2 - tp.x1, 0);
	DrawLine(0, y2 - y1);
	DrawLine(tp.x1 - tp.x2, 0);
	DrawLine(0, y1 - y2);
	EndFilledRegion();
	SetPenColor("White");
	MovePen(tp.x1, y1);
	DrawLine(tp.x2 - tp.x1, 0);
	DrawLine(0, y2 - y1);
	DrawLine(tp.x1 - tp.x2, 0);
	DrawLine(0, y1 - y2);
	string type = GetFont();
	SetFont(SYSFONT);
	/*居中绘制字符串*/
	MovePen((tp.x1 + tp.x2) / 2 - TextStringWidth(tps) / 2, (y1 + y2) / 2 - GetFontHeight() / 4);
	DrawTextString(tps);
	SetPenColor(SYSCOLOR);
	SetFont(type);
}

void DrawMenu(void) {
	int fontsize = GetPointSize();
	SetPointSize(SysFontSize);/*设置菜单对应的默认字体*/
	DrawMenuRec(curMenu.Line, curMenu.my1, curMenu.my2, curMenu.Line.name);
	DrawMenuRec(curMenu.Rec, curMenu.my1, curMenu.my2, curMenu.Rec.name);
	DrawMenuRec(curMenu.Ellipse, curMenu.my1, curMenu.my2, curMenu.Ellipse.name);
	DrawMenuRec(curMenu.Word, curMenu.my1, curMenu.my2, curMenu.Word.name);
	DrawMenuRec(curMenu.Draw, curMenu.my1, curMenu.my2, curMenu.Draw.name);
	DrawMenuRec(curMenu.Help, curMenu.my1, curMenu.my2, curMenu.Help.name);
	DrawMenuRec(curMenu.FontType, curMenu.my1, curMenu.my2, curMenu.FontType.name);
	DrawMenuRec(curMenu.Delete, curMenu.my1, curMenu.my2, curMenu.Delete.name);
	SetPointSize(fontsize);
}

bool InMenu(double x, double y) {
	if (x >= 0 && x <= curMenu.Delete.x2&&y <= curMenu.my2&&y >= curMenu.my1)
		return TRUE;
	else
		return FALSE;
}

bool InFont(double x, double y) {
	if (x >= curMenu.FontType.x1&&x <= curMenu.FontType.x2&&y <= Font.Bodoni_MT.x2&&y >= Font.YaHei.x1)
		return TRUE;
	else
		return FALSE;
}

void ShowFont(void) {
	/*为了方便,这里就是x对应y,y对应x_(:з」∠)_*/
	static StateTT tp;
	tp.x1 = Font.y1;
	tp.x2 = Font.y2;
	DrawMenuRec(tp, Font.Bodoni_MT.x1, Font.Bodoni_MT.x2, Font.Bodoni_MT.name);
	DrawMenuRec(tp, Font.Consolas.x1, Font.Consolas.x2, Font.Consolas.name);
	DrawMenuRec(tp, Font.Lucida_Fax.x1, Font.Lucida_Fax.x2, Font.Lucida_Fax.name);
	DrawMenuRec(tp, Font.Times_New_Roman.x1, Font.Times_New_Roman.x2, Font.Times_New_Roman.name);
	DrawMenuRec(tp, Font.YaHei.x1, Font.YaHei.x2, Font.YaHei.name);
}

void DrawHelp(void) {
	string color = GetPenColor();
	string font = GetFont();
	int fontsize = GetPointSize();
	double y = 25 * GetFontHeight();
	double x = GetWindowWidth() * 5 / 7;

	SetFont("Consolas");/*设置字体*/
	StartFilledRegion(1.0);
	SetPenColor("Light Gray");
	SimpleDrawRect(x, 0, GetWindowWidth(), y);
	EndFilledRegion();
	SetPenColor("White");
	SetPointSize(22);
	y -= GetFontHeight();/*进入下一行*/
	MovePen((x + GetWindowWidth()) / 2 - GetFontAscent() / 2, y);/*居中绘制*/
	DrawTextString("HELP");
	SetPointSize(15);
	y -= GetFontHeight();
	x += 0.3;
	MovePen(x, y);
	DrawTextString("    Use left button to select");
	y -= GetFontHeight();
	MovePen(x, y);
	DrawTextString("and move object. Use right  ");
	y -= GetFontHeight();
	MovePen(x, y);
	DrawTextString("button to change the size of  ");
	y -= GetFontHeight();
	MovePen(x, y);
	DrawTextString("the object.");
	y -= 2 * GetFontHeight();
	MovePen(x, y);
	DrawTextString("    If you want to edit the  ");
	y -= GetFontHeight();
	MovePen(x, y);
	DrawTextString("text, press F1 after select  ");
	y -= GetFontHeight();
	MovePen(x, y);
	DrawTextString("the text.");
	y -= 2 * GetFontHeight();
	MovePen(x, y);
	DrawTextString("    If you want to hide the  ");
	y -= GetFontHeight();
	MovePen(x, y);
	DrawTextString("menu, you can press F2 and ");
	y -= GetFontHeight();
	MovePen(x, y);
	DrawTextString("vice versa.");
	y -= 2 * GetFontHeight();
	MovePen(x, y);
	DrawTextString("    In DRAW mode, you can ");
	y -= GetFontHeight();
	MovePen(x, y);
	DrawTextString("draw the figures as you like.");

	/*重设各个参数*/
	SetPenColor(color);
	SetPointSize(fontsize);
	SetFont(font);
}

/*for Line*/
void DrawLineD(void *line)
{
	LineT lptr = (LineT)line;
	int pensize = GetPenSize();/*保存当前系统笔画粗细*/
	string color = GetPenColor();/*保存当前系统颜色*/

	SetPenSize(lptr->PenSize);/*设置粗细*/
	SetPenColor(lptr->color);/*设置颜色*/
	MovePen(lptr->x1, lptr->y1);
	DrawLine(lptr->x2 - lptr->x1, lptr->y2 - lptr->y1);
	SetPenSize(pensize); /*恢复粗细*/
	SetPenColor(color);/*恢复颜色*/
}

bool LineEqual(void *line1, void *line2)
{
	LineT lptr1 = (LineT)line1, lptr2 = (LineT)line2;

	return lptr1->x1 == lptr2->x1 && lptr1->y1 == lptr2->y1 &&
		lptr1->x2 == lptr2->x2 && lptr1->y2 == lptr2->y2;
}

double distLine(double x, double y, LineT line)
{
	double x0, y0;

	x0 = (line->x1 + line->x2) / 2;
	y0 = (line->y1 + line->y2) / 2;
	return fabs(x - x0) + fabs(y - y0);
}

LineT SelectNearestNodeL(linkedlistADT list, double mx, double my)
{
	linkedlistADT nearestnode = NULL, ptr;
	double mindistance, dist;

	ptr = NextNode(list, list);
	if (ptr == NULL) return NULL;
	nearestnode = ptr;
	mindistance = distLine(mx, my, (LineT)NodeObj(list, ptr));
	while (NextNode(list, ptr) != NULL) {
		ptr = NextNode(list, ptr);
		dist = distLine(mx, my, (LineT)NodeObj(list, ptr));
		if (dist < mindistance) {
			nearestnode = ptr;
			mindistance = dist;
		}
	}
	minDistance[LINE] = mindistance;

	return (LineT)NodeObj(list, nearestnode);
}

/*for rectangle */
void DrawRect(void *rect)
{
	RectT r = (RectT)rect;
	int pensize = GetPenSize();/*保存当前系统笔画粗细*/
	string color = GetPenColor();/*保存当前系统颜色*/

	SetPenSize(r->PenSize);/*设置粗细*/
	SetPenColor(r->color);/*设置颜色*/
	SimpleDrawRect(r->x1, r->y1, r->x2, r->y2);
	SetPenSize(pensize); /*恢复粗细*/
	SetPenColor(color);/*恢复颜色*/
}

void SimpleDrawRect(double x1, double y1, double x2, double y2) {
	MovePen(x1, y1);
	DrawLine(x2 - x1, 0.0);
	DrawLine(0.0, y2 - y1);
	DrawLine(x1 - x2, 0.0);
	DrawLine(0.0, y1 - y2);
}

bool RectEqual(void *rect1, void *rect2)
{
	RectT r1, r2;

	r1 = (RectT)rect1;
	r2 = (RectT)rect2;
	return r1->x1 == r2->x1 && r1->y1 == r2->y1 &&
		r1->x2 == r2->x2 && r1->y2 == r2->y2;
}

double distRect(double x, double y, RectT rect)
{
	double x0, y0;

	x0 = (rect->x1 + rect->x2) / 2;
	y0 = (rect->y1 + rect->y2) / 2;
	return fabs(x - x0) + fabs(y - y0);
}

RectT SelectNearestNodeR(linkedlistADT list, double mx, double my)
{
	linkedlistADT nearestnode = NULL, ptr;
	double mindistance, dist;

	ptr = NextNode(list, list);
	if (ptr == NULL) return NULL;
	nearestnode = ptr;
	mindistance = distRect(mx, my, (RectT)NodeObj(list, ptr));
	while (NextNode(list, ptr) != NULL) {
		ptr = NextNode(list, ptr);
		dist = distRect(mx, my, (RectT)NodeObj(list, ptr));
		if (dist < mindistance) {
			nearestnode = ptr;
			mindistance = dist;
		}
	}
	minDistance[RECT] = mindistance;

	return (RectT)NodeObj(list, nearestnode);
}

/*For ellipse*/
void DrawCenteredEllipse(void *ellipse)
{
	EllipseT eptr = (EllipseT)ellipse;
	int pensize = GetPenSize();
	string color = GetPenColor();

	SetPenSize(eptr->PenSize);
	SetPenColor(eptr->color);
	MovePen(eptr->cx + eptr->rx, eptr->cy);
	DrawEllipticalArc(eptr->rx, eptr->ry, 0.0, 360.0);
	SetPenSize(pensize);
	SetPenColor(color);
}

bool EllipseEqual(void *ellipse1, void *ellipse2)
{
	EllipseT eptr1 = (EllipseT)ellipse1, eptr2 = (EllipseT)ellipse2;

	return eptr1->cx == eptr2->cx && eptr1->cy == eptr2->cy &&
		eptr1->rx == eptr2->rx && eptr1->ry == eptr2->ry;

}

double distEllipse(double x, double y, EllipseT ellipse)
{
	return fabs(x - ellipse->cx) + fabs(y - ellipse->cy);
}

EllipseT SelectNearestNodeE(linkedlistADT list, double mx, double my)
{
	linkedlistADT nearestnode = NULL, ptr;
	double mindistance, dist;

	ptr = NextNode(list, list);
	if (ptr == NULL) return NULL;
	nearestnode = ptr;
	mindistance = distEllipse(mx, my, (EllipseT)NodeObj(list, ptr));
	while (NextNode(list, ptr) != NULL) {
		ptr = NextNode(list, ptr);
		dist = distEllipse(mx, my, (EllipseT)NodeObj(list, ptr));
		if (dist < mindistance) {
			nearestnode = ptr;
			mindistance = dist;
		}
	}
	minDistance[ELLIPSE] = mindistance;

	return (EllipseT)NodeObj(list, nearestnode);
}

/*for text*/
void DrawTextT(void *text)
{
	TextT tptr = (TextT)text;
	string color = GetPenColor();
	string type = GetFont();
	int pointsize = GetPointSize();

	SetFont(tptr->FontType);
	MovePen(tptr->x, tptr->y);
	SetPenColor(tptr->color);
	SetPointSize(tptr->PointSize);
	DrawTextString(tptr->text);
	SetFont(type);
	SetPenColor(color);
	SetPointSize(pointsize);
}

/*画光标*/
void DrawCursor(string str, int curPos, double startx, double starty)
{
	if (curPos < 0 || curPos > strlen(str)) return;
	MovePen(startx + TextStringWidth(SubString(str, 0, curPos - 1)), starty);
	DrawTextString(CURSOR);
	return;
}

bool TextEqual(void *text1, void *text2)
{
	TextT tptr1 = (TextT)text1, tptr2 = (TextT)text2;

	return StringEqual(tptr1->text, tptr2->text) &&
		tptr1->x == tptr2->x && tptr1->y == tptr2->y;
}

double distText(double x, double y, TextT text)
{
	return fabs(x - text->x) + fabs(y - text->y);
}

TextT SelectNearestNodeT(linkedlistADT list, double mx, double my)
{
	linkedlistADT nearestnode = NULL, ptr;
	double mindistance, dist;

	ptr = NextNode(list, list);
	if (ptr == NULL) return NULL;
	nearestnode = ptr;
	mindistance = distText(mx, my, (TextT)NodeObj(list, ptr));
	while (NextNode(list, ptr) != NULL) {
		ptr = NextNode(list, ptr);
		dist = distText(mx, my, (TextT)NodeObj(list, ptr));
		if (dist < mindistance) {
			nearestnode = ptr;
			mindistance = dist;
		}
	}
	minDistance[STRING] = mindistance;

	return (TextT)NodeObj(list, nearestnode);
}

/*从pos位置向字符串插入字符*/
void InsertCharToString(string str, int pos, char c)
{
	int len;

	if (pos < 0 || pos >= TEXTLEN) return;
	len = strlen(str);
	*(str + len + 1) = '\0';/*插入后形成字符串*/
	while (len > pos) {
		*(str + len) = *(str + len - 1);
		len--;
	}
	*(str + len) = c;
	return;
}

/*删除当前字符串中pos位置对应的字母*/
void DeleteCharFromString(string str, int pos)
{
	int len;

	len = strlen(str);
	if (pos < 0 || pos >= len) return;

	while (*(str + pos) != '\0') {
		*(str + pos) = *(str + pos + 1);
		pos++;
	}
	return;
}

/*for all*/
void PickNearestNode(linkedlistADT list[], double mx, double my)
{
	int i, minlist;

	curLine = SelectNearestNodeL(list[LINE], mx, my);
	curRect = SelectNearestNodeR(list[RECT], mx, my);
	curEllipse = SelectNearestNodeE(list[ELLIPSE], mx, my);
	curText = SelectNearestNodeT(list[STRING], mx, my);

	minlist = 0;
	for (i = 1; i < NLIST; i++) {
		if (minDistance[i] < minDistance[minlist]) minlist = i;
	}
	curList = minlist;
}

void TraverseAllList()
{
	TraverseLinkedList(list[LINE], DrawLineD);
	TraverseLinkedList(list[RECT], DrawRect);
	TraverseLinkedList(list[ELLIPSE], DrawCenteredEllipse);
	TraverseLinkedList(list[STRING], DrawTextT);
	if (curMenu.isDisplayed == TRUE)
		DrawMenu();
	if (HelpIsDisplayed == TRUE)
		DrawHelp();
}

/*绘制整个白色背景*/
void DrawBackground(void) {
	double x = GetWindowWidth(), y = GetWindowHeight();
	SetPenColor("White");
	StartFilledRegion(1.0);
	MovePen(0.0, 0.0);
	DrawLine(x, 0.0);
	DrawLine(0.0, y);
	DrawLine(-x, 0.0);
	DrawLine(0.0, -y);
	EndFilledRegion();
	SetPenColor(SYSCOLOR);
}

This snippet took 0.10 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).