📜  C / C++中的鼠标编程

📅  最后修改于: 2021-05-25 23:46:33             🧑  作者: Mango

到目前为止,在C / C++中,我们只看到黑色窗口上的静态输出,而没有任何外围设备交互(如鼠标)。此处的静态表示通过鼠标与输出屏幕进行交互以运行动态事件或任务。目的是使鼠标指针在我们的输出屏幕上可见,当在同一输出窗口上单击鼠标时,鼠标可以通过它看到任何新事件。

基本知识:
这个想法是告诉鼠标在输出屏幕上执行任何操作。实际上,无法直接通过鼠标进行通信,而只能通过提供的驱动程序进行通信。这个想法是使用中断来访问该驱动程序。计算机提供的每个设备都有一个唯一的端口,该端口是一个十六进制值,被设计为与机器无关,从而增强了程序的可移植性。鼠标已连接端口0X33 。访问这些端口也需要使用地址寄存器。这些基本上是在“DOS.H”定义类型REGSUNION。使用两个寄存器与设备驱动程序进行通信,一个用于输入,一个用于输出,并通过输入寄存器将值发送到设备驱动程序,并接收嵌入在输出寄存器中的信息。
现在,有两种方法可以在C / C++屏幕上显示鼠标指针。第一个是非图形模式,第二个是图形模式,这里我们使用图形模式。在图形模式下切换输出窗口的步骤如下:

启用图形模式:要启用图形模式,请使用用于初始化图形模式的initgraph()函数。该函数位于“ graphics.h”头文件中。

initgraph()的语法:

  • gdriver:它是一个整数,指定要使用的图形驱动程序。使用DETECT意味着编译器会根据需要自动选择合适的驱动程序。
  • gmode:也是指定初始图形模式的整数。当gdriver = DETECT时在这种情况下,initgraph()将gmode设置为可用于检测到的驱动程序的最高分辨率。
  • pathtodriver:它表示initgraph必须在其中查找图形驱动程序的目录路径。

程序1:

C
// C program to show how to
// enable Graphics mode
#include 
#include 
  
// Driver Code
int main()
{
    int gdriver = DETECT, gmode, errorcode;
    initgraph(&gdriver, &gmode, "C:\\TC\\BGI");
  
    errorcode = graphresult();
  
    // If error occurs
    if (errorcode == grOk)
        printf("Graphics enabled: %s\n",
               grapherrormsg(errorcode));
    else
        printf("Graphics error: %s\n",
               grapherrormsg(errorcode));
  
    getch();
  
    // Close the graph init()
    closegraph();
    return 0;
}


C++
// C++ program to show how to
// enable Graphics mode
#include 
#include 
#include 
  
// Driver Code
int main()
{
    int gdriver = DETECT, gmode, errorcode;
    initgraph(&gdriver, &gmode, "C:\\TC\\BGI");
  
    errorcode = graphresult();
  
    // If error occurs
    if (errorcode == grOk)
        cout << "Graphics enabled: \n"
             << grapherrormsg(errorcode);
    else
        cout << "Graphics error: \n"
             << grapherrormsg(errorcode);
  
    getch();
  
    // Close the graph init()
    closegraph();
    return 0;
}


C
// C program to check mouse status
#include 
#include 
#include 
#include 
union REGS in, out;
  
// Function to implement the functionality
// of detecting Mouse
void detectMouse()
{
    in.x.ax = 0;
  
    // Invoke interrupt (in86 method
    // description mentionde above)
    int86(0X33, &in, &out);
  
    if (out.x.ax == 0)
        printf("\nMouse Failed To Initialize");
    else
        printf("\nMouse was Succesfully Initialized");
}
  
// Driver Code
int main()
{
    clrscr();
  
    int gdriver = DETECT, gmode;
  
    // Method to enable graphics
    initgraph(&gdriver, &gmode, "c:\tc\bgi");
  
    // Function Call
    detectMouse();
    getch();
  
    // Close graphics mode
    closegraph();
  
    return 0;
}


C++
// C++ program to check mouse status
#include 
#include 
#include 
#include 
#include 
union REGS in, out;
  
// Function to implement the functionality
// of detecting Mouse
void detectMouse()
{
    in.x.ax = 0;
  
    // Invoke interrupt (in86 method
    // description mentionde above)
    int86(0X33, &in, &out);
  
    if (out.x.ax == 0)
        cout << "\nMouse Failed To"
             << " Initialize";
    else
        cout << "\nMouse was Succesfully"
             << " Initialized";
}
  
