⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 第十章:关于颜色系统(all about color).htm

📁 curses界面编程
💻 HTM
📖 第 1 页 / 共 3 页
字号:
<p class=MsoNormal><b style='mso-bidi-font-weight:normal'><span lang=EN-USstyle='font-size:14.0pt'>10.1</span></b><b style='mso-bidi-font-weight:normal'><spanstyle='font-size:14.0pt;font-family:宋体;mso-ascii-font-family:"Times New Roman";mso-hansi-font-family:"Times New Roman"'>基础知识(</span></b><a name=COLORBASICS><bstyle='mso-bidi-font-weight:normal'><span lang=EN-US style='font-size:14.0pt'>Thebasics</span></b></a><span style='mso-bookmark:COLORBASICS'></span><bstyle='mso-bidi-font-weight:normal'><span style='font-size:14.0pt;font-family:宋体;mso-ascii-font-family:"Times New Roman";mso-hansi-font-family:"Times New Roman"'>)</span></b><bstyle='mso-bidi-font-weight:normal'><span lang=EN-US style='font-size:14.0pt'><o:p></o:p></span></b></p><p class=MsoNormal><span style='font-family:宋体;mso-ascii-font-family:"Times New Roman";mso-hansi-font-family:"Times New Roman"'>生命中如果没有颜色的将是单调无趣的。</span><spanlang=EN-US>CURSES</span><span style='font-family:宋体;mso-ascii-font-family:"Times New Roman";mso-hansi-font-family:"Times New Roman"'>有一个非常棒的颜色处理机制。让我们通过以下这个小程序了解一下:</span></p><p class=MsoNormal><span lang=EN-US><o:p>&nbsp;</o:p></span></p><p class=MsoNormal><b style='mso-bidi-font-weight:normal'><spanstyle='font-family:宋体;mso-ascii-font-family:"Times New Roman";mso-hansi-font-family:"Times New Roman"'>例</span><span lang=EN-US>9</span></b><b style='mso-bidi-font-weight:normal'><span style='font-family:宋体;mso-ascii-font-family:"Times New Roman";mso-hansi-font-family:"Times New Roman"'>:一个简单的颜色使用例子</span><span lang=EN-US><o:p></o:p></span></b></p><p class=MsoNormal><a name=BSICO><b style='mso-bidi-font-weight:normal'><spanlang=EN-US style='font-family:Tahoma'>#include &lt;ncurses.h&gt;<o:p></o:p></span></b></a></p><p class=MsoNormal><span style='mso-bookmark:BSICO'><b style='mso-bidi-font-weight:normal'><span lang=EN-US style='font-family:Tahoma'><o:p>&nbsp;</o:p></span></b></span></p><p class=MsoNormal><span style='mso-bookmark:BSICO'><b style='mso-bidi-font-weight:normal'><span lang=EN-US style='font-family:Tahoma'>void print_in_middle(WINDOW*win, int starty, int startx, int width, char *string);<o:p></o:p></span></b></span></p><p class=MsoNormal><span style='mso-bookmark:BSICO'><b style='mso-bidi-font-weight:normal'><span lang=EN-US style='font-family:Tahoma'>int main(int argc, char*argv[])<o:p></o:p></span></b></span></p><p class=MsoNormal><span style='mso-bookmark:BSICO'><b style='mso-bidi-font-weight:normal'><span lang=EN-US style='font-family:Tahoma'>{<span style='mso-tab-count:1'>&nbsp;&nbsp;&nbsp;&nbsp; </span>initscr();<span style='mso-tab-count:3'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><spanstyle='mso-spacerun:yes'>&nbsp;&nbsp;&nbsp; </span></span></b></span><spanstyle='mso-bookmark:BSICO'><span lang=EN-US style='font-family:Tahoma'>/*</span></span><spanstyle='mso-bookmark:BSICO'><span style='font-family:宋体;mso-ascii-font-family:Tahoma;mso-hansi-font-family:"Times New Roman";mso-bidi-font-family:Tahoma'>启动</span></span><spanstyle='mso-bookmark:BSICO'><span lang=EN-US style='font-family:Tahoma'> curses</span></span><spanstyle='mso-bookmark:BSICO'><span style='font-family:宋体;mso-ascii-font-family:Tahoma;mso-hansi-font-family:"Times New Roman";mso-bidi-font-family:Tahoma'>模式</span></span><spanstyle='mso-bookmark:BSICO'><span lang=EN-US style='font-family:Tahoma'>*/<bstyle='mso-bidi-font-weight:normal'><o:p></o:p></b></span></span></p><p class=MsoNormal><span style='mso-bookmark:BSICO'><b style='mso-bidi-font-weight:normal'><span lang=EN-US style='font-family:Tahoma'><span style='mso-tab-count:1'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>if(has_colors() == FALSE)<o:p></o:p></span></b></span></p><p class=MsoNormal><span style='mso-bookmark:BSICO'><b style='mso-bidi-font-weight:normal'><span lang=EN-US style='font-family:Tahoma'><span style='mso-tab-count:1'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>{<span style='mso-tab-count:1'>&nbsp;&nbsp;&nbsp;&nbsp; </span>endwin();<o:p></o:p></span></b></span></p><p class=MsoNormal><span style='mso-bookmark:BSICO'><b style='mso-bidi-font-weight:normal'><span lang=EN-US style='font-family:Tahoma'><span style='mso-tab-count:2'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>printf(&quot;</span></b></span><spanstyle='mso-bookmark:BSICO'><span lang=EN-US style='font-family:Tahoma'>Youterminal does not support color\n<b style='mso-bidi-font-weight:normal'>&quot;);<o:p></o:p></b></span></span></p><p class=MsoNormal><span style='mso-bookmark:BSICO'><b style='mso-bidi-font-weight:normal'><span lang=EN-US style='font-family:Tahoma'><span style='mso-tab-count:2'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>exit(1);<o:p></o:p></span></b></span></p><p class=MsoNormal><span style='mso-bookmark:BSICO'><b style='mso-bidi-font-weight:normal'><span lang=EN-US style='font-family:Tahoma'><span style='mso-tab-count:1'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>}<o:p></o:p></span></b></span></p><p class=MsoNormal><span style='mso-bookmark:BSICO'><b style='mso-bidi-font-weight:normal'><span lang=EN-US style='font-family:Tahoma'><span style='mso-tab-count:1'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>start_color();<spanstyle='mso-tab-count:3'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span></b></span><spanstyle='mso-bookmark:BSICO'><span lang=EN-US style='font-family:Tahoma'>/*</span></span><spanstyle='mso-bookmark:BSICO'><span style='font-family:宋体;mso-ascii-font-family:Tahoma;mso-hansi-font-family:"Times New Roman";mso-bidi-font-family:Tahoma'>启动</span></span><spanstyle='mso-bookmark:BSICO'><span lang=EN-US style='font-family:Tahoma'> color</span></span><spanstyle='mso-bookmark:BSICO'><span style='font-family:宋体;mso-ascii-font-family:Tahoma;mso-hansi-font-family:"Times New Roman";mso-bidi-font-family:Tahoma'>机制</span></span><spanstyle='mso-bookmark:BSICO'><span lang=EN-US style='font-family:Tahoma'><spanstyle='mso-tab-count:1'> </span>*/<b style='mso-bidi-font-weight:normal'><o:p></o:p></b></span></span></p><p class=MsoNormal><span style='mso-bookmark:BSICO'><b style='mso-bidi-font-weight:normal'><span lang=EN-US style='font-family:Tahoma'><span style='mso-tab-count:1'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>init_pair(1, COLOR_RED,COLOR_BLACK);<o:p></o:p></span></b></span></p><p class=MsoNormal><span style='mso-bookmark:BSICO'><b style='mso-bidi-font-weight:normal'><span lang=EN-US style='font-family:Tahoma'><o:p>&nbsp;</o:p></span></b></span></p><p class=MsoNormal><span style='mso-bookmark:BSICO'><b style='mso-bidi-font-weight:normal'><span lang=EN-US style='font-family:Tahoma'><span style='mso-tab-count:1'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>attron(COLOR_PAIR(1));<o:p></o:p></span></b></span></p><p class=MsoNormal><span style='mso-bookmark:BSICO'><b style='mso-bidi-font-weight:normal'><span lang=EN-US style='font-family:Tahoma'><span style='mso-tab-count:1'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>print_in_middle(stdscr, LINES /2, 0, 0, &quot;</span></b></span><span style='mso-bookmark:BSICO'><spanlang=EN-US style='font-family:Tahoma'>Viola !!! In color ...<bstyle='mso-bidi-font-weight:normal'>&quot;);<o:p></o:p></b></span></span></p><p class=MsoNormal><span style='mso-bookmark:BSICO'><b style='mso-bidi-font-weight:normal'><span lang=EN-US style='font-family:Tahoma'><span style='mso-tab-count:1'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>attroff(COLOR_PAIR(1));<o:p></o:p></span></b></span></p><p class=MsoNormal><span style='mso-bookmark:BSICO'><b style='mso-bidi-font-weight:normal'><span lang=EN-US style='font-family:Tahoma'><spanstyle='mso-spacerun:yes'>&nbsp;&nbsp;&nbsp; </span><span style='mso-tab-count:1'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>getch();<o:p></o:p></span></b></span></p><p class=MsoNormal><span style='mso-bookmark:BSICO'><b style='mso-bidi-font-weight:normal'><span lang=EN-US style='font-family:Tahoma'><span style='mso-tab-count:1'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>endwin();<o:p></o:p></span></b></span></p><p class=MsoNormal><span style='mso-bookmark:BSICO'><b style='mso-bidi-font-weight:normal'><span lang=EN-US style='font-family:Tahoma'>}<o:p></o:p></span></b></span></p><p class=MsoNormal><span style='mso-bookmark:BSICO'><b style='mso-bidi-font-weight:normal'><span lang=EN-US style='font-family:Tahoma'>void print_in_middle(WINDOW*win, int starty, int startx, int width, char *string)<o:p></o:p></span></b></span></p><p class=MsoNormal><span style='mso-bookmark:BSICO'><b style='mso-bidi-font-weight:normal'><span lang=EN-US style='font-family:Tahoma'>{<span style='mso-tab-count:1'>&nbsp;&nbsp;&nbsp;&nbsp; </span>int length, x, y;<o:p></o:p></span></b></span></p><p class=MsoNormal><span style='mso-bookmark:BSICO'><b style='mso-bidi-font-weight:normal'><span lang=EN-US style='font-family:Tahoma'><span style='mso-tab-count:1'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>float temp;<o:p></o:p></span></b></span></p><p class=MsoNormal><span style='mso-bookmark:BSICO'><b style='mso-bidi-font-weight:normal'><span lang=EN-US style='font-family:Tahoma'><span style='mso-tab-count:1'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>if(win == NULL)<o:p></o:p></span></b></span></p><p class=MsoNormal><span style='mso-bookmark:BSICO'><b style='mso-bidi-font-weight:normal'><span lang=EN-US style='font-family:Tahoma'><span style='mso-tab-count:2'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>win= stdscr;<o:p></o:p></span></b></span></p><p class=MsoNormal><span style='mso-bookmark:BSICO'><b style='mso-bidi-font-weight:normal'><span lang=EN-US style='font-family:Tahoma'><span style='mso-tab-count:1'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>getyx(win, y, x);<o:p></o:p></span></b></span></p><p class=MsoNormal><span style='mso-bookmark:BSICO'><b style='mso-bidi-font-weight:normal'><span lang=EN-US style='font-family:Tahoma'><span style='mso-tab-count:1'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>if(startx != 0)<o:p></o:p></span></b></span></p><p class=MsoNormal><span style='mso-bookmark:BSICO'><b style='mso-bidi-font-weight:normal'><span lang=EN-US style='font-family:Tahoma'><span style='mso-tab-count:2'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>x= startx;<o:p></o:p></span></b></span></p><p class=MsoNormal><span style='mso-bookmark:BSICO'><b style='mso-bidi-font-weight:normal'><span lang=EN-US style='font-family:Tahoma'><span style='mso-tab-count:1'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>if(starty != 0)<o:p></o:p></span></b></span></p><p class=MsoNormal><span style='mso-bookmark:BSICO'><b style='mso-bidi-font-weight:normal'><span lang=EN-US style='font-family:Tahoma'><span style='mso-tab-count:2'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>y= starty;<o:p></o:p></span></b></span></p><p class=MsoNormal><span style='mso-bookmark:BSICO'><b style='mso-bidi-font-weight:normal'><span lang=EN-US style='font-family:Tahoma'><span style='mso-tab-count:1'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>if(width == 0)<o:p></o:p></span></b></span></p><p class=MsoNormal><span style='mso-bookmark:BSICO'><b style='mso-bidi-font-weight:normal'><span lang=EN-US style='font-family:Tahoma'><span style='mso-tab-count:2'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>width= 80;<o:p></o:p></span></b></span></p><p class=MsoNormal><span style='mso-bookmark:BSICO'><b style='mso-bidi-font-weight:normal'><span lang=EN-US style='font-family:Tahoma'><o:p>&nbsp;</o:p></span></b></span></p><p class=MsoNormal><span style='mso-bookmark:BSICO'><b style='mso-bidi-font-weight:normal'><span lang=EN-US style='font-family:Tahoma'><span style='mso-tab-count:1'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>length = strlen(string);<o:p></o:p></span></b></span></p><p class=MsoNormal><span style='mso-bookmark:BSICO'><b style='mso-bidi-font-weight:normal'><span lang=EN-US style='font-family:Tahoma'><span style='mso-tab-count:1'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>temp = (width - length)/ 2;<o:p></o:p></span></b></span></p><p class=MsoNormal><span style='mso-bookmark:BSICO'><b style='mso-bidi-font-weight:normal'><span lang=EN-US style='font-family:Tahoma'><span style='mso-tab-count:1'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>x = startx + (int)temp;<o:p></o:p></span></b></span></p><p class=MsoNormal><span style='mso-bookmark:BSICO'><b style='mso-bidi-font-weight:normal'><span lang=EN-US style='font-family:Tahoma'><span style='mso-tab-count:1'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>mvwprintw(win, y, x, &quot;</span></b></span><spanstyle='mso-bookmark:BSICO'><span lang=EN-US style='font-family:Tahoma'>%s<bstyle='mso-bidi-font-weight:normal'>&quot;, string);<o:p></o:p></b></span></span></p><p class=MsoNormal><span style='mso-bookmark:BSICO'><b style='mso-bidi-font-weight:normal'><span lang=EN-US style='font-family:Tahoma'><span style='mso-tab-count:1'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>refresh();<o:p></o:p></span></b></span></p><p class=MsoNormal><span style='mso-bookmark:BSICO'><b style='mso-bidi-font-weight:normal'><span lang=EN-US style='font-family:Tahoma'>}</span></b></span><bstyle='mso-bidi-font-weight:normal'><span lang=EN-US style='font-family:Tahoma'><o:p></o:p></span></b></p><p class=MsoNormal><span lang=EN-US><o:p>&nbsp;</o:p></span></p><p class=MsoNormal><span style='font-family:宋体;mso-ascii-font-family:"Times New Roman";mso-hansi-font-family:"Times New Roman"'>通过这个例子你可以看到,如果要启动颜色机制,必须先调用</span><spanlang=EN-US>start_color()</span><span style='font-family:宋体;mso-ascii-font-family:"Times New Roman";mso-hansi-font-family:"Times New Roman"'>函数。在使用这个函数后,你就可以在你的终端屏幕上使用各种各样的颜色控制函数。如果您要检测你的屏幕的颜色显示能力,可以使用</span><spanlang=EN-US>has_colors()</span><span style='font-family:宋体;mso-ascii-font-family:"Times New Roman";mso-hansi-font-family:"Times New Roman"'>函数,如果你的终端屏幕不支持彩色显示,那么函数将返回</span><span

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -