该程序通过并行机制处理鼠标和键盘的消息,为了简化程序裁减了一些代码,该程序可用为基础在该程序上添加代码可用于文本编辑,游戏和其他鼠标键盘通用的程序.
#include <graphics.h>
#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!=*px||my!=*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;
}
Word教程网 | Excel教程网 | Dreamweaver教程网 | Fireworks教程网 | PPT教程网 | FLASH教程网 | PS教程网 |
HTML教程网 | DIV CSS教程网 | FLASH AS教程网 | ACCESS教程网 | SQL SERVER教程网 | C语言教程网 | JAVASCRIPT教程网 |
ASP教程网 | ASP.NET教程网 | CorelDraw教程网 |