Jump to content
Sign in to follow this  
obrienj

How do I shadow the text in the following example

Recommended Posts

obrienj

Given the code fragment below (assume all the pre-requisites are in-place), how do I shadow the text in the label?

I have searched and done some experimenting but I can't seem to get what I want, the text "Button" shadowed.

The code fragment is:

GUISetFont(16, 800, 0, "Arial")
$cLabel3 = GUICtrlCreateLabel("Button", 200, 140, 220, 50)
GUICtrlSetBkColor($cLabel3, $GUI_BKCOLOR_TRANSPARENT)

Regards,

Jim

Share this post


Link to post
Share on other sites
Xenobiologist

I found this searching my scripts with like 'shadow' :-)

#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', 600, 400, -1, -1, $WS_POPUP)
$Pic = GUICtrlCreatePic('', 20, 20, 460, 80)
$hPic = GUICtrlGetHandle($Pic)
;~ GUISetStyle($WS_POPUP, -1, $hForm)
$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)
$hFont = _WinAPI_CreateFont(65, 0, 0, 0, $FW_NORMAL, 0, 0, 0, $DEFAULT_CHARSET, $OUT_DEFAULT_PRECIS, $CLIP_DEFAULT_PRECIS, $ANTIALIASED_QUALITY, BitOR($DEFAULT_PITCH, $FF_DONTCARE), 'Arial')
$hBmp = _WinAPI_CreateSolidBitmap($hPic, _WinAPI_GetSysColor($COLOR_3DFACE), $Width, $Height)
_WinAPI_SelectObject($hFront, $hFont)
_WinAPI_SelectObject($hFront, $hBmp)
_WinAPI_DrawShadowText($hFront, 'Xenobiologist', 0x00FF00, 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)
_WinAPI_DeleteDC($hBack)
_WinAPI_DeleteDC($hFront)
_WinAPI_DeleteObject($hFont)
_WinAPI_DeleteObject($hBmp)
_SendMessage($hPic, $STM_SETIMAGE, 0, $hBitmap)
$hObj = _SendMessage($hPic, $STM_GETIMAGE)
If $hObj <> $hBitmap Then
_WinAPI_DeleteObject($hBitmap)
EndIf
_WInApi_SetLayeredWindowAttributes($hForm, 0xABCDEF, 255)
GUISetState()
Do
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)
  EndIf
EndIf
$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)
EndIf
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

Scripts & functions Organize Includes Let Scite organize the include files

Yahtzee The game "Yahtzee" (Kniffel, DiceLion)

LoginWrapper Secure scripts by adding a query (authentication)

_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)

Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.

MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times

Share this post


Link to post
Share on other sites
obrienj

Thank you.

Unfortunately this is one of several similar approaches I have experimented with and, like you, I do get a shadow. However, the one of the issues I am having is with the following line:

$hFont = _WinAPI_CreateFont(65, 0, 0, 0, $FW_NORMAL, 0, 0, 0, $DEFAULT_CHARSET, $OUT_DEFAULT_PRECIS, $CLIP_DEFAULT_PRECIS, $ANTIALIASED_QUALITY, BitOR($DEFAULT_PITCH, $FF_DONTCARE), 'Arial')

This is a "skinning" application and the only font size information I have is what the font is to be , eg., "Arial size 16" like the GUISetFont shows, but the illustrated line requires a font height and I can't find a way to get from one to the other other than a WAG.

I also encountered another issue with the text color in the "_WinAPI_DrawShadowText" call. If I specify total black 0x00000000, I get no shadow. However, if I specify 0x00000001, I get "black" and the requested shadow.

But I can live with that by simply changing any black text color request to 0x00000001.

However, the font size is a problem.

Again thanks.

Regards,

Jim

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  

×