通过并行机制处理鼠标和键盘
关注图老师设计创意栏目可以让大家能更好的了解电脑,知道有关于电脑的更多有趣教程,今天给大家分享通过并行机制处理鼠标和键盘教程,希望对大家能有一点小小的帮助。
#include stdio.h
#include conio.h
#include dos.h
#include bios.h
union REGS regs;
unsigned size;
void far *Buffer;
int ScanKey();
void Mouse();
void Init();
int InitMouseDrive();
int main()
{
int key,x,y,cx,cy;
x=1,y=1;
Init();/*初始化图形模式*/
if(InitMouseDrive(&cx,&cy))/*安装鼠标*/
{
printf("Mouse Drive Error!");
exit(0);
}
size=imagesize(cx-2,cy-2,cx+2,cy+2);
Buffer=(void *)malloc(size);
getimage(cx-2,cy-2,cx+2,cy+2,Buffer);
do
{
if(kbhit())
{
key=ScanKey();/*得到按键结果*/
rectangle(x,y,x+5,y+5);
x+=10;
if(x=630)
{
x=1;
y+=10;
}
}
else
Mouse(&cx,&cy);/*访问鼠标信息的到新的光标坐标*/
}while(key!=0x01);
closegraph();
}
int ScanKey()/*键盘*/
{
return(bioskey(0)8);
}
void Init()/*图形驱动*/
{
int graphdrive=0,graphmode=0;
initgraph(&graphdrive,&graphmode,"");
setcolor(15);
}
void Mouse(int *px,int *py)/*鼠标*/
{
int mx,my,omx,omy;
regs.x.ax=3;
int86(0x33,®s,®s);
omx=*px,omy=*py;
mx=regs.x.cx;
my=regs.x.dx;
if(mx!=*pxmy!=*py)/*当鼠标位置改变是重新绘制光标*/
{
*px=mx; *py=my;
putimage(omx-2,omy-2,Buffer,COPY_PUT);
free(Buffer);
size=imagesize(mx-2,my-2,mx+2,my+2);
Buffer=(void *)malloc(size);
getimage(mx-2,my-2,mx+2,my+2,Buffer);
rectangle(mx-2,my-2,mx+2,my+2);
}
}
int InitMouseDrive(int *rpx,int *rpy)/*鼠标驱动*/
{
int Found=0;
regs.x.ax=0;
int86(0x33,®s,®s);
Found=regs.x.ax;
regs.x.ax=7;
regs.x.cx=3;
regs.x.dx=635;
int86(0x33,®s,®s);
regs.x.ax=8;
regs.x.cx=3;
regs.x.dx=480;
int86(0x33,®s,®s);
regs.x.ax=4;
regs.x.cx=*rpx=640/2;
regs.x.dx=*rpy=480/2;
int86(0x33,®s,®s);
if(Found==0)
return 1;
return 0;
}