Introduction
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.
Background
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.
The string provided to lpString
must be NULL
terminated. The function is fully MBCS
and 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 '\n'
.
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.
In the uFormat
parameter, you may pass one or more of the following values:
DV_HCENTER |
Center the block of text horizontally |
May not be used in combination with DV_RIGHT |
DV_RIGHT |
Align the block of text to the right of the provided rectangle |
May not be used in combination with DV_HCENTER |
DV_VCENTER |
Center the block of text vertically |
May not be used in combination with DV_BOTTOM |
DV_BOTTOM |
Align the block of text to the bottom of the provided rectangle |
May not be used in combination with DV_VCENTER |
DV_CENTERALL |
Center the text both horizontally and vertically within the provided rectangle |
May not be used in combination with DV_BOTTOM or DV_RIGHT |
DV_TEXTTOP |
Align the text to the top |
Aligns the text relative to the longest line |
DV_TEXTCENTER |
Center the text |
Aligns the text relative to the longest line |
DV_TEXTBOTTOM |
Align the text to the bottom |
Aligns the text relative to the longest line |
DV_TEXTJUSTIFY |
Justify the text to the top and bottom |
Aligns the text relative to the longest line |
DV_CALCRECT |
Calculate the rectangle needed to draw; but does not draw |
The rectangle is calculated relative to the one you provide in the lpRect parameter |
DV_DOUBLEBUFFER |
Forces the use of an internal double buffer |
By default, no double buffer is used |
HcharSpacing
and 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.
History
- 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
DV_TEXTJUSTIFY
and DV_DOUBLEBUFFER
flags.
- Corrected the problem with Italic fonts.
- Corrected the problem with the background mode.