PDA

View Full Version : mouse applications



danielpaval
01-01-2003, 11:54 AM
Does anyone know a good tutorial about how to use the mouse in DOS applications?

Wunna
01-01-2003, 06:22 PM
#include <stdio.h>
#include <conio.h>
#include <graphics.h>
void LoadMouse()
{
asm{
mov ax,0x0000;
int 0x33;

}


}
void Beep()
{
asm{
mov ax,0x0e07;
xor bx,bx;

int 0x10;

}

}
void Show()
{
asm{
mov ax,0x0001;
int 0x33;
}


}
void Hide()
{
asm{
mov ax,0x0002;
int 0x33;
}


}
main()
{
int gD=DETECT,gM;
initgraph(&gD,&gM,"C:\\TC\\BGI");
LoadMouse();
Show();
getch();
Hide();
Beep();
getch();
Show();
getch();
}

Hammer
01-01-2003, 07:33 PM
Use code tags when posting code (http://www.cprogramming.com/cboard/showthread.php?s=&threadid=26463)

danielpaval
01-02-2003, 07:29 AM
Thanks for the code Wunna. I'm sure that it's correct but when I try to compile it there is a linker error:

"Undefined simbol _initgraph in module new.cpp"

The file I'm writting is new.cpp in C:\Borlandc\Bin, the current directory.

Of course I changed the call of the initgraph function in Wunna's code with initgraph(&gD,&gM,"C:\\BORLANDC\\BGI"); but the error still appears.

What can I do?

I still want a tutorial about mouse use in C++ (DOS mode)...

Thanks.

ronin
01-02-2003, 06:20 PM
Do you link in graphics.lib? I dunno which version of Borland you're using, but older versions required that runtime library to be included. Check your project and check off that option.

Use a dos based compiler for int 10h operations or you'll have problems with protection faults at runtime.

vasanth
01-15-2003, 10:08 AM
Hi...
Try using my class... It has more options..





# include <conio.h>
# include <string.h>
# include <fstream.h>
# include <stdio.h>
# include <dos.h>
# include <graphics.h>




//Mouse Class starting//
//------------------------------------------------------------------------------------------------------//
int graphicsdriver,gm,maxx,maxy,graphicsx,graphicsy,bu tton;
static union REGS inregs, outregs;
static struct SREGS segregs;
typedef enum {ARROW, HAND, HOUR_GLASS, PEN} cursor_type;
union REGS graphicsi,graphicso;

static const int cursor_data[] = {
/* standard arrow cursor */
1, 0, /* x, graphicsy of hotpoint */

0x9FFF, 0x8FFF, 0x87FF, 0x83FF, /* SCREEN MASK (16 words) */
0x81FF, 0x80FF, 0x807F, 0x803F, /* Each word marks 16 pixels (lowest */
0x801F, 0x800F, 0x80FF, 0x887F, /* bit is rightmost pixel). The mask */
0x987F, 0xBC3F, 0xFC3F, 0xFE7F, /* is AND_ed with screen contents. */

0x0000, 0x2000, 0x3000, 0x3800, /* CURSOR MASK (16 words) */
0x3C00, 0x3E00, 0x3F00, 0x3F80, /* After the screen is AND_ed with the */
0x3FC0, 0x3E00, 0x3600, 0x2300, /* screen mask it is XOR_ed with this */
0x0300, 0x0180, 0x0180, 0x0000, /* cursor mask. */

/* pointing hand cursor */
2, 1,
0xCFFF, 0x87FF, 0x87FF, 0x87FF, 0x813F, 0x8007, 0x8003, 0x8003,
0x8003, 0x8003, 0x8003, 0x8003, 0x8007, 0xC007, 0xC007, 0xE03F,

0x0000, 0x3000, 0x3000, 0x3000, 0x3000, 0x36C0, 0x36D8, 0x36D8,
0x36D8, 0x36D8, 0x3FF8, 0x3FF8, 0x3FF0, 0x1FF0, 0x1FC0, 0x0000,

/* hourglass cursor */
7, 7,
0xC003, 0xC003, 0xC003, 0xC003, 0xE007, 0xF00F, 0xF81F, 0xFC3F,
0xFC3F, 0xF81F, 0xF00F, 0xE007, 0xC003, 0xC003, 0xC003, 0xC003,

0x0000, 0x1FF8, 0x0000, 0x1FF8, 0x0D50, 0x06A0, 0x0340, 0x0180,
0x0180, 0x03C0, 0x07E0, 0x0D50, 0x1AA8, 0x0000, 0x1FF8, 0x0000,

/* pen cursor */
0, 15,
0xFFF1, 0xFFE0, 0xFFC0, 0xFF80, 0xFF01, 0xFE03, 0xFC07, 0xF80F,
0xF01F, 0xE03F, 0xE07F, 0xC0FF, 0xC1FF, 0x87FF, 0x1FFF, 0x3FFF,

0x0000, 0x000E, 0x001C, 0x003A, 0x0074, 0x00E8, 0x01D0, 0x03A0,
0x0740, 0x0E80, 0x0D00, 0x1A00, 0x1800, 0x2000, 0x4000, 0x0000

};



static class mouse{
protected:

public:
mouse();
initmouse();
showmouseptr();
getpoint();
restrictmouseptr(int,int,int,int);
getmousepos(int*,int*,int*);
void setCursor(cursor_type cursor);
void disableMouse();


};

mouse::mouse()
{
setviewport(1,57,maxx-1,maxy-1,1);
restrictmouseptr(1,1,maxx-1,maxy-1);
}

mouse::getpoint()
{
int x,y,button;
getmousepos(&button,&x,&y);
if(x>30 && x<430 && y>70 && y<470)
{
int num=0;
x=x-30;
y=y-70;
x=x/50;
y=y/50;
num=y*8;
num=num+x;
return num;
}
return -1;
}



mouse::initmouse()
{
graphicsi.x.ax=0;
int86(0x33,&graphicsi,&graphicso);
return(graphicso.x.ax);
}

mouse::showmouseptr()
{
graphicsi.x.ax=1;
int86(0x33,&graphicsi,&graphicso);

return 0;
}

mouse::restrictmouseptr(int x1,int y1,int x2,int y2)
{
graphicsi.x.ax=7;
graphicsi.x.cx=x1;
graphicsi.x.dx=x2;
int86(0x33,&graphicsi,&graphicso);

graphicsi.x.ax=8;
graphicsi.x.cx=y1;
graphicsi.x.dx=y2;
int86(0x33,&graphicsi,&graphicso);
return 0;
}

mouse::getmousepos(int *button,int *x, int *graphicsy)
{
graphicsi.x.ax=3;
int86(0x33,&graphicsi,&graphicso);
*button=graphicso.x.bx;
*x=graphicso.x.cx;
*graphicsy=graphicso.x.dx;
return 0;
}


void mouse::setCursor(cursor_type cursor) { /* types defined in header */
inregs.x.ax = 9;
inregs.x.bx = *(cursor_data + 34 * cursor); /* x hotpoint */
inregs.x.cx = *(cursor_data + 34 * cursor + 1); /* graphicsy hotpoint */
inregs.x.dx = FP_OFF(cursor_data + 34 * cursor + 2); /* pointer to */
segregs.es = FP_SEG(cursor_data + 34 * cursor + 2); /* the masks */
int86x(0x33, &inregs, &outregs, &segregs);
}


void mouse::disableMouse()
{
inregs.x.ax = 2;
int86(0x33,&inregs,&outregs);
}

//------------------------------------------------------------------------------------------------------//
//Mouse Class End//







The functions available are
mouse();
initmouse();
showmouseptr();
getpoint();
restrictmouseptr(int,int,int,int);
getmousepos(int*,int*,int*);
void setCursor(cursor_type cursor);
void disableMouse();


You can even set the cursor image like pointer, hand, hour glass etc...


For this to work you have to run showmouseptr in a loop....
like



int mian()
{
mouse m1;
m1.initmouse();

int x,y,button=0;
while(button!=2)
{
showmouseptr();
getmousepos(&button,&x,&y);
}

return 0;
}

VirtualAce
01-24-2003, 10:19 PM
Using the mouse in DOS is very easy. I had a thread on here at one time that showed about 9 sub-functions. I won't go into that many here but you really only need 5 functions. Here they are.


struct Mousestruct
{
int x;
int y;
int button_down;
}Mouse;


void ResetMouse(void)
{
union REGS regs;
regs.x.ax=0x0000;
int86(0x33,&regs,&regs);
}

void ShowMouse(void)
{
union REGS regs;
regs.x.ax=0x0001;
int86(0x33,&regs,&regs);
}

void HideMouse(void)
{
union REGS regs;
regs.x.ax=0x0002;
int86(0x33,&regs,&regs);
}

void GetCoords(void)
{
union REGS regs;
regs.x.ax=0x0003;
int86(0x33,&regs,&regs);
Mouse.x=regs.x.cx; //shift right by 1 if in 320x200 mode
Mouse.y.=regs.x.dx;
Mouse.button_down=regs.x.bx;
}

void PutMouse(int x,int y)
{
union REGS regs;
regs.x.ax=0x0004;
regs.x.cx=x;
regs.x.dx=y;
int86(0x33,&regs,&regs)
}


You can also do this in assembly but you probably won't gain any speed since this is a very simple task to perform. I have 2 libraries for the mouse - 1 in C and 1 in pure asm. If you are using assembly with C/C++ you must declare your functions like this.

extern "C" void ResetMouse(void)


For example let's call ResetMouse from C.

In assembly:

.model large
.code

public _ResetMouse

;No arguments so no need to push bp mov bp,sp ... pop bp
_Reset Mouse proc
mov ax,0000h
int 33h
ret
_ResetMouse endp


end


In C:

extern "C" void ResetMouse(void)

int main(void)
{
ResetMouse();
return 0;
}



Hope this helps. Some of the above posts looked a bit confusing - really all you need are the 5 functions that I provided you with.

Reset mouse and driver - sub function 00h

Show mouse cursor - sub function 01h

Hide mouse cursor - sub function 02h

Get mouse button data (also returns cursor info) - sub function 03h

Position mouse cursor - sub function 04h