The WINBGIM Library -- Version 6.0 -- August 9, 2004
Borland BGI Graphics and Mouse
For Windows Applications

The winbgim library allows you to use BGI graphics routines and simple mouse support for Windows applications that you write with CS1300's mingw32 gnu C++ compiler or with the Borland C++ compiler (version 5.02). It will probably work with other Windows compilers, too, but I haven't tried it.

The library originates from Konstantin Knizhnik's winbgi shareware. I spent some time adding a new initwindow function (to allow the graphics window to be initially opened at any size) and three new functions to allow simple mouse-driven processing. Mark Richardson then added some new functions to allow more than sixteen colors.

You are welcome to use and modify this library as you like. Let me know if you make interesting modifications.

How to use the winbgim library for the free mingw32 gnu C++ compiler

  1. Download and install the free cs1300 tools, as described in

  2. The header file: Change to the cs1300\include subdirectory and check whether you have the file graphics.h. If not, then download this file into that directory by holding down the left-shift key and clicking on this link:

  3. The archieved library file: Change to the cs1300\lib subdirectory and check whether you have the file libbgi.a. If not, then download this file into that directory by holding down the left-shift key and clicking on this link:

  4. Write your program as you normally would, using the BGI graphics functions. Be sure to include graphics.h.

  5. When you create an executable (exe) program that uses the winbgim library, you must add the six options -lbgi -lgdi32 -lcomdlg32 -luuid -loleaut32 -lole32 (in that order) after any other file arguments to the compiler. For example, to compile the bgidemo0.cpp program you would give the compile command:
    g++ bgidemo0.cpp -lbgi -lgdi32 -lcomdlg32 -luuid -loleaut32 -lole32 -o bgidemo0.exe
    Note that each of these library options begins with the letter l (not the number one).

    If you are working with the CS1300 version of the g++ compiler (from, then you can get all these libraries automatically by using the bgi++ command instead of g++. For example:

    bgi++ bgidemo0.cpp -o bgidemo0.exe
    This command creates an executable file called bgidemo0.exe. Note that when it runs, it creates a small graphics window where all the bgi operations are performed. Any text i/o with cin and cout will be done in the other (console) window.

Graphics Stuff in the Library

Usual BGI Stuff: You can use any of the Borland BGI graphics functions.

Initialization: Normally, you initialize the BGI graphics by a call to detectgraph and a call to initgraph. You can still use those two function calls, or you may call a new function named initwindow. The function has two arguments that are the size of the graphics window that you want to create in pixels (width and height). For example, you can create a window that is 450 pixels wide and 300 pixels high with the statement:

initwindow(450, 450);

RGB Colors: The winbgim package supports two types of colors that may be used with any of the functions that expect colors as arguments:

  1. The sixteen ordinary BGI colors. These are the integers 0 through 15 or you may use the symbolic names:
         BLACK          BLUE          GREEN         CYAN
         RED            MAGENTA       BROWN         LIGHTGRAY

  2. A color may be specified from red, green and blue components using a new macro called COLOR(r,g,b). Each of the r,g,b arguments must be a number in the range 0 to 255. For example, COLOR(255,100,0) is a mostly red color with some green and no blue. If you create one of these colors, it may be used as an argument to any of the BGI functions that expect a color. These colors may also be returned from BGI functions such as getbkcolor.

Three other macros (RED_VALUE, GREEN_VALUE, BLUE_VALUE, IS_BGI_COLOR and IS_RGB_COLOR) are explained in the examples below.

RGB Examples:

    setcolor(BLUE);             // Change drawing color to BLUE.
    setcolor(COLOR(255,100,0);  // Change drawing color to reddish-green.
    setpalette(4, BLUE);        // Change palette entry 4 to BLUE.
    setpalette(4, COLOR(9,9,9));// Change palette entry 4 to nearly black.
    int current = getcolor( );  // Set current to current drawing color.

    if (IS_BGI_COLOR(current))  // Check whether it is a BGI color.
       cout << "Current BGI drawing color is: " << current << end;

    if (IS_RGB_COLOR(current))  // Check whether it is an RGB color.
       cout << "Current RGB drawing color has these components:\n"
            << "Red:   " << RED_VALUE(current)   << '\n'
            << "Green: " << GREEN_VALUE(current) << '\n'
            << "Blue:  " << BLUE_VALUE(current)  << '\n'
int getdisplaycolor(int color)
The actual color placed on the screen with putpixel might not be this exact rgb color that you want because of video mode limitations. The return value of this function tells you what actual color will be put on the screen for a requested color.

Mouse Stuff in the Library

There are three functions to use the mouse:

A Mouse Example with ismouseclick and getmouseclick

Suppose that you want a program to wait for a left mouse click. Then the program should print the x and y coordinates of the most recent left click. Here's a function to accomplish that:
void wait_for_left_click( )
    const int DELAY = 50; // Milliseconds of delay between checks
    int x, y;

    while (!ismouseclick(WM_LBUTTONDOWN))
    getmouseclick(WM_LBUTTONDOWN, x, y);
    cout << "Latest left click at: " << x << " " << y << endl;

A Mouse Example with a Handler

Suppose that whenever the right mouse button is clicked you want to print a message with the x and y coordinates of the click. Then you would start by writing your handler function, perhaps like this:
void my_right_click_handler(int x, int y)
    cout << "Right mouse click at " 
         << x << " and " << y << endl;
Now, somewhere in your program (after you've initialized the graphics window), you must register this fine handler with the statement:
registermousehandler(WM_RBUTTONDOWN, my_right_click_handler);

In general, the work carried out in a handler must be small. If you need to do a lot of work, have the handler change a global variable that will later trigger the large work. Then the handler can return quickly.

Keyboard Stuff in the Library

There are three functions that were originally part of Borland's conio.h. These functions are now in winbgim.h, and you do not need to include conio.h to use them::

Double-Buffering Support

I added two functions, getactivepage() and getvisualpage() to help support double-buffering. They return the page number of the active page (where drawing is currently taking place) and the visual page (the one on the screen). The original winbgi was designed to support up to 16 pages, but I have only used pages 1 and 2 myself. NOTE: Using page number 0 might mess up the colors. I use pages 1-2 for double buffering.

Support for Writing to the Graphics Screen

I added an outputstream, bgiout. It's used is described in bgiout.html.

Support for Printing, Reading and Writing Images

The graphics window now has a print option in the windows menu button, so the user can print the screen at any time. In additon, there are these new functions:

Support for Multiple Windows

The initiwindow function can now be called multiple times to create more than one graphics window. The initwindow documentation describes how these multiple windows are used.

Other Relevant Files

Writing Windows Programs

You may have noticed that any program that's compiled with g++ or bgi++ will always open a DOS command window when the program is run. If you want to stop this window from opening, then add the option -mwindows to the compile line (just after g++ or bgi++).

WINBGIM Version 6.0 Posted August 9, 2004
Michael Main (email