How to Control Mouse Pointer by C Programming Language

mouse pointer or cursor program using C or C++ language

Introduction :

The mouse pointer or cursor has become an integral part of modern computer systems. It allows users to interact with graphical user interfaces in an intuitive and efficient manner. After you learn mouse programming using graphics.h library, you can create graphical applications that can respond to mouse events. In this article, I shall explore the fundamentals (essential concepts and techniques) of handling mouse pointer or cursor using C or C++ programming language.

Here, I show you how to develop a basic mouse pointer or cursor programs by C or C++ graphics programming language. This article is about describes interfacing of the mouse pointer or cursor that can detect and respond to various mouse events using C or C++ graphics programming language. The mouse pointer or cursor also allows the user to interact with graphical elements on the screen. You can see the following programs which show you how to handle the mouse by C or C++ graphics programming language.

What is mouse pointer or cursor :

There are different type of input devices used in computer. The mouse pointer or cursor is an input device that allows user to give input or instructions to the computer system. The mouse enables users to interact with and control the computer and its software. The mouse is a pointing device which allows user to interact with graphical user interfaces (GUI) by moving the mouse pointer or cursor on the screen. The mouse is very essential for user interaction in graphical user interfaces (GUI) program. It is very faster input device than other input devices like keyboard. The mouse is used in both text mode and graphics mode. Generally, it is mostly used in graphics mode.

Use of mouse pointer or cursor :

A mouse program can be used for various purposes depending on the requirements and goals of your application or program. The mouse program is used in different application such as graphical user interface (GUI) applications, gaming applications, drawing applications, accessibility applications for physical disabilities, software testing and automating applications.

How implement mouse pointer or cursor using C or C++ language :

At the beginning, you have to know how to give commands to a mouse for does anything on the output screen. You cannot communicate with the mouse directly but through the driver provided. Using interrupts, you can access the driver. There is a unique port (0X33) which is responsible for mouse program. Address registers are also required to access this port. Mouse interrupt is handled by int86() function of dos.h library. After include the dos.h library, you can use UNION type of REGS variable.

There are two address registers to communicate with a device driver one for input and one for output. Using the input register, you can send a value to device driver and the input value embedded in the output register. There are four members such as AX, BX, CX and DX in UNION REGS. You can access various mouse events by setting up with different values of AX (input register) which pass to mouse port using an interrupt. The following values are used in the AX input register for different event of the mouse pointer.

To initialize or reset the mouse pointer or cursor
AX = 0
If mouse pointer or cursor initialize or supported return FFFFh, otherwise returns 0.

For display the mouse pointer or cursor
AX = 1
Here, the return value is nothing

To hide the mouse pointer or cursor
AX = 2
Here, the return value is nothing

Know the current position of the mouse pointer or cursor and which mouse button is clicked
AX = 3
It return the following values for different event
CX = x coordinate of mouse pointer
DX = y coordinate of mouse pointer
BX = mouse button status
if BX = 0 means no button is pressed
if BX = 1 means left button is pressed
if BX = 2 means right button is pressed
if BX = 3 means center button is pressed

Set the position of the mouse pointer on screen
AX = 4.
CX = x coordinate of mouse pointer
DX = y coordinate of mouse pointer
Here, the return value is nothing

Restrict mouse pointer or cursor to set horizontal limits
AX = 7
CX = minimum x coordinate of mouse pointer or cursor
DX = maximum x coordinate of mouse pointer or cursor
Here, the return value is nothing

Restrict mouse pointer or cursor to set vertical limits
AX = 8
CX = minimum y coordinate of mouse pointer or cursor
DX = maximum y coordinate of mouse pointer or cursor
Here, the return value is nothing

About int86() function :

The int86() is an important function of dos.h library in Turbo C++ IDE. It is primarily used for making software interrupts in DOS-based systems to access various low-level services. It interacts with hardware devices. To handle mouse pointer in DOS-based systems, you can use int86() function with interrupt number 0x33.

Syntax of int86() function
int int86(int interrupt_num, union REGS* regs_in, union REGS* regs_out);

