Did you ever want to draw vertical text, but the output was out of proportion? Here is the answer. A simple function written in plain Win32, no MFC required. The function behaves almost exactly the same as
DrawText(), with the exception that the output is vertical.
This started out when I wanted to print tables. The data within my app had its own method of being displayed, so it seemed pointless to add support for tables; I instead opted to draw them. A line here and a line there, it was easy and quick. The problem came when I wanted to insert the header text. Many English words are too long to be displayed horizontally in this manner, going vertical was the most reasonable thing to do. But I found no function capable of doing this with a pleasant output. So I made my own.
Using the Code
If you know the
DrawText() function, then you will have no problems using this. It is almost identical. First, include the required files into your project. Anywhere you wish to use the function, include the header
#include "VertDraw.h". The prototype for the function is:
int DrawVertText( HDC hdc, LPCTSTR lpString, int nCount, LPRECT lpRect, UINT uFormat, UINT HcharSpacing = 3, UINT VcharSpacing = 5 );
The parameters are almost identical to
DrawText(), with the exception of the last two.
string provided to
lpString must be
NULL terminated. The function is fully
UNICODE compatible, so don't worry about your build configuration. To divide the
string into columns, just do the same as you would with any other
string. Anywhere you want a new column, add a newline character
lpRect defines the area within which the function may draw. For best results, keep this rectangle as small as possible. Passing the whole client area will only serve to use up your processor time.
uFormat parameter, you may pass one or more of the following values:
|Center the block of text horizontally ||May not be used in combination with |
|Align the block of text to the right of the provided rectangle ||May not be used in combination with |
|Center the block of text vertically ||May not be used in combination with |
|Align the block of text to the bottom of the provided rectangle ||May not be used in combination with |
|Center the text both horizontally and vertically within the provided rectangle ||May not be used in combination with |
|Align the text to the top ||Aligns the text relative to the longest line |
|Center the text ||Aligns the text relative to the longest line |
|Align the text to the bottom ||Aligns the text relative to the longest line |
|Justify the text to the top and bottom ||Aligns the text relative to the longest line |
|Calculate the rectangle needed to draw; but does not draw ||The rectangle is calculated relative to the one you provide in the |
|Forces the use of an internal double buffer ||By default, no double buffer is used |
VcharSpacing allow you to define how far apart you would like the characters and columns to be. Passing a value of
0 will result in the characters touching each other. For best results, leave them at their default values.
How It Works
The code will kick into a loop, drawing each character to a small monochrome bitmap of screen. Each of the bits is examined in this bitmap to determine the width and height of the character. Note, any character from any language will work, provided you have the correct locale set. The dimensions of each character are stored. When this loop is complete, the character positions are calculated relative to the longest line and the character spacing entered by the user. All that's left is to call the regular
DrawText() function for each of the characters, drawing them to the newly calculated positions.
Limitations and Things to Come
I think I have fixed them all in the new version.
- Version 1.0 released - August 25th, 2006
- Version 1.1 released - September 10th, 2006
- Removed all the slow off-screen drawing, implemented a storage of positions in its place
- Added the
- Corrected the problem with Italic fonts
- Corrected the problem with the background mode
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.
There is not much to say really, I first started programing using my Commodore Vic 20 when I was about 10 years old. I later had to give it up due to studies and work issues. In recent years I have started playing with C++.