How can I get the mouse position in a console program?

前端 未结 3 1467
既然无缘
既然无缘 2020-12-03 15:13

How can I get the mouse click position in C++ in a Windows console program? (A variable that returns the position of the mouse when clicked)

I want to draw a menu wi

3条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-03 16:00

    You'll need to use the *ConsoleInput family of methods (peek, read, etc). These operate on the console's input buffer, which includes keyboard and mouse events. The general strategy is:

    1. wait on the console's input buffer handle (ReadConsoleInput)
    2. determine the number of waiting events (lpNumberOfEventsRead)
    3. handle them as you see fit (i.e. MOUSE_EVENT and MOUSE_EVENT_RECORD)

    You'll have to indicate that you want to retrieve mouse input using SetConsoleMode first though, as illustrated in this MSDN article.

提交回复
热议问题