interrupt_num – An integer value representing the interrupt number
regs_in – A pointer to a structure of type union REGS containing the input registers for the interrupt.
regs_out – A pointer to a structure of type union REGS to store the output registers after the interrupt.

How to run all the programs :

In all the program, I use Turbo C++ compiler. So you have to install the Turbo C++ IDE on your pc. You can copy my program and use in your program. You have to know how to copy paste in Turbo C++. The graphics.h library is used in most of the program. So you have to also know how use graphics.h in Turbo C++ IDE. First, open the Turbo C++ IDE and create a C or C++ file with .c or .cpp extension. Now copy paste my programs on your file and run it.

How to initialize mouse pointer or cursor by C or C++ programming language :

In the program, I show you how you initialize mouse pointer or cursor by C or C++ programming language. Using this program, you can check if a mouse driver is loaded or not. Here you see mouse pointer is supported or not by C or C++ programming language. First include dos.h, conio.h and stdio.h library in the program. Then declare two variables (regs_in, regs_out) of type union REGS which are declared in dos.h library.

Next, initialize the input ax register (regs_in) to 0. Now call the int86() function with proper arguments such as interrupt number (0X33) and two union REGS type variables (regs_in, regs_out). If the return value of the output ax register (regs_out) is 0, the mouse does not initialize or load. The getch() function is used to get a key press before stopping the program.

/*Developed by Puskar Jasu*/
#include <dos.h>
#include <conio.h>
#include <stdio.h>
int main()
{
    union REGS regs_in, regs_out;
    regs_in.x.ax = 0;
    int86(0X33, &regs_in, &regs_out);
    if (regs_out.x.ax == 0)
        printf("Mouse does not initialize\n");
    else
        printf("Mouse is succesfully initialized\n");
    getch();
    return 0;
}

Output :

initialize mouse pointer or cursor by C or C++ programming language

How to display the mouse pointer or cursor in text mode by C or C++ language :

In the program, I show you how to display the mouse pointer or cursor in text mode by C or C++ programming language. At first, include dos.h and conio.h library in the program. Then declare two variables (regs_in, regs_out) of type union REGS which are declared in dos.h library. Next, initialize the input ax register (regs_in) to 0 and call the int86() function for load the mouse. Then further initialize the input ax register (regs_in) to 1 and call the int86() function for display or show the mouse pointer. After that, call getch() function to get a key press before stopping the program. Now you see the mouse in text mode which looks like a square.

/*Developed by Puskar Jasu*/
#include <dos.h>
#include <conio.h>
int main()
{
    union REGS regs_in, regs_out;
    regs_in.x.ax = 0;
    int86(0X33, &regs_in, &regs_out);
    regs_in.x.ax = 1;
    int86(0X33, &regs_in, &regs_out);
    getch();
    return 0;
}

Output :

display the mouse pointer or cursor in text mode by C or C++ programming language

How to display the mouse pointer in graphics mode by C or C++ language :

In this program, you can display the mouse pointer or cursor in graphics mode by C or C++ programming language. At first, include dos.h, graphics.h and conio.h library in the program. Then declare two variables (regs_in, regs_out) of type union REGS which are declared in dos.h library. Now using initgraph() function, initialize the graphics mode with proper arguments and set the background color of window by setbkcolor() function.

Next, initialize the input ax register (regs_in) to 0 and call the int86() function for load the mouse. Then further initialize the input ax register (regs_in) to 1 and call the int86() function for display or show the mouse pointer on the screen. After that, call getch() function to get a key press before stopping the program. Finally, call the closegraph() function for close the graphic mode. Now you see the mouse in graphics mode which looks like a pointer.

/*Developed by Puskar Jasu*/
#include <conio.h>
#include <dos.h>
#include <graphics.h>
int main()
{
    union REGS regs_in, regs_out;
    int graphic_driver = DETECT, graphic_mode;
    initgraph(&graphic_driver, &graphic_mode, "//turboc3/bgi");
    setbkcolor(4);
    regs_in.x.ax = 0;
    int86(0X33, &regs_in, &regs_out);
    regs_in.x.ax = 1;
    int86(0X33, &regs_in, &regs_out);
    getch();
    closegraph();
    return 0;
}

