Jump to content
Sign in to follow this  

Drawing text with a shadow.

Recommended Posts


Based on rover`s idea.

This function will be also included in the next version WinAPIEx.au3 library.

#Include <GUIConstantsEx.au3>
#Include <FontConstants.au3>
#Include <WinAPI.au3>
#Include <WindowsConstants.au3>

Opt('MustDeclareVars', 1)

Global Const $STM_SETIMAGE = 0x0172
Global Const $STM_GETIMAGE = 0x0173

Global $hForm, $Pic, $hPic, $tRECT, $Width, $Height, $hBmp, $hDC, $hBack, $hFront, $hFont, $hBitmap, $hObj

$hForm = GUICreate('MyGUI', 400, 100)
$Pic = GUICtrlCreatePic('', 20, 20, 360, 60)
$hPic = GUICtrlGetHandle($Pic)

$tRECT = _WinAPI_GetClientRect($hPic)
$Width = DllStructGetData($tRECT, 3) - DllStructGetData($tRECT, 1)
$Height = DllStructGetData($tRECT, 4) - DllStructGetData($tRECT, 2)
$hDC = _WinAPI_GetDC($hPic)
$hBack = _WinAPI_CreateCompatibleDC($hDC)
$hBitmap = _WinAPI_CreateCompatibleBitmap($hDC, $Width, $Height)
_WinAPI_SelectObject($hBack, $hBitmap)
$hFront = _WinAPI_CreateCompatibleDC($hDC)
$hBmp = _WinAPI_CreateSolidBitmap($hPic, _WinAPI_GetSysColor($COLOR_3DFACE), $Width, $Height)
_WinAPI_SelectObject($hFront, $hFont)
_WinAPI_SelectObject($hFront, $hBmp)
_WinAPI_DrawShadowText($hFront, 'Shadow Text', 0xF06000, 0x808080, 3, 3, $tRECT, BitOR($DT_CENTER, $DT_SINGLELINE, $DT_VCENTER))
_WinAPI_BitBlt($hBack, 0, 0, $Width, $Height, $hFront, 0, 0, $MERGECOPY)

_WinAPI_ReleaseDC($hPic, $hDC)

_SendMessage($hPic, $STM_SETIMAGE, 0, $hBitmap)
$hObj = _SendMessage($hPic, $STM_GETIMAGE)
If $hObj <> $hBitmap Then


Until GUIGetMsg() = -3

; #FUNCTION# ====================================================================================================================
; Name...........: _WinAPI_DrawShadowText
; Description....: Draws formatted text in the specified rectangle with a drop shadow.
; Syntax.........: _WinAPI_DrawShadowText ( $hDC, $sText, $rgbText, $rgbShadow [, $iXOffset [, $iYOffset [, $tRECT [, $iFlags]]]] )
; Parameters.....: $hDC       - Handle to a device context.
;                  $sText     - The string that contains the text to be drawn.
;                  $rgbText   - The color of the text, in RGB.
;                  $rgbShadow - The color of the shadow, in RGB.
;                  $iXOffset  - The x-coordinate of where the text should begin.
;                  $iYOffset  - The y-coordinate of where the text should begin.
;                  $tRECT     - $tagRECT structure that contains, in logical coordinates, the rectangle in which the text is to
;                               be drawn. If this parameter is 0, the size will be equal size of the device context ($hDC).
;                  $iFlags    - The flags that specifies how the text is to be drawn. This parameter can be a combination of
;                               the formatting text constants ($DT_...).
; Return values..: Success    - 1.
;                  Failure    - 0 and sets the @error flag to non-zero.
; Author.........: Rover
; Modified.......: Yashied
; Remarks........: None
; Related........:
; Link...........: @@MsdnLink@@ DrawShadowText
; Example........: Yes
; ===============================================================================================================================

Func _WinAPI_DrawShadowText($hDC, $sText, $rgbText, $rgbShadow, $iXOffset = 0, $iYOffset = 0, $tRECT = 0, $iFlags = 0)

    Local $Ret

    If Not IsDllStruct($tRECT) Then
        $tRECT = DllStructCreate($tagRECT)
        $Ret = DllCall('user32.dll', 'hwnd', 'WindowFromDC', 'hwnd', $hDC)
        $Ret = DllCall('user32.dll', 'int', 'GetClientRect', 'hwnd', $Ret[0], 'ptr', DllStructGetPtr($tRECT))
        If (@error) Or ($Ret[0] = 0) Then
            Return SetError(1, 0, 0)
    $Ret = DllCall('comctl32.dll', 'int', 'DrawShadowText', 'hwnd', $hDC, 'wstr', $sText, 'uint', -1, 'ptr', DllStructGetPtr($tRECT), 'dword', $iFlags, 'int', _WinAPI_SwitchColor($rgbText), 'int', _WinAPI_SwitchColor($rgbShadow), 'int', $iXOffset, 'int', $iYOffset)
    If (@error) Or ($Ret[0] = 0) Then
        Return SetError(1, 0, 0)
    Return 1
EndFunc   ;==>_WinAPI_DrawShadowText

; #FUNCTION# ====================================================================================================================
; Name...........: _WinAPI_SwitchColor
; Description....: Converts a color from BGR to RGB and back.
; Syntax.........: _WinAPI_SwitchColor ( $iColor )
; Parameters.....: $iColor - The color to conversion.
; Return values..: Converted color (RGB or BGR - depends on the $iColor value, BGR > RGB > BGR etc).
; Author.........: Yashied
; Modified.......:
; Remarks........: None
; Related........:
; Link...........: None
; Example........: No
; ===============================================================================================================================

Func _WinAPI_SwitchColor($iColor)
    Return BitOR(BitAND($iColor, 0x00FF00), BitShift(BitAND($iColor, 0x0000FF), -16), BitShift(BitAND($iColor, 0xFF0000), 16))
EndFunc   ;==>_WinAPI_SwitchColor
Edited by Yashied

Share this post

Link to post
Share on other sites
Michel Claveau


It is fun. Thanks.

But, in your exemple, you forget _WinAPI_FreeObject()

Share this post

Link to post
Share on other sites

Just add #include <WinAPIEx.au3> and remove Func _WinAPI_SwitchColor($iColor)!

Nice work as usual Yashied >_<


Edited by UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this