使用事件处理程序

时间:2009-01-29 16:30:12

标签: c++ c events mouse handler

有没有办法在c / c ++中使用鼠标作为事件处理程序我是一名学生并且有一个迷你项目要做,即时制作蛇和梯子游戏(着名的棋盘游戏)并尝试用基本游戏制作borland c ++编译器使用一个名为graphics.h的头文件,这是非常基本的,并提供640 X 480 res的输出,所以我想知道是否有可能使用鼠标作为事件处理程序(我没有经验)控制板上的硬币。

我在二年级工程学(分支机构科学)

事先得到帮助!

2 个答案:

答案 0 :(得分:0)

您可以使用Win32编程(在Visual Studio中)使用鼠标和键盘事件。

是否需要使用borland c ++。

我认为类似的API存在于borland c ++中。

有关使用Win32编程在Visual Studio中进行事件处理的更多信息,可以参考http://www.functionx.com/win32/index.htm

答案 1 :(得分:0)

我不确定您碰巧有哪个版本的graphics.h,但有getmouseygetmouseyclearmouseclickgetmouseclick等函数。 See this了解一些可能适合您的文档。

它表明您可以使用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( );
}