Click here to Skip to main content
15,668,735 members
Articles / Programming Languages / C++
Posted 17 Apr 2002


21 bookmarked

Windows Fortune Application

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
17 Apr 20023 min read
This application will display a random fortune in the font and colors of your choice.

Sample Image - Fortune.gif


First I want to point out that this application is primarily for amusement. There are some moderately useful pieces of code for beginners but I mainly wanted to share this app with the Code Project denizens.

In one of my first programming jobs I worked on a Unix-derivative called QNX. It had a little command-line program that displayed fortunes and my original list of fortunes began with their's. Many of those originals are from Robert Heinlein novels and lately I have added several from the signatures of various CP members. Where possible (and I remembered to) I have noted the origin of the saying. Please feel free to send additions by e-mail and I will periodically update this article.

To use this app, place the executable and the fortunes file (Fortunes.txt) into the directory of your choice. The app requires the fortunes file to be in the same directory as it. Once the app is started you can alter the display colors and font. All settings will be kept in a file called Fortune.ini also in the same directory as the application. The app will display a random fortune every time it is started. The current time is used to seed the random number generator. Click the button labelled "New" to see a new fortune.

The fortunes are read from a standard text file. Each fortune occupies one line in the file. Currently the program has definitions that limit it to 2000 fortunes of up to 1020 bytes each. My current list of fortunes has 472 entries in it and the longest is 206 bytes. Note that only the exact amount of text is stored internally. Any standard text editor can be used to add or remove fortunes from the file.

This program was written in straight C using no other libraries. Actually, it is the first application I ever wrote for Windows and it was written many years ago. As such, some of the code is a bit inelegant to put it lightly. I recently went through and cleaned some things up a bit but I do not think that a full re-write was warranted.

There are a few interesting pieces of code in the app. The gradient background fill is one I conjured up myself. It does a simple linear interpolation between the top and bottom color on each shade for each vertical line in the window. An array of brushes is maintained for the entire window and it is updated when the colors are changed or when the window is resized. It uses floating point values to do the interpolation in an attempt to be as smooth as possible. There is also a handy little profile reading and writing library. It includes functions for handling fonts, colors, rectangles, and window position. The text toolbar buttons came from an old MSDN sample.

If you just want to run the app you can grab its zip file. The fortunes and a release build executable are included in that zip. Otherwise, grab the source and you can build it yourself. The fortunes are included in the source zip also. There is a little batch file for building from the command line. It is called _makeme.bat and it will generate a release build.

Well, that's about it. Send me any good sayings you run across and have fun !


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Written By
Software Developer (Senior)
United States United States
I work on an industrial HPC project that can run on either the CPU or the GPU. I usually use which ever one has the most horsepower on a given machine. It's written with CUDA with very few ifdefs used. My company is quite large, in the top five in our industry in North America, and I work in a small group with just five programmers.

Comments and Discussions

GeneralPuzzle Pin
Jason Hooper19-Apr-02 6:50
Jason Hooper19-Apr-02 6:50 
GeneralRe: Puzzle Pin
Rick York19-Apr-02 12:04
mveRick York19-Apr-02 12:04 
GeneralRe: Puzzle Pin
Jason Hooper19-Apr-02 16:09
Jason Hooper19-Apr-02 16:09 
GeneralRe: Puzzle Pin
Jordan Walters5-Apr-05 22:55
Jordan Walters5-Apr-05 22:55 
Generalwoohoo! Pin
Chris Maunder18-Apr-02 14:01
cofounderChris Maunder18-Apr-02 14:01 
GeneralRe: woohoo! Pin
Rick York18-Apr-02 14:52
mveRick York18-Apr-02 14:52 
GeneralRe: woohoo! Pin
Chris Losinger19-Apr-02 7:05
professionalChris Losinger19-Apr-02 7:05 
i think that title bar code just creates a new brush for each line. i've reused his code many times.

i think this is the code:

static void PaintRect(CDC& dc, int x, int y, int w, int h, COLORREF color)
	CBrush brush(color);
	CBrush* pOldBrush = dc.SelectObject(&brush);
	dc.PatBlt(x, y, w, h, PATCOPY);

void MakeCaptionBitmap()
	if (m_bm.m_hObject)
		return;								   // already have bitmap; return

   CRect cr;
   int w = cr.Width();
   int h = cr.Height();

	// Create bitmap same size as caption area and select into memory DC
	CWindowDC dcWin(this);
	CDC dc;
	m_bm.CreateCompatibleBitmap(&dcWin, w, h);
	CBitmap* pOldBitmap = dc.SelectObject(&m_bm);

   COLORREF clrBG = ::GetSysColor(COLOR_3DFACE); // background color
	int r = GetRValue(clrBG);				// red..
	int g = GetGValue(clrBG);				//
	int b = GetBValue(clrBG);				// color vals
	int x = 8*cr.right/8;					// start 5/6 of the way right
	int w1 = x - cr.left;					// width of area to shade

   int NCOLORSHADES = 128;		// this many shades in gradient

	int xDelta= max( w / NCOLORSHADES , 1);	// width of one shade band

	PaintRect(dc, x, 0, cr.right-x, h, clrBG);

	while (x > xDelta) 
   {												// paint bands right to left
		x -= xDelta;							// next band
		int wmx2 = (w1-x)*(w1-x);			// w minus x squared
		int w2  = w1*w1;						// w squared
		PaintRect(dc, x, 0, xDelta, h,	
			RGB(r-(r*wmx2)/w2, g-(g*wmx2)/w2, b-(b*wmx2)/w2));

	PaintRect(dc,0,0,x,h,RGB(0,0,0));  // whatever's left ==> black

	// draw the 'constant' text

	// create a font, if we need to
	if (m_nameFont.GetSafeHandle()==NULL)
		m_nameFont.CreateFont( 18, 0, 0, 0, FW_BOLD,
											0, 0, 0, ANSI_CHARSET,


Civilization is the limitless multiplication of unnecessary necessities.
   Mark Twain

GeneralRe: woohoo! Pin
Chris Losinger19-Apr-02 7:02
professionalChris Losinger19-Apr-02 7:02 

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.