LinuxUnix Terminal Graphic Library CURSES

zhaozj2021-02-16  100

Introduction to the Linux / Unix Terminal Graphic Library CURSES As the popularity of computer knowledge, more and more computer enthusiasts begin to understand and use Linux .. The relaxed freedom of Linux gives us different feelings.

But we cannot meet the basic commands and KDE, GNOME and other user interfaces. What should we do? Programming, right, programming! For program enthusiasts, Linux has a good programming environment: GCC (GNU Compiler Collection) can compile C Many languages ​​such as C , Java, and there are many libraries in the Linux environment. Understand some of these libraries, will add many useful tools to your programming toolbox. Today, let us know Curses --- It is not a medieval witch's spell, but a graphics function library widely applied under Linux / Unix. I have learned TC2.0 before I remember that there is a graphics library BGI in TC (Borland Graphics Interface, Remember the header file :). Use it we can draw the user interface and beautiful graphics under DOS. And Linux / UNIX programming is more than "Cool", as if Linux programming It is carried out under the black terminal. It is true that many Linux masters like working in terminal way, familiar with some commands, such work mode is still very high. But long-term look at the black screen It is inevitable that people are tired, especially like I have contacted Linux rookies :) Is there a tool to make us a good graph in Linux? The answer is sure, it is the name of Curses! Curses originated from "Cursor Optimization", the cursor optimization. It was first written by Bill Joy and Ken Arnold, US Berkeley University, used to handle a game Rogue screen display. Later, Bell Laboratory's Mark Horton was re-written in System III UNIX. Now almost all UNIX, the Linux operating system has a Curses function library, and Curses also joined the mouse support, some menus and panel processing. It can be said that Curses is a non-two choice for Linux terminal graphics programming (such as famous The text editor VI is based on CURSES-based) OK, gossip less, now we start to enter the topic: First, we should understand that the screen mode used in the terminal is based on the text. So before starting using Curses, you need to use inIitsCR () Function Initialization screen. Corresponding, the program ends need to call the endwin (); function to close the Curses state. With this concept, we can write the famous "Hello, World!" Curses version: / * -------------------------------------------------- --------------- A Very Simple Example of Curses Programming Coder: Jellen Date: 3-26-2004 ------------------ --------------------------------------------- * / #include int main () {initscr (); box (stdscr, acs_vline, acs_hline); / * Draw A Box * / Move (lines / 2, cols / 2); / * Move the cursor to the center * / WaddStr (stdscr, "hello, world!"); Refresh (); getch (); endwin (); return 0;} Oh, just forgot to say. We call Curses library generally use C language (not strange , C can be said to be Linux's official language, but you can also use C

Or Python and other languages, this is not a need to save this program as hello.c but don't worry with gcc -o hello hello.c (I know you will use gcc :) that can't pass Because the Curses library is not on the standard path, we have to add -lcurses connection options, like this: gcc -o hello hello.c -lcurses now you use ./Hello runs a program, do you have anything? The desired window. Now let's take a row to analyze the code. # Include / * This is the header file that must contain each CURSES program, indicating the use of the CURSES library * / then the first sentence in the main function INITSCR (); initialized the screen, starting to enter the Curses graphic mode. In fact, we don't have its own window now, use the standard screen stdscr (就 里 里 里 里 里 输入 输入 输入 输入一 concept), it is The computer screen in front of us (but now black-black :) The following sentence: box (stdscr, acs_vline, acs_hline); Draw a box. We have a "form" feeling. STDSCR is the standard screen, ACS_VLINE and ACS_HLINE represent the basic elements on both sides of the box, you can also replace it with '|' and '-', but there may be no ACS_VLINE, ACS_HLINE looks good. Move (lines / 2, cols / 2); Waddstr (stdscr, " Hello, World! "); These two sentences are moving the cursor to the screen, then output our" Hello, World! "LINES and CURS is the macro defined by the CURSES, represent the maximum number of rows and columns of the current screen. Waddstr The function of the function is to print strings "Hello, World!" Screen (we see) and logical screen (in memory), and we call the function when we call the function when calling the function, which is not a logical screen when the STDSCR is printed. It will be displayed on the current physical screen. So now there is still nothing on the screen, you need to call REFRESH () to display our changes to the logical screen in the physical screen (display). Then use getCh () to pause the screen. Finally, the endwin () ends CURSES, restores the original screen. Well, we have completed the first example analysis. Is it very simple? But if you are not satisfactory, the screen is still the same, there is no color To add color? That is also very easy: first use the start_color () function to open color mode, then set the color we want to do. The color in Curses is paired, you want a background color to a foreground. Use it before use INIT_PAIR () initializes. For example, init_pair (1, color_blue, color_green); set a set of colors, color_blue is the foreground color, color_green is the background color. 1 is a background color. 1 is a tag number (used for other functions to use) Let's take a look Simple example: / * -------------------------------------------------------------------------------------------------------------------------------------------------- ---------------- A Simple Curses Color Demo Program CODER: Jellen Date: 3-26-2004 ---------------- --------------------------------------------- * / #include < Curses.h> int main () {initscr (); / * Initialization screen * / if (start_color () == ok) / * Open color * / {init_pair (1, color_red, color_green); / * Create a color pair * / Attron (color_pair (1)); / * Open Character Output Color * / Move (Lines / 2, Cols / 2); WaddStr (stdscr, "

YET ANOTHER HELLO, World! "); Attroff (color_pair (1)); / * Close color display * / refresh ();} else {WaddStr (stdscr," can not init color "); refresh ();} endwin ); / * Close the CURSES status * / return 0;} This program If you save to Color.c, you can compile this: gcc -o color color.c -lcurses use ./color run, is there a few colors Word? Oh, although it is not good, I can write beautiful procedures with Curses in the future. Ok, I said so much, I hope that I will not cause public anger :) But I want to pass me This essay, you must have a general understanding of Curses. If you are interested in curses, you can search some information to see some information. However, if you find something good, don't forget to tell everyone Ah. This is the spirit of sharing. The following two articles are what I feel better. You can take a look: http://www.chinalinuxuxpub.com/doc/pro/curses1.html [This is a entry Classic Good Articles] http://www-900.ibm.com/developerWorks/cn/linux/guitkit/curs/hanoi/index.shtml [Here author gives us a demonstration to write a small game with Curses] [full text] - ----------------- Jellen 2004-3-26: This is my first time in the 9CBS articles, something is very difficult to read, I hope everyone can understand. I am a Linux's beginners and enthusiasts, I hope to make some like-minded friends: jellen_2001@163.com author blog:

http://blog.9cbs.net/jellen/

转载请注明原文地址:https://www.9cbs.com/read-10673.html

New Post(0)