I have written a program in turbo cpp, which uses mouse for a basic GUI. I have also written a function that determines if the mouse has been clicked on a certain text displayed at a certain position. Everything works fine on the first run. But when i run the program, a second time a problem arises. Even when the mouse is just passed over an option(not clicking) it gets selected and the next page is displayed. Thanks again for those who answers. I am also attaching the code for mouse functions..
union REGS in,out;
int callmouse()
{
in.x.ax=1;
int86(51,&in,&out);
return 1;
}
void mouseposi(int &xpos,int &ypos,int &click)
{
in.x.ax=3;
int86(51,&in,&out);
click=out.x.bx; //CLICK ==1,IF LEFT BUTTON PRESSED
xpos=out.x.cx; //CLICK ==2,IF RIGHT BUTTON PRESSED
ypos=out.x.dx;
}
int mousehide()
{
in.x.ax=2;
int86(51,&in,&out);
return 1;
}
void setposi(int xpos,int ypos)
{
in.x.ax=4;
in.x.cx=xpos;
in.x.dx=ypos;
int86(51,&in,&out);
}
void restrictmouseptr(int x1,int y1,int x2,int y2)
{
in.x.ax=7;
in.x.cx=x1;
in.x.dx=x2;
int86(51,&in,&out);
in.x.ax=8;
in.x.cx=y1;
in.x.dx=y2;
int86(51,&in,&out);
}
int mouseclick(int x_org,int y_org,char str[], int x_cur, int y_cur, int cl)
{
static int flag=0;
int y_diff=y_cur-y_org;
int x_diff=x_cur-x_org;
if((y_diff>=0 && y_diff<=textheight(str))&&(x_diff>=0 && x_diff<=textwidth(str)))
{
if(flag==0)
{ int oldcolor=getcolor();
setcolor(15);
outtextxy(x_org,y_org,str);
setcolor(oldcolor);
flag=1;
}
if(cl!=1)
return 0; //RETURNS 0 IF LEFT BUTTON IS NOT PRESSED
else
{
mousehide();
return 1; //RETURNS 1 IF X AND Y COORDINATES ARE
//WITHIN THEIR EXTREMITIES.
}
}
else if(flag==1);
{
setcolor(11);
flag=0;
outtextxy(x_org,y_org,str);
return 0;
}
}
I do not see the architecture of your program as the important stuff is missing. I would expect you got a list of objects per page with their positions,size,labels,color etc. in form of some arrays of
struct
and loop through that for booth rendering and mouse handling. Something like thisjust ignore the VCL stuff
TCanvas,Graphics::TBitmap
so ignore lines starting withbmp->
orscr->
(or port them into your graphics).not sure if it helps but I just dig my ancient mine sweep game with partial solver using mouse in Turbo C++. It looks like this:
Here Turbo C++ source for it:
Sorry it is not much commented. Left mouse button sweeps and right mouse button add flag (expecting bomb). If you need help understanding the VGA gfx part then see:
If you look at the
main
then you see I got one "infinite" loop redrawing the screen and handling mouse events + game logic. I expect you should have something similar. The difference is that my objects are in grid so the selection is done just by scaling/offsetting mouse position.In your case you should have list of objects and should loop through them and find the one closest to mouse and not too far ... similarly like in the Drag&Drop example link at the top of Answer.
Also here another related QA: