专注于互联网--专注于架构

最新标签
网站地图
文章索引
Rss订阅

首页 »C语言教程 » 鼠标画:鼠标画点 »正文

鼠标画:鼠标画点

来源: 发布时间:星期四, 2008年9月25日 浏览:87次 评论:0

#include<dos.h>
#include<graphics.h>
#include<conio.h>
union REGS regs;
int X__max,Y__max,x_max,y_max;
void Initgr(void) /*屏幕初始化成图形模式*/
{int w,h,grdriver=DETECT,grmode;
initgraph(&grdriver,&grmode,\"\");/*在双引号中可加你tc放的路径,但要在tc里有EGAVGA.BGI这个来初始图形*/
if(graphresult())/*若调用不成功,退出*/
{printf(\"\\n 在双引号中可加你tc放的路径,但要在tc里有EGAVGA.BGI这个来初始图形.\\n\");
exit(1);
}
X__max=getmaxx();/*求横向象点坐标数*/
Y__max=getmaxy();/*求纵向象点坐标数*/
getaspectratio(&w,&h);/*求纵横比*/
x_max=1000; /*设置屏幕坐标的宽度*/
y_max=x_max*(float)Y__max*h/((float)X__max*w);
}

int Msinit(int Xlo,int Xhi,int Ylo,int Yhi)/*鼠标初始化*/
{int retcode;
regs.x.ax=0;/*初始化鼠标*/
int86(0x33,&regs,&regs);
retcode=regs.x.ax;
if(retcode==0) return 0;
regs.x.ax=7;/*设置鼠标X方向的移动范围*/
regs.x.cx=Xlo;
regs.x.dx=Xhi;
int86(0x33,&regs,&regs);
regs.x.ax=8;/*设置鼠标Y方向的移动范围*/
regs.x.cx=Ylo;
regs.x.dx=Yhi;
int86(0x33,&regs,&regs);
regs.x.ax=15;/*设置mickey与象素的比,这各会影响鼠标移动速度*/
regs.x.cx=(int)(x_max/X__max);
regs.x.dx=(int)(y_max/Y__max);
int86(0x33,&regs,&regs);
return retcode;
}
int Msread(int *px,int *py,int *pbuttons)/*读鼠标位置及状态*/
{static int x0=320,y0=240,but0=0;
int xnew,ynew,ch;
do{
if(kbhit()){
ch=getch();
if(ch==13){
*pbuttons=1;
return -1;
}
else return ch; /*返回键盘输入*/
}
regs.x.ax=3; /*调用功能3,读鼠标位置及状态*/
int86(0x33,&regs,&regs);
xnew=regs.x.cx;/*返回鼠标当前的位置的X坐标*/
ynew=regs.x.dx;/*返回鼠标当前的位置的Y坐标*/
*pbuttons=regs.x.bx;/*返回鼠标当前的状态*/
}while(xnew==x0&&ynew==y0&&*pbuttons==but0);
/*当鼠标状态改变或位置改变终止循环*/
but0=*pbuttons;/*将鼠标状态保存到静态变量中*/
x0=xnew;y0=ynew;/*将鼠标位置保存到静态变量中*/
*px=xnew;*py=(int)(y_max-ynew);
return -1;
}
int Xpixel(int x)/*由象素坐标变换为屏幕坐标*/
{ return (int)((long)X__max*x/x_max);}
int Ypixel(int y)
{ return Y__max-(int)((long)Y__max*y/y_max);}
void Cursor(int x,int y) /*显示十字光标*/
{int X=Xpixel(x),Y=Ypixel(y),color;
char *str=\" \\0\";
line(X-8,Y,X-3,Y);
line(X,Y-8,X,Y-4);
line(X+3,Y,X+8,Y);
line(X,Y+4,X,Y+8);
color=getcolor();
setcolor(BLACK);
outtextxy(X__max-100,10,str);/*删除前次显示值*/
sprintf(str,\"%d,%d\",x,y);
setcolor(WHITE);
outtextxy(X__max-100,10,str);/*在屏幕右上角显示当前光标的坐标*/
setcolor(color);
}
main()
{int buttons,X,Y,x,y,a,b;
char i;
Initgr();/*初始化图形屏幕*/
setcolor(EGA_LIGHTRED);/*设置屏幕前景色*/
line(1,1,X__max-1,1); /*在屏幕四周画一矩形*/
line(1,1,1,Y__max-1);
line(X__max-1,1,X__max-1,Y__max-1);
line(1,Y__max-1,X__max-1,Y__max-1);
setcolor(EGA_WHITE);
printf(\"\\n 按鼠标右键终止程序\\n\");
printf(\" 然后按任意键退出\");
setwritemode(XOR_PUT);/*设置屏幕输出模式*/
Msinit(0,(int)x_max,0,(int)y_max);/*初始化鼠标*/
a=x_max;b=y_max;
x=0;
Cursor(a,b); /*在a=x_max;b=y_max;处画指针*/
while(x!=2)
{Msread(&X,&Y,&x);
Cursor(a,b); /*删除之前的鼠标,因为屏幕输出模式的关系*/
if(x==1){
a=(int)(X*1.0/x_max*X__max);b=(int)((y_max-Y)*1.0/y_max*Y__max);
circle(a,b,1); } /*画点*/
Cursor(X,Y);
a=X;b=Y;
}
Cursor(X,Y);/*再调用一次把原来的指针削掉*/
getch();
closegraph();}

/*运行后在屏幕上移动鼠标并点左键,即可画点*/

0

相关文章

读者评论

发表评论

  • 昵称:
  • 内容: