如何在程序中设计使用鼠标
程序清单如下:#include "dos.h"
#include "bios.h"
#include "graphics.h"
#include "io.h"
#define M-Pressed 1
union REGS in,out;
struct SREGS seg;
char menu[4][12]={"menumenu-1","menumenu-2","menumenu-3","exit"};
int chz;
int m-exist=0;
unsigned mx,my;
/*鼠标当前位置*/
main()
{ int mz,zt=1;
int gdrive=VGA,gmode=VGAHI;
initgraph(&gdrive,&gmode,"d:\tc ");
setfillstyle(1,BLUE);
bar(0,0,639,479);
m-exist=checkmouse();
initm(1);
while(1)
{ initm(zt);
if((chz=mgetch())==M-Pressed && m-exist)
{ delay(100); /*鼠标处理*/
if(in-range(20,20,140,30)) zt=1;
if(in-range(20,40,140,50)) zt=2;
if(in-range(20,60,140,70)) zt=3;
if(in-range(20,80,140,90)) break;}
else{ if(chz==0x4800)
/*键盘处理*/
if(zt>1) zt--;
else zt=4;
if(chz==0x5000)
if(zt<4) zt++;
else zt=1;
if(chz==0x000d && zt==4) break;
if(chz==0x001b) break;}} /*按ESC退出*/
closegraph();} /*菜单处理*/
int initm(int par1)
{ int i;
if(m-exist) hidemouse();
setfillstyle(1,RED);
for(i=0;i<4;i++)
{ bar(20,20+i*20,140,30+i*20);
outtextxy(20,20+i*20,menu[i]);}
setfillstyle(1,GREEN);
bar(20,20*par1,140,30+20*(par1-1));
outtextxy(20,20*par1,menu[par1-1]);}
int in-range(int mx1,int my1,int mx2,int my2)
{ if(mx>my1 && mx<my2 &&my>mx1 && my<mx2)
return 1;
else return 0;}
/*鼠标模拟键盘输入函数getch()*/
int mgetch()
{ char ch;
while(!bioskey(1))
{ showmouse();
in.x.bx=0; /*取鼠标当前位置*/
in.x.ax=3;
int86(0x33,&in,&out);
delay(30);
if(out.x.bx==1)
{mx=out.x.dx;
my=out.x.cx;
return M-Pressed;}}
return(getchr());} /*隐藏鼠标*/
hidemouse()
{in.x.ax=02;
int86(0x33,&in,&out);} /*显示鼠标*/
showmouse()
{in.x.ax=01;
int86(0x33,&in,&out);} /*检查鼠标是否存在,1-存在,0-不存在*/
int checkmouse()
{in.x.ax=0;
int86(0x33,&in,&out);
if(out.x.bx!=2)
return 0;
return 1; }
int getchr()
{ int ch1,ch2;
ch1=getch();
if(ch1==0)
{ch2=getch();
ch2=ch2<<8;
ch2|=ch1;
return(ch2);}
else return (ch1);}