المثال مأخوذ من المنتدى (vbzoom)
#include<stdiostr.h>
#include<conio.h>
#include<process.h>
#include<graphics.h>
#include<dos.h>
#define MOUSE 0x33
#define BUT1PRESSED 1
#define BUT2PRESSED 2
#define TRUE 1
#define FALSE 0
void ActivMouse()
{
/* activate mouse */
_AX=32;
geninterrupt(MOUSE);
}
int ResetMouse()
{
/* mouse reset */
_AX=0;
geninterrupt(MOUSE);
return(_AX);
}
void ShowMouse()
{
/* turn on mouse cursor */
_AX=1;
geninterrupt(MOUSE);
}
void HideMouse()
{
/* turn off mouse cursor */
_AX=2;
geninterrupt(MOUSE);
}
void ReadMouse(int *v,int *h,int *but)
{
int temp;
_AX=3;
geninterrupt(MOUSE);
// which buttons pressed: 1=left, 2=right, 3=both
temp=_BX;
*but=temp;
// horizontal coordinates
*h =_CX;
// vert coordinates
*v=_DX;
}
int main( void )
{
int gdriver = EGA, gmode=EGAHI, errorcode;
int mouseX, mouseY, mouseButton=0, currentColor=1;
int oldMouseX=0, oldMouseY=0;
char button1Down,button2Down=FALSE;
if (!ResetMouse())
{
printf("No Mouse Driver!");
return(1);
}
printf( "Instructions:\nHold down left button to draw,\n"
"Click right button to change color,\n"
"When done drawing, hit key.\n"
"Press any key to continue\n" );
getch();
initgraph(&gdriver,&gmode, "");
if (graphresult()!=grOk)
{
printf( "Graphics Init Error -- EGAVGA.BGI not in "
"current directory?" );
return(1);
}
setcolor(WHITE);
outtextxy(1,1,"COLOR");
ActivMouse();
ShowMouse();
while (!kbhit())
{
ReadMouse(&mouseY,&mouseX,&mouseButton);
if (mouseButton&BUT1PRESSED)
{
if (mouseX!=oldMouseX || mouseY!=oldMouseY)
{
// hide mouse cursor before drawing anything!
HideMouse();
if (button1Down)
line(mouseX,mouseY,oldMouseX,oldMouseY);
oldMouseX=mouseX; oldMouseY=mouseY;
ShowMouse();
}
button1Down=TRUE;
}
else
button1Down=FALSE;
if (mouseButton&BUT2PRESSED)
{
button2Down=TRUE;
}
else if (button2Down)
{
// If button 2 was down and is now up, it was
// clicked!
button2Down=FALSE;
// so change the color
++currentColor;
if (currentColor==16)
currentColor=1;
setcolor(currentColor);
HideMouse();
outtextxy(1,1,"COLOR");
ShowMouse();
}
}
closegraph();
return( 0 );
}
