Sunday, July 17, 2011

how to use cursor in c++ program

one year ago i was goggling the giant for code to access pointer in turbo c++ 3.0
after a long search i found one site but the tutorial was very complex and it took me a whole day to figure out how it work
but here is a simple tutorial for you.

screen-shoots of program

sreenshoot1sreenshoot2
download program + source-code


you can access pointers in both graphics and text mode
but i recommend that you use graphics mode because it looks more beautiful.

if you don't know how to change to graphics mode please read me first

now lets finish our chat and start the tutorial part

cursor functionality is provided in dos.h
so, here it is the main part of the post:-

Interrupt Service Purpose
51 0
Reset mouse and get status
Call with AX = 0
Returns: AX = FFFFh If mouse support is available
Ax = 0 If mouse support is not available
51 1
Show mouse pointer
Call with AX = 1 
Returns: Nothing
51 2
Hide mouse pointer
Call with AX = 2
Returns: Nothing
51 3
Get mouse position and button status
Call with AX = 3
Returns: BX = mouse button status
BitSignificance
0button not pressed
1left button is pressed
2right button is pressed
3center button is pressed
CX = x coordinate
DX = y coordinate
51 4 Set mouse pointer position
Call with AX = 4
CX = x coordinate
DX = y coordinate
Returns: Nothing
51 7 Set horizontal limits for pointer
Call with AX = 7
CX = minimum x coordinate
DX = maximum x coordinate
Returns: Nothing
51 8 Set vertical limits for pointer
Call with AX = 8
CX = minimum y coordinate
DX = maximum y coordinate
Returns: Nothing
so here is a working code that you can attach to your program and easily add cursor to your program
/*you must include dos.h*/
#include<dos.h>
/*you must include stdlib.h for exit function*/
#include<stdlib.h>
/*----------------------------------*/
struct mouse{int x,y,b;};
void mouse_ini();
int mouse_show();
int mouse_hide();
void mouse_get(mouse *);
void mouse_set(int x,int y);
void mouse_restrict(int x_left,int y_top,int x_right,int y_bottom);
/*-----------------------------------*/
/*to initialize the mouse when program starts*/
int mouse_ini()
{
if(!mouse_show())
 {
 printf("error in inializing mouse");
 printf("Press any key to halt:");
 getch();
 exit(1);
 }
else mouse_restrict(0,0,getmaxx(),getmaxy());
/*note:-getmaxx() and getmaxy() are functions of graphics.h*/
}
/*to enable cursor or show cursor to user*/
int mouse_show()
{
 in.x.ax=1;
 return int86(51,&in,&out);
}
/*to disable cursor or hide cursor to user*/
int mouse_hide()
{
 in.x.ax=2;
 return int86(51,&in,&out);
}
/*get current mouse information
b for button
x for x-axis
u for y-axis
*/
void mouse_get(mouse *mus)
{
 in.x.ax=3;
 int86(51,&in,&out);
 mus->b=out.x.bx;
 mus->x=out.x.cx;
 mus->y=out.x.dx;
}
/*when you want to get the mouse position and button use this method
mouse mus;
mouse_get(&mus); */
/*set new position for the cursor*/
void mouse_set(int x,int y)
{
 in.x.ax=4;
 in.x.cx=x;
 in.x.dx=y;
 int86(51,&in,&out);
}
/*restrict the mouse pointer between the specific positions*/
void mouse_restrict(int x_left,int y_top,int x_right,int y_bottom)
{
 in.x.ax=7;
 in.x.cx=x_left;
 in.x.dx=x_right;
 int86(51,&in,&out);
 in.x.ax=8;
 in.x.cx=y_top;
 in.x.dx=y_bottom;
 int86(51,&in,&out);
}
/*include this code just after graphics initialization*/
void main()
{
mouse_ini();
}
that's all folks for now
so long for next time for a new and exiting post.
waiting for you comment's.
download the example its very helpful for those who want to see cursor in c++ 3.0

Labels: , , , , , , , , ,

2 Comments:

At August 16, 2014 at 7:07 AM , Anonymous Anonymous said...

sir,
what if want to make two buttons simultaneously . then how to chose one of them.?

 
At April 18, 2016 at 7:17 AM , Anonymous Anonymous said...

Thanks a lot sir..

 

Post a Comment

Subscribe to Post Comments [Atom]

Links to this post:

Create a Link

<< Home