vibudh is talking

Posts tagged ‘graphical c++’

Gallery

Inserting Mouse Pointer in your C++ program (for TC)

The C++ code that we are going to discuss is a simple part of a program that can be used to insert a mouse pointer in your C++ program, however the code is self sufficient and will work perfectly in any standard C++ compiler. This part of the program when inserted in your C++ program will make your project user friendly and at the same time give you an edge over other programs by enhancing the execution. It is advisable to include this part of the program before you start the project. Any relevant query will be most welcome. The source code for inserting the mouse pointer is as follows with relevant comment lines.

 
/*relevant header files for the program */
#include 
#include
#include 
#include
#include

int intmouse();
/*function to show mouse pointer*/
void showmouseptr(); 

/*gets the current position of the mouse */
void getmousepos(int *, int *, int*);

/*restricts the mouse in the graphical area */
void restrictmouseptr(int x,int y,int x1,int y1);

union REGS i,o,a,b;
int intmouse()
{
	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 x,int y,int x1,int y1)
{
	i.x.ax=7;
	i.x.cx=x;
	i.x.dx=x1;
	int86(0x33,&i,&o);
	i.x.ax=8;
	i.x.cx=y;
	i.x.dx=y1;
	int86(0x33,&i,&o);
}

void getmousepos(int *p,int *x,int *y)
{
	i.x.ax=3;

	int86(0x33,&i,&o);
	*p=o.x.bx;
	*x=o.x.cx;
	*y=o.x.dx;
}
void main()
{
 int gd=DETECT,gm;
 initgraph(&gd,&gm,"\\tc\\bgi");
 int x=getmaxx();
 int y=getmaxy();
 if(intmouse()==0)
  exit(1);
 showmouseptr();
 cleardevice();
 setcolor(WHITE);
 setbkcolor(BLACK);
 int button,n,m;
 outtextxy(x-100,y-50,"Exit...");
 getmousepos(&button,&n,&m); /*n=x-coordinate& m=y-coordinate*/
 int z=1;
 restrictmouseptr(1,1,x-1,y-1);

 while(z)
 {
  getmousepos(&button,&n,&m);
  /*this will keep in track with the current position of the mouse*/

  /*displaying the current position of the mouse */
  gotoxy(1,1);
  cout<<"n ="<<n<=532 && n<=592) && (m>=429 && m<=444))
  {
   exit(0);
   button=0;
   z=0;
  }
 }

 closegraph();
 restorecrtmode();
 getch();
 closegraph();
 restorecrtmode();
}

Now we will be discussing how this programme actually works and what does each function do. initgraph() is used to initialize the graph grid. showmouseptr() is used to show the mouse pointer in the output screen. cleardevice() is simply used to clear the scree. getmouseposition(&button,&n,&m) is used to get the present status of the mouse button and its present coordinates. restrictmouseptr() is used to prevent the mouse pointer to go out of bouonds, so that is does not go beyond the display screen. Here as you can see in the main() we are using a while loop, that stays true, and has infinite loop. This is important because we need the dynamic execution of the programme as long as the user does not press Exit… to get out of the programme. This while loop is also the place where other dynamic components of the program will be executed, as we discuss in some other projects that we take on. Hope this programme helps you in making your programme more interactive and user friendly.