Click here to Skip to main content
13,195,379 members (68,813 online)
Click here to Skip to main content
Add your own
alternative version


27 bookmarked
Posted 13 May 2010

Handling Mouse Events and Simulating Push Buttons and List Boxes in a Win32 Console

, 4 Jun 2010
Rate this:
Please Sign up or sign in to vote.
Handling mouse events and simulating push buttons and list boxes in a Win32 Console

Designing apps in a Win32 Console is an adventure in itself. There exists no user interface libraries, push buttons, list boxes, and so on. This article will demonstrate how to simulate push buttons and list boxes and how to handle mouse events.

In a Win32 console, a push button can be simulated by first defining a rectangular region using the x and y coordinates and then drawing a button image above that rectangular region. And using an if statement, when the mouse is above the defined rectangular x and y region and a mouse button is pressed and released, you can pretty much simulate the behavior of the push buttons in Windows.

And the same applies to list boxes. You define a rectangular region using the x and y coordinates and then you draw a list box above that rectangular region and add an if statement. But before we begin, let's talk about a header file called cMichaelJanssonTextMode.h.

The C Header File cMichaelJanssonTextMode.h contains the functions that are of outmost importance for the application to be able to position the cursor and text characters in the correct x and y location. Also included in the file textmode.h are the functions to draw simple dialogs, draw lines, plot points and of course set a text character's color.

Functions in cMichaelJanssonTextMode.h:

delay()         delays x milliseconds
gotoxy()        Goes to position on 80x25 console screen
setcolor()      Changes text and background color
clrbox()        draws colored box without frames
box()           draws framed box
gotoxy() only works with printf(), cprintf() and not cout<

The gotoxy(int x, int y) Method

The gotoxy(int x, int y) event is called upon to set the cursor at a specific x and y location.

void gotoxy(int x, int y)
    COORD coord;
    coord.X = x; coord.Y = y;
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);

The setcolor(WORD color) Method

The setcolor(WORD color) event is called upon to set the color of text characters.

void setcolor(WORD color)

The clrscr() Method

The clrscr() event is called upon to clear the screen or to fill the screen with a specific color.

void clrscr()
    COORD coordScreen = { 0, 0 };
    DWORD cCharsWritten;
    DWORD dwConSize;
    HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);

    GetConsoleScreenBufferInfo(hConsole, &csbi);
    dwConSize = csbi.dwSize.X * csbi.dwSize.Y;
    FillConsoleOutputCharacter(hConsole, TEXT(' '), 
		dwConSize, coordScreen, &cCharsWritten);
    GetConsoleScreenBufferInfo(hConsole, &csbi);
    FillConsoleOutputAttribute(hConsole, csbi.wAttributes, 
		dwConSize, coordScreen, &cCharsWritten);
    SetConsoleCursorPosition(hConsole, coordScreen);

Handling Mouse Events in a Win32 Console

For our application to work, we will have to devise a method in which we will process events. The Console Input Buffer is a queue of input event records and when a console window has the keyboard focus, the console formats each input events like keys being pressed, mouse buttons being clicked or, mouse being moved as an input record and it places in the console's input buffer so that applications can access the records.

Let us look at an example:

 HANDLE handleIn;
 HANDLE handleOut;
 COORD KeyWhere;
 COORD MouseWhere;
 COORD EndWhere;
 DWORD NumRead;

 handleIn = GetStdHandle(STD_INPUT_HANDLE);
 handleOut = GetStdHandle(STD_OUTPUT_HANDLE);

 while (true)
     ReadConsoleInput(handleIn, &recordIn, 1, &NumRead);

     switch (recordIn.EventType)
     case KEY_EVENT:
                // Do stuff

     case MOUSE_EVENT:
                // Do stuff

Keyboard Events

When any key is pressed or released, keyboard events are generated and placed in a KEY_EVENT_RECORD structure.

Mouse Events

Mouse events are generated and placed in a MOUSE_EVENT_RECORD structure whenever the user moves the mouse or presses or releases one of the mouse buttons.

And now the application itself.

The project files consistents are:

  • cMichaelJanssonJokeConsole_main.cpp -The main file
  • cMichaelJanssonJokeConsole.cpp - The class definition and method body file
  • cMichaelJanssonJokeConsole.h - The class declaration file
  • cMichaelJanssonTextMode.h - The Console textmode graphics file

