Advertisement
Shaun_B

Win console application colour attributes

Sep 8th, 2012
397
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.18 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <conio.h>
  4. #include <windows.h>
  5. #define SETCOLOUR(a,b)  a<<4|b
  6.  
  7. /**
  8.  * A programme demonstrating the use of colour nybbles for Windows
  9.  * Tested using Code::Blocks and Windows 7 Professional SP1
  10.  *
  11.  * @Author:     Shaun B
  12.  * @Version:    1.0.0.3 - 2012-09-12
  13.  * @Todo:
  14.  * @Changes:    Refactored with varibale names that might make sense
  15.  *              Found an odd bug with the release build, which is now
  16.  *              fixed.
  17.  *
  18.  **/
  19.  
  20. int main();
  21. void gotoxy(int, int);
  22. void hideCursor();
  23.  
  24. /// Sets variables for:
  25. ///     Running boolean -
  26. static bool run = true;
  27. ///     Reusable stuff -
  28. static unsigned char index[2] = "";
  29. static unsigned char count = 0;
  30. static unsigned char xAxis = 0;
  31. static unsigned char yAxis = 0;
  32. static unsigned char character [2] = "";
  33.  
  34. int main()
  35. {
  36.     // Sets default console size, colours and title:
  37.     system("mode CON: COLS=80 LINES=25");
  38.     system("cls");
  39.     system("title Demonstrating the use of colour nybbles Donkeysoft MMXII");
  40.     system("echo off");
  41.     HANDLE hConsole;
  42.     // Switches off blinking cursor:
  43.     hConsole=   GetStdHandle(STD_OUTPUT_HANDLE);
  44.     hideCursor();
  45.     // Sets cursor position for each scrolly text:
  46.     gotoxy(0, 0);
  47.     printf("For console applications, the colours are defined in a byte, so f0 would"
  48.            "\ngive you white background and black text.\n");
  49.     printf("Enter a byte in hex (00 - ff) to see what happens. Space to exit programme.\n");
  50.     xAxis=  4;
  51.     yAxis=  3;
  52.     printf("C:\\>");
  53.     // Main loop:
  54.     while(run)
  55.     {
  56.         gotoxy(xAxis, yAxis);
  57.         printf("²");
  58.         // Scans keyboard into buffer:
  59.         character[count] = _getch();
  60.         if(character[count] == 32)
  61.         {
  62.             run=false;
  63.         }
  64.         else
  65.         if(character[count] >= 48 && character[count] <= 57 && count < 2)
  66.         {
  67.             gotoxy(xAxis, yAxis);
  68.             printf("%c", character[count]);
  69.             index[count] = character[count]-48;
  70.             xAxis++;
  71.             count++;
  72.         }
  73.         else
  74.         if(character[count] >= 97 && character[count] <= 102 && count < 2)
  75.         {
  76.             gotoxy(xAxis, yAxis);
  77.             printf("%c", character[count]);
  78.             index[count] = character[count]-87;
  79.             xAxis++;
  80.             count++;
  81.         }
  82.         else
  83.         if(character[count] >= 65 && character[count] <= 70 && count < 2)
  84.         {
  85.             gotoxy(xAxis, yAxis);
  86.             printf("%c", character[count]);
  87.             index[count] = character[count]-55;
  88.             xAxis++;
  89.             count++;
  90.         }
  91.         else
  92.         if(character[count] == 13 && count == 2)
  93.         {
  94.             gotoxy(xAxis-2, yAxis);
  95.             printf("   ");
  96.             unsigned char kCol = (unsigned char) SETCOLOUR(index[0], index[1]);
  97.             SetConsoleTextAttribute(hConsole, kCol);
  98.             printf("\nNew colour set to %3d!", kCol);
  99.             xAxis = 4;
  100.             count = 0;
  101.             kCol = 0;
  102.             index[2] = '\0';
  103.             character[2] =  '\0';
  104.             gotoxy(0, yAxis);
  105.             printf("C:\\>");
  106.             if(!run)
  107.             {
  108.                 run = true;
  109.             }
  110.         }
  111.         else
  112.         {
  113.             character[count] = '\0';
  114.         }
  115.     }
  116.     system("color f0");
  117.     system("cls");
  118.     printf("Cheers!");
  119.     // Returns when loop is exited:
  120.     return 0;
  121. }
  122.  
  123. // Sets co-ordinates within the command line:
  124. void gotoxy(int x, int y)
  125. {
  126.     COORD coord;
  127.     coord.X = x;
  128.     coord.Y = y;
  129.     SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
  130. }
  131.  
  132. // Turns off blinking cursor:
  133. void hideCursor()
  134. {
  135.     CONSOLE_CURSOR_INFO lpCursor;
  136.     lpCursor.bVisible = 0;
  137.     lpCursor.dwSize = 20;
  138.     SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&lpCursor);
  139. }
  140.  
  141. /**
  142.  * Old function:
  143.  *unsigned char setColour(unsigned char a, unsigned char b)
  144.  *{
  145.  *    if(i[0]!=a && i[1]!=b)
  146.  *    {
  147.  *        // Just in case there's an odd error...
  148.  *        return -1;
  149.  *    }
  150.  *    else
  151.  *    {
  152.  *        // Bitwise shift a four bits and then 'or' with b
  153.  *        return (a<<4)|b;
  154.  *    }
  155.  *}
  156.  **/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement