您需要安装 DOS 鼠标驱动程序。如果使用 DOSBox,则已经可以使用一个。您需要通过 (Int 0x33) 对鼠标服务1 例程进行中断调用。 DOS 中断相关信息的最佳来源之一是Ralph Brown's Interrupt List。 Turbo-C 支持通过int86 函数调用 BIOS 服务(包括 DOS.H)。
Int 0x33 提供鼠标服务,但为了让您立即开始使用鼠标功能:
使用 RBIL 上的鼠标中断信息来指定输入和输出参数,您可以为 Turbo-C 代码创建一些基本的鼠标包装器。基本包装器看起来像:
#include<stdio.h>
#include<conio.h>
#include<dos.h>
#define MOUSE_INT 0x33
#define MOUSE_FUNC_SHOW 1
#define MOUSE_FUNC_HIDE 2
#define MOUSE_FUNC_GETSTATE 3
#define MOUSE_FUNC_SETPOS 4
#define MOUSE_LEFT_BUTTON 0x01
#define MOUSE_RIGHT_BUTTON 0x02
typedef struct {
unsigned int cur_x;
unsigned int cur_y;
unsigned int buttons;
} mouse_state_t;
void mouse_hide()
{
union REGS regs_in, regs_out;
regs_in.x.ax = MOUSE_FUNC_HIDE;
int86(MOUSE_INT, ®s_in, ®s_out);
}
void mouse_show()
{
union REGS regs_in, regs_out;
regs_in.x.ax = MOUSE_FUNC_SHOW;
int86(MOUSE_INT, ®s_in, ®s_out);
}
void mouse_get_state(mouse_state_t *mouse_state)
{
union REGS regs_in, regs_out;
regs_in.x.ax = MOUSE_FUNC_GETSTATE;
int86(MOUSE_INT, ®s_in, ®s_out);
mouse_state->cur_x = regs_out.x.cx;
mouse_state->cur_y = regs_out.x.dx;
mouse_state->buttons = regs_out.x.bx;
/* buttons field is a bit mask. Bit 0 (lowest bit) is set when left
mouse button is pressed. Bit 1 is set when right mouse button is pressed */
}
void mouse_set_pos(int pos_x, int pos_y)
{
union REGS regs_in, regs_out;
regs_in.x.ax = MOUSE_FUNC_SETPOS;
regs_in.x.cx = pos_x;
regs_in.x.dx = pos_y;
int86(MOUSE_INT, ®s_in, ®s_out);
}
int main()
{
mouse_state_t mouse_state;
/* Show mouse and set initial position to middle of text screen */
clrscr();
mouse_show();
mouse_set_pos(80*8/2, 25*8/2);
/* Repeat until any key is pressed */
do
{
/* Get current mouse state and display it */
mouse_get_state(&mouse_state);
gotoxy(1, 1);
printf("Mouse X: %3d\n", mouse_state.cur_x);
printf("Mouse Y: %3d\n", mouse_state.cur_y);
printf("Buttons: %3d\n", mouse_state.buttons);
/* Display the button state as LEFT/RIGHT when pressed */
if (mouse_state.buttons & MOUSE_LEFT_BUTTON)
printf("LEFT ");
else
printf(" ");
if (mouse_state.buttons & MOUSE_RIGHT_BUTTON)
printf("RIGHT");
else
printf(" ");
} while (!kbhit());
mouse_hide();
return 0;
}
此代码提供了mouse_show、mouse_hide、mouse_get_state 和mouse_set_pos 函数,以使用Turbo-C 的int86 函数提供基本的鼠标轮询接口。测试代码反复更新屏幕左上角的当前鼠标状态,直到按下某个键。这是在 DOSBox 中出现的:
由于您正在做作业,我不会为您提供检查按钮状态和绘制图像的代码,但这足以让您确定当前的鼠标状态并自己触发动画。
鼠标界面应该可以在大多数标准图形模式下工作。
脚注
-
1一般 PC BIOS 不直接支持 `Int 0x33`。 DOS 鼠标驱动程序实现此接口以增强标准 BIOS 服务。