سؤال

هل هناك طريقة لاستخدام الماوس كمعالج للأحداث في c/c++، أنا طالب ولدي مشروع صغير لأقوم به، أقوم بإنشاء لعبة على الثعابين والسلم (لعبة اللوحة الشهيرة) وأحاول صنعها باستخدام مترجم borland c++ الأساسي العمل باستخدام ملف رأس يسمى graphics.h، وهو أساسي جدًا ويعطي مخرجات بدقة 640 × 480، لذلك كنت أتساءل عما إذا كانت هناك إمكانية لاستخدام الماوس كمعالج للأحداث (ليس لدي أي خبرة فيه) للتحكم فوق العملات المعدنية الشاحبة الموجودة على السبورة.

انا في السنة الثانية هندسة (فرع علوم حاسوب)

شكرا مقدما للمساعدة!

هل كانت مفيدة؟

المحلول

ولست متأكدا أي إصدار من 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).

هل هو شرط لاستخدام بورلاند C++.

أعتقد أن واجهات برمجة التطبيقات المماثلة موجودة في borland c++.

يمكنك الرجوع http://www.functionx.com/win32/index.htm لمزيد من المعلومات حول معالجة الأحداث في Visual Studio باستخدام برمجة Win32.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top