Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
112 views
in Technique[技术] by (71.8m points)

c++ - Mouse program in Turbo CPP

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;
}
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 this

just ignore the VCL stuff TCanvas,Graphics::TBitmap so ignore lines starting with bmp-> or scr-> (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:

minesweep

Here Turbo C++ source for it:

//===========================================================================
//=== Includes: =============================================================
//===========================================================================
#include <stdlib.h>
//===========================================================================
//=== Types: ================================================================
//===========================================================================
typedef unsigned char byte;
typedef unsigned int  word;
typedef unsigned long dword;
//===========================================================================
//=== Headers: ==============================================================
//===========================================================================
void initscr();         // 320*200*256, BW palette
void clrscr();          // clear  scr
void rfsscr();          // vga << scr
void box(word x,word y,word a); // draw box a <0..cfree> at x,y
void boardint();        // init mines, clear board map
void boardprn();        // draw board map
void mouseint();        // init mouse
void mousetst();        // test mouse (x,y,buttons)
void numprn(word x,word y,word n);  // write number n <0..9999> at x,y

void boardtst0(int x,int y,int &n); // sub test count number of mines around
void boardtst(int x,int y);     // test number of mines around if error then boardend;
void boardend();            // game over (lose)
void boardwin();            // game over (win)
int  boardopt0(int x,int y);
void boardopt1(int x,int y,int &c);
int  boardopt2(int x,int y);
void boardopt3(int x,int y,int &c);
void boardopt();            // optimized search
//===========================================================================
//=== Global data: ==========================================================
//===========================================================================
const   c0=25; // used colors
const   c1=30;
const   c2=20;
const   c3=40;
const   c4=34;
const   c5=35;
const   c6=5;
const   cfree=13;

const   boardx=30; // board size
const   boardy=16;
const   minesn=99; // number of mines

const   boardx0=(320-(10*boardx))/2;
const   boardy0=(190-(10*boardy))/2+10;



byte        board[32][20];
byte        mines[32][20];
word far    *pit;

long        xtime0,xtime;
int     flags;


word        scrseg,scrofs;
word        mousex=0,mousey=0,
        mousel=0,mouser=0,
        mousel0=0,mouser0=0,
        mousebx=255,mouseby=255;
byte far    scr[64000],*vga; // 320x200x8bpp backbuffer and VGA screen

byte far    mouset[10][10]= // mouse cursor
        {
        1,1,0,0,0,0,0,0,0,0,
        1,1,1,1,0,0,0,0,0,0,
        1,1,1,1,1,1,0,0,0,0,
        1,1,1,1,0,0,0,0,0,0,
        1,1,1,1,0,0,0,0,0,0,
        1,0,0,1,1,0,0,0,0,0,
        1,0,0,0,1,0,0,0,0,0,
        0,0,0,0,0,0,0,0,0,0,
        0,0,0,0,0,0,0,0,0,0,
        0,0,0,0,0,0,0,0,0,0
        };

byte far    txr[14*100]= // board tiles
        {
        c2,c1,c1,c1,c1,c1,c1,c1,c1,c1,
        c1,c0,c0,c0,c0,c0,c0,c0,c0,c2,
        c1,c0,c0,c0,c5,c5,c0,c0,c0,c2,
        c1,c0,c0,c5,c0,c0,c5,c0,c0,c2,
        c1,c0,c0,c5,c0,c0,c5,c0,c0,c2,
        c1,c0,c0,c5,c0,c0,c5,c0,c0,c2,
        c1,c0,c0,c5,c0,c0,c5,c0,c0,c2,
        c1,c0,c0,c0,c5,c5,c0,c0,c0,c2,
        c1,c0,c0,c0,c0,c0,c0,c0,c0,c2,
        c1,c2,c2,c2,c2,c2,c2,c2,c2,c2,

        c2,c1,c1,c1,c1,c1,c1,c1,c1,c1,
        c1,c0,c0,c0,c0,c0,c0,c0,c0,c2,
        c1,c0,c0,c0,c0,c3,c0,c0,c0,c2,
        c1,c0,c0,c0,c3,c3,c0,c0,c0,c2,
        c1,c0,c0,c0,c0,c3,c0,c0,c0,c2,
        c1,c0,c0,c0,c0,c3,c0,c0,c0,c2,
        c1,c0,c0,c0,c0,c3,c0,c0,c0,c2,
        c1,c0,c0,c0,c3,c3,c3,c0,c0,c2,
        c1,c0,c0,c0,c0,c0,c0,c0,c0,c2,
        c1,c2,c2,c2,c2,c2,c2,c2,c2,c2,

        c2,c1,c1,c1,c1,c1,c1,c1,c1,c1,
        c1,c0,c0,c0,c0,c0,c0,c0,c0,c2,
        c1,c0,c0,c0,c3,c3,c0,c0,c0,c2,
        c1,c0,c0,c3,c0,c0,c3,c0,c0,c2,
        c1,c0,c0,c0,c0,c0,c3,c0,c0,c2,
        c1,c0,c0,c0,c3,c3,c0,c0,c0,c2,
        c1,c0,c0,c3,c0,c0,c0,c0,c0,c2,
        c1,c0,c0,c3,c3,c3,c3,c0,c0,c2,
        c1,c0,c0,c0,c0,c0,c0,c0,c0,c2,
        c1,c2,c2,c2,c2,c2,c2,c2,c2,c2,

        c2,c1,c1,c1,c1,c1,c1,c1,c1,c1,
        c1,c0,c0,c0,c0,c0,c0,c0,c0,c2,
        c1,c0,c0,c0,c3,c3,c0,c0,c0,c2,
        c1,c0,c0,c4,c0,c0,c3,c0,c0,c2,
        c1,c0,c0,c0,c0,c3,c0,c0,c0,c2,
        c1,c0,c0,c0,c0,c0,c3,c0,c0,c2,
        c1,c0,c0,c4,c0,c0,c3,c0,c0,c2,
        c1,c0,c0,c0,c3,c3,c0,c0,c0,c2,
        c1,c0,c0,c0,c0,c0,c0,c0,c0,c2,
        c1,c2,c2,c2,c2,c2,c2,c2,c2,c2,

        c2,c1,c1,c1,c1,c1,c1,c1,c1,c1,
        c1,c0,c0,c0,c0,c0,c0,c0,c0,c2,
        c1,c0,c0,c0,c3,c0,c0,c0,c0,c2,
        c1,c0,c0,c0,c3,c0,c0,c0,c0,c2,
        c1,c0,c0,c3,c0,c0,c0,c0,c0,c2,
        c1,c0,c0,c3,c0,c3,c0,c0,c0,c2,
        c1,c0,c0,c3,c3,c3,c3,c0,c0,c2,
        c1,c0,c0,c0,c0,c3,c0,c0,c0,c2,
        c1,c0,c0,c0,c0,c0,c0,c0,c0,c2,
        c1,c2,c2,c2,c2,c2,c2,c2,c2,c2,

        c2,c1,c1,c1,c1,c1,c1,c1,c1,c1,
        c1,c0,c0,c0,c0,c0,c0,c0,c0,c2,
        c1,c0,c0,c3,c3,c3,c3,c0,c0,c2,
        c1,c0,c0,c3,c0,c0,c0,c0,c0,c2,
        c1,c0,c0,c3,c3,c3,c0,c0,c0,c2,
        c1,c0,c0,c0,c0,c0,c3,c0,c0,c2,
        c1,c0,c0,c3,c0,c0,c3,c0,c0,c2,
        c1,c0,c0,c0,c3,c3,c0,c0,c0,c2,
        c1,c0,c0,c0,c0,c0,c0,c0,c0,c2,
        c1,c2,c2,c2,c2,c2,c2,c2,c2,c2,

        c2,c1,c1,c1,c1,c1,c1,c1,c1,c1,
        c1,c0,c0,c0,c0,c0,c0,c0,c0,c2,
        c1,c0,c0,c0,c3,c3,c4,c0,c0,c2,
        c1,c0,c0,c3,c0,c0,c0,c0,c0,c2,
        c1,c0,c0,c3,c3,c3,c0,c0,c0,c2,
        c1,c0,c0,c3,c0,c0,c3,c0,c0,c2,
        c1,c0,c0,c3,c0,c0,c3,c0,c0,c2,
        c1,c0,c0,c0,c3,c3,c0,c0,c0,c2,
        c1,c0,c0,c0,c0,c0,c0,c0,c0,c2,
        c1,c2,c2,c2,c2,c2,c2,c2,c2,c2,

        c2,c1,c1,c1,c1,c1,c1,c1,c1,c1,
        c1,c0,c0,c0,c0,c0,c0,c0,c0,c2,
        c1,c0,c0,c3,c3,c3,c3,c0,c0,c2,
        c1,c0,c0,c0,c0,c0,c3,c0,c0,c2,
        c1,c0,c0,c0,c0,c3,c0,c0,c0,c2,
        c1,c0,c0,c0,c0,c3,c0,c0,c0,c2,
        c1,c0,c0,c0,c3,c0,c0,c0,c0,c2,
        c1,c0,c0,c0,c3,c0,c0,c0,c0,c2,
        c1,c0,c0,c0,c0,c0,c0,c0,c0,c2,
        c1,c2,c2,c2,c2,c2,c2,c2,c2,c2,

        c2,c1,c1,c1,c1,c1,c1,c1,c1,c1,
        c1,c0,c0,c0,c0,c0,c0,c0,c0,c2,
        c1,c0,c0,c0,c3,c3,c0,c0,c0,c2,
        c1,c0,c0,c3,c0,c0,c3,c0,c0,c2,
        c1,c0,c0,c0,c3,c3,c0,c0,c0,c2,
        c1,c0,c0,c3,c0,c0,c3,c0,c0,c2,
        c1,c0,c0,c3,c0,c0,c3,c0,c0,c2,
        c1,c0,c0,c0,c3,c3,c0,c0,c0,c2,
        c1,c0,c0,c0,c0,c0,c0,c0,c0,c2,
        c1,c2,c2,c2,c2,c2,c2,c2,c2,c2,

        c2,c1,c1,c1,c1,c1,c1,c1,c1,c1,
        c1,c0,c0,c0,c0,c0,c0,c0,c0,c2,
        c1,c0,c0,c0,c3,c3,c0,c0,c0,c2,
        c1,c0,c0,c3,c0,c0,c3,c0,c0,c2,
        c1,c0,c0,c0,c3,c3,c3,c0,c0,c2,
        c1,c0,c0,c0,c0,c0,c3,c0,c0,c2,
        c1,c0,c0,c4,c0,c0,c3,c0,c0,c2,
        c1,c0,c0,c0,c3,c3,c0,c0,c0,c2,
        c1,c0,c0,c0,c0,c0,c0,c0,c0,c2,
        c1,c2,c2,c2,c2,c2,c2,c2,c2,c2,

        c2,c1,c1,c1,c1,c1,c1,c1,c1,c1,
        c1,c0,c0,c0,c0,c0,c0,c0,c0,c2,
        c1,c0,c0,c6,c6,c0,c0,c0,c0,c2,
        c1,c0,c0,c6,c6,c6,c6,c0,c0,c2,
        c1,c0,c0,c6,c6,c6,c6,c0,c0,c2,
        c1,c0,c0,c6,c0,c6,c6,c0,c0,c2,
        c1,c0,c0,c6,c0,c0,c0,c0,c0,c2,
        c1,c0,c0,c6,c0,c0,c0,c0,c0,c2,
        c1,c0,c0,c0,c0,c0,c0,c0,c0,c2,
        c1,c2,c2,c2,c2,c2,c2,c2,c2,c2,

        c2,c1,c1,c1,c1,c1,c1,c1,c1,c1,
        c1,c0,c0,c0,c0,c0,c0,c0,c0,c2,
        c1,c0,c0,c0,c0,c0,c0,c0,c0,c2,
        c1,c0,c0,c0,c6,c6,c0,c0,c0,c2,
        c1,c0,c0,c6,c6,c6,c6,c0,c0,c2,
        c1,c0,c0,c6,c6,c6,c6,c0,c0,c2,
        c1,c0,c0,c0,c6,c6,c0,c0,c0,c2,
        c1,c0,c0,c0,c0,c0,c0,c0,c0,c2,
        c1,c0,c0,c0,c0,c0,c0,c0,c0,c2,
        c1,c2,c2,c2,c2,c2,c2,c2,c2,c2,

        c2,c1,c1,c1,c1,c1,c1,c1,c1,c1,
        c1,c0,c0,c0,c0,c0,c0,c0,c0,c2,
        c1,c0,c6,c0,c0,c0,c0,c6,c0,c2,
        c1,c0,c0,c6,c0,c0,c6,c0,c0,c2,
        c1,c0,c0,c0,c6,c6,c0,c0,c0,c2,
        c1,c0,c0,c0,c6,c6,c0,c0,c0,c2,
        c1,c0,c0,c6,c0,c0,c6,c0,c0,c2,
        c1,c0,c6,c0,c0,c0,c0,c6,c0,c2,
        c1,c0,c0,c0,c0,c0,c0,c0,c0,c2,
        c1,c2,c2,c2,c2,c2,c2,c2,c2,c2,

        c2,c1,c1,c1,c1,c1,c1,c1,c1,c1,
        c1,c0,c0,c0,c0,c0,c0,c0,c0,c2,
        c1,c0,c0,c0,c0,c0,c0,c0,c0,c2,
        c1,c0,c0,c0,c0,c0,c0,c0,c0,c2,
        c1,c0,c0,c0,c0,c0,c0,c0,c0,c2,
        c1,c0,c0,c0,c0,c0,c0,c0,c0,c2,
        c1,c0,c0,c0,c0,c0,c0,c0,c0,c2,
        c1,c0,c0,c0,c0,c0,c0,c0,c0,c2,
        c1,c0,c0,c0,c0,c0,c0,c0,c0,c2,
        c1,c2,c2,c2,c2,c2,c2,c2,c2,c2
        };
//===========================================================================
//=== Subroutines: ==========================================================
//===========================================================================
void initscr()
    {
    dword   h,l;
    int i;
    vga=(byte far*)0x0A0000000;
    h=(unsigned long)scr; l=h;
    l=l & 65535;
    h=h >> 16;
    scrseg=h;
    scrofs=l;
    asm {
        mov ax,0x0013
        int 0x10
        }
    for (i=0;i<256;i++) // set color palette to grayscale
    asm {
        mov dx,0x03C8
        mov ax,i
        out dx,al
        mov dx,0x03C9
        mov ax,i
        out dx,al
        out dx,al
        out dx,al
        }
    }
//===========================================================================
void clrscr()
    {
    asm {
        pusha
        push    es
        mov di,scrofs
        mov es,scrseg
        mov ax,0x1313
        mov cx,32000
        rep stosw
        pop es
        popa
        }
    }
//===========================================================================
void rfsscr()
    {
    asm {
        pusha
        push    ds
        push    es
        mov di,0
        mov si,scrofs
        mov ds,scrseg
        mov ax,0xA000
        mov es,ax
        mov cx,32000
        rep movsw
        pop es
        pop ds
        popa
        }
    }
//===========================================================================
void box(word x,word y,word a)
    {
    word    b;
    a=a*100;
    b=x+320*y;
    for (y=0;y<10;y++)
        {
        for (x=0;x<10;x++) scr[b+x]=txr[a+x];
        b+=320;
        a+=10;
        }
    }
//===========================================================================
void boardint()
    {
    word i,x,y;
    for (y=0;y<boardy;y++)
    for (x=0;x<boardx;x++)
        {
        board[x][y]=cfree;
        mines[x][y]=0;
        }
    randomize();
    for (i=0;i<minesn;i++)
        {
        x=random(boardx);
        y=random(boardy);
        if (mines[x][y]) i--;
        else mines[x][y]=1;
        }
    }
//==============

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...