题
有没有使用鼠标在C / C ++ IM学生的事件处理程序的方式,得到了一个小型的项目做,即时通讯让蛇和梯子游戏(著名的棋盘游戏),并试图用基本使它用Borland C ++编译器称为graphics.h中一个头文件,这是非常基本的,并给出了640 X 480个RES输出工作,所以我想知道是否有使用小鼠作为一个事件处理程序(关于哪些我没有的experiance)的方法可行以具有过主板上的在线播放硬币控制。
我在第二年工程(分支计算机科学)
THX提前的帮助!
解决方案
我不知道它的你碰巧graphics.h中有版本,但有功能getmousey
,getmousey
,clearmouseclick
和getmouseclick
。 参见本一些文档可能为你工作。
据appeas,您可以使用registermousehandler
和回调函数来执行基于事件的编程的一些水平。下面是我给你的文件的样本。
// The click_handler will be called whenever the left mouse button is
// clicked. It checks copies the x,y coordinates of the click to
// see if the click was on a red pixel. If so, then the boolean
// variable red_clicked is set to true. Note that in general
// all handlers should be quick. If they need to do more than a little
// work, they should set a variable that will trigger the work going,
// and then return.
bool red_clicked = false;
void click_handler(int x, int y)
{
if (getpixel(x,y) == RED)
red_clicked = true;
}
// Call this function to draw an isosoles triangle with the given base and
// height. The triangle will be drawn just above the botton of the screen.
void triangle(int base, int height)
{
int maxx = getmaxx( );
int maxy = getmaxy( );
line(maxx/2 - base/2, maxy - 10, maxx/2 + base/2, maxy - 10);
line(maxx/2 - base/2, maxy - 10, maxx/2, maxy - 10 - height);
line(maxx/2 + base/2, maxy - 10, maxx/2, maxy - 10 - height);
}
void main(void)
{
int maxx, maxy; // Maximum x and y pixel coordinates
int divisor; // Divisor for the length of a triangle side
// Put the machine into graphics mode and get the maximum coordinates:
initwindow(450, 300);
maxx = getmaxx( );
maxy = getmaxy( );
// Register the function that handles a left mouse click
registermousehandler(WM_LBUTTONDOWN, click_handler);
// Draw a white circle with red inside and a radius of 50 pixels:
setfillstyle(SOLID_FILL, RED);
setcolor(WHITE);
fillellipse(maxx/2, maxy/2, 50, 50);
// Print a message and wait for a red pixel to be double clicked:
settextstyle(DEFAULT_FONT, HORIZ_DIR, 2);
outtextxy(20, 20, "Left click in RED to end.");
setcolor(BLUE);
red_clicked = false;
divisor = 2;
while (!red_clicked)
{
triangle(maxx/divisor, maxy/divisor);
delay(500);
divisor++;
}
cout << "The mouse was clicked at: ";
cout << "x=" << mousex( );
cout << " y=" << mousey( ) << endl;
// Switch back to text mode:
closegraph( );
}
其他提示
您可以使用Win32编程(在Visual Studio)使用鼠标和键盘事件。
时它使用Borland要求C ++。
我认为相似的API是否有在用Borland C ++。
可以参考 http://www.functionx.com/win32/index.htm 关于使用Win32编程在Visual Studio中事件处理的详细信息。
不隶属于 StackOverflow