2005年10月24日13:33:51

在Pascal中实现对鼠标的控制

把以下代码复制到Pascal中,保存为Mouse.pas,编译后就是Mouse.tpu。然后把Mouse.tpu复制到units文件夹中,在你自己的程序中加入”uses Mouse”一行就行了。
这段程序通过调用33h中断实现对鼠标的控制。

  1. {**BEGIN***}
  2. unit Mouse;
  3. interface
  4. const LeftKey=0;
  5.       RightKey=1;
  6.       MidKey=2;
  7. procedure InitMouse(var s:boolean;var c:word);
  8. procedure ShowMouse;
  9. procedure HideMouse;
  10. procedure GetMouse(var s,x,y:word);
  11. procedure SetMouse(x,y:word);
  12. procedure DownMouse(b:word;var s,c,x,y:word);
  13. procedure UpMouse(b:word;var s,c,x,y:word);
  14. implementation
  15. procedure InitMouse(var s:boolean;var c:word);
  16. var _s,_c:word;
  17. begin
  18.      asm
  19.         mov ax,0
  20.         int $33
  21.         mov _s,ax
  22.         mov _c,bx
  23.      end;
  24.      s:=_s<>0;
  25.      c:=_c;
  26. end;
  27. procedure ShowMouse;
  28. begin
  29.      asm
  30.         mov ax,1
  31.         int $33
  32.      end;
  33. end;
  34. procedure HideMouse;
  35. begin
  36.      asm
  37.         mov ax,2
  38.         int $33
  39.      end;
  40. end;
  41. procedure GetMouse(var s,x,y:word);
  42. var _s,_x,_y:word;
  43. begin
  44.      asm
  45.         mov ax,3
  46.         int $33
  47.         mov _s,bx
  48.         mov _x,cx
  49.         mov _y,dx
  50.      end;
  51.      s:=_s;
  52.      x:=_x;
  53.      y:=_y;
  54. end;
  55. procedure SetMouse(x,y:word);
  56. begin
  57.      asm
  58.         mov ax,4
  59.         mov cx,x
  60.         mov dx,y
  61.         int $33
  62.      end;
  63. end;
  64. procedure DownMouse(b:word;var s,c,x,y:word);
  65. var _s,_c,_x,_y:word;
  66. begin
  67.      asm
  68.         mov ax,5
  69.         mov bx,b
  70.         int $33
  71.         mov _s,ax
  72.         mov _c,bx
  73.         mov _x,cx
  74.         mov _y,dx
  75.      end;
  76.      s:=_s;
  77.      c:=_c;
  78.      x:=_x;
  79.      y:=_y;
  80. end;
  81. procedure UpMouse(b:word;var s,c,x,y:word);
  82. var _s,_c,_x,_y:word;
  83. begin
  84.      asm
  85.         mov ax,6
  86.         mov bx,b
  87.         int $33
  88.         mov _s,ax
  89.         mov _c,bx
  90.         mov _x,cx
  91.         mov _y,dx
  92.      end;
  93.      s:=_s;
  94.      c:=_c;
  95.      x:=_x;
  96.      y:=_y;
  97. end;
  98. begin
  99. end.
  100. {***EOF***}

各过程的说明:
procedure InitMouse(var s:boolean;var c:word);
初始化鼠标
s :如果初始化成功s为true,否则为false。
c :鼠标按键个数。
procedure ShowMouse;
显示光标。
procedure HideMouse;
隐藏光标。
procedure GetMouse(var s,x,y:word);
获取鼠标状态。
s: 各按键状态。
x: 光标当前x坐标值。
y: 光标当前y坐标值。
procedure SetMouse(x,y:word);
设置光标坐标。
x: 光标x坐标值。
y: 光标y坐标值。
procedure DownMouse(b:word;var s,c,x,y:word);
取某一按键按下信息。
b: 欲检测的按键。左键:0或Leftkey 右键:1或RightKey 中键:2或MidKey
s: 各按键状态。
c: 在这次调用之前该按键被按下的次数。
x: 光标被按下的x坐标值。
y: 光标被按下的y坐标值。
procedure UpMouse(b:word;var s,c,x,y:word);
取某一按键释放信息。
b: 欲检测的按键。左键:0或Leftkey 右键:1或RightKey 中键:2或MidKey
s: 各按键状态。
c: 在这次调用之前该按键被释放的次数。
x: 光标被释放的x坐标值。
y: 光标被释放的y坐标值。
程序返回的各按键状态的信息通过返回值的二进制位的值来表达,如下所示:
值 0 1
第0位 左键未按下 左键正在按下
第1位 右键未按下 右键正在未按下
例如:当只有左键按下时,按键状态的信息为1(10),右键按下为2(10),左键右键一起按下时为3(10),只有中键按下时为4(10)。
以下是一段示例代码,用左键绘图,中键换色,右键退出。

  1. {***BEGIN***}
  2. program example;
  3. uses Mouse,Graph;
  4. var gDrv,gMode,cl:integer;
  5.     x1,x2,y1,y2,s,c,x,y,t:word;
  6.     p:boolean;
  7. begin
  8.      gDrv:=Detect;
  9.      gMode:=0;
  10.      InitGraph(gDrv,gMode,'..\BGI');
  11.      InitMouse(p,t);
  12.      ShowMouse;
  13.      cl:=1;
  14.      SetColor(cl);
  15.      if p then
  16.         repeat
  17.            x2:=x1;
  18.            y2:=y1;
  19.            GetMouse(s,x1,y1);
  20.            if s=1 then
  21.            begin
  22.                 HideMouse;  {为什么要HideMouse?自己想吧。}
  23.                 Line(x1,y1,x2,y2);
  24.                 ShowMouse;
  25.            end;
  26.            DownMouse(MidKey,s,c,x,y);
  27.            if c>0 then
  28.            begin
  29.                 inc(cl);
  30.                 if cl>15 then cl:=1;
  31.                 SetColor(cl);
  32.            end;
  33.            DownMouse(RightKey,s,c,x,y);
  34.         until c>0
  35.      else
  36.          writeln('Mouse not found.');
  37.      CloseGraph;
  38. end.
  39. {***EOF***}

本文共有 0 条评论在Pascal中实现对鼠标的控制


  1. 没有评论

请留下您的评论:

您可以使用以下XHTML代码: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>