0% found this document useful (0 votes)
95 views13 pages

C Mouse Programs

The document contains C programming code examples that demonstrate how to use mouse functions in C including checking if mouse support is available, displaying the mouse pointer, getting the current mouse position, determining which mouse button is clicked, and restricting the mouse pointer within a rectangle or circle. The examples initialize the mouse, show/hide the pointer, and get mouse coordinates and button status using BIOS interrupts.

Uploaded by

shiv_ryder
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
95 views13 pages

C Mouse Programs

The document contains C programming code examples that demonstrate how to use mouse functions in C including checking if mouse support is available, displaying the mouse pointer, getting the current mouse position, determining which mouse button is clicked, and restricting the mouse pointer within a rectangle or circle. The examples initialize the mouse, show/hide the pointer, and get mouse coordinates and button status using BIOS interrupts.

Uploaded by

shiv_ryder
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

www.downloadmela.

com

C Program to check if mouse support is available or not


/* Program to check if mouse driver is loaded or not */

C programming code
#include<dos.h> #include<conio.h> int initmouse(); union REGS i, o; main() { int status; status = initmouse(); if ( status == 0 ) printf("Mouse support not available.\n"); else printf("Mouse support available.\n"); getch(); return 0;

int initmouse() { i.x.ax = 0; int86(0X33,&i,&o); return ( o.x.ax ); }

www.downloadmela.com - World's number one free education download portal

www.downloadmela.com
Output of program:

C Program to display mouse pointer in textmode


/* Program to display mouse-pointer in text-mode */
#include<dos.h> #include<conio.h> int initmouse(); void showmouseptr(); union REGS i, o; main() { int status; status = initmouse(); if ( status == 0 ) printf("Mouse support not available.\n"); else showmouseptr(); getch(); return 0; } int initmouse() { i.x.ax = 0; int86(0X33,&i,&o); return ( o.x.ax ); }

www.downloadmela.com - World's number one free education download portal

www.downloadmela.com
void showmouseptr() { i.x.ax = 1; int86(0X33,&i,&o); }

C Program to display mouse pointer in graphics mode


This program displays mouse pointer in graphics mode. First graphics mode is initialized and then mouse using initmouse.

C programming code
#include<graphics.h> #include<conio.h> #include<dos.h> int initmouse(); void showmouseptr(); union REGS i, o; main() { int status, gd = DETECT, gm; initgraph(&gd,&gm,"C:\\TC\\BGI"); status = initmouse(); if ( status == 0 ) printf("Mouse support not available.\n"); else showmouseptr(); getch(); return 0;

int initmouse() { i.x.ax = 0; int86(0X33,&i,&o); return ( o.x.ax ); } void showmouseptr() { i.x.ax = 1; int86(0X33,&i,&o); }

www.downloadmela.com - World's number one free education download portal

www.downloadmela.com
Output of program:

C Program to hide mouse pointer


This program hides mouse pointer. We require to hide mouse pointer when we want to draw something on screen as it may interfere with drawing, after that we again make it visible. /* Program to show and hide mouse-pointer alternatively */

C programming code
#include<graphics.h> #include<conio.h> #include<dos.h> int initmouse(); void showmouseptr(); void hidemouseptr(); union REGS i, o; main() { int status, count = 1, gd = DETECT, gm; initgraph(&gd,&gm,"C:\\TC\\BGI"); status = initmouse();

www.downloadmela.com - World's number one free education download portal

www.downloadmela.com
if ( status == 0 ) printf("Mouse support not available.\n"); else { showmouseptr(); while(count<=10) { getch(); count++; if(count%2==0) hidemouseptr(); else showmouseptr(); } } getch(); return 0; } int initmouse() { i.x.ax = 0; int86(0X33,&i,&o); return ( o.x.ax ); } void showmouseptr() { i.x.ax = 1; int86(0X33,&i,&o); } void hidemouseptr() { i.x.ax = 2; int86(0X33,&i,&o); }

// to hide mouse

C Program to get current position of mouse pointer


This program prints the x and y coordinates of current position of mouse pointer i.e. wherever you move the mouse coordinates of that point will be printed on the screen. /* Program to get mouse-pointer coordinates - where is the mouse */

C programming code
#include<graphics.h> #include<conio.h> #include<stdio.h> #include<dos.h> int initmouse(); void showmouseptr(); void hidemouseptr(); void getmousepos(int*,int*,int*); union REGS i, o; main() { int gd = DETECT, gm, status, button, x, y, tempx, tempy;

www.downloadmela.com - World's number one free education download portal

www.downloadmela.com
char array[50]; initgraph(&gd,&gm,"C:\\TC\\BGI"); settextstyle(DEFAULT_FONT,0,2); status = initmouse(); if ( status == 0 ) printf("Mouse support not available.\n"); else { showmouseptr(); getmousepos(&button,&x,&y); tempx = x; tempy = y; while(!kbhit()) { getmousepos(&button,&x,&y); if( x == tempx && y == tempy ) {} else { cleardevice(); sprintf(array,"X = %d, Y = %d",x,y); outtext(array); tempx = x; tempy = y; }

} }

