kapai
asked on
How to do graphics in C
I'm learning C using MS Visual c++
Can I do graphics in C?
Something easy like coloured text or maybe simple graphics?
ASCII is ok but want to do more.
Thanks.
Can I do graphics in C?
Something easy like coloured text or maybe simple graphics?
ASCII is ok but want to do more.
Thanks.
The last parameter of the SetAttribute function can be of any of the below combinations.
FOREGROUND_RED
FOREGROUND_GREEN
FOREGROUND_BLUE
FOREGROUND_INTENSITY
BACKGROUND_RED
BACKGROUND_GREEN
BACKGROUND_BLUE
BACKGROUND_INTENSITY
To get white foreground colour and red background colour, use this combination.
SetAttribute(hOutput, 1, 1, strlen("Hello World"), FOREGROUND_RED|FOREGROUND_ GREEN|FORE GROUND_BLU E|BACKGROU ND_RED);
hongjun
FOREGROUND_RED
FOREGROUND_GREEN
FOREGROUND_BLUE
FOREGROUND_INTENSITY
BACKGROUND_RED
BACKGROUND_GREEN
BACKGROUND_BLUE
BACKGROUND_INTENSITY
To get white foreground colour and red background colour, use this combination.
SetAttribute(hOutput, 1, 1, strlen("Hello World"), FOREGROUND_RED|FOREGROUND_
hongjun
Any progress?
hongjun
hongjun
ASKER
Ran it, not sure about couple things but it does the job.
Compiler doesn't like the way you declare 'int i' then tried to use it in 'for' loop.
Double points if you can tell me how to change entire background color.
Compiler doesn't like the way you declare 'int i' then tried to use it in 'for' loop.
Double points if you can tell me how to change entire background color.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks, didnt realise they put an upper limit on points.
Haha you could always create a question just for me if you wish to give more :)
hongjun
hongjun
ASKER
Dont worry got lots of questions
hehehe
hehehe
#include <windows.h>
#include <stdio.h>
#include <string.h>
#include <conio.h>
void SetAttribute(HANDLE hOutput, int x, int y, int z, unsigned short Attribute);
void PrintXY(HANDLE hOutput, int x, int y, char *string);
void main()
{
HANDLE hOutput;
hOutput = GetStdHandle(STD_OUTPUT_HA
SetAttribute(hOutput, 1, 1, strlen("Hello World"), FOREGROUND_RED);
PrintXY(hOutput, 0, 0, "Hello World");
}
// set the color of text
void SetAttribute(HANDLE hOutput, int x, int y, int z, unsigned short Attribute)
{
DWORD cWritten;
COORD coord;
for ( int i=0; i<z; i++)
{
coord.X = x + i;
coord.Y = y;
WriteConsoleOutputAttribut
hOutput, // screen buffer handle
&Attribute, // pointer to source string
(DWORD)1, // length of string
coord, // first cell to write to
&cWritten); // actual number written
}
}
// print string literally onto screen at specified position
void PrintXY(HANDLE hOutput, int x, int y, char *string)
{
DWORD cWritten;
COORD coord;
coord.X = x;
coord.Y = y;
WriteConsoleOutputCharacte
hOutput, // screen buffer handle
string, // pointer to source string
lstrlen(string), // length of string
coord, // first cell to write to
&cWritten); // actual number written
}
hongjun