// Driver Code
int main()
{
    clrscr();
  
    int gdriver = DETECT, gmode;
  
    // Method to enable graphics
    initgraph(&gdriver, &gmode, "c:\tc\bgi");
  
    // Function Call
    detectMouse();
  
    getch();
  
    // Close graphics mode
    closegraph();
  
    return 0;
}


输出:

鼠标编程的先决条件:

AX寄存器可以使用不同的AX输入寄存器值访问各种鼠标功能,并使用中断将这些值传递到鼠标端口。下表列出了功能:
AXBXCXDXUNION REGS的成员。

Interrupt Service Discription

0X33

OR

51

0

  • Reset mouse and get status.
  • Call with AX = 0

    .

  • Returns AX = FFFFh , if mouse support is available, otherwise, returns Ax = 0.

1

  • Show mouse pointer.
  • Call with AX = 1.
  • Returns Nothing

2

  • Hide mouse pointer.
  • Call with AX = 2.
  • Returns Nothing.

3

  • Get mouse position and button status.
  • Call with AX = 3.
  • Returns BX = mouse button status.
  • BX Bit Significance:
    • 0 button not pressed
    • 1 left button is pressed
    • right button is pressed
    • 3 center button is pressed
    • CX = x coordinate
    • DX = y coordinate

4

  • Set mouse pointer position.
  • Call with AX = 4.
  • CX = x coordinate
  • DX = y coordinate
  • Returns Nothing.

7

  • Set horizontal limits for pointer.
  • Call with AX = 7.
  • CX = minimum x coordinate.
  • DX = maximum x coordinate.
  • Returns Nothing.

8

  • Set vertical limits for pointer.
  • Call with AX = 8.
  • CX = minimum y coordinate.
  • DX = maximum y coordinate.
  • Returns Nothing.

int86()函数 int86()是C库函数帮助访问裸骨DOS和BIOS服务中断。它是内联汇编中断调用的包装器。它以对象为对象将CPU寄存器值带到一个结构,其中成员变量等效于CPU寄存器。它需要三个参数。

鼠标编程的用例

下面列出了Mouse Programming的各种用例:

  1. 检测鼠标
  2. 在图形模式下显示鼠标指针
  3. 隐藏指针
  4. 确定当前位置
  5. 标识单击了哪个鼠标按钮
  6. 限制鼠标指针
  7. 自由手绘(使用所有函数)

程式2:
下面是检查是否加载了鼠标驱动程序的程序:

C

// C program to check mouse status
#include 
#include 
#include 
#include 
union REGS in, out;
  
// Function to implement the functionality
// of detecting Mouse
void detectMouse()
{
    in.x.ax = 0;
  
    // Invoke interrupt (in86 method
    // description mentionde above)
    int86(0X33, &in, &out);
  
    if (out.x.ax == 0)
        printf("\nMouse Failed To Initialize");
    else
        printf("\nMouse was Succesfully Initialized");
}
  
// Driver Code
int main()
{
    clrscr();
  
    int gdriver = DETECT, gmode;
  
    // Method to enable graphics
    initgraph(&gdriver, &gmode, "c:\tc\bgi");
  
    // Function Call
    detectMouse();
    getch();
  
    // Close graphics mode
    closegraph();
  
    return 0;
}

C++

// C++ program to check mouse status
#include 
#include 
#include 
#include 
#include 
union REGS in, out;
  
// Function to implement the functionality
// of detecting Mouse
void detectMouse()
{
    in.x.ax = 0;
  
    // Invoke interrupt (in86 method
    // description mentionde above)
    int86(0X33, &in, &out);
  
    if (out.x.ax == 0)
        cout << "\nMouse Failed To"
             << " Initialize";
    else
        cout << "\nMouse was Succesfully"
             << " Initialized";
}
  
// Driver Code
int main()
{
    clrscr();
  
    int gdriver = DETECT, gmode;
  
    // Method to enable graphics
    initgraph(&gdriver, &gmode, "c:\tc\bgi");
  
    // Function Call
    detectMouse();
  
    getch();
  
    // Close graphics mode
    closegraph();
  
    return 0;
}

输出:

想要从精选的最佳视频中学习和练习问题,请查看《基础知识到高级C的C基础课程》。