Jump to content

Recommended Posts

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 

  1. The handle returned from CreateGUI is 0 (despite there being NO error or extended information) 
  2. 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. 

#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)


   $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)


   $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)


   Return $hGUI

Func OverlayWindow_Exit()
   ConsoleWrite("* * * Exit event called" & @CRLF)

Func Handle_Esc()
   $Done = True



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


_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




Edited by GregEisenberg
changed post title
Link to comment
Share on other sites

  • Moderators

Moved to the appropriate thread, as the DEV forum very clearly states:


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

Func OverlayWindow($hwndParent, $rectToHighlight, $rectForText, $textToWrite)

   $style = $WS_POPUP

   $hGUI = GUICreate("transparent overlay", 420, 420, 0, 0, $style, $exstyle)
  _WinAPI_SetLayeredWindowAttributes($hGUI, 0x112233, 0, $LWA_COLORKEY)

   ConsoleWrite("**** $hGUI: " & $hGUI & @CRLF)


Link to comment
Share on other sites

@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. 

  _WinAPI_SetLayeredWindowAttributes($hGUI, 0xFFFFFF, 0, $LWA_COLORKEY)


So again - much thanks!  


Link to comment
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

  • Create New...