Use cases of mouse programming in C/C++ - GeeksforGeeks

28 Nov, 2021
In this article, we will discuss some use cases of Mouse Programming:
For displaying the Mouse Pointer, first enable the graphic mode using initgraph() function, if it is successfully initialized than it calls initMouse() function to check mouse is available or not, If initMouse() function returns not equal 0 means it will return ffffh then it calls the showMouse() function and display the cursor on the console window in graphic mode. Below are the functions used: 
initMouse(): use to initialized mouse.
showMouse(): shows the mouse pointer on the output screen.
Below is the program for the same:
C
C++
// C program to display Mouse// pointer in Graphics Mode#include <conio.h>#include <dos.h>#include <graphics.h>#include <stdio.h>union REGS in, out;  // Function to display the mouse// pointervoid showMouse(){    // Set service AX = 1 for    // displaying mouse    in.x.ax = 1;    int86(0X33, &in, &out);}  // Function to initialize the mouse// pointerint initMouse(){    // Set service AX = 0 for    // detecting mouse    in.x.ax = 0;    int86(0x33, &in, &out);    return out.x.ax;}  // Driver Codevoid main(){    int status, gd = DETECT, gm;    clrscr();      initgraph(&gd, &gm,              "C:\\TURBOC3\\BGI");      // Get the status of mouse pointer    status = initMouse();      // Check mouse is available or not    if (status == 0)        printf("Mouse support not"               " available.\n");    else {        printf("Display mouse");        showMouse();    }      getch();      // Close the graphics    closegraph();}
// C++ program to display Mouse// pointer in Graphics Mode#include <bits/stdc++.h>#include <conio.h>#include <dos.h>#include <graphics.h>#include <stdio.h>using namespace std;union REGS in, out;  // Function to display the mouse// pointervoid showMouse(){    // Set service AX = 1 for    // displaying mouse    in.x.ax = 1;    int86(0X33, &in, &out);}  // Function to initialize the mouse// pointerint initMouse(){    // Set service AX = 0 for    // detecting mouse    in.x.ax = 0;    int86(0x33, &in, &out);      return out.x.ax;}  // Driver Codevoid main(){    int status, gd = DETECT, gm;    clrscr();      initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");      // Get the status of mouse pointer    status = initMouse();      // Check if mouse is available or not    if (status == 0)        cout << "Mouse support not"             << " available.\n";    else {        cout << "Display mouse";        showMouse();    }      getch();      // Close the graphics    closegraph();}
 
 
Output:
 
 
 
For hiding the mouse pointer, first check the status of mouse if it is available than simply control will enter in for loop make delay of 500 millisecond  then the mouse will appear, then disappear after 500 milliseconds and will show again after 500 millisecond. This sequence will continue until the loop ends. The delay function has been used to show you the impact of  hidemouse() function . 
 
initmouse(): use to initialized mouse.
showmouse(): shows the mouse pointer on the output screen.
hidemouse(): hides the mouse pointer
 
Below is the program for the same:
 
C
C++
// C program for hiding Mouse Pointer#include <conio.h>#include <dos.h>#include <graphics.h>  union REGS in, out;  // Function to initialize the mouse// pointer using graphicsint initMouse(){    in.x.ax = 0;    int86(0X33, &in, &out);      return out.x.ax;}  // Function to display the mouse// pointer using graphicsvoid showMouse(){    in.x.ax = 1;    int86(0X33, &in, &out);}  // Function to hide the mouse// pointer using graphicsvoid hideMouse(){    // Set AX=2 to hide mouse    in.x.ax = 2;    int86(0X33, &in, &out);}  // Driver Codevoid 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 {        for (i = 0; i <= 10; i++) {              // Pause execution for .5 sec            delay(500);            showMouse();            delay(500);            hideMouse();        }    }      getch();      // Close the graphics    closegraph();}
// C++ program for hiding Mouse Pointer#include <bits/stdc++.h>#include <conio.h>#include <dos.h>#include <graphics.h>using namespace std;  union REGS in, out;  // Function to initialize the mouse// pointer using graphicsint initMouse(){    in.x.ax = 0;    int86(0X33, &in, &out);      return out.x.ax;}  // Function to display the mouse// pointer using graphicsvoid showMouse(){    in.x.ax = 1;    int86(0X33, &in, &out);}  // Function to hide the mouse// pointer using graphicsvoid hideMouse(){    // Set AX=2 to hide mouse    in.x.ax = 2;    int86(0X33, &in, &out);}  // Driver Codevoid 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 {        for (i = 0; i <= 10; i++) {              // Pause execution for .5 sec            delay(500);            showMouse();            delay(500);            hideMouse();        }    }      getch();      // Close the graphics    closegraph();}
 
 
Output:
 
In this program, the current location of the mouse is printed as well as which key of the mouse is being clicked at which location using the below functions:
 
initmouse(): use to initialized mouse.
showmouse(): shows the mouse cursor on the output screen.
getmouseposition(): fetches the mouse coordinate & which key is being pressed in BX, CX, DX register respectively.If BX = 1 left key clickedIf BX = 2 Right key clicked.If BX = 3 Center Button is PressedCX = Mouse X CoordinateDX = Mouse Y Coordinate
If BX = 1 left key clicked
If BX = 2 Right key clicked.
If BX = 3 Center Button is Pressed
CX = Mouse X Coordinate
DX = Mouse Y Coordinate
 
Below is the program for the same:
 
C
C++
// C program to determine the current// mouse position and button clicked#include <conio.h>#include <dos.h>#include <graphics.h>#include <stdio.h>union REGS in, out;  // Function to initialize the mouse// pointer using graphicsint initMouse(){    in.x.ax = 0;    int86(0X33, &in, &out);      return out.x.ax;}  // Function to display the mouse// pointer using graphicsvoid showMouse(){    in.x.ax = 1;    int86(0X33, &in, &out);}  // Function to get the current clicked// mouse position on the screenvoid getMousePosition(int* click,                      int* x, int* y){    in.x.ax = 3;    int86(0X33, &in, &out);      // Get the coordinates    *click = out.x.bx;      // Update the x and y coordinates    *x = out.x.cx;    *y = out.x.dx;}  // Driver Codevoid 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();          // Get the current mouse position        getMousePosition(&click, &x, &y);          while (!kbhit()) {              getMousePosition(&click, &x, &y);            gotoxy(1, 1);            printf("X = %d, Y = %d", x, y);              if (click == 1)                printf("\nLeft click at "                       "position = %d, %d\t",                       x, y);            if (click == 2)                printf("\nRight click at "                       "position = %d, %d\t",                       x, y);        }    }      getch();      // Close the graphics    closegraph();}
// C program to determine the current// mouse position and button clicked#include <bits/stdc++.h>#include <conio.h>#include <dos.h>#include <graphics.h>#include <stdio.h>using namespace std;union REGS in, out;  // Function to initialize the mouse// pointer using graphicsint initMouse(){    in.x.ax = 0;    int86(0X33, &in, &out);      return out.x.ax;}  // Function to display the mouse// pointer using graphicsvoid showMouse(){    in.x.ax = 1;    int86(0X33, &in, &out);}  // Function to get the current clicked// mouse position on the screenvoid getMousePosition(int* click,                      int* x, int* y){    in.x.ax = 3;    int86(0X33, &in, &out);      // Get the coordinates    *click = out.x.bx;      // Update the x and y coordinates    *x = out.x.cx;    *y = out.x.dx;}  // Driver Codevoid 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();          // Get the current mouse position        getMousePosition(&click, &x, &y);          while (!kbhit()) {              getMousePosition(&click, &x, &y);            gotoxy(1, 1);            printf("X = %d, Y = %d", x, y);              if (click == 1)                cout << "\nLeft click at "                     << "position = "                     << x << ", " << y;              if (click == 2)                cout << "\nRight click at "                     << "position = "                     << x << ", " << y;        }    }      getch();      // Close the graphics    closegraph();}
 
 
Output:
 
 
surindertarika1234
C-programming
Articles
C Language
C Programs
C++
C++ Programs
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Time Complexity and Space Complexity
Docker - COPY Instruction
Time complexities of different data structures
Difference between Min Heap and Max Heap
SQL | Date functions
Dynamic Memory Allocation in C using malloc(), calloc(), free() and realloc()
Arrays in C/C++
std::sort() in C++ STL
Bitwise Operators in C/C++
Multidimensional Arrays in C / C++