www.pudn.com > microwindows.example10.rar > demotpdraw.c
/**************************************************************** **Microwindows 触摸屏画板演示程序 zhang kaohua 2003.12.3*********** **演示内容:构建一个按钮,触摸屏绘图. *****************************************************************/ #define MWINCLUDECOLORS #include#include #include"nano-X.h" static GR_WINDOW_ID w; //声明根窗口 static GR_GC_ID gc; //声明根窗口绘图上下文 static GR_GC_ID gLine; // static GR_GC_ID gRect; static GR_GC_ID gRound; static GR_GC_ID gid; //声明按钮的绘图上下文 static GR_GC_ID gb; //cotext of the button when it had been putted. static GR_GC_ID t; //声明欢迎词绘图上下文 static GR_EVENT event; //声明事件 static GR_WINDOW_ID btLine; //声明按钮窗口 static GR_WINDOW_ID btRect; static GR_WINDOW_ID btRound; static GR_WINDOW_ID btClear; static GR_WINDOW_ID bt; static GR_WINDOW_ID we; //static GR_FONT_ID font; // void draw_main_win(GR_WINDOW_ID w,GR_GC_ID gc); void draw_Error_win(char *arg[]); void draw(GR_EVENT_BUTTON *bevent, GR_EVENT_MOUSE *mevent); void drawLine(); void drawRect(); void drawRound(); static int i; // point num static int r; //radius of round static int width, height; //width and height of rectangle static int flag; //draw num #define DRAWLINE 1 #define DRAWRECT 2 #define DRAWROUND 3 #define CLEAR 4 //#define LINEQUENE(x,mod) ((++(x)) & ((mod) - 1)) typedef struct { int x; int y; }COOR; static COOR coor[2]; int main(int ac,char **av) { i=0; flag=0; //done=1; if (GrOpen()<0) { printf("Can't open graphics\n"); exit(1); } //GrGetNextEvent(&event); w=GrNewWindow(GR_ROOT_WINDOW_ID,10,10,300,180,3,GREEN,BLUE);// gc=GrNewGC();// GrMapWindow(w);// draw_main_win(w,gc); GrClose(); return 0; } void draw_main_win(GR_WINDOW_ID w,GR_GC_ID gc) {//draw main window /*w=GrNewWindow(GR_ROOT_WINDOW_ID,10,10,300,180,3,GREEN,BLUE);//实例化根窗口 gc=GrNewGC();//实例化上下文 GrMapWindow(w);//显示窗口 GR_GC_ID gid; */ /*编译为Native版时变量可在函数内设,但交叉编译时要在程序声明段设*/ bt=GrNewWindow(GR_ROOT_WINDOW_ID,10,190,300,40,3,RED,BLUE); btLine=GrNewWindow(bt,2,5,65,30,0,RED,BLUE); btRect=GrNewWindow(bt,65,5,65,30,0,RED,BLUE); btRound=GrNewWindow(bt,170,5,65,30,0,RED,BLUE); btClear=GrNewWindow(bt,235,5,65,30,0,RED,BLUE); we=GrNewWindow(bt,135,15,30,20,2,YELLOW,BLUE); GrMapWindow(bt); GrMapWindow(btLine); GrMapWindow(btRect); GrMapWindow(btRound); GrMapWindow(btClear); GrMapWindow(we); //draw line button gid=GrNewGC(); GrSetGCForeground(gid,GREEN); GrSetGCBackground(gid,WHITE); GrFillEllipse(btLine,gid,32,15,30,13); //draw rectangle button gid=GrNewGC(); GrSetGCForeground(gid,GREEN); GrSetGCBackground(gid,WHITE); GrFillEllipse(btRect,gid,32,15,30,13); //draw round button gid=GrNewGC(); GrSetGCForeground(gid,GREEN); GrSetGCBackground(gid,WHITE); GrFillEllipse(btRound,gid,32,15,30,13); //draw clear button gid=GrNewGC(); GrSetGCForeground(gid,GREEN); GrSetGCBackground(gid,WHITE); GrFillEllipse(btClear,gid,32,15,30,13); //draw exit button GrSetGCForeground(gc,RED); GrSetGCBackground(gc,YELLOW); GrText(we,gc,2,15,"EXIT",-1,GR_TFASCII); //GR_GC_ID t;//为欢迎词选择字体颜色. t=GrNewGC(); GrSetGCForeground(t,RED); GrSetGCBackground(t,GREEN); /*font=GrCreateFont("times.ttf",30,NULL);//为欢迎词设置字体 GrSetGCFont(t,font); //GrText(w,t,75,35," ",-1,GR_TFASCII); //GrText(w,t,60,35,"WELCOME TO BEIJING CHINA",-1,GR_TFASCII); */ GrSetGCForeground(t,YELLOW); GrSetGCBackground(t,GREEN); GrText(btLine,t,23,20,"LINE",-1,GR_TFASCII); GrSetGCForeground(t,YELLOW); GrSetGCBackground(t,GREEN); GrText(btRect,t,18,20,"RECT",-1,GR_TFASCII); GrSetGCForeground(t,YELLOW); GrSetGCBackground(t,GREEN); GrText(btRound,t,15,20,"ROUND",-1,GR_TFASCII); GrSetGCForeground(t,YELLOW); GrSetGCBackground(t,GREEN); GrText(btClear,t,15,20,"CLEAR",-1,GR_TFASCII); //为个窗口(按钮)选择事件 GrSelectEvents(btRect,GR_EVENT_MASK_EXPOSURE | GR_EVENT_MASK_BUTTON_DOWN | GR_EVENT_MASK_BUTTON_UP); GrSelectEvents(btRound,GR_EVENT_MASK_EXPOSURE | GR_EVENT_MASK_BUTTON_DOWN | GR_EVENT_MASK_BUTTON_UP); GrSelectEvents(btLine,GR_EVENT_MASK_EXPOSURE | GR_EVENT_MASK_BUTTON_DOWN | GR_EVENT_MASK_BUTTON_UP); GrSelectEvents(btClear,GR_EVENT_MASK_EXPOSURE | GR_EVENT_MASK_BUTTON_DOWN | GR_EVENT_MASK_BUTTON_UP); GrSelectEvents(w,GR_EVENT_MASK_BUTTON_UP | GR_EVENT_MASK_BUTTON_DOWN | GR_EVENT_MASK_MOUSE_ENTER | GR_EVENT_MASK_MOUSE_EXIT | GR_EVENT_MASK_MOUSE_MOTION | GR_EVENT_MASK_CLOSE_REQ); GrSelectEvents(we,GR_EVENT_MASK_EXPOSURE | GR_EVENT_MASK_BUTTON_UP); while(1) { GrGetNextEvent(&event); switch(event.type) { case GR_EVENT_TYPE_BUTTON_DOWN: if(event.button.wid==btLine) { printf("drawline\n"); flag=DRAWLINE; gLine=GrNewGC(); GrSetGCForeground(gLine,RED); gb=GrNewGC(); GrSetGCForeground(gb,GRAY); GrFillEllipse(btLine,gb,32,15,30,13); GrSetGCForeground(gb,GREEN); GrSetGCBackground(gb,GRAY); GrText(btLine,gb,23,20,"LINE",-1,GR_TFASCII); coor[0].x=coor[1].x=0; coor[0].y=coor[1].y=0; i=0; } //printf("drawline\n"); if(event.button.wid==btRect) { printf("drawRectangle\n"); flag=DRAWRECT; gRect=GrNewGC(); GrSetGCForeground(gRect,YELLOW); gb=GrNewGC(); GrSetGCForeground(gb,GRAY); GrFillEllipse(btRect,gb,32,15,30,13); GrSetGCForeground(gb,GREEN); GrSetGCBackground(gb,GRAY); GrText(btRect,gb,18,20,"RECT",-1,GR_TFASCII); coor[0].x=coor[1].x=0; coor[0].y=coor[1].y=0; i=0; //GrFillEllipse(btLamp,gLamp,25,25,22,22); //return; } if(event.button.wid==btRound) { printf("drawRound\n"); flag=DRAWROUND; gRound=GrNewGC(); GrSetGCForeground(gRound,BLUE); gb=GrNewGC(); GrSetGCForeground(gb,GRAY); GrFillEllipse(btRound,gb,32,15,30,13); GrSetGCForeground(gb,GREEN); GrSetGCBackground(gb,GRAY); GrText(btRound,gb,15,20,"ROUND",-1,GR_TFASCII); coor[0].x=coor[1].x=0; coor[0].y=coor[1].y=0; i=0; } if(event.button.wid==btClear) { printf("drawClear\n"); flag=CLEAR; gb=GrNewGC(); GrSetGCForeground(gb,GRAY); GrFillEllipse(btClear,gb,32,15,30,13); GrSetGCForeground(gb,GREEN); GrSetGCBackground(gb,GRAY); GrText(btClear,gb,15,20,"CLEAR",-1,GR_TFASCII); GrFillRect(w,gb, 0, 0, 300, 180); coor[0].x=coor[1].x=0; coor[0].y=coor[1].y=0; i=0; //break; } if(event.button.wid==w) { printf("draw...\n"); draw(&event.button, &event.mouse); } break; case GR_EVENT_TYPE_BUTTON_UP: if(event.button.wid==btLine) { //draw line button again gb=GrNewGC(); GrSetGCForeground(gb,GREEN); GrFillEllipse(btLine,gb,32,15,30,13); t=GrNewGC(); GrSetGCForeground(t,YELLOW); GrSetGCBackground(t,GREEN); GrText(btLine,t,23,20,"LINE",-1,GR_TFASCII); } //printf("drawline\n"); if(event.button.wid==btRect) { //draw rectangle button again gb=GrNewGC(); GrSetGCForeground(gb,GREEN); GrFillEllipse(btRect,gb,32,15,30,13); t=GrNewGC(); GrSetGCForeground(t,YELLOW); GrSetGCBackground(t,GREEN); GrText(btRect,t,18,20,"RECT",-1,GR_TFASCII); } if(event.button.wid==btRound) { //draw round button again gb=GrNewGC(); GrSetGCForeground(gb,GREEN); GrFillEllipse(btRound,gb,32,15,30,13); t=GrNewGC(); GrSetGCForeground(t,YELLOW); GrSetGCBackground(t,GREEN); GrText(btRound,t,15,20,"ROUND",-1,GR_TFASCII); } if(event.button.wid==btClear) { printf("drawClear\n"); flag=0; gb=GrNewGC(); GrSetGCForeground(gb,GREEN); GrFillEllipse(btClear,gb,32,15,30,13); t=GrNewGC(); GrSetGCForeground(t,YELLOW); GrSetGCBackground(t,GREEN); GrText(btClear,t,15,20,"CLEAR",-1,GR_TFASCII); coor[0].x=coor[1].x=0; coor[0].y=coor[1].y=0; i=0; } if (event.button.wid==we) { GrClose(); exit(0); } break; } } //return redo; } void draw(GR_EVENT_BUTTON *bevent, GR_EVENT_MOUSE *mevent) { if(i<2) { //GrText(w,t,60,35," ",-1,GR_TFASCII); if(mevent->buttons==GR_BUTTON_L) { coor[i].x=bevent->x; coor[i].y=bevent->y; printf("draw...inw x%d=%d,y%d=%d\n,flag=%d\n",i,coor[i].x,i,coor[i].y,flag); i++; } if(event.mouse.buttons==GR_BUTTON_R) { coor[0].x=coor[1].x=0; coor[0].y=coor[1].y=0; printf("draw...inw x=%d,y=%d\n,flag=%d\n",coor[i].x,coor[i].y,flag); i=0; } switch(flag) { case DRAWLINE: drawLine(); break; case DRAWRECT: drawRect(); break; case DRAWROUND: drawRound(); break; } } else { t=GrNewGC(); GrSetGCForeground(t,RED); GrSetGCBackground(t,GREEN); //GrText(w,t,60,35," ",-1,GR_TFASCII); //GrText(w,t,60,35,"Please select one type to draw!",-1,GR_TFASCII); } return; } void drawLine() { if(i>1)//(coor[0].x!=0)&&(coor[0].y!=0)&&(coor[1].x!=0)&&(coor[1].y!=0)) { GrLine(w,gLine,coor[0].x, coor[0].y, coor[i-1].x, coor[i-1].y); coor[0].x=coor[i-1].x; coor[0].y=coor[i-1].y; i--; printf("drawline...\n"); } } void drawRect() { if(i>1) { i=0; width=coor[1].x-coor[0].x; height=coor[1].y-coor[0].y; GrRect(w,gRect ,coor[0].x, coor[0].y, width, height); printf("drawRect:x1=%d,y1=%d,x2=%d,y2=%d\n",coor[0].x,coor[0].y,coor[1].x,coor[1].y); } } void drawRound() { int x=coor[1].x-coor[0].x; int y=coor[1].y-coor[0].y; if(i>1) { i=0; r=sqrt(x*x+y*y); GrEllipse(w,gRound,coor[0].x, coor[0].y, r, r); printf("drawRound: x=%d,y=%d,r=%d\n",coor[0].x,coor[0].y,r); } } void draw_Error_win(char *arg[]) { GR_WINDOW_ID winError; winError=GrNewWindow(w,20,100,400,300,1,WHITE,BLUE); }