[SOLVED] DrawText() & TextOut() WinAPI on Layered Window
Posted: Tue May 08, 2012 2:22 pm
[This issue has been solved by PureBasic expert Danilo] (linked)

In the following code, which creates a layered window, I'm able to draw text on it using PureBasic's DrawText() function, but get this cut-out effect (illustrated above) if the WinAPI DrawText() or TextOut() functions are used.
Does anyone know what causes this behaviour?
NOTE: You'll need a PNG image to test this code (or just download this one):

In the following code, which creates a layered window, I'm able to draw text on it using PureBasic's DrawText() function, but get this cut-out effect (illustrated above) if the WinAPI DrawText() or TextOut() functions are used.
Does anyone know what causes this behaviour?
NOTE: You'll need a PNG image to test this code (or just download this one):
Code: Select all
; modified from original code by Le Soldat Inconnu - Thank You!
; http://forum.purebasic.com/english/viewtopic.php?f=12&t=39079&hilit=transparency
UsePNGImageDecoder()
Enumeration
#MainWindow
#imageFile
EndEnumeration
Global.l appQuit
Procedure WndProc(hWnd, uMsg, wParam, lParam)
Shared sysProc
result = CallWindowProc_(sysProc, hWnd, uMsg, wParam, lParam)
Select uMsg
Case #WM_NCHITTEST
SendMessage_(hWnd, #WM_NCLBUTTONDOWN, #HTCAPTION, 0)
Case #WM_LBUTTONDOWN
If WindowMouseX(#MainWindow)>0 And WindowMouseX(#MainWindow)<60
If WindowMouseY(#MainWindow)>0 And WindowMouseY(#MainWindow)<60
appQuit = 1
ProcedureReturn 0
EndIf
EndIf
EndSelect
ProcedureReturn result
EndProcedure
Procedure AlphaImageWindow(WindowID, ImageID, Alpha)
Protected Image_HDC, Image_Bitmap.BITMAP, Image_BitmapInfo.BITMAPINFO, ContextOffset.POINT, Blend.BLENDFUNCTION
Protected xx, yy, x, y, Rouge, Vert, Bleu, AlphaChannel
Protected Dim Echelle.f($FF)
For x = 0 To $FF
Echelle(x) = x / $FF
Next
Image_HDC = CreateCompatibleDC_(#Null)
Image_Ancienne = SelectObject_(Image_HDC, ImageID)
GetObject_(ImageID, SizeOf(BITMAP), @Image_Bitmap)
Image_BitmapInfo\bmiHeader\biSize = SizeOf(BITMAPINFOHEADER)
Image_BitmapInfo\bmiHeader\biWidth = Image_Bitmap\bmWidth
Image_BitmapInfo\bmiHeader\biHeight = Image_Bitmap\bmHeight
Image_BitmapInfo\bmiHeader\biPlanes = 1
Image_BitmapInfo\bmiHeader\biBitCount = 32
xx = Image_Bitmap\bmWidth - 1
yy = Image_Bitmap\bmHeight - 1
Protected Dim Image.l(xx, yy)
GetDIBits_(Image_HDC, ImageID, 0, Image_Bitmap\bmHeight, @Image(), @Image_BitmapInfo, #DIB_RGB_COLORS)
For x = 0 To xx
For y = 0 To yy
Couleur = Image(x, y)
AlphaChannel = Couleur >> 24 & $FF
If AlphaChannel < $FF
Rouge = (Couleur & $FF) * Echelle(AlphaChannel)
Vert = (Couleur >> 8 & $FF) * Echelle(AlphaChannel)
Bleu = (Couleur >> 16 & $FF) * Echelle(AlphaChannel)
Image(x, y) = Rouge | Vert << 8 | Bleu << 16 | AlphaChannel << 24
EndIf
Next
Next
SetDIBits_(Image_HDC, ImageID, 0, Image_Bitmap\bmHeight, @Image(), @Image_BitmapInfo, #DIB_RGB_COLORS)
Blend\AlphaFormat = 1
Blend\BlendOp = 0
Blend\BlendFlags = 0
Blend\SourceConstantAlpha = Alpha
UpdateLayeredWindow_(WindowID, 0, 0, @Image_BitmapInfo + 4, Image_HDC, @ContextOffset, 0, @Blend, 2)
SelectObject_(Image_HDC, Image_Ancienne)
DeleteDC_(Image_HDC)
EndProcedure
Define imageFile.s = OpenFileRequester("Select Image:", "", "PNG|*.png", 0)
If imageFile And LoadImage(#imageFile, imageFile)
wFlags = #PB_Window_BorderLess | #PB_Window_ScreenCentered | #PB_Window_Invisible
OpenWindow(#MainWindow, #PB_Any, #PB_Any, ImageWidth(#imageFile), ImageHeight(#imageFile), "Layered Window", wFlags)
sysProc = SetWindowLong_(WindowID(#MainWindow), #GWL_WNDPROC, @WndProc())
SetWindowLong_(WindowID(#MainWindow), #GWL_EXSTYLE, GetWindowLong_(WindowID(#MainWindow), #GWL_EXSTYLE) | #WS_EX_LAYERED)
HideWindow(#MainWindow, 0)
;PureBasic drawing
;==============
LoadFont(1, "Arial", 20, #PB_Font_Bold)
StartDrawing(ImageOutput(#imageFile))
DrawingFont(FontID(1))
DrawingMode(#PB_2DDrawing_Transparent)
DrawText(50, 130, "SOLID TEXT...", #Red)
StopDrawing()
;Windows drawing
;=============
ddc = StartDrawing(ImageOutput(#imageFile))
SelectObject_(ddc, FontID(1))
SetBkMode_(ddc, #TRANSPARENT)
SetTextColor_(ddc, $FFFF)
TextOut_(ddc, 50, 180, "CUT-OUT TEXT!", 13)
StopDrawing()
AlphaImageWindow(WindowID(#MainWindow), ImageID(#imageFile), 255)
Repeat
Select WaitWindowEvent()
Case #PB_Event_CloseWindow
appQuit = 1
EndSelect
Until appQuit = 1
EndIf
End