GregEisenberg Posted June 15, 2018 Share Posted June 15, 2018 (edited) Hi there I am building a "statistics kiosk" where we will be enumerating through Web Pages, and Excel files, PDF reports and other applications each displaying statistics relevant to our project. I want to overlay some context over each page/screen so that viewers of this "statistics kiosk" have some info about that the data they are looking at is. I have written a function called OverlayWindow which is supposed to create a transparent window which displays some text. The function SEEMS to work BUT The handle returned from CreateGUI is 0 (despite there being NO error or extended information) The window is created but not maintained. In other words it appears but since it is not really maintained it is repainted as other content changes. The result is that the overlay appears BUT as soon as there is a reason for the underlying windows to repaint the overlay starts to "disappear" I have extracted the relevant code and created the most simplistic of samples here. expandcollapse popup#include <FileConstants.au3> #include <WinAPIFiles.au3> #include <GDIPlus.au3> #include <GUIConstantsEx.au3> #include <WindowsConstants.au3> #include <WinAPISysWin.au3> Opt("GUIOnEventMode", 1) ; Change to OnEvent mode ; #FUNCTION# ==================================================================================================================== ; Name ..........: OverlayWindow ; Description ...: Throws up a child window with transparent background over current window ; ; Parameters : ; $hwndParent parent hwnd ; $rectToHighlight rect to draw (or if null, skip) ; $rectForText rect for textToWrite ; $textToWrite the text to write ; =============================================================================================================================== Func OverlayWindow($hwndParent, $rectToHighlight, $rectForText, $textToWrite) $style = BitOR($WS_CHILD, $WS_CLIPCHILDREN, $WS_CLIPSIBLINGS) $exstyle = BitOR($WS_EX_TOPMOST, $WS_EX_COMPOSITED, $WS_EX_TRANSPARENT) $hGUI = GUICreate("transparent overlay", -1, -1, -1, -1, $style, $exstyle) ConsoleWrite("**** $hGUI: " & $hGUI & @CRLF) ConsoleWrite("**** @error: " & @error & @CRLF) ConsoleWrite("**** @extended: " & @extended & @CRLF) GUISetOnEvent($GUI_EVENT_CLOSE, "OverlayWindow_Exit") GUISetState(@SW_SHOW, $hGUI) $hGraphics = _GDIPlus_GraphicsCreateFromHWND($hGUI) ConsoleWrite("**** $hGraphics : " & $hGraphics & @CRLF) _GDIPlus_GraphicsSetSmoothingMode($hGraphics, $GDIP_SMOOTHINGMODE_HIGHQUALITY) $hPen = _GDIPlus_PenCreate(0xFFFF0000, 4) ; Red If ($rectToHighlight <> Null) Then _GDIPlus_GraphicsDrawRect($hGraphics, $rectToHighlight[0], $rectToHighlight[1], $rectToHighlight[2], $rectToHighlight[3], $hPen) EndIf _GDIPlus_PenDispose($hPen) $hBrush = _GDIPlus_BrushCreateSolid(0xFFFF0000) ; RED $hFormat = _GDIPlus_StringFormatCreate() $hFamily = _GDIPlus_FontFamilyCreate("Arial") $hFont = _GDIPlus_FontCreate($hFamily, 14, 2) $tLayout = _GDIPlus_RectFCreate($rectForText[0], $rectForText[1], $rectForText[2], $rectForText[3]) $aInfo = _GDIPlus_GraphicsMeasureString($hGraphics, $textToWrite, $hFont, $tLayout, $hFormat) _GDIPlus_GraphicsDrawStringEx($hGraphics, $textToWrite, $hFont, $aInfo[0], $hFormat, $hBrush) _GDIPlus_BrushDispose($hBrush) _GDIPlus_StringFormatDispose($hFormat) _GDIPlus_FontFamilyDispose($hFamily) _GDIPlus_FontDispose($hFont) _GDIPlus_GraphicsDispose($hGraphics) Return $hGUI EndFunc Func OverlayWindow_Exit() ConsoleWrite("* * * Exit event called" & @CRLF) EndFunc Func Handle_Esc() $Done = True EndFunc ;----------------------------------------------------------------- ;Main() _GDIPlus_Startup() Global $Done = False Local $rect = [10, 10, 400, 400] Local $rectForText = [15, 15, 380, 380] $hGUI = OverlayWindow(Null, $rect, $rectForText, "This is a test with long text long text and should automatically wrap long text " & @CRLF & "and " & @CRLF & " handle " & @CRLF & "cariage returns and line feeds") HotKeySet ( "{Esc}", Handle_Esc) While Not $Done Sleep(100) WEnd GUIDelete($hGUI) _GDIPlus_Shutdown () Exit 0 I am assuming the since CreateGUI returns 0 that the _GDIPlus_GraphicsCreateFromHWND is simply creating a "graphics context" based on the desktop then and not on my window. And thus - there IS no window (despite no @error nor no @extended data) So I think the issue is in the "styles" of window I am using. If I don't use WS_CHILD (for example I use WS_POPUP) then I get a handle back for my window and the lifetime of the window is what i want but it is not transparent as I want. Setting styles on the window to be transparent (either using extended styles, or using WinSetTrans or _WinAPI_SetBkMode) results in my rectangle and string being transparent - not what I want either. Any suggestions? any and all help is appreciated Thanks! Edited June 15, 2018 by GregEisenberg changed post title Link to comment Share on other sites More sharing options...
Moderators JLogan3o13 Posted June 15, 2018 Moderators Share Posted June 15, 2018 Moved to the appropriate thread, as the DEV forum very clearly states: Quote Do not create AutoIt-related topics here "Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball How to get your question answered on this forum! Link to comment Share on other sites More sharing options...
InnI Posted June 15, 2018 Share Posted June 15, 2018 Func OverlayWindow($hwndParent, $rectToHighlight, $rectForText, $textToWrite) $style = $WS_POPUP $exstyle = BitOR($WS_EX_TOPMOST, $WS_EX_TRANSPARENT, $WS_EX_LAYERED) $hGUI = GUICreate("transparent overlay", 420, 420, 0, 0, $style, $exstyle) GUISetBkColor(0x112233) _WinAPI_SetLayeredWindowAttributes($hGUI, 0x112233, 0, $LWA_COLORKEY) ConsoleWrite("**** $hGUI: " & $hGUI & @CRLF) ... Skysnake and GregEisenberg 1 1 Link to comment Share on other sites More sharing options...
GregEisenberg Posted June 16, 2018 Author Share Posted June 16, 2018 @InnI If you are ever near Los Angeles I owe you a beer (or two!) I was pretty sure I did not want to use the WS_CHILD style and was fairly sure I wanted WS_POPUP, but I was not aware of the need for GUISetBkColor and _WinAPI_SetLayeredWindowAttributes Since you specified the color of 0x112233 in both APIs I did a little playing and this only works if they are BOTH the same color.. which I i know understand better why that is, however using 0xFFFFFF is a much better experience since it does not cause funny aliasing around the various items drawn in the overlay. _GUISetBkColor(0xFFFFFF) _WinAPI_SetLayeredWindowAttributes($hGUI, 0xFFFFFF, 0, $LWA_COLORKEY) So again - much thanks! Greg Link to comment Share on other sites More sharing options...
InnI Posted June 16, 2018 Share Posted June 16, 2018 10 hours ago, GregEisenberg said: If you are ever near Los Angeles I owe you a beer (or two!) Too far from me... but thank you Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now