首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

c语言中鼠标点击操作,没点击,但是regs.x.bx就被置零了

2013-12-02 
c语言中鼠标点击操作,没有点击,但是regs.x.bx就被置零了?#include stdio.h#include dos.h#include gr

c语言中鼠标点击操作,没有点击,但是regs.x.bx就被置零了?
#include <stdio.h>
#include <dos.h>
#include <graphics.h>

union REGS regs;

int init(int xmi, int xma, int ymi, int yma);
int read(int *mx, int *my, int *mbutt);
void cursor(int x, int y);
void newxy(int *mx, int *my, int *mbutt);

int xmin, xmax, ymin, ymax, x_max = 639, y_max = 479;

int main(void)
{
   int buttons, xm, ym, x0, y0, x, y;
   char str[100];
   int driver = VGA;
   int mode = VGAHI;

   initgraph(&driver, &mode, "d:\\turboc");
   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)
   {
   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)
   {
   cleardevice();
   exit(0);
   }
   }

   return 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(0x33, &regs, &regs);

   retcode = regs.x.ax;
   if (retcode == 0)
   return 0;

   regs.x.ax = 7;
   regs.x.cx = xmi;
   regs.x.dx = xma;
   int86(0x33, &regs, &regs);

   regs.x.ax = 8;
   regs.x.cx = ymi;
   regs.x.dx = yma;
   int86(0x33, &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(0x33, &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;
}这是一个关于鼠标操作的函数,刚开始执行的几次,鼠标点击后,能够跳出界面。但是到后面只要鼠标移动到指定的区域,但是还没有点击就跳出了那个图形界面。
我通过单步调试发现,那个regs.x.bx的数值已经是1了,但是我根本就没有点击呀!
[解决办法]
使用鼠标时,应在cmd窗口左上角图标上点左键、属性中的快速编辑模式关掉。

不要使用
while (条件)
更不要使用
while (组合条件)
要使用
while (1) {
 if (条件1) break;
 //...
 if (条件2) continue;
 //...
 if (条件3) return;
 //...
}
因为前两种写法在语言表达意思的层面上有二义性,只有第三种才忠实反映了程序流的实际情况。
典型如:
下面两段的语义都是当文件未结束时读字符
whlie (!feof(f)) {
 a=fgetc(f);
 //...
 b=fgetc(f);//可能此时已经feof了!
 //...
}
而这样写就没有问题:
whlie (1) {
 a=fgetc(f);
 if (feof(f)) break;
 //...
 b=fgetc(f);
 if (feof(f)) break;
 //...
}
类似的例子还可以举很多。

热点排行