在这里我用STRUCT和指向函数的指针定义结合实现了模拟鼠标类,并用它来写了橡皮筋画线法
#include <stdio.h>
#include <graphics.h>
#include <dos.h>
typedef struct
{
void (*Inits)();/*初始化鼠标驱动*/
int (*MButton)();/*按键位置与按键状态*/
void (*MouseIco)();/*画鼠标光标*/
int x;/*横向坐标*/
int y;/*竖向坐标*/
int ox;
int oy;
int button;/*按键标志*/
}MOUSE;/*鼠标类*/
void mInits();
int mMButton();
void mMouseIco();
union REGS rg;
int main()
{
MOUSE *pM,mouse;
int graphdrive=0,graphmode=0;
int tracking=0,x1,y1,ox1,oy1;
pM=&mouse;
pM->Inits=mInits;
pM->MButton=mMButton;
pM->MouseIco=mMouseIco;
initgraph(&graphdrive,&graphmode,"");
setbkcolor(1);
setwritemode(1);/*画线模式*/
pM->Inits(&pM->x,&pM->y);
pM->ox=pM->x;
pM->oy=pM->y;
do
{
pM->button=pM->MButton(&pM->x,&pM->y);
pM->MouseIco(&pM->x,&pM->y,&pM->ox,&pM->oy);
if(pM->button==1)
{
if(!tracking)/*捕捉开关*/
{
ox1=x1=pM->x;
oy1=y1=pM->y;
tracking=1;
}
if(pM->x!=ox1||pM->y!=oy1)
{
line(x1,y1,ox1,oy1);
ox1=pM->x;
oy1=pM->y;
line(x1,y1,pM->x,pM->y);
}
}
else tracking=0;
}while(pM->button!=3);
}
void mInits(int *mx,int *my)
{
rg.x.ax=0;
int86(0x33,&rg,&rg);
if(rg.x.ax==0)
exit(1);
rg.x.ax=7;
rg.x.cx=2;
rg.x.dx=640;
int86(0x33,&rg,&rg);
rg.x.ax=8;
rg.x.cx=2;
rg.x.dx=480;
int86(0x33,&rg,&rg);
rg.x.ax=4;
*mx=rg.x.cx=640/2;
*my=rg.x.dx=480/2;
int86(0x33,&rg,&rg);
rectangle(rg.x.cx-2,rg.x.dx-2,rg.x.cx+2,rg.x.dx+2);
}
int mMButton(int *lx,int *ly)
{
rg.x.ax=3;
int86(0x33,&rg,&rg);
*lx=rg.x.cx;
*ly=rg.x.dx;
return rg.x.bx;
}
void mMouseIco(int *x,int *y,int *oldx,int *oldy)
{
if(*x!=*oldx||*y!=*oldy)
{
rectangle(*oldx-2,*oldy-2,*oldx+2,*oldy+2);
*oldx=*x;
*oldy=*y;
rectangle(*x-2,*y-2,*x+2,*y+2);
}
}
Word教程网 | Excel教程网 | Dreamweaver教程网 | Fireworks教程网 | PPT教程网 | FLASH教程网 | PS教程网 |
HTML教程网 | DIV CSS教程网 | FLASH AS教程网 | ACCESS教程网 | SQL SERVER教程网 | C语言教程网 | JAVASCRIPT教程网 |
ASP教程网 | ASP.NET教程网 | CorelDraw教程网 |