1 #include//鼠标 2 #include 3 #include 4 int init(); 5 int read_mouse(); 6 void cursor(); 7 void newxy(); 8 int main() 9 { 10 int buttons,x,y; 11 char str[100]; 12 int driver=VGA; 13 int mode=VGAHI; 14 initgraph(&driver,&mode,""); 15 cleardevice(); 16 setfillstyle(SOLID_FILL,BLUE); 17 bar(1,1,639,478); 18 outtextxy(3,15,"move mouse using any button."); 19 outtextxy(285,15,"quit"); 20 setwritemode(XOR_PUT); 21 if(init(2,638,8,477)==0) 22 { 23 printf("Mouse or Driver Absent,Please install!"); 24 delay(5000); 25 closegraph(); 26 exit(1); 27 } 28 x=320; 29 y=240; 30 cursor(x,y); 31 for(;;) 32 { 33 newxy(&x,&y,&buttons); 34 if(x>=280&&x<=330&&y>=12&&y<=33&&buttons) 35 { 36 cleardevice(); 37 closegraph(); 38 exit(1); 39 } 40 } 41 } 42 void cursor(int x,int y) 43 { 44 int x1,y1,x2,y2; 45 x1=x-4; 46 y1=y+4; 47 x2=x-3; 48 y2=y+3; 49 line(x1,y,x2,y); 50 line(x,y1,x,y2); 51 } 52 int init(int xmin,int xmax,int ymin,int ymax) 53 { 54 union REGS regs; 55 regs.x.ax=0; 56 int86(51,®s,®s); 57 if(regs.x.ax==0) 58 return 0; 59 regs.x.ax=7; 60 regs.x.cx=xmin; 61 regs.x.dx=xmax; 62 int86(51,®s,®s); 63 regs.x.ax=8; 64 regs.x.cx=ymin; 65 regs.x.dx=ymax; 66 int86(51,®s,®s); 67 return -1; 68 } 69 int read_mouse(int *mx,int *my,int *mbutton) 70 { 71 union REGS regs; 72 int x0=*mx,y0=*my,button0=*mbutton; 73 int xnew,ynew; 74 do{regs.x.ax=3; 75 int86(0x33,®s,®s); 76 xnew=regs.x.cx; 77 ynew=regs.x.dx; 78 *mbutton=regs.x.bx; 79 }while(xnew==x0&&ynew==y0&&*mbutton==button0); 80 *mx=xnew; 81 *my=ynew; 82 switch(*mbutton) 83 { 84 case 0:return 0; 85 case 1:return 1; 86 case 2:return 2; 87 case 3:return 3; 88 default:return 4; 89 } 90 } 91 void newxy(int *mx,int *my,int *mbutt) 92 { 93 int ch,xx0=*mx,yy0=*my; 94 int xm,ym; 95 ch=read_mouse(&xm,&ym,mbutt); 96 cursor(xx0,yy0); 97 cursor(xm,ym); 98 switch(ch){ 99 case 0:break;100 case 1:circle(xm,ym,6);break;101 case 2:rectangle(xm-6,ym-6,xm+6,ym+6);break;102 default:putpixel(xm,ym,7);break;103 }104 *mx=xm;105 *my=ym; 106 }