getch(); return 0; } int initmouse() { i.x.ax = 0; int86(0X33,&i,&o); return ( o.x.ax ); } void showmouseptr() { i.x.ax = 1; int86(0X33,&i,&o); } void getmousepos(int *button, int *x, int *y) { i.x.ax = 3; int86(0X33,&i,&o); *button = o.x.bx; *x = o.x.cx; *y = o.x.dx;

www.downloadmela.com - World's number one free education download portal

www.downloadmela.com
Output of program:

c program to determine which mouse button is clicked


This program print which mouse button is clicked whether left or right, coordinates of the point where button is clicked are also printed on the screen. /* Program to determine which mouse button is clicked */

C programming code
#include<graphics.h> #include<conio.h> #include<dos.h> union REGS i, o; int initmouse() { i.x.ax = 0; int86(0X33,&i,&o); return ( o.x.ax );

www.downloadmela.com - World's number one free education download portal

www.downloadmela.com
} void showmouseptr() { i.x.ax = 1; int86(0X33,&i,&o); } void getmousepos(int *button, int *x, int *y) { i.x.ax = 3; int86(0X33,&i,&o); *button = o.x.bx; *x = o.x.cx; *y = o.x.dx;

main() { int gd = DETECT, gm, status, button, x, y; char array[50]; initgraph(&gd,&gm,"C:\\TC\\BGI"); settextstyle(DEFAULT_FONT,0,2); status = initmouse(); if ( status == 0 ) printf("Mouse support not available.\n"); else { showmouseptr(); getmousepos(&button,&x,&y); while(!kbhit()) { getmousepos(&button,&x,&y); if( button == 1 ) { button = -1; cleardevice(); sprintf(array,"Left Button clicked x = %d y = %d",x,y); outtext(array); } else if( button == 2 ) { button = -1; cleardevice(); sprintf(array,"Right Button clicked x = %d y = %d",x,y); outtext(array); } } }

getch(); return 0;

www.downloadmela.com - World's number one free education download portal

www.downloadmela.com
Output of program:

c program to restrict mouse pointer


This program restricts mouse pointer in a rectangle. When this program is executed the mouse pointer is restricted in a rectangle i.e. you can't move mouse pointer out of the rectangle. /* Program to restrict mouse-pointer */

C programming code
#include<dos.h> #include<graphics.h> #include<conio.h> int initmouse(); void showmouseptr(); void hidemouseptr(); void restrictmouseptr(int, int, int, int); union REGS i, o; main() { int status, gd = DETECT, gm;

www.downloadmela.com - World's number one free education download portal

www.downloadmela.com

initgraph(&gd,&gm,"C:\\TC\\BGI"); settextstyle(DEFAULT_FONT,0,2); status = initmouse(); if ( status == 0 ) outtext("Mouse support not available.\n"); else { showmouseptr(); rectangle(120,70,520,410); restrictmouseptr(120,70,520,410); } getch(); return 0; } int initmouse() { i.x.ax = 0; int86(0X33,&i,&o); return ( o.x.ax ); } void showmouseptr() { i.x.ax = 1; int86(0X33,&i,&o); } void restrictmouseptr(int x1, int y1, int x2, int y2) { i.x.ax = 7; i.x.cx = x1; i.x.dx = x2; int86(0X33,&i,&o); i.x.ax = 8; i.x.cx = y1; i.x.dx = y2; int86(0X33,&i,&o); }

www.downloadmela.com - World's number one free education download portal

www.downloadmela.com
Output of program:

c program to restrict mouse pointer in a circle


This program restricts mouse pointer in a circle i.e you can't move mouse out of a circle. When you try to bring mouse pointer outside the circle, mouse pointer is moved to it's previous location which is inside the circle. Code to restrict mouse in circle is given below :-

C programming code
#include<graphics.h> #include<conio.h> #include<dos.h> #include<stdlib.h> #include<math.h> union REGS i, o; int initmouse() { i.x.ax = 0; int86(0X33, &i, &o); return ( o.x.ax ); }

www.downloadmela.com - World's number one free education download portal

www.downloadmela.com

void showmouseptr() { i.x.ax = 1; int86(0X33, &i, &o); } void hidemopuseptr() { i.x.ax = 2; int86(0X33,&i,&o); } void getmousepos(int *x, int *y) { i.x.ax = 3; int86(0X33, &i, &o); *x = o.x.cx; *y = o.x.dx; } void movemouseptr(int x, int y) { i.x.ax = 4; i.x.cx = x; i.x.dx = y; int86(0X33, &i, &o); } main() { int gd = DETECT, gm, midx, midy, radius, x, y, tempx, tempy; radius = 100; initgraph(&gd, &gm, "C:\\TC\\BGI"); if(!initmouse()) { closegraph(); exit(1); } midx = getmaxx()/2; midy = getmaxy()/2; showmouseptr(); movemouseptr(midx, midy); circle(midx, midy, radius); x = tempx = midx; y = tempy = midy; while(!kbhit()) { getmousepos(&x, &y); if((pow(x-midx,2)+pow(y-midy,2)-pow(radius,2))>0) { movemouseptr(tempx, tempy); x = tempx; y = tempy; } tempx = x; tempy = y; } closegraph(); return 0;

www.downloadmela.com - World's number one free education download portal

www.downloadmela.com
}

www.downloadmela.com - World's number one free education download portal

You might also like