在Pascal中实现对鼠标的控制

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


{**BEGIN***}
unit Mouse;
interface
const LeftKey=0;
RightKey=1;
MidKey=2;
procedure InitMouse(var s:boolean;var c:word);
procedure ShowMouse;
procedure HideMouse;
procedure GetMouse(var s,x,y:word);
procedure SetMouse(x,y:word);
procedure DownMouse(b:word;var s,c,x,y:word);
procedure UpMouse(b:word;var s,c,x,y:word);
implementation
procedure InitMouse(var s:boolean;var c:word);
var _s,_c:word;
begin
asm
mov ax,0
int $33
mov _s,ax
mov _c,bx
end;
s:=_s<>0;
c:=_c;
end;
procedure ShowMouse;
begin
asm
mov ax,1
int $33
end;
end;
procedure HideMouse;
begin
asm
mov ax,2
int $33
end;
end;
procedure GetMouse(var s,x,y:word);
var _s,_x,_y:word;
begin
asm
mov ax,3
int $33
mov _s,bx
mov _x,cx
mov _y,dx
end;
s:=_s;
x:=_x;
y:=_y;
end;
procedure SetMouse(x,y:word);
begin
asm
mov ax,4
mov cx,x
mov dx,y
int $33
end;
end;
procedure DownMouse(b:word;var s,c,x,y:word);
var _s,_c,_x,_y:word;
begin
asm
mov ax,5
mov bx,b
int $33
mov _s,ax
mov _c,bx
mov _x,cx
mov _y,dx
end;
s:=_s;
c:=_c;
x:=_x;
y:=_y;
end;
procedure UpMouse(b:word;var s,c,x,y:word);
var _s,_c,_x,_y:word;
begin
asm
mov ax,6
mov bx,b
int $33
mov _s,ax
mov _c,bx
mov _x,cx
mov _y,dx
end;
s:=_s;
c:=_c;
x:=_x;
y:=_y;
end;
begin
end.
{***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)。
以下是一段示例代码,用左键绘图,中键换色,右键退出。


{***BEGIN***}
program example;
uses Mouse,Graph;
var gDrv,gMode,cl:integer;
x1,x2,y1,y2,s,c,x,y,t:word;
p:boolean;
begin
gDrv:=Detect;
gMode:=0;
InitGraph(gDrv,gMode,'..\BGI');
InitMouse(p,t);
ShowMouse;
cl:=1;
SetColor(cl);
if p then
repeat
x2:=x1;
y2:=y1;
GetMouse(s,x1,y1);
if s=1 then
begin
HideMouse; {为什么要HideMouse?自己想吧。}
Line(x1,y1,x2,y2);
ShowMouse;
end;
DownMouse(MidKey,s,c,x,y);
if c>0 then
begin
inc(cl);
if cl>15 then cl:=1;
SetColor(cl);
end;
DownMouse(RightKey,s,c,x,y);
until c>0
else
writeln('Mouse not found.');
CloseGraph;
end.
{***EOF***}

zp8497586rq

Comments are closed.