winapi - How to use SetConsoleTextAttribute C++ -
i have searched countless forums , websites can't seem find answer. i'm trying use setconsoletextattribute affects text. how can affect whole screen command color 1f
would? code is:
#include <iostream> #include <stdio.h> #include <stdlib.h> #include <windows.h> #include <wincon.h> using namespace std; int main() { setconsoletitle("c++ calculator"); // title of window int x; // decision int a; // first number int b; // second number int c; // answer handle con; con = getstdhandle(std_output_handle); setconsoletextattribute(con, background_blue | foreground_blue | foreground_green | foreground_red); cout << "calculator" << endl << endl; cout << "1:addition" << endl << "2:subtraction" << endl << "3:multiplication"; cout << endl << "4:division" << endl << "5:exit" << endl; cin >> x; switch (x) { case 1: // addition code cout << endl << "addition" << endl << "first number:"; cin >> a; cout << endl << "second number:"; cin >> b; c = + b; cout << endl << "answer:" << c; break; case 2: // subtraction code cout << endl << "subtraction" << endl << "first number:"; cin >> a; cout << endl << "second number:"; cin >> b; c = - b; cout << endl << "answer:" << c; break; case 3: // multiplication code cout << endl << "multiplication" << endl << "first number:"; cin >> a; cout << endl << "second number:"; cin >> b; c = * b; cout << endl << "answer:" << c; break; case 4: // division code cout << endl << "division" << endl << "first number:"; cin >> a; cout << endl << "second number:"; cin >> b; c = / b; cout << endl << "answer:" << c; break; case 5: // exit code return 0; } }
this solution relies on these winapi functions , structures:
- getconsolescreenbufferinfo screen dimensions
- fillconsoleoutputattribute fill screen attribute
- console_screen_buffer_info structure store screen information
the code follows:
handle hcon; console_screen_buffer_info csbiscreeninfo; coord coordstart = { 0, 0 }; // screen coordinate upper left dword dwnumwritten = 0; // holds # of cells written // fillconsoleoutputattribute dword dwscrsize; word wattributes = background_blue | foreground_blue | foreground_green | foreground_red; hcon = getstdhandle(std_output_handle); // screen buffer information including size , position of window if (!getconsolescreenbufferinfo(hcon, &csbiscreeninfo)) { // put error handling here return 1; } // calculate number of cells on screen screen size dwscrsize = csbiscreeninfo.dwmaximumwindowsize.x * csbiscreeninfo.dwmaximumwindowsize.y; // fill screen specified attribute fillconsoleoutputattribute(hcon, wattributes, dwscrsize, coordstart, &dwnumwritten); // set attribute newly written text setconsoletextattribute(hcon, wattributes);
the inline comments should enough understand basics of going supplied documentation links. screen size getconsolescreenbufferinfo , use determine number of cells on screen update new attribute using fillconsoleoutputattribute . use setconsoletextattribute ensure new text gets printed matches attribute used color entire console screen.
for brevity have left off error check calls fillconsoleoutputattribute , setconsoletextattribute. put stub error handling getconsolescreenbufferinfo . leave exercise original poster add appropriate error handling if choose.
Comments
Post a Comment