Output :

display the mouse pointer or cursor in graphics mode by C or C++ programming language

How to hide the mouse pointer or cursor by C or C++ programming language :

In a drawing program, you need to hide mouse pointer to draw something on screen as it may interfere with drawing, after that again make it visible. In this program, I show you how to hide the mouse pointer or cursor by C or C++ graphics programming language. Here, I create a program to show and hide mouse pointer or cursor alternatively.

At the beginning of the program, you have to include dos.h, graphics.h and conio.h library. Then declare two variables (regs_in, regs_out) of type union REGS. After that, initialize the graphics mode using initgraph() function and set the background color of the graphics window by setbkcolor() function. Next, initialize the input ax register (regs_in) to 0 and call the int86() function for load the mouse.

Then in the while loop, initialize the input ax register (regs_in) to 1 for display or show the mouse pointer and initialize ax register (regs_in) to 2 for hide the mouse on the screen. Here, I use delay() function for proper show the mouse. In while loop I use kbhit() function to get a key press before stopping the program. Finally, call the closegraph() function for close the graphic mode. Now you can see the mouse is displayed and hide alternatively until you press a key.

/*Developed by Puskar Jasu*/
#include <conio.h>
#include <dos.h>
#include <graphics.h>
int main()
{
    union REGS regs_in, regs_out;
    int graphic_driver = DETECT, graphic_mode;
    initgraph(&graphic_driver, &graphic_mode, "//turboc3/bgi");
    setbkcolor(3);
    regs_in.x.ax = 0;
    int86(0X33, &regs_in, &regs_out);
    while (!kbhit())
    {
        regs_in.x.ax = 1;
        int86(0X33, &regs_in, &regs_out);
        delay(500);
        regs_in.x.ax = 2;
        int86(0X33, &regs_in, &regs_out);
        delay(500);
    }
    closegraph();
    return 0;
}

Output :

hide the mouse pointer or cursor by C or C++ programming language

How to know position of mouse pointer and which mouse button is clicked :

In this program, I show you how to know the current position (coordinates) of the mouse pointer or cursor and which mouse button is clicked or pressed by C or C++ graphics programming language. I also show you in which position the mouse is clicked by C or C++ graphics programming language. Maximum screen resolution for mouse pointer or cursor in text mode is 640×200 and in graphics mode is 640×480.

At first, include dos.h, graphics.h, stdio.h and conio.h library in this program. After declaring require variables, initialize the graphics mode using initgraph() function. The settextstyle() function use for text style. Then, initialize the mouse pointer by set ax registers to 0 and show the mouse pointer by set ax registers to 1. Now, set ax to 3 which returns the current position or coordinates (x and y coordinate) of the mouse pointer by cx and dx register respectively and which mouse button is clicked by bx register.

Using the output value, display the x and y coordinate of the mouse pointer and which mouse pointer is clicked with position on the screen. Here, I use cleardevice() function to clear the screen, sprintf() function to convert int to string and outtextxy() function to display the string value on the screen.

In the while loop, I use kbhit() function to get a key press before stopping the program. At last, call the closegraph() function for close the graphic mode. Now you can see the x and y coordinates of the current position of the mouse pointer. If you move the mouse pointer, you can see the change of coordinates (x and y) will be shown on the screen. You can also see which mouse button is clicked whether left, right or center with the coordinates of the mouse pointer shown on the screen.

/*Developed by Puskar Jasu*/
#include <graphics.h>
#include <conio.h>
#include <stdio.h>
#include <dos.h>
int main()
{
    union REGS regs_in, regs_out;
    int click, x, y, x1, y1;
    char str[20], str1[20];
    int graphic_driver = DETECT, graphic_mode;
    initgraph(&graphic_driver, &graphic_mode, "//turboc3/bgi");
    settextstyle(DEFAULT_FONT, 0, 2);
    regs_in.x.ax = 0;
    int86(0X33, &regs_in, &regs_out);
    regs_in.x.ax = 1;
    int86(0X33, &regs_in, &regs_out);
    regs_in.x.ax = 3;
    int86(0X33, &regs_in, &regs_out);
    click = regs_out.x.bx;
    x1 = regs_out.x.cx;
    y1 = regs_out.x.dx;
    while (!kbhit())
    {
	regs_in.x.ax = 3;
	int86(0X33, &regs_in, &regs_out);
	click = regs_out.x.bx;
	x = regs_out.x.cx;
	y = regs_out.x.dx;
	if (x != x1 || y != y1)
	{
	    cleardevice();
	    regs_in.x.ax = 1;
	    int86(0X33, &regs_in, &regs_out);
	    sprintf(str, "X = %d, Y = %d", x, y);
	    outtextxy(10, 20, str);
	    x1 = x;
	    y1 = y;
	}
	if (click == 1)
	{
	    cleardevice();
	    regs_in.x.ax = 1;
	    int86(0X33, &regs_in, &regs_out);
	    sprintf(str, "X = %d, Y = %d", x, y);
	    outtextxy(10, 20, str);
	    outtextxy(10, 40, "Left clicked at");
	    sprintf(str1, "%d %d", x, y);
	    outtextxy(270, 40, str1);
	}
	if (click == 2)
	{
	    cleardevice();
	    regs_in.x.ax = 1;
	    int86(0X33, &regs_in, &regs_out);
	    sprintf(str, "X = %d, Y = %d", x, y);
	    outtextxy(10, 20, str);
	    outtextxy(10, 40, "Right clicked at");
	    sprintf(str1, "%d %d", x, y);
	    outtextxy(280, 40, str1);
	}
	 if (click == 3)
	{
	    cleardevice();
	    regs_in.x.ax = 1;
	    int86(0X33, &regs_in, &regs_out);
	    sprintf(str, "X = %d, Y = %d", x, y);
	    outtextxy(10, 20, str);
	    outtextxy(10, 40, "Center clicked at");
	    sprintf(str1, "%d %d", x, y);
	    outtextxy(280, 40, str1);
	}
    }
    closegraph();
    return 0;
}

Output :

know the current position (coordinates) of the mouse pointer or cursor and which mouse button is clicked or pressed by C or C++ graphics programming language

How to set the position of the mouse pointer by C or C++ language :

In many programs, sometimes you have to set the mouse pointer to specific position. In this program, I show you how to set the position of the mouse pointer or cursor on screen by C or C++ graphics programming language.

At first, include dos.h, graphics.h and conio.h library in this program. Now, you have to declare require variables. Then, using initgraph() function initializes the graphics mode. The setbkcolor() function use for set the background color of the graphics window. Then, set ax register to 0 for initialize the mouse pointer and set ax register to 1 for show the mouse pointer.

Now, set ax to 4 for set the mouse pointer to specific position. To set the specific position of mouse pointer, you have to set the value of cx and dx. Here, I set both cx and dx value with 100 to set the mouse pointer with 100 x coordinate and 100 y coordinate. After that, I use getch() function to get a key press before stopping the program. At last, using the closegraph() function close the graphics mode. Now you see the mouse pointer at the position of 100 x coordinate and 100 y coordinate.

/*Developed by Puskar Jasu*/
#include <conio.h>
#include <dos.h>
#include <graphics.h>
int main()
{
    union REGS regs_in, regs_out;
    int graphic_driver = DETECT, graphic_mode;
    initgraph(&graphic_driver, &graphic_mode, "//turboc3/bgi");
    setbkcolor(9);
    regs_in.x.ax = 0;
    int86(0X33, &regs_in, &regs_out);
    regs_in.x.ax = 1;
    int86(0X33, &regs_in, &regs_out);
    regs_in.x.ax = 4;
    regs_in.x.cx = 100;
    regs_in.x.dx = 100;
    int86(0x33, &regs_in, &regs_out);
    getch();
    closegraph();
    return 0;
}

Output :

set the position of the mouse pointer on screen by C or C++ programming language

How to restrict mouse pointer within a rectangle by C or C++ language :

Sometime, you have to restrict mouse pointer or cursor within a rectangular boundary. In this program, I show you how to restrict mouse pointer or cursor within a rectangular boundary by C or C++ graphics programming language.

First, you have to include dos.h, graphics.h and conio.h library in this program. After that, declare require variables. Next, using initgraph() function initializes the graphics mode. The setbkcolor() function use to set the background color of the graphics window. Then, set ax register to 0 for initialize the mouse pointer and set ax register to 1 for show the mouse pointer. Then, draw a rectangle by rectangle() function. Using setfillstyle() and floodfill() color the outer portion of the rectangle.

Now, set ax to 7 for set the horizontal limit of the mouse pointer. Here, cx set for minimum x coordinate and dx set for maximum x coordinate. Next, set ax to 8 for set the vertical limit of the mouse pointer. Here, cx set for minimum y coordinate and dx set for maximum y coordinate. After that, I use getch() function to get a key press before stopping the program. At last, using the closegraph() function close the graphics mode. Now you see the mouse pointer can move within a rectangular boundary on the screen.

/*Developed by Puskar Jasu*/
#include <conio.h>
#include <dos.h>
#include <graphics.h>
int main()
{
    union REGS regs_in, regs_out;
    int graphic_driver = DETECT, graphic_mode;
    initgraph(&graphic_driver, &graphic_mode, "//turboc3/bgi");
    setbkcolor(1);
    regs_in.x.ax = 0;
    int86(0X33, &regs_in, &regs_out);
    regs_in.x.ax = 1;
    int86(0X33, &regs_in, &regs_out);
    setcolor(6);
    setfillstyle(1, 6);
    rectangle(200, 100, 500, 400);
    floodfill(30, 30, 6);
    regs_in.x.ax = 7;
    regs_in.x.cx = 200;
    regs_in.x.dx = 500;
    int86(0X33, &regs_in, &regs_out);
    regs_in.x.ax = 8;
    regs_in.x.cx = 100;
    regs_in.x.dx = 400;
    int86(0X33, &regs_in, &regs_out);
    getch();
    closegraph();
    return 0;
}

Output :

restrict mouse pointer or cursor within a rectangular boundary by C or C++ programming language

How to restrict mouse pointer within a circle by C or C++ language :

Sometime, you have to restrict mouse pointer or cursor within a circle. In this program, I show you how to restrict mouse pointer or cursor within a circle by C or C++ graphics programming language.

First, you have to include dos.h, graphics.h, math.h and conio.h library in this program. After that, declare require variables. Next, using initgraph() function initializes the graphics mode. Then, set ax register to 0 for initialize the mouse pointer and set ax register to 1 for show the mouse pointer. Now, set ax to 4 for set the mouse pointer to the specific position and also set the value of cx and dx.

The setbkcolor() function uses to set the background color of the graphics window. Then, draw a circle by circle() function. Using setfillstyle() and floodfill() color the circle. In the while loop, set ax to 3 to get the x and y coordinate of the mouse pointer. Here I check that if mouse pointer is out of the circle, move the pointer using ax value to set 4 and also set the cx and dx value. In the while loop, I use kbhit() function to check any key press or not. At last, the closegraph() function uses to close the graphics mode. Now you see the mouse pointer can move within a circle on the screen.

/*Developed by Puskar Jasu*/
#include <graphics.h>
#include <conio.h>
#include <dos.h>
#include <math.h>
int main()
{
    union REGS regs_in, regs_out;
    int x, y, x1, y1;
    int graphic_driver = DETECT, graphic_mode;
    initgraph(&graphic_driver, &graphic_mode, "//turboc3/bgi");
    regs_in.x.ax = 0;
    int86(0X33, &regs_in, &regs_out);
    regs_in.x.ax = 1;
    int86(0X33, &regs_in, &regs_out);
    regs_in.x.ax = 4;
    regs_in.x.cx = x;
    regs_in.x.dx = y;
    int86(0X33, &regs_in, &regs_out);
    setbkcolor(10);
    setcolor(4);
    setfillstyle(1, 4);
    circle(320, 240, 200);
    floodfill(320, 240, 4);
    x = x1 = 320;
    y = y1 = 240;
    while (!kbhit())
    {
        regs_in.x.ax = 3;
        int86(0X33, &regs_in, &regs_out);
        x = regs_out.x.cx;
        y = regs_out.x.dx;
        if ((pow(x - 320, 2) + pow(y - 240, 2) - pow(200, 2)) > 0)
        {
            regs_in.x.ax = 4;
            regs_in.x.cx = x1;
            regs_in.x.dx = y1;
            int86(0X33, &regs_in, &regs_out);
            x = x1;
            y = y1;
        }
        x1 = x;
        y1 = y;
    }
    closegraph();
    return 0;
}