Simulated Listboxes

Fast fowarding a bit, we've simulated listboxes by drawing a listbox and defining a set of coordinates on which if a user clicks, listbox action should occur. So if a user clicks on "up", the file list will proceed to scroll up and likewise when down is clicked, the file list will scroll downwards. Now we are ready to process user inputs through mouse events.


if ((mouseX>38) && (mouseX<42) && (mouseY==23))


The cMichaelJanssonJokeConsole::fileSelected() Method

The cMichaelJanssonJokeConsole::fileSelected() method is called upon when the left button is pressed down while above this listBox.
Once the index has changed, this method is called upon and it represents the user changing from one textfile containing jokes to another.

void cMichaelJanssonJokeConsole::fileSelected()

The cMichaelJanssonJokeConsole::loadFile(char *file) Method

The cMichaelJanssonJokeConsole::loadFile(char *file) method is called upon to load jokes from a textfile to a char array.

void cMichaelJanssonJokeConsole::loadFile(char *file)
	char buffer[8192];
	ifstream in;

    for (int ii=0;ii<200;ii++) 	for (int jj=0;jj<1024;jj++)	line[ii][jj]=' ';


	gotoxy(2,1);printf("Random Jokes:");
	setcolor(224);gotoxy(25,1);printf("%s        ",JokeTitle);

	if (!in.is_open())
		cout << "Error opening file";
	int i=0;
	while (!(in.eof()))

The cMichaelJanssonJokeConsole::lineSelected() Method

The cMichaelJanssonJokeConsole::lineSelected() method is called upon when the left button is pressed down while above this listBox.
Once the index has changed, this method is called upon and it represents the user changing from one line of joke to another.

void cMichaelJanssonJokeConsole::lineSelected()

The cMichaelJanssonJokeConsole::displayJokeLine() Method

Finally the cMichaelJanssonJokeConsole::displayJokeLine() method is called upon to display one line of joke from a string array onto the console window.

void cMichaelJanssonJokeConsole::displayJokeLine()
	int pos=0,x=0,y=0;

	while((line[JokeLine][pos]!='\0') &&(pos<700))
		  if ((x>65) && (line[JokeLine][pos]==' '))
			  x=0 ;

The Main File

The file cMichaelJanssonJokeConsole_main.cpp is where we create an instance for our class and run our application.

#include "cMichaelJanssonJokeConsole.h"

int main()
  cMichaelJanssonJokeConsole  *MichaelJanssonJokeConsole = 
				new cMichaelJanssonJokeConsole(1);
  return 0;

And that is how easy it is to create a Joke Jukebox in Win32 Console application.

Thanks for reading.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

About me:
I attended programming college and I have a degree in three most famous and successful programming languages. C/C++, Visual Basic and Java. So i know i can code. And there is a diploma hanging on my wall to prove it.
I am a professional, I am paid tons of cash to teach or do software development. I am roughly 30 years old .

I hold lectures in programming. I have also coached students in C++, Java and Visual basic.

In my spare time i do enjoy developing computer games, and i am developing a rather simple flight simulator game
in the c++ programming language using the openGL graphics libray.

I've written hundreds of thousands of code syntax lines for small simple applications and games.

You may also be interested in...


Comments and Discussions

GeneralMy vote of 4 Pin
jbaolee4-Sep-10 14:10
memberjbaolee4-Sep-10 14:10 
helpful to me
GeneralExisting library for UIs in console mode Pin
sgllama31-Aug-10 22:43
membersgllama31-Aug-10 22:43 
GeneralMy vote of 1 Pin
xliqz4-Jun-10 5:13
memberxliqz4-Jun-10 5:13 
GeneralMy vote of 1 Pin
eds2-Jun-10 8:44
membereds2-Jun-10 8:44 
QuestionSo many Articles in a Single day? Pin
KunalChowdhury30-May-10 1:41
mentorKunalChowdhury30-May-10 1:41 
GeneralArticle writing suggestion [modified] Pin
David MacDermot14-May-10 8:31
memberDavid MacDermot14-May-10 8:31 
GeneralRe: Got it. Pin
TopCoder2318-May-10 9:11
memberTopCoder2318-May-10 9:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.171019.1 | Last Updated 4 Jun 2010
Article Copyright 2010 by Software_Developer
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid