📜  用C / C++进行鼠标编程的用例套装2

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

在本文中,我们将讨论鼠标编程的一些用例:

限制鼠标指针

可以将鼠标指针限制在特定的矩形中。这个想法是创建一个名为strictmouse()的函数,该函数接受包含X坐标和Y坐标的四个参数。第一点提到矩形的顶部,第二点提到矩形的底部。以下是用于相同功能的功能:

  • initmouse():用于初始化鼠标。
  • showmouse():在输出屏幕上显示鼠标指针。
  • limitmouse():用于通过设置以下参数来设置鼠标指针的水平和垂直限制。 AX = 7 (水平)和AX = 8 (垂直)。

以下是相同的程序:

C
// C program to restrict the mouse
// pointer
#include 
#include 
#include 
#include 
union REGS in, out;
 
// Function to initialize the mouse
// pointer using graphics
int initMouse()
{
    in.x.ax = 0;
    int86(0X33, &in, &out);
 
    return out.x.ax;
}
 
// Function to display the mouse
// pointer using graphics
void showMouse()
{
    in.x.ax = 1;
    int86(0X33, &in, &out);
}
 
// Function to restrict the mouse
// pointers
void restrictMouse(int x1, int y1,
                   int x2, int y2)
{
    // Set Horizontal limit
    in.x.ax = 7;
    in.x.cx = x1;
    in.x.dx = x2;
    int86(0X33, &in, &out);
 
    // Set Vertical limit
    in.x.ax = 8;
    in.x.cx = y1;
    in.x.dx = y2;
    int86(0X33, &in, &out);
}
 
// Driver Code
void main()
{
    int status, i, gd = DETECT, gm;
 
    // Initialize graphics
    initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");
 
    // Get the status of the mouse
    status = initMouse();
 
    // Check if mouse is available or not
    if (status == 0)
        printf("Mouse support "
               "not available.\n");
    else {
        showMouse();
 
        // Draw rectangle for displaying
        // the boundary
        rectangle(100, 70, 400, 200);
        restrictMouse(100, 70, 400, 200);
    }
    getch();
 
    // Close the graphics
    closegraph();
 
    return 0;
}


C++
// C++ program to restrict the mouse
// pointer
#include 
#include 
#include 
#include 
#include 
using namespace std;
 
union REGS in, out;
 
// Function to initialize the mouse
// pointer using graphics
int initMouse()
{
    in.x.ax = 0;
    int86(0X33, &in, &out);
 
    return out.x.ax;
}
 
// Function to display the mouse
// pointer using graphics
void showMouse()
{
    in.x.ax = 1;
    int86(0X33, &in, &out);
}
 
// Function to restrict the mouse
// pointers
void restrictMouse(int x1, int y1,
                   int x2, int y2)
{
    // Set Horizontal limit
    in.x.ax = 7;
    in.x.cx = x1;
    in.x.dx = x2;
    int86(0X33, &in, &out);
 
    // Set Vertical limit
    in.x.ax = 8;
    in.x.cx = y1;
    in.x.dx = y2;
    int86(0X33, &in, &out);
}
 
// Driver Code
void main()
{
    int status, i, gd = DETECT, gm;
 
    // Initialize graphics
    initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");
 
    // Get the status of the mouse
    status = initMouse();
 
    // Check if mouse is available or not
    if (status == 0)
        cout << "Mouse support "
             << "not available.\n";
    else {
        showMouse();
 
        // Draw rectangle for displaying
        // the boundary
        rectangle(100, 70, 400, 200);
        restrictMouse(100, 70, 400, 200);
    }
    getch();
 
    // Close the graphics
    closegraph();
 
    return 0;
}


C
// C program to perform Free Hand Drawing
// using mouse programming
#include 
#include 
#include 
union REGS in, out;
 
// Function to initialize the mouse
// pointer using graphics
int initMouse()
{
    in.x.ax = 0;
    int86(0X33, &in, &out);
 
    return out.x.ax;
}
 
// Function to display the mouse
// pointer using graphics
void showMouse()
{
    in.x.ax = 1;
    int86(0X33, &in, &out);
}
 
// Function to hide the mouse
// pointer using graphics
void hideMouse()
{
    // Set AX=2 to hide mouse
    in.x.ax = 2;
    int86(0X33, &in, &out);
}
 
// Function to get the exact position
// of the mouse
getMousePosition(int* x, int* y,
                 int* click)
{
    in.x.ax = 3;
 
    // Get the coordinates
    int86(0x33, &in, &out);
 
    // Update the coordinates
    *x = out.x.cx;
    *y = out.x.dx;
    *click = out.x.bx & 1;
}
 
// Driver Code
void main()
{
    int status, i, gd = DETECT, gm,x1,y1,x2,y2;
 
    // Initialize graphics
    initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");
 
    initMouse();
 
    // kbhit If a key has been pressed
    // then it returns a non zero value
    // otherwise returns zero(false)
    while (!kbhit()) {
 
        // Show the mouse pointer
        showMouse();
 
        // Get the mouse position
        getMousePosition(&x1, &y1, &click);
        x2 = x1;
        y2 = y1;
 
        // When mouse is clicked
        while (click == 1) {
            hideMouse();
 
            // Draw line
            line(x1, y1, x2, y2);
            x1 = x2;
            y1 = y2;
 
            // Get the mouse position
            getMousePosition(&x2, &y2, &click);
        }
    }
 
    getch();
 
    // Close the graphics
    closegraph();
 
    return 0;
}


C++
// C++ program to perform Free Hand
// Drawing using mouse programming
#include 
#include 
#include 
#include 
using namespace std;
 
union REGS in, out;
 
// Function to initialize the mouse
// pointer using graphics
int initMouse()
{
    in.x.ax = 0;
    int86(0X33, &in, &out);
 
    return out.x.ax;
}
 
