📄 3_10.c
字号:
/*------例程3-10-------*/
#include <dos.h>
#include <stdio.h>
#include <graphics.h>
union REGS regs;
int init();
int read();
void cursor(),newxy();
int xmin,xmax,ymin,ymax,x_max=639,y_max=479;
int main()
{
int buttons,xm,ym,x0,y0,x,y;
char str[100];
int driver=VGA;
int mode=VGAHI;
initgraph(&driver,&mode,"");
clrscr();
rectangle(0,0,x_max,y_max);
setfillstyle(SOLID_FILL,BLUE);
bar(1,1,x_max-1,y_max-1);
outtextxy(3,15,"move mouse using any button.");
outtextxy(285,15,"quit");
xmin=2;
xmax=x_max-1;
ymin=8;
ymax=y_max-2;
setwritemode(XOR_PUT);
if(init(xmin,xmax,ymin,ymax)==0 ) /* 调用init函数对鼠标器初始化*/
{
printf("Mouse or Mouse Driver Absent,P1ease install!");
delay(5000);
exit(1);
}
x=320;y=240;
cursor(x,y); /* 置十字光标在屏幕中心。 */
for(;;)
{
newxy(&x,&y,&buttons);
if(x>=280&& x<=330 &&y>=12&&y<=33&& buttons) /*十字光标移到quit处时*/
{
cleardevice();
exit(0); /* 回到系统*/
}
}
}
/*画光标函数cursor()*/
/*该函数将用画线函数line()画出一个十字形光标*/
void cursor(int x,int y)
{
int x1,x2,y1,y2;
x1=x-4;
x2=x+4;
y1=y-3;
y2=y+3;
line(x1,y,x2,y);
line(x,y1,x,y2);
}
/*鼠标器初始化函数init ()*/
/*该函数将通过调用int 33H的0号功能调用对鼠标器进行初始化,调用7号和8号功能,设置x和y位置的最小和最大值。这就为鼠标器移动进行了初始化准备。由于0号功能调用是测试鼠标驱动程序是否安装,因此在运行该程序前必须首先执行鼠标驱动程序mouse.com,若调用该函数执行了0号功能调用,当返回值为0时(即返回参数为0),表示未安装成功,这可能是鼠标器或驱动程序末安装。这时程序将显示Mouse or Mouse Driver Absent,并回到系统。*/
int init(int xmi,int xma,int ymi,int yma)
{
int retcode;
regs.x.ax=0;
int86(51,®s,®s);
retcode=regs.x.ax;
if(retcode==0)
return 0; /* 返回0表示鼠标或鼠标驱动程序未安装 */
regs.x.ax=7;
regs.x.cx=xmi;
regs.x.dx=xma;
int86(51,®s,®s);
regs.x.ax=8;
regs.x.cx=ymi;
regs.x.dx=yma;
int86(51,®s,®s); /* 表示鼠标器和驱动程序已安装 */
return retcode;
}
/*读鼠标的位置和按钮状态函数read()*/
/*该函数将通过调用int 33H的3号功能调用,读鼠标的位置和按钮状态。鼠标的x、y位置值将由指针mx和my给出,而按钮状态则由mbutt指针给出。*/
int read(int *mx,int *my,int *mbutt)
{
int xx0=*mx,yy0=*my,but0=0,mb;
int xnew, ynew;
do {
regs.x.ax=3;
int86(51,®s,®s);
xnew=regs.x.cx;
ynew=regs.x.dx;
*mbutt=regs.x.bx;
} while(xnew==xx0 && ynew==yy0 && *mbutt == but0 );
*mx=xnew;
*my=ynew;
mb=(*mbutt);
if(mb){
if(mb==1) return 1; /*左键按下*/
if (mb==2) return 2; /*右键按下*/
return 3; /*其它的按键情况*/
}
else
return 0;
}
/*该函数将通过调用read()函数来判断是否有按钮按下,若按下,则调用cursor()函数在新位置画出一十字光标*/
void newxy(int *mx,int *my,int *mbutt)
{
int ch,xx0=*mx,yy0=*my,x,y;
int xm,ym;
ch=read(&xm,&ym,mbutt);
switch (ch) {
case 0:
cursor(xx0,yy0);
cursor(xm,ym);
break;
case 1:
cursor(xx0,yy0);
cursor(xm,ym);
circle(xm,ym,6);
break;
case 2:
cursor(xx0,yy0);
cursor(xm,ym);
rectangle(xm,ym,xm+12,ym+12);
break;
case 3:
cursor(xx0,yy0);
cursor(xm,ym);
putpixel(xm,ym,7);
break;
}
*mx=xm;
*my=ym;
}
/*------例程3-10结束-------*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -