Use cases of mouse programming in C/C++
Last Updated :
02 Nov, 2023
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++
#include <bits/stdc++.h>
#include <conio.h>
#include <dos.h>
#include <graphics.h>
#include <stdio.h>
using namespace std;
union REGS in, out;
void showMouse()
{
in.x.ax = 1;
int86(0X33, &in, &out);
}
int initMouse()
{
in.x.ax = 0;
int86(0x33, &in, &out);
return out.x.ax;
}
void main()
{
int status, gd = DETECT, gm;
clrscr();
initgraph(&gd, &gm, "C:\\TURBOC3\\BGI" );
status = initMouse();
if (status == 0)
cout << "Mouse support not"
<< " available.\n" ;
else {
cout << "Display mouse" ;
showMouse();
}
getch();
closegraph();
}
|
C
#include <conio.h>
#include <dos.h>
#include <graphics.h>
#include <stdio.h>
union REGS in, out;
void showMouse()
{
in.x.ax = 1;
int86(0X33, &in, &out);
}
int initMouse()
{
in.x.ax = 0;
int86(0x33, &in, &out);
return out.x.ax;
}
void main()
{
int status, gd = DETECT, gm;
clrscr();
initgraph(&gd, &gm,
"C:\\TURBOC3\\BGI" );
status = initMouse();
if (status == 0)
printf ( "Mouse support not"
" available.\n" );
else {
printf ( "Display mouse" );
showMouse();
}
getch();
closegraph();
}
|
Output:
Hide Mouse Pointer:
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++
#include <bits/stdc++.h>
#include <conio.h>
#include <dos.h>
#include <graphics.h>
using namespace std;
union REGS in, out;
int initMouse()
{
in.x.ax = 0;
int86(0X33, &in, &out);
return out.x.ax;
}
void showMouse()
{
in.x.ax = 1;
int86(0X33, &in, &out);
}
void hideMouse()
{
in.x.ax = 2;
int86(0X33, &in, &out);
}
void main()
{
int status, i, gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\TURBOC3\\BGI" );
status = initMouse();
if (status == 0)
cout << "Mouse support "
<< "not available.\n" ;
else {
for (i = 0; i <= 10; i++) {
delay(500);
showMouse();
delay(500);
hideMouse();
}
}
getch();
closegraph();
}
|
C
#include <conio.h>
#include <dos.h>
#include <graphics.h>
union REGS in, out;
int initMouse()
{
in.x.ax = 0;
int86(0X33, &in, &out);
return out.x.ax;
}
void showMouse()
{
in.x.ax = 1;
int86(0X33, &in, &out);
}
void hideMouse()
{
in.x.ax = 2;
int86(0X33, &in, &out);
}
void main()
{
int status, i, gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\TURBOC3\\BGI" );
status = initMouse();
if (status == 0)
printf ( "Mouse support "
"not available.\n" );
else {
for (i = 0; i <= 10; i++) {
delay(500);
showMouse();
delay(500);
hideMouse();
}
}
getch();
closegraph();
}
|
Output:
Determine the current mouse position and button clicked:
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 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++
#include <bits/stdc++.h>
#include <conio.h>
#include <dos.h>
#include <graphics.h>
#include <stdio.h>
using namespace std;
union REGS in, out;
int initMouse()
{
in.x.ax = 0;
int86(0X33, &in, &out);
return out.x.ax;
}
void showMouse()
{
in.x.ax = 1;
int86(0X33, &in, &out);
}
void getMousePosition( int * click,
int * x, int * y)
{
in.x.ax = 3;
int86(0X33, &in, &out);
*click = out.x.bx;
*x = out.x.cx;
*y = out.x.dx;
}
void main()
{
int status, i, gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\TURBOC3\\BGI" );
status = initMouse();
if (status == 0)
printf ( "Mouse support "
"not available.\n" );
else {
showMouse();
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();
closegraph();
}
|
C
#include <conio.h>
#include <dos.h>
#include <graphics.h>
#include <stdio.h>
union REGS in, out;
int initMouse()
{
in.x.ax = 0;
int86(0X33, &in, &out);
return out.x.ax;
}
void showMouse()
{
in.x.ax = 1;
int86(0X33, &in, &out);
}
void getMousePosition( int * click,
int * x, int * y)
{
in.x.ax = 3;
int86(0X33, &in, &out);
*click = out.x.bx;
*x = out.x.cx;
*y = out.x.dx;
}
void main()
{
int status, i, gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\TURBOC3\\BGI" );
status = initMouse();
if (status == 0)
printf ( "Mouse support "
"not available.\n" );
else {
showMouse();
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();
closegraph();
}
|
Output:
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...