// Function to display the mouse
// pointer using graphics
void showMouse()
{
    in.x.ax = 1;
    int86(0X33, &in, &out);
}
 
// Function to hide the mouse
// pointer using graphics
void hideMouse()
{
    // Set AX=2 to hide mouse
    in.x.ax = 2;
    int86(0X33, &in, &out);
}
 
// Function to get the exact position
// of the mouse
getMousePosition(int* x, int* y,
                 int* click)
{
    in.x.ax = 3;
 
    // Get the coordinates
    int86(0x33, &in, &out);
 
    // Update the coordinates
    *x = out.x.cx;
    *y = out.x.dx;
    *click = out.x.bx & 1;
}
 
// Driver Code
void main()
{
    int status, i, gd = DETECT, gm;
 
    // Initialize graphics
    initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");
 
    initMouse();
 
    // kbhit If a key has been pressed
    // then it returns a non zero value
    // otherwise returns zero(false)
    while (!kbhit()) {
 
        // Show the mouse pointer
        showMouse();
 
        // Get the mouse position
        getMousePosition(&x1, &y1, &click);
        x2 = x1;
        y2 = y1;
 
        // When mouse is clicked
        while (click == 1) {
            hideMouse();
 
            // Draw line
            line(x1, y1, x2, y2);
            x1 = x2;
            y1 = y2;
 
            // Get the mouse position
            getMousePosition(&x2, &y2, &click);
        }
    }
 
    getch();
 
    // Close the graphics
    closegraph();
 
    return 0;
}


输出:

自由手绘图

下面的程序利用了前面已经讨论过的一些子函数,并展示了如何使用它们来编写有用的程序,例如手绘图。以下是使用的功能:

  • initmouse():用于初始化鼠标。
  • showmouse():在输出屏幕上显示鼠标指针。
  • hidemouse():用于在绘制时隐藏鼠标。
  • getmouseposition():获取指针的当前位置并相应绘制线。

以下是相同的程序:

C

// C program to perform Free Hand Drawing
// using mouse programming
#include 
#include 
#include 
union REGS in, out;
 
// Function to initialize the mouse
// pointer using graphics
int initMouse()
{
    in.x.ax = 0;
    int86(0X33, &in, &out);
 
    return out.x.ax;
}
 
// Function to display the mouse
// pointer using graphics
void showMouse()
{
    in.x.ax = 1;
    int86(0X33, &in, &out);
}
 
// Function to hide the mouse
// pointer using graphics
void hideMouse()
{
    // Set AX=2 to hide mouse
    in.x.ax = 2;
    int86(0X33, &in, &out);
}
 
// Function to get the exact position
// of the mouse
getMousePosition(int* x, int* y,
                 int* click)
{
    in.x.ax = 3;
 
    // Get the coordinates
    int86(0x33, &in, &out);
 
    // Update the coordinates
    *x = out.x.cx;
    *y = out.x.dx;
    *click = out.x.bx & 1;
}
 
// Driver Code
void main()
{
    int status, i, gd = DETECT, gm,x1,y1,x2,y2;
 
    // Initialize graphics
    initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");
 
    initMouse();
 
    // kbhit If a key has been pressed
    // then it returns a non zero value
    // otherwise returns zero(false)
    while (!kbhit()) {
 
        // Show the mouse pointer
        showMouse();
 
        // Get the mouse position
        getMousePosition(&x1, &y1, &click);
        x2 = x1;
        y2 = y1;
 
        // When mouse is clicked
        while (click == 1) {
            hideMouse();
 
            // Draw line
            line(x1, y1, x2, y2);
            x1 = x2;
            y1 = y2;
 
            // Get the mouse position
            getMousePosition(&x2, &y2, &click);
        }
    }
 
    getch();
 
    // Close the graphics
    closegraph();
 
    return 0;
}

C++

// C++ program to perform Free Hand
// Drawing using mouse programming
#include 
#include 
#include 
#include 
using namespace std;
 
union REGS in, out;
 
// Function to initialize the mouse
// pointer using graphics
int initMouse()
{
    in.x.ax = 0;
    int86(0X33, &in, &out);
 
    return out.x.ax;
}
 
// Function to display the mouse
// pointer using graphics
void showMouse()
{
    in.x.ax = 1;
    int86(0X33, &in, &out);
}
 
// Function to hide the mouse
// pointer using graphics
void hideMouse()
{
    // Set AX=2 to hide mouse
    in.x.ax = 2;
    int86(0X33, &in, &out);
}
 
// Function to get the exact position
// of the mouse
getMousePosition(int* x, int* y,
                 int* click)
{
    in.x.ax = 3;
 
    // Get the coordinates
    int86(0x33, &in, &out);
 
    // Update the coordinates
    *x = out.x.cx;
    *y = out.x.dx;
    *click = out.x.bx & 1;
}
 
// Driver Code
void main()
{
    int status, i, gd = DETECT, gm;
 
    // Initialize graphics
    initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");
 
    initMouse();
 
    // kbhit If a key has been pressed
    // then it returns a non zero value
    // otherwise returns zero(false)
    while (!kbhit()) {
 
        // Show the mouse pointer
        showMouse();
 
        // Get the mouse position
        getMousePosition(&x1, &y1, &click);
        x2 = x1;
        y2 = y1;
 
        // When mouse is clicked
        while (click == 1) {
            hideMouse();
 
            // Draw line
            line(x1, y1, x2, y2);
            x1 = x2;
            y1 = y2;
 
            // Get the mouse position
            getMousePosition(&x2, &y2, &click);
        }
    }
 
    getch();
 
    // Close the graphics
    closegraph();
 
    return 0;
}

输出:

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