Console Application GUIs
How are GUIs like this made, but in console applications?
There are m开发者_JAVA技巧any examples, such as alsamixer:
The Curses library can be used to create such user interfaces. Here's an introduction to NCurses - the most well-known Curses implementation.
Most console platforms have functions like goto(x, y) to position a cursor somewhere on the screen.
This can be used to "paint" the screen with whatever ascii characters / colors that you want.
Termbox is an interesting alternative to ncurses, that might also be of interest to you.
On DOS there were a good many different 'GUI' APIs available before Windows came along, all of which basically hid the underlying VGA calls you needed to make if you wanted to code directly. Ones I personally worked with included TurboPascal and Foxpro. There was even, briefly, a Visual Basic for DOS which included a similar API. As well as outputting text these APIs would often include such functionality as drop-down menus, drawing simple boxes, and shading. Some of the more complex ones would even have character-based 'Windows' you could drag around the screen with the cursor.
VGA could be accessed in either Text (very much like a mainframe terminal where you effectively just output a character at row/column) or Graphics mode which was much more similar to GDI (although you had to do everything yourself, was slow, and rarely used for anything except early paint and cad programs). Your example is text-mode.
On Unix, where such considerations are much more current, the standard is to use Curses, which is a similar text-mode API which hides underlying terminal characteristics.
Libraries such as Curses do it much the same way that window managers run graphical environments. Controls such as windows, buttons, checkboxes are all defined by the library.
Instead of WriteLining to the screen, you're writing text to a window or setting the text of some control. The library keeps track of windows, overlap, border effects, drop shadows, drag behavior (if the mouse is supported). It builds up a character buffer of characters, foreground, and background colors.
The goto(x, y) example posted would work, but if the implementation did this for each character, the performance would lag noticeably. To get the quickness, when you make a change to some text or other properties, the library knows the rectangular regions that need to be updated from the character buffer and writes the new character data for that region to the screen in one chunk with optimized calls to the video drivers.
Unlike with basic console apps which just echo every printable character to the screen, keyboard input in these advanced apps is intercepted and such characters are more explicitly written to (or not written to, in the case of your app's hotkeys) the right window or whatever.
I wish .Net had a whole Console windowing library built in. ... [Sigh] I think those old console-style apps look pretty sweet.
精彩评论