Creating and Styling Graphics Windows with EasyX in C

Creating a Graphics Window

A window consists of a specific width and height, measured in pixels. To initialize a basic graphics window, use the initgraph function.

#include <graphics.h>
#include <conio.h>

int main() {
    int screenWidth = 640;
    int screenHeight = 480;
    
    initgraph(screenWidth, screenHeight);
    getch();
    closegraph();
    return 0;
}

In this example, 640 represents the window width, and 480 represents the height in pixels.

Controlling Window Properties

EasyX allows you to control the behavior of the window buttons using optional flags. You can pass a third argument to initgraph to customize window settings.

#include <graphics.h>
#include <conio.h>

int main() {
    initgraph(800, 600, EX_NOCLOSE);
    getch();
    closegraph();
    return 0;
}

You can combine multiple flags using the bitwise OR operater |:

initgraph(800, 600, EX_NOCLOSE | EX_NOMINIMIZE);

Available window property flags include:

// EasyX Window Flags
#define EX_SHOWCONSOLE      1    // Keep console window visible
#define EX_NOCLOSE          2    // Disable the close button
#define EX_NOMINIMIZE       4    // Disable the minimize button
#define EX_DBLCLKS          8    // Enable double-click detection

For instance, to keep the console window open alongside the graphics window:

initgraph(800, 600, EX_SHOWCONSOLE);

Setting Background Colors

To fill the window with a specific color, you must first set the background color and then clear the device to apply it.

#include <graphics.h>
#include <conio.h>

int main() {
    initgraph(640, 480, EX_SHOWCONSOLE);
    
    setbkcolor(WHITE);
    cleardevice();
    
    getch();
    closegraph();
    return 0;
}

EasyX provides a set of predefined color constants:

#define BLACK           0
#define BLUE            0xAA0000
#define GREEN           0x00AA00
#define CYAN            0xAAAA00
#define RED             0x0000AA
#define MAGENTA         0xAA00AA
#define BROWN           0x0055AA
#define LIGHTGRAY       0xAAAAAA
#define DARKGRAY        0x555555
#define LIGHTBLUE       0xFF5555
#define LIGHTGREEN      0x55FF55
#define LIGHTCYAN       0xFFFF55
#define LIGHTRED        0x5555FF
#define LIGHTMAGENTA    0xFF55FF
#define YELLOW          0x55FFFF
#define WHITE           0xFFFFFF

Using Custom Colors

You can define custom colors using RGB values. The RGB() macro accepts three parameters ranging from 0 to 255:

#include <graphics.h>
#include <conio.h>

int main() {
    initgraph(640, 480);
    
    // Set a teal background using RGB
    setbkcolor(RGB(57, 155, 143));
    cleardevice();
    
    getch();
    closegraph();
    return 0;
}

Alternatively, you can use hexadecimal color codes directly. For example, "Medium Violet Red" has the hex code #C71585, which corresponds to RGB(199, 21, 133):

setbkcolor(RGB(199, 21, 133));
cleardevice();

Or using the hex value:

setbkcolor(0xC71585);
cleardevice();

If you frequently use a specific custom color, you can add it to the graphics header file or define it in your source code:

#define MEDIUM_VIOLET_RED  0xC71585

// Usage
setbkcolor(MEDIUM_VIOLET_RED);
cleardevice();

Tags: EasyX C Language Graphics Programming RGB colors Windows programming

Posted on Wed, 30 Sep 2026 16:07:44 +0000 by biltong