通过C语言程序修改控制台的背景和前景颜色
2016-03-24 17:30
351 查看
本文主要讲解如何通过C语言来修改dos背景和前景颜色,我们首先来看一下dos的背景颜色的属性。
打开开始菜单,点击运行,弹出运行对话框,输入cmd,回车。(打开dos控制台)
在命令提示符界面下,输入help color,弹出下图所示的提示信息。
3. 源程序如下:
程序运行效果截图:
提示:程序中出现的函数以及对应的头文件
函数:rand()
int rand( void );
函数:system()
int system( const char *command);
int _wsystem( const wchar_t *command);
函数:Sleep()
Sleep
The Sleep function suspends the execution of the current thread for a specified interval.
no other threads of equal priority ready to run, the function returns immediately, and the thread continues execution. A value of INFINITE causes an infinite delay.
打开开始菜单,点击运行,弹出运行对话框,输入cmd,回车。(打开dos控制台)
在命令提示符界面下,输入help color,弹出下图所示的提示信息。
3. 源程序如下:
#include<time.h> #include<stdlib.h> #include<stdio.h> #include<windows.h> int main() { while (1) { srand(time(NULL)); int _nPreColorFlag = rand() % 10; int _nBackColorFlag = rand() % 6; char _cBackColorFlag; switch(_nBackColorFlag) { case 0: _cBackColorFlag = 'A'; break; case 1: _cBackColorFlag = 'B'; break; case 2: _cBackColorFlag = 'C'; break; case 3: _cBackColorFlag = 'D'; break; case 4: _cBackColorFlag = 'E'; break; case 5: _cBackColorFlag = 'F'; break; } char strColorFormat[2]; sprintf(strColorFormat,"color %d%c",_nPreColorFlag,_cBackColorFlag); system(strColorFormat); //通过system函数来设置背景颜色 printf("Console Application\n"); Sleep(1000); //程序睡眠1秒 system("cls"); //清空屏幕 } return 0; }
程序运行效果截图:
提示:程序中出现的函数以及对应的头文件
函数:rand()
rand
Generates a pseudorandom number.int rand( void );
Routine | Required Header | Compatibility |
rand | <stdlib.h> | ANSI, Win 95, Win NT |
system, _wsystem
Execute a command.int system( const char *command);
int _wsystem( const wchar_t *command);
Routine | Required Header | Compatibility |
system | <process.h> or <stdlib.h> | ANSI, Win 95, Win NT |
_wsystem | <process.h> or <stdlib.h> or <wchar.h> | Win NT |
Sleep
The Sleep function suspends the execution of the current thread for a specified interval.
VOID Sleep( DWORD dwMilliseconds // sleep time in milliseconds );
Parameters
dwMilliseconds
Specifies the time, in milliseconds, for which to suspend execution. A value of zero causes the thread to relinquish the remainder of its time slice to any other thread of equal priority that is ready to run. If there areno other threads of equal priority ready to run, the function returns immediately, and the thread continues execution. A value of INFINITE causes an infinite delay.
Return Values
This function does not return a value
在VC中Sleep中的第一个英文字符为大写的"S",
在标准C中是sleep, 不要大写,具体用什么看你用什么编译器. 简单的说VC用Sleep, 别的一律使用sleep.
相关文章推荐
- VC++图像存取总结
- C语言深度剖析读书笔记(一)
- C++ 的并行和异步
- C++标准库 vector排序
- C++面试出现频率最高的30道题目
- 使用c语言来扩展python模块
- c++作业2
- google c++ 编码规范
- C++ 引用和指针 const与#define #define与内联函数
- 我的C++第二次实验报告
- C++重新学习心得(1)(《C++Primer第四版》1-8章)
- (水题)坐电梯时间(problem G)
- C++ 容器及选用总结
- 国外找的一个base64转码的函数,简单好用。
- C++中的各种容器实现原理
- 《windows程序设计(C语言版)》笔记
- 每天一题(48) - C++实现Singleton模式
- C++中求类的大小
- Fibonacci 数列
- c/c++中静态局部变量以及全局变量的使用(转)