Skip to main content

Past Blast

Featured Products

Windows Mobile Developer Controls
Windows Mobile Developer Controls
Stay in touch using the DEVBUSS RSS feeds.
 

News

Windows Mobile Developer Controls
Sapphire Soltuions

Drawing Fonts Directly on an eVB Form

Written by Christopher Tacke  [author's bio]  [read 38854 times]
Edited by Derek

Download the code

Page 1  Page 2 

Once you have created the LOGFONT structure, you simply pass it as an argument to the CreateFontIndirect() API to get a handle to the actual created logical font. This handle can then be selected into a DC to direct it how to render text. Selecting the font into the DC is done by calling the SelectObject() API. In our case, SelectObject() sets the current font for the DC and returns a handle to the replaced font. It is important to store this return value so you can select the original font back into the DC when you're done. Listing 2 is a snippet from the PocketFont source code that creates a font then selects it into frmMain's device context.

Listing 2

' Create a Logical Font
LOGFONT = CreateLogicalFont(FontHeight, 0, _
Escapement, FontWeight, Italic, Underline, False, "Tahoma")

' Create the Font to use
m_FontInUse = CreateFontIndirect(LOGFONT)

' Select our font into the Form's HDC, keeping the old one
oldFont = SelectObject(frmMain.hdc, m_FontInUse)

Drawing Text

Now that we've created and selected our font into Form's DC, let's look at the steps necessary to actually draw the text. First you need to create a formatting rectangle in which to draw the text. Parameters such as alignment are related to this rectangle so if you don't specify any alignment, the text will end up in the upper left of the rectangle. The size of the formatting rectangle will not affect the size of the text drawn.

Since a rectangle is represented by a RECT struct, again we must use a UDT workaround to build it. For the PocketFont example, I've created a wrapper function that takes the four RECT members as parameters and returns the RECT struct as a binary string (Listing 3).

Listing 3

Public Function CreateRECT(ByRef Left As Long, _
ByRef Top As Long, _
ByRef Right As Long, _
ByRef Bottom As Long) As String
Dim r As String

r = ToBinaryString(Left, CE_LONG)
r = r & ToBinaryString(Top, CE_LONG)
r = r & ToBinaryString(Right, CE_LONG)
r = r & ToBinaryString(Bottom, CE_LONG)

CreateRECT = r
End Function

Now that we have a a formatting rectangle, as well as a font and a target hDC, we can call the DrawText() API. DrawText() takes as parameters the hDC, the text itself, the maximum number of characters to draw (-1 means all characters), the formatting rectangle and any formatting constants such as alignment.

Once you're done drawing the text, remember to use SelectObject to select the old font back into the hDC.

TextColor, BkColor and BackMode

It's quite reasonable to assume that you'll find a need to draw text in a color other than black or that you'll want the background to be something other than either transparent (which, for some reason, it doesn't default to in eVB) or gray. This is done by simply calling the SetTextColor() or SetBkColor() APIs with the correct color constant prior to drawing the text. Keep in mind that setting the back color has no effect unless the back style is set to be opaque. Listing 4 shows how to set the text and back colors to red and blue respectively.

Listing 4

SetTextColor frmMain.hdc, vbRed
SetBkColor frmMain.hdc, vbBlue
SetBkMode frmMain.hdc, OPAQUE

As you've seen, drawing text in a non-horizontal orientation may be a bit daunting or confusing at first glance, but with a little practice and a couple wrapper methods, you've got the ability to improve your application's user interface with features not exposed by eVB. In fact this feature isn't exposed by VB 6 either and the technique used here is identical (except for the UDT work arounds) to drawing non-horizontal text on the desktop PC.

Remember, the worst thing that can happen by playing with APIs (provided you've backed up your device's data) is that you'll be forced to hard reset and you might be pleasantly surprised at the added features you give your applications through some simple API work.

Previous Page