2017-04-17 9 views
0

CLIをWindowのCMDと似たものにする必要があります。カラーコマンドを作成するには、rlutil::setColorrlutil::setBackgroundColorrlutil.hに使用しています。しかし、すべてのコンソールで色を変更するには、画面(rlutil::cls())をクリアするか、画像にあるようにこの変更を加えた新しい出力のみが表示されます。 The same codes used in the cmdコンソールの色を変更し、出力をC++に保存してください

:CLSなし

:CLSで

Without the cls

:CMD(私は現在のディレクトリを表示しないようにオフ@echo使用)で Before the command After the first command - colors af

これは私が作った機能です:

void colors(string value) {//I recive the user's input (like in the cmd) 
    char foo[3];//I save each character in this array 
    int c_text = 0, c_bg = 0;//Variables to get the numeric value of each character 
    if(value.length() == 2) {//This is to only accept 2 characters as parameter for the command 
     strcpy(foo, value.c_str());//Copy the values of the string in the array 
     c_bg= chartoHEX(foo[0]);//Take the int value of each character 
     //(if the parameter in chartoHEX is '0', returns 0, if it's 'A', returns 10, and so on) 
     c_text = chartoHEX(foo[1]); 
     //If the function returns -1 means that the parameter wasn't an HEX number 
     if(c_text != -1 && c_bg != -1) { 
      rlutil::setColor(c_text);//Changes the text color 
      rlutil::setBackgroundColor(c_bg);//Changes the background color 
     } 
    } 
} 

私は関数を呼び出す:

colors("0a"); 
rlutil::cls(); 
cout << "C:\\Users\\Raven>"; 

私は色を変更した後、出力を維持するにはどうすればよいですか?

答えて

1

低レベルのネイティブWindowsコンソール機能を使用する場合は、テキストに影響を与えずに色を変更できます。 GetStdHandleまたは_get_osfhandleでコンソールハンドルを取得してから、WriteConsoleOutputAttributeに電話してください。

+0

これは動作しませんでしたが、これは解決策を見つけるのに役立ちました。代わりに、 'WriteConsoleOutputAttribute'を使用する代わりに' FillConsoleOutputAttribute'を使用しました。とにかくありがとう。 –

+0

@ RavenH.-ここで解決方法を明らかにするのはどうですか? – lit

+0

FillConsoleOutputAttributeはすべてのセルを同じ色で塗りつぶすため、WriteConsoleOutputAttributeは配列内のセルごとの色を指定します。あなたのニーズに最も適した機能を選択するだけです。 – Anders

関連する問題