Page 1 of 1

Back On ShadowText

Posted: Mon Apr 10, 2017 8:38 pm
by bmcs
I am back trying to expand my knowledge of PB.

The following (highly condensed) code snippet draws shadowed text under Windows, but doesn't utilize the loaded font.
Questions:
1. I got an excellent answer from RASHAD in another thread, but can this this be done without using an API callback?
2. Can PB draw shadowed text under Linux?

Code: Select all

; Code adapted from forum topic:
; http://purebasic.fr/german/viewtopic.php?f=6&t=18567
;
Prototype DrawShadowText(hDC,pszText,cch,p,flags,crText,crShadow,ixOffset,iyOffset)
Lib = OpenLibrary(#PB_Any, "ComCtl32.dll")
Global DrawShadowText_.DrawShadowText = GetFunction(Lib, "DrawShadowText")
; Load the text font
LoadFont(0, "Times New Roman", 12, #PB_Font_Italic)
; Define the required items
Text$ = "This Is A Test"
w = 300 : h = 50
r.RECT : r\left = 0 : r\top = 10 : r\right = w : r\bottom = h
Flags.l = #DT_CENTER | #DT_TOP
CreateImage(1, w, h)
; Start the drawing
hDC = StartDrawing(ImageOutput(1))
  Box(0, 0, w, h, #White)
  DrawingFont(FontID(0)) ; This appears to be the wrong option
  DrawShadowText_(hDC, @Text$, Len(Text$), r, Flags, #Blue, #Gray, 4, 3)
StopDrawing()
; Obviously the font displayed is not "Times New Roman"
OpenWindow(0, 0, 0, 400, 200, "Draw Shadow Text", #PB_Window_SystemMenu | #PB_Window_ScreenCentered) 
ImageGadget(1, 50, 50, w, h, ImageID(1))
Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
Dave

Re: Back On ShadowText

Posted: Mon Apr 10, 2017 9:15 pm
by mk-soft
Simple example
But not perfect...

Code: Select all

Procedure DrawShadowText(x, y, text.s, c1 = 0, c2 = $C0C0C0)
  DrawingMode(#PB_2DDrawing_Transparent)
  DrawText(x + 2 , y + 3, text, c2 | $0F0F0F)
  DrawText(x + 1 , y + 2, text, c2)
  DrawText(x , y, text, c1)
  DrawingMode(#PB_2DDrawing_Default)
EndProcedure

LoadFont(0, "Times New Roman", 16);, #PB_Font_Italic)

If OpenWindow(0, 0, 0, 200, 200, "2DDrawing Example", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  If CreateImage(0, 200, 200) And StartDrawing(ImageOutput(0))
    DrawingMode(#PB_2DDrawing_Transparent)
    Box(0, 0, 200, 200, RGB(255, 255, 255))
    DrawingFont(FontID(0)) ; This appears to be the wrong option
    DrawShadowText(10, 10, "Hello World!" , #Blue)
    
    StopDrawing() 
    ImageGadget(0, 0, 0, 200, 200, ImageID(0))
  EndIf
  
  Repeat
    Event = WaitWindowEvent()
  Until Event = #PB_Event_CloseWindow
EndIf
Maybe someone has a better shadow function

Re: Back On ShadowText

Posted: Mon Apr 10, 2017 9:18 pm
by infratec
Hi,

if you use window API, than you need also window API for setting a font.

But since you ask for a crossplatform solution:

Why not something like this:

Code: Select all

Procedure DrawShadowText(x.i, y.i, Text$, color.i, shadowcolor.i)
  
  DrawText(x + 3, y + 3, Text$, shadowcolor)
  DrawText(x, y, Text$, color)
  
EndProcedure


LoadFont(0, "Times New Roman", 12, #PB_Font_Italic)
; Define the required items
Text$ = "This Is A Test"
w = 300
h = 50

CreateImage(1, w, h)

If StartDrawing(ImageOutput(1))
  Box(0, 0, w, h, #White)
  DrawingFont(FontID(0)) ; This appears to be the wrong option
  DrawingMode(#PB_2DDrawing_Transparent)
  DrawShadowText(100, 10, Text$, #Blue, #Gray)
  StopDrawing()
EndIf

OpenWindow(0, 0, 0, 400, 200, "Draw Shadow Text", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
ImageGadget(1, 50, 50, w, h, ImageID(1))
Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
This should work an all 3 OSs.

Bernd

Ups... to slow :mrgreen:

Re: Back On ShadowText

Posted: Mon Apr 10, 2017 9:44 pm
by bmcs
Thanks to you both, I now have the pointer for the direction I wanted to go.
Much appreciated.
Dave