Output :

restrict mouse pointer or cursor within a circle by C or C++ programming language

How to draw using mouse pointer or cursor by C or C++ language :

Sometimes, you have to know how the pencil tool work in a paint program. In this program, I show you how to draw using mouse pointer or cursor by C or C++ graphics programming language.

First, you have to include dos.h, graphics.h and conio.h library in this program. After that, declare require variables. Next, using initgraph() function initializes the graphics mode. The setbkcolor() function use for set the background color of the graphics window. Then, draw a rectangle by rectangle() function. Using setfillstyle() and floodfill() color the outer portion of the rectangle. The setcolor() use to set the color of the pencil (mouse pointer).

Then, set ax register to 0 for initialize the mouse pointer. Now, set ax to 7 for set the horizontal limit of the mouse pointer. Here, cx set for minimum x coordinate and dx set for maximum x coordinate. Next, set ax to 8 for set the vertical limit of the mouse pointer. Here, cx set for minimum y coordinate and dx set for maximum y coordinate. In the while loop, set ax register to 1 for show the mouse pointer and set ax to 3 to get the x and y coordinate of the mouse pointer.

Within the while loop, run another while loop where check left button press or not if press hide the mouse by set ax to 2 and at the same time draw line by line() function. To get and set the x and y coordinates draw the line like pencil on the screen. In outer while loop use kbhit() for check any key press or not. At last, using the closegraph() function close the graphics mode. You can see how draw anything on the screen using the mouse pointer.

/*Developed by Puskar Jasu*/
#include <dos.h>
#include <graphics.h>
#include <conio.h>
int main()
{
    union REGS regs_in, regs_out;
    int status, i, x1, y1, x2, y2;
    int graphic_driver = DETECT, graphic_mode;
    initgraph(&graphic_driver, &graphic_mode, "//turboc3/bgi");
    setbkcolor(10);
    setcolor(6);
    setfillstyle(1, 6);
    rectangle(100, 100, 540, 400);
    floodfill(30, 30, 6);
    setcolor(9);
    regs_in.x.ax = 0;
    int86(0X33, &regs_in, &regs_out);
    regs_in.x.ax = 7;
    regs_in.x.cx = 100;
    regs_in.x.dx = 540;
    int86(0X33, &regs_in, &regs_out);
    regs_in.x.ax = 8;
    regs_in.x.cx = 100;
    regs_in.x.dx = 400;
    int86(0X33, &regs_in, &regs_out);
    while (!kbhit())
    {
        regs_in.x.ax = 1;
        int86(0X33, &regs_in, &regs_out);
        regs_in.x.ax = 3;
        int86(0x33, &regs_in, &regs_out);
        x1 = regs_out.x.cx;
        y1 = regs_out.x.dx;
        x2 = x1;
        y2 = y1;
        while (regs_out.x.bx == 1)
        {
            regs_in.x.ax = 2;
            int86(0x33, &regs_in, &regs_out);
            line(x1, y1, x2, y2);
            x1 = x2;
            y1 = y2;
            regs_in.x.ax = 3;
            int86(0x33, &regs_in, &regs_out);
            x2 = regs_out.x.cx;
            y2 = regs_out.x.dx;
        }
    }
    closegraph();
    return 0;
}

Output :

draw using mouse pointer or cursor by C or C++ programming language

Conclusion :

After completely read the above article, you have a solid understanding of how to detect and respond to mouse events such as clicks, movements and button presses using C or C++ graphics programming language. By following the above post, you have gain the necessary knowledge and skills to develop your own mouse programs by C or C++ programming language. Thank you for visiting my site.

Scroll to Top