Till now we have seen only static output on the black window in C/C++ without any peripheral device interaction(like mouse). Here static means that interaction with the output screen through the mouse to run a dynamic event or task. The goal will be to make the pointer of the mouse visible on our output screen, through which it can see any new event when the mouse is clicked on the same output window.
Requirement: Turbo C++ IDE or DOS BOX
Fundamental Knowledge:
The idea is to tell a mouse to do anything on the output screen. In actual the communication with the mouse directly is not possible but through the driver provided. The idea is to use interrupts to get access to this driver. Each device provided by a computer has a unique port which is a hexadecimal value that is designed to be machine-independent enhancing the portability of the program. Mouse has port 0X33 attached to it. Use of address registers is also required to access these port. These are basically UNION of type REGS defined in “dos.h“. Use two registers to communicate to a device driver one for input and one for output and send value to device driver through the input register and receive information in it embedded in the output register.
Now there are two ways to display mouse pointer on C/C++ screen. First is the non-graphic mode and the second is Graphic mode, Here we use graphic mode. To switch our output window in Graphic mode steps are listed below:
Enable Graphic mode: For enabling the graphics mode use initgraph() function which is used to initialize the graphics mode. This function is present in “graphics.h“ header file.
Syntax of initgraph():
void initgraph(int *gdriver, int *gmode, char *pathtodriver);
- gdriver: It is an integer that specifies which graphics driver is to be used. Use DETECT it means compiler auto select the suitable driver according to requirement.
- gmode: It is also an integer that specifies the initial graphics mode. when gdriver = DETECT In this case, initgraph() sets gmode to the highest resolution available for the detected driver.
- pathtodriver: It denotes the directory path where initgraph must look for graphic drivers.
Program 1:
C
#include <conio.h>
#include <graphics.h>
int main()
{
int gdriver = DETECT, gmode, errorcode;
initgraph(&gdriver, &gmode, "C:\\TC\\BGI" );
errorcode = graphresult();
if (errorcode == grOk)
printf ( "Graphics enabled: %s\n" ,
grapherrormsg(errorcode));
else
printf ( "Graphics error: %s\n" ,
grapherrormsg(errorcode));
getch();
closegraph();
return 0;
}
|
C++
#include <bits/stdc++.h>
#include <conio.h>
#include <graphics.h>
int main()
{
int gdriver = DETECT, gmode, errorcode;
initgraph(&gdriver, &gmode, "C:\\TC\\BGI" );
errorcode = graphresult();
if (errorcode == grOk)
cout << "Graphics enabled: \n"
<< grapherrormsg(errorcode);
else
cout << "Graphics error: \n"
<< grapherrormsg(errorcode);
getch();
closegraph();
return 0;
}
|
Output:

Prerequisite for Mouse Programming:
AX Register: The various mouse functions can be access using different values of AX input Register and passing those values to mouse port using an interrupt. Functions are listed below in the table:
Here AX, BX, CX, and DX are members of UNION REGS.
Interrupt | Service | Description |
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
- 2 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() function: The int86() is a C library function that facilitates access to bare bone DOS and BIOS service interrupts. It is a wrapper over inline assembly interrupt call. It takes CPU register values with object to a structure where member variables are equivalent to CPU registers. It takes three arguments.
// Declaration syntax
int int86(int intno, union REGS* inregs, union REGS* outregs);
// intno – Interrupt vector /service number
// inregs – Input argument registers as REGS
// outregs – Output argument registers as REGS
Use Cases of Mouse Programming:
There are various use cases of the Mouse Programming that are listed below:
- Detecting Mouse
- Display mouse pointer in graphics mode
- Hide pointer
- Determine Current position
- Identified which mouse button is clicked
- Restrict mouse pointer
- Free hand drawing (Use of all function)
Program 2:
Below is the program to check if a mouse driver is loaded or not:
C
#include <conio.h>
#include <dos.h>
#include <graphics.h>
#include <stdio.h>
union REGS in, out;
void detectMouse()
{
in.x.ax = 0;
int86(0X33, &in, &out);
if (out.x.ax == 0)
printf ( "\nMouse Failed To Initialize" );
else
printf ( "\nMouse was Successfully Initialized" );
}
int main()
{
clrscr();
int gdriver = DETECT, gmode;
initgraph(&gdriver, &gmode, "c:\tc\bgi" );
detectMouse();
getch();
closegraph();
return 0;
}
|
C++
#include <bits/stdc++.h>
#include <conio.h>
#include <dos.h>
#include <graphics.h>
#include <stdio.h>
union REGS in, out;
void detectMouse()
{
in.x.ax = 0;
int86(0X33, &in, &out);
if (out.x.ax == 0)
cout << "\nMouse Failed To"
<< " Initialize" ;
else
cout << "\nMouse was Successfully"
<< " Initialized" ;
}
int main()
{
clrscr();
int gdriver = DETECT, gmode;
initgraph(&gdriver, &gmode, "c:\tc\bgi" );
detectMouse();
getch();
closegraph();
return 0;
}
|
Output:
