Text functions in Windows API
Under "Font and Text Functions" there are about 50 functions listed at msdn (March 2009). Many are concerned with details of font character size etc. If you don't need to worry about that, then you will only need the more general purpose ones, as follows:
Main text functions | |
---|---|
CreateFont | See below |
TextOut | BOOL TextOut(HDC hdc, int x, int y, LPCTSTR str, int nchars); |
ExtTextOut | BOOL ExtTextOut(HDC hdc, int x, int y, UINT options, const RECT* rect, LPCTSTR str, UINT nchars, const int* dx); |
rect and dx are optional (set to 0 if not being used). rect and options together allow some further functionality: options can be one or more of CLIPPED | OPAQUE | RTLREADING dx is an array giving the spacing of each character from the next e.g. to paint a rectangle rect in current background colour, you can use ExtTextOut(hdc, 0, 0, OPAQUE, &rect, "", 0, 0); (this is quicker than FillRect(hdc, &r, br) where br is an HBRUSH) | |
GetTextAlign | UINT GetTextAlign(HDC hdc); |
SetTextAlign | UINT SetTextAlign(HDC hdc, UINT mode); |
mode is one or more of TA_BASELINE base line of the text. TA_LEFT, TA_RIGHT, TA_CENTER, TA_TOP, TA_BOTTOM edge of the bounding rectangle. TA_UPDATECP, TA_NOUPDATECP update, do not update the current position | |
SetTextColor | COLORREF SetTextColor(HDC hdc, COLORREF colour); |
SetBkColor | COLORREF SetBkColor(HDC hdc, COLORREF colour); |
SetBkMode | int SetBkMode(HDC hdc, int mode); |
mode is one of OPAQUE or TRANSPARENT | |
GetTextExtentPoint32 | BOOL GetTextExtentPoint32(_hdc, LPCTSTR str, size_t len, SIZE* sz); |
Compute the width, height in pixels of a displayed string: find the answer in sz.cx, sz.cy |
BOOL CreateFont(
int height, int width, // height is the font size, you can set width to 0 int escapement, int orientation, // angles: usually 0 int weight, // e.g. FW_BOLD = 700, FW_NORMAL = 400 BYTE italic, BYTE underline, BYTE strikeOut, // these are bool: see below BYTE nCharSet, BYTE nOutPrecision, BYTE nClipPrecision, BYTE nQuality, BYTE nPitchAndFamily, LPCTSTR facename // e.g. "Arial", "Courier New", "Times New Roman" ); // For example string face = "Arial"; int fontsize = 16; int weight = FW_BOLD; bool italic = true; HFONT newfont; newfont = CreateFont( fontsize, 0, 0, 0, weight, italic, FALSE, FALSE, ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_SWISS, face.c_str()); if (newfont==0) textout(1,1,"Font creation failed"); return newfont;