Jalaj

March 12, 2007

WinAPI : Displaying Text

After getting a feel of how Pens and Brushes are used for drawing on a Device Context, let’s get on to displaying texts on a Device Context.

The most basic function for the purpose is the TextOut. The function takes handle to the device context as the first parameter. The next two parameters are the X and Y co-ordinates of the reference point that system uses to align the string. The next parameter is the pointer to the string that is to be displayed and the last parameter is the number of characters in the string. Function returns non-zero if it succeeds otherwise a zero.

Private Declare Function TextOut Lib "gdi32" Alias "TextOutA" ( _
    ByVal hDC As Long, ByVal x As Long, ByVal y As Long, _
    ByVal lpString As String, ByVal nCount As Long) As Long

By default, the text is displayed Top-Left with reference to the co-ordinates passed. This is the default behaviour of the device context, however can be changed using the SetTextAlign function. Further the current position on the device context is neither used nor updated by the TextOut function, this behaviour however can also be modified using SetTextAlign function.

The function takes first parameter the handle to the device context and the next parameter is one or more of the constants defined below while will determine the alignment of texts that would be drawn next time.

Private Declare Function SetTextAlign Lib "gdi32" ( _
    ByVal hdc As Long, ByVal wFlags As Long) As Long

Const TA_LEFT = 0
Const TA_TOP = 0
Const TA_NOUPDATECP = 0
Const TA_UPDATECP = 1
Const TA_RIGHT = 2
Const TA_CENTER = 6
Const TA_BOTTOM = 8
Const TA_BASELINE = 24
Const TA_MASK = (TA_BASELINE + TA_CENTER + TA_UPDATECP)

Similarly SetTextColor function can be used to change the text color associated with the device context. The function takes handle to the device context as the first parameter. The second parameter is the Color reference calculated using RGB macro.

Private Declare Function SetTextColor Lib "gdi32" Alias "SetTextColor" ( _
    ByVal hdc As Long, ByVal crColor As Long) As Long

2 Comments »

  1. [...] : DrawText & DrawTextEx My last post on the WinAPI series was WinAPI : Displaying Text. Let’s get ahead with more WinAPI functions starting with DrawText and DrawTextEx [...]

    Pingback by WinAPI : DrawText & DrawTextEx « Jalaj — May 9, 2007 @ 6:54 am

  2. [...] : The Font Object In the previous posts concerning displaying text on Device Contexts (WinAPI : Displaying Text and WinAPI : DrawText & DrawTextEx), we saw nothing of how to write text in a particular font. [...]

    Pingback by WinAPI : The Font Object « Jalaj — May 16, 2007 @ 12:04 pm

RSS feed for comments on this post. TrackBack URI

Leave a comment

Blog at WordPress.com.