Page 1 of 1

Microsoft : Using Console Functions To Achieve Blinking Text Rate Topic: -----

#1 Martyn.Rae  Icon User is offline

  • The programming dinosaur
  • member icon

Reputation: 543
  • View blog
  • Posts: 1,406
  • Joined: 22-August 09

Posted 22 April 2011 - 08:49 AM

Microsoft : Using Console Functions To Achieve Blinking Text


There is a lot of quite nice features supported by Microsoft Windows, for console applications, that are rarely seen by end-users.

This tutorial provides the code for displaying a blinking "Hello World" message in the middle of the console screen in red. More about the Windows Console functions can be found here.

Handling Ctrl-C and Ctrl-break signals

I have included a console event handler in the example code below, to permit you to break out of the application by using either Ctrl-C or Ctrl-break (it also simplifies the code such that I did not need to create a separate thread for the 'blinking' code).

The code for the console event handler is called ConsoleHandler and is located immediately below the include file statement. The function is called whenever a console event occurs (such as keyboard and mouse events). We are only interested in the CTRL_C_EVENT and CTRL_BREAK_EVENT for the purpose of this tutorial. If either of those signals are reported, the code simply calls ExitProcess to terminate. If the signal is not one of the aforementioned, the handler returns FALSE to indicate that the next handler (if any) in the chain of handlers needs to be called.

The Main Code

The first three lines of code (after the variable declarations) creates a console screen buffer, sets that screen buffer as the active buffer and sets the console handler (described above). You can create any number of console screen buffers using the CreateConsoleScreenBuffer function, but only the screen buffer that is activated using the SetConsoleActiveScreenBuffer is visible to the user at any given point in time. This is quite useful as it means that multiple screens can be created at the start of the application and then presented to the user as and when needed.

The SetConsoleScreenBufferSize function sets the size of the screen buffer to 80 columns wide and 24 rows in height (this used to be the default size for most VDU screens back in the good old days). A call is then made to the SetConsoleCursorInfo in order to hide the cursor. The next line, sets the attributes for the text that will be written to the screen. As I have chosen to use intensified red as the colour, we set the FOREGROUND_RED bit along with the FOREGROUND_INTENSITY bit and call SetConsoleTextAttribute. These attributes are retained for the remainder of the application run.

Finally, we enter an infinite loop which alternates between our text "Hello World" and spaces, using the SetConsoleCursorPosition to ensure that we overwrite the same row and columns on the screen. Et voila!

#include <Windows.h>

BOOL WINAPI ConsoleHandler(DWORD ctrl_type) {
	if ( ctrl_type == CTRL_C_EVENT || ctrl_type == CTRL_BREAK_EVENT ) {
	return FALSE;

int main(int argv, char *argc[]) {
	HANDLE console_handle;
	COORD dwSize;
	COORD dwPosition;
	DWORD written;
	DWORD flag = 0;

	console_handle = 
                                          0, NULL, 
	SetConsoleCtrlHandler(ConsoleHandler, TRUE);
	dwSize.X = 80;
	dwSize.Y = 24;
	SetConsoleScreenBufferSize(console_handle, dwSize);
	cursor_info.dwSize = 1;
	cursor_info.bVisible = FALSE;
	SetConsoleCursorInfo(console_handle, &cursor_info);
                                FOREGROUND_RED | FOREGROUND_INTENSITY);
	dwPosition.X = 35;
	dwPosition.Y = 12;
	while ( TRUE ) {
		SetConsoleCursorPosition(console_handle, dwPosition);
		if ( flag ^= 1 )
			WriteFile(console_handle, "Hello World", 
			          11, &written, NULL);
			WriteFile(console_handle, "           ", 
			          11, &written, NULL);

Is This A Good Question/Topic? 1
  • +

Page 1 of 1