zoukankan      html  css  js  c++  java
  • 通过C语言程序改动控制台的背景和前景颜色

    本文主要解说怎样通过C语言来改动dos背景和前景颜色。我们首先来看一下dos的背景颜色的属性。

    1. 打开開始菜单,点击执行,弹出执行对话框。输入cmd,回车。

      (打开dos控制台)

    2. 在命令提示符界面下,输入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
    ");  
            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()

    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()
    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 are 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.

    Return Values

    This function does not return a value

    在VC中Sleep中的第一个英文字符为大写的"S",

    在标准C中是sleep, 不要大写。详细用什么看你用什么编译器. 简单的说VC用Sleep, 别的一律使用sleep.

  • 相关阅读:
    Kubernetes stateful set讲解以及一个基于postgreSQL的具体例子
    如何在Kubernetes里给PostgreSQL创建secret
    如何使用Kubernetes的configmap通过环境变量注入到pod里
    使用Gardener在Google Cloud Platform上创建Kubernetes集群
    通过describe命令学习Kubernetes的pod属性详解
    使用describe命令进行Kubernetes pod错误排查
    一个简单的例子理解Kubernetes的三种IP地址类型
    不同编程语言在发生stackoverflow之前支持的调用栈最大嵌套层数
    (十)golang--运算符
    (九)golang--标识符的命名规则
  • 原文地址:https://www.cnblogs.com/yjbjingcha/p/7160290.html
Copyright © 2011-2022 走看看