⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 例5-1.c

📁 王为青 刘变红 编著《C语言高级编程及实例剖析》源代码
💻 C
字号:
/* Note:Your choice is C IDE */
#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;
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,Please 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);    /*    回到系统  */
		}
	}
} 

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);
}

int init(int xmi,int xma,int ymi,int yma) /*    鼠标初始化函数  */   
{
	int retcode;
	regs.x.ax=0;
	int86(51,&regs,&regs);
	retcode=regs.x.ax;
	if(retcode==0)
		return 0;    /*    返回0表示鼠标或鼠标驱动程序未安装    */    
	regs.x.ax=7;
	regs.x.cx=xmi;
	regs.x.dx=xma;
	int86(51,&regs,&regs);
	regs.x.ax=8;
	regs.x.cx=ymi;
	regs.x.dx=yma;
	int86(51,&regs,&regs);    /*   表示鼠标和驱动程序已安装    */
	return retcode;
}
int read(int*mx,int*my,int*mbutt)    /*   读鼠标的位置和按钮状态函数*/
{
	int xx0=*mx,yy0=*my,but0=0;
	int xnew,ynew;
	do  
	{
		regs.x.ax=3;
		int86(51,&regs,&regs);
		xnew=regs.x.cx;
		ynew=regs.x.dx;
		*mbutt=regs.x.bx;
	}while(xnew==xx0&&ynew==yy0&&*mbutt==but0);
	*mx=xnew;
	*my=ynew;
	if(*mbutt)    
	{
		*mx=xnew;
		*my=ynew;
		return -1;
	}
	else 
	{
		*mx=xnew;
		*my=ynew;
		return 1;
	}
} 

void newxy(int*mx,int*my,int*mbutt)    /*   是否有按钮按下,若按下,在新位置画十字  */
{
	int ch,xx0=*mx,yy0=*my,x,y;
	int xm,ym;
	ch=read(&xm,&ym,mbutt);
	if(ch>0)    
	{
		cursor(xx0,yy0);
		cursor(xm,ym);
	}
	else
	{
		cursor(xx0,yy0);
		cursor(xm,ym);
		putpixel(xm,ym,7);
	}
	*mx=xm;
	*my=ym;
}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -