Use cases of mouse programming in C/C++ | Set 2
Last Updated :
02 Nov, 2023
In this article, we will discuss some use cases of Mouse Programming:
Restrict mouse pointer:
The mouse pointer can be restricted in particular rectangle. The idea is to create a function called restrictmouse() which takes four parameters which containing X coordinate and Y coordinate. First point mention the top of the rectangle and the second point mention the bottom of the rectangle. Below are the functions used for the same:
- initmouse(): use to initialize mouse.
- showmouse(): shows the mouse pointer on the output screen.
- restrictmouse(): used to set Horizontal and vertical limit of the mouse pointer by setting the following parameters. AX = 7 for horizontal and AX = 8 for vertical.
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 restrictMouse( int x1, int y1,
int x2, int y2)
{
in.x.ax = 7;
in.x.cx = x1;
in.x.dx = x2;
int86(0X33, &in, &out);
in.x.ax = 8;
in.x.cx = y1;
in.x.dx = y2;
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 {
showMouse();
rectangle(100, 70, 400, 200);
restrictMouse(100, 70, 400, 200);
}
getch();
closegraph();
return 0;
}
|
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 restrictMouse( int x1, int y1,
int x2, int y2)
{
in.x.ax = 7;
in.x.cx = x1;
in.x.dx = x2;
int86(0X33, &in, &out);
in.x.ax = 8;
in.x.cx = y1;
in.x.dx = y2;
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 {
showMouse();
rectangle(100, 70, 400, 200);
restrictMouse(100, 70, 400, 200);
}
getch();
closegraph();
return 0;
}
|
Output:
Free hand drawing:
This following program makes use of some sub function, which were already discussed previously, and shows how they can be used to write useful programs like free-hand drawing. Below are the functions used:
- initmouse(): use to initialize mouse.
- showmouse(): shows mouse pointer on the output screen.
- hidemouse(): used to hide mouse while drawing.
- getmouseposition(): Fetches current location of the pointer and draw line accordingly.
Below is the program for the same:
C++
#include <bits/stdc++.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 hideMouse()
{
in.x.ax = 2;
int86(0X33, &in, &out);
}
getMousePosition( int * x, int * y,
int * click)
{
in.x.ax = 3;
int86(0x33, &in, &out);
*x = out.x.cx;
*y = out.x.dx;
*click = out.x.bx & 1;
}
void main()
{
int status, i, gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\TURBOC3\\BGI" );
initMouse();
while (!kbhit()) {
showMouse();
getMousePosition(&x1, &y1, &click);
x2 = x1;
y2 = y1;
while (click == 1) {
hideMouse();
line(x1, y1, x2, y2);
x1 = x2;
y1 = y2;
getMousePosition(&x2, &y2, &click);
}
}
getch();
closegraph();
return 0;
}
|
C
#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 hideMouse()
{
in.x.ax = 2;
int86(0X33, &in, &out);
}
getMousePosition( int * x, int * y,
int * click)
{
in.x.ax = 3;
int86(0x33, &in, &out);
*x = out.x.cx;
*y = out.x.dx;
*click = out.x.bx & 1;
}
void main()
{
int status, i, gd = DETECT, gm,x1,y1,x2,y2;
initgraph(&gd, &gm, "C:\\TURBOC3\\BGI" );
initMouse();
while (!kbhit()) {
showMouse();
getMousePosition(&x1, &y1, &click);
x2 = x1;
y2 = y1;
while (click == 1) {
hideMouse();
line(x1, y1, x2, y2);
x1 = x2;
y1 = y2;
getMousePosition(&x2, &y2, &click);
}
}
getch();
closegraph();
return 0;
}
|
Output:
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...