Browsing: Development

Colour in the Windows console with Curses

Console programs in Linux often use the Curses library to display colourful and attractive user interfaces even when running in a terminal. I wanted to use something similar for the display of a Windows console utility I am writing in Pelles C. The program processes large data files and can take a while to complete, so it is useful to show progress as the program is working.

Pelles C has a CONIO library similar to the one I remember from Turbo Pascal years ago, but I wanted something that would still work if I need to use a different compiler in future. Plus I’ve always been impressed by what is possible with Curses and wanted to give it a go as a learning exercise.

There are several implementations of Curses for use with Windows but I am using PDCurses.

Start by downloading the file pdc34dllw.zip.

The ZIP should contain 4 files:

  • Copy the files curses.h and panel.h into C:\Program Files\PellesC\Include

  • Copy the pdcurses.lib file into C:\Program Files\PellesC\Lib

  • Create a new console project in Pelles C and copy the pdcurses.dll file into the project folder

At the top of your C program, add the following 2 lines:

#include <curses.h>
#pragma comment(lib, "pdcurses.lib")

You can now add code to your program to use the Curses functions.

All of the functions available are described in the PDCurses documentation and I also suggest you check out the NCurses documentation as it is very comprehensive and mostly applies equally to PDCurses. If you are not familiar with programming using Curses, have a look at the NCURSES Programming HOWTO to get started.

Here is a screenshot of my test program as an example:

Screenshot

At the top of the screen is a header that shows the program name and version, the input filename and a count of the lines processed so far. Underneath, the main part of the screen is used to show scrolling text as the process is working.

Download my example program cursetest.c if you want to give it a try.

{ Comments are closed }

Using Pelles C to compile simple C programs

Recently I have been refreshing my knowledge of C programming by reading C Primer Plus by Stephen Prata and trying out the code samples. Although I use Microsoft's Visual Studio for most stuff, it's a bit heavyweight for the sort of 10 line example programs in the book.

After searching for a leaner alternative, I found Pelles C which is a really great pure C development system for Windows.

As well as a compiler supporting the latest C11 standard, it has a nice simple IDE and a lightweight project manager. Also the help text is short and to the point and you don't need to scroll through multiple screens as you often do in Visual Studio/MSDN.

Get the 32-bit or 64-bit setup file from the download area of the website. The 32-bit version was only 9.6MB so pretty lean!

After installing, to create a project for a basic console program follow these steps:

1) Choose File -> New -> Project from the main menu

2) From the the New Project window, Empty projects section, choose Win32 Console program (EXE)

3) Type a name such as HelloWorld for the name of the new project

4) In Location use the .. button to choose the folder where your projects will be saved, eg C:\Source\CPrimer\. The name from step 3 will be added to give a project folder of C:\Source\CPrimer\HelloWorld

5) You should now have a screen with the empty project showing at the right side of the screen. From the menu choose File -> New -> Source code

6) Use File -> Save and name the file HelloWorld.c

7) A message box will ask if you want to add the file to the current project, so click the Yes button

8) Now you can add your code to the edit window

9) Use the items on the Project menu to build and execute your program

Any compiler errors will be displayed in a panel at the bottom of the window, and you can double-click on a message to go to the location of the error in the edit window.

{ Comments are closed }