Basic mouse programming in C - Show/hide mouse pointer in C

Program Description

Basic mouse programming in C.

Show/hide mouse pointer in C.
This program demonstrates the initialization of mouse pointer in C and simply shows and hides the mouse pointer based on user input.
The program first checks for mouse support using the init() function.

It then waits for user input in a continuous loop:
on pressing s - mouse pointer is shown
on pressing h - mouse pointer is hidden
on pressing any other key - the program terminates


This program demonstrates how mouse can be enabled in C programs.

Program
/******************************************************************
*
*    Basic mouse programming in C - Show/hide mouse pointer in C 
*
*    code.cheraus.com
*
********************************************************************/

#include<iostream.h>
#include<conio.h>
#include<math.h>
#include<graphics.h>
#include<dos.h>

union REGS i,o;


//function prototypes
void showpointer();
void hidepointer();
int init_mouse();
int main()
{
  int gd=DETECT,gm,status;
  
  //initialization of graphics mode
   initgraph(&gd,&gm,"C:\\turboc3\\bgi");
   
   //checking mouse support
    status = init_mouse();
	
	//if mouse is not supported
    if(status==0)
     {
	   outtextxy(200,240,"NO MOUSE SUPPORT.");
     }
	 
	 
     //if mouse is supported
	 else
     {
	   
	   while(1)
	   {
	     char opt;
		 
	     cleardevice();
		 
		 
	     cout<<"\n1.Press s to show pointer\n2.Press h to hide pointer\n3.Press any key to exit\n";
	     cout<<"\nEnter the option : ";
		 
		 //take user input
	     opt=getch();
		 
		 
		 if(opt == 's')
		  showpointer();      //shows the mouse pointer
		 else if (opt == 'h')
		   hidepointer();    //hides the mouse pointer
		 else
		   break;   //exits the loop
	  }
     }
	 
	 closegraph();
	 
	 return 0;
}

void showpointer()
{
   i.x.ax=1;
   int86(0X33,&i,&o);
}

void hidepointer()
{
    i.x.ax=2;
    int86(0X33,&i,&o);
}

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

Output
Show Mouse Pointer



Hide Mouse Pointer

Comments
comments powered by Disqus