DrawThemeText Function

Draws text using the color and font defined by the visual style.

Syntax

HRESULT DrawThemeText(      
    HTHEME hTheme,     HDC hdc,     int iPartId,     int iStateId,     LPCWSTR pszText,     int iCharCount,     DWORD dwTextFlags,     DWORD dwTextFlags2,     LPCRECT pRect );

Parameters

hTheme
[in] Handle to a window's theme data. Use OpenThemeData to create an HTHEME.
hdc
[in] Handle to a device context (HDC) to use for drawing.
iPartId
[in] The control part that has the desired text appearance. See Parts and States. If this value is 0, the text is drawn in the default font, or a font selected into the device context.
iStateId
[in] The control state that has the desired text appearance. See Parts and States.
pszText
[in] Pointer to a string that contains the text to draw.
iCharCount
[in] Value of type int that contains the number of characters to draw. If the parameter is set to -1, all the characters in the string are drawn.
dwTextFlags
[in] DWORD that contains one or more values that specify the string's formatting. See Format Values for possible parameter values.
Note  DrawThemeText does not support DT_CALCRECT. However, DrawThemeTextEx does support DT_CALCRECT.
dwTextFlags2
[in] Not used. Set to zero.
pRect
[in] Pointer to a >RECT structure that contains the rectangle, in logical coordinates, in which the text is to be drawn. It is recommended to use pExtentRect from GetThemeTextExtent to retrieve the correct coordinates.

Return Value

Returns S_OK if successful, or an error value otherwise.

Remarks

The function always uses the themed font for the specified part and state if one is defined. Otherwise it uses the font currently selected into the device context. To find out if a themed font is defined, you can call GetThemeFont or GetThemePropertyOrigin with TMT_FONT as the property identifier.

Example

DrawThemeText uses parameters similar to the Microsoft Win32 DrawText function, but with a few differences. One of the most notable is support for wide-character strings. Therefore, non-wide strings must be converted to wide strings, as in the following example.

security note Security Alert  Using MultiByteToWideChar incorrectly can compromise the security of your application. Ensure that when creating wide-character buffers they are large enough to accomodate the size of the string in wide characters, not in bytes.
INT cchText = GetWindowTextLength(_hwnd);
if (cchText > 0)
{
  TCHAR *pszText = new TCHAR[cchText+1];
  if (pszText)
  {
    if (GetWindowText(_hwnd, pszText, cchText+1))
    {
      int widelen = MultiByteToWideChar(CP_ACP, 0, pszText, cchText+1, NULL, 0);
      WCHAR *pszWideText = new WCHAR[widelen+1];
      MultiByteToWideChar(CP_ACP, 0, pszText, cchText, pszWideText, widelen);

      SetBkMode(hdcPaint, TRANSPARENT);
      DrawThemeText(_hTheme,
                    hdcPaint,
                    BP_PUSHBUTTON,
                    _iStateId,
                    pszWideText,
                    cchText,
                    DT_CENTER | DT_VCENTER | DT_SINGLELINE,
                    NULL,
                    &rcContent);

       delete [] pszWideText;
    }

    delete [] pszText;
  }
}

Function Information

Stock ImplementationUxTheme.dll
Custom ImplementationNo
Headeruxtheme.h
Import libraryUxTheme.lib
Minimum operating systems Windows XP

See Also

Property Identifiers
Tags :


Community Content

Matteo Italia
DT_CALCRECT does not work
The DT_CALCRECT flag does not work as expected in this function, as pointed out by Larry Osterman in his blog (http://blogs.msdn.com/larryosterman/archive/2008/09/30/drawthemetext-doesn-t-really-support-the-dt-calcrect-format-value.aspx).
Tags : contentbug

Page view tracker