Jump to content

WinGetTrans not always working?


Recommended Posts

I'm working with the code found on page

If I open an application window, e.g. "Untitled - Notepad", and use the code, I get an expected transparency value.

But when I attempt to implement the code in a project, passing a window handle to it, it doesn't pick it up.  Within the function I verified the window handle is passed correctly, and that the associated window still exists (WinExists()).  I also verified the dllcall is not @erroring.  It looks like the

Not $aRet[0]

is where it's dying, so the data isn't being returned by dllcall.

 

Since the code does work in testing, what might lead up to the dllcall failing in some circumstances?  My project does involve updating windows' states, such as minimize, normal, and transparency.

 

Edit: Adding the code I'm testing with:

Func WinGetTrans($iHwnd)
    if Not WinExists($iHwnd) then msgbox(0,"WinExists - " & $iHwnd, "not exist") ; testing
    Local $val = DllStructCreate("int")
    Local $aRet = DllCall("user32.dll", "int", "GetLayeredWindowAttributes", "hwnd", $iHwnd, "ulong_ptr", 0, "int_ptr", DllStructGetPtr($val), "ulong_ptr", 0)
    If @error Or Not $aRet[0] Then Return -1
    Return DllStructGetData($val, 1)
EndFunc

 

Edited by SlowCoder74
Added code ...
Link to comment
Share on other sites

Try this

#include <WinApi.au3>
#include <WindowsConstants.au3>

Local $iTransColor
Local $iTrans
Local $hWndNotepad = WinGetHandle("Untitled - Notepad")
If (@error) Then Exit MsgBox("", "Failed", "Failed to get notepad handle")

Local $iInfo = _WinAPI_GetLayeredWindowAttributes($hWndNotepad, $iTransColor, $iTrans)
If ($iTransColor = -1) Then $iTransColor = "N/A"
If ($iTrans = -1) Then $iTrans = "N/A"

MsgBox("", WinGetTitle($hWndNotepad) & " window attributes", "Information about window '" & WinGetTitle($hWndNotepad) & "' with handle " & $hWndNotepad & ": " & @CRLF & _
        "Transparent Color: " & $iTransColor & @CRLF & _
        "Alpha Value: " & $iTrans & @CRLF & _
        "LWA_COLORKEY: " & (BitAND($iInfo, $LWA_COLORKEY) = $LWA_COLORKEY) & @CRLF & _
        "LWA_ALPHA: " & (BitAND($iInfo, $LWA_ALPHA) = $LWA_ALPHA))

 

Link to comment
Share on other sites

@InunoTaishou,

That's a no-go.  It sees the window (provides handle), but the values are N/A and false, respectively.

At the same time, my following test code is working, returning 128:

Func _WinGetTrans($iHwnd)
    Local $val = DllStructCreate("int")
    Local $aRet = DllCall("user32.dll", "int", "GetLayeredWindowAttributes", "hwnd", $iHwnd, "ulong_ptr", 0, "int_ptr", DllStructGetPtr($val), "ulong_ptr", 0)
    If @error Or Not $aRet[0] Then Return -1
    Return DllStructGetData($val, 1)
EndFunc
$hWnd = WinGetHandle("Untitled - Notepad")
WinSetTrans($hWnd,"",128)
MsgBox(0,"",_WinGetTrans($hWnd))

 

Link to comment
Share on other sites

I think I figured out the the bug ...

Take this code:

Func _WinGetTrans($iHwnd)
    Local $val = DllStructCreate("int")
    Local $aRet = DllCall("user32.dll", "int", "GetLayeredWindowAttributes", "hwnd", $iHwnd, "ulong_ptr", 0, "int_ptr", DllStructGetPtr($val), "ulong_ptr", 0)
    If @error Or Not $aRet[0] Then Return -1
    Return DllStructGetData($val, 1)
EndFunc
$hWnd = WinGetHandle("Untitled - Notepad")
WinSetTrans($hWnd,"",254)
MsgBox(0,"",_WinGetTrans($hWnd))

If I use WinSetTrans($hWnd,"",255), the return value seems to alternate between 255 and the error code (-1) each time I execute.  Any other transparency value seems to return the value normally.  Simply setting the trans value to 254 allows my project to run as expected.

 

Is this a bug in AutoIt's WinSetTrans() function, or the dllcall?

Link to comment
Share on other sites

1 minute ago, InunoTaishou said:

It doesn't look like a bug at all. If it's 255 then there is no transparency at all, i.e., the alpha channel is at the maximum state. That's why I changed mine to N/A instead of 255, because the transparency is not applicable.

I'm not talking specifically of your code.  I'm talking about the inconsistent return values when the code I showed in post 4 is executed multiple times, without changing anything.

 

Steps to reproduce:

1.  Open a blank Notepad window.

2.  Paste the code in post 4 to Scite and run multiple times.

As is, with the WinSetTrans() at 254, it the value returned from _WinGetTrans() is consistently returned as 254.

3.  Change the WinSetTrans() to 255, and run multiple times.

At 255, I get -1 and 255 alternately returned to me each time I run.

 

Seems to me, the code should consistently error, or provide the same result every time.  Not the case here.  This is why, either my assumptions about setting transparency are incorrect, or there is a bug in the dll call, or WinSetTrans().

Link to comment
Share on other sites

The DllCall is using a MSDN Win32 function call. If you feel there is a bug there you could email, or create a ticket with, Microsoft and let them know. I don't know why it would give -1 and 255, in the few tests I used on it it just gave me -1. -1 is not an error though, 0 is an error.

After a few tests it alternates between -1 and 255 consistently.

Link to comment
Share on other sites

It's 255 and -1 over and over, with any other number being the actual transparency value. The transparency value is always -1 if you do NOT use WinSetTrans. If you open up notepad and just request the trans value it will always be -1 (at least with the test I just ran, opened notepad, got the trans value 10 times in a row, all -1). If you use WinSetTrans with 255 it will then start doing -1 and 255. I'm guessing -1 just means that the transparency is the max value.

Link to comment
Share on other sites

Alright.  Well, I'm gonna role with it.  My project code verifies the window exists before checking the transparency.  So I modified it so that if it receives -1 from _WinGetTrans(), then it pretends to be 255.  It's working well, so I'm happy.

But I would still like to know why the alternating values, if anyone is able to come up with an explanation.

Link to comment
Share on other sites

It's return -1 because the function return -1 if @error or "Not $aRet[0]"

If @error Or Not $aRet[0] Then Return -1

The _WinAPI_GetLayeredWindowAttributes in the WinAPI NEVER check the 0 based array but only two, three and four and just the @error from DllCall, i have stripped into the example so you can see with your eyes.  Next time:

1) Don't use DllCall if you don't know how to manage.

2) Check the help and MSDN for the function, failure = 0

; Johnmcloud - 2016
Local $iInfo, $iTransparency, $iTransColor, $hWnd

Run("notepad")
WinWait("[CLASS:Notepad]")
$hWnd = WinGetHandle("[CLASS:Notepad]")

WinSetTrans($hWnd, "", 255)

Local $iCounter = 0
Do
;~  WinSetTrans($hWnd, "", 255) you can un-comment out if you want
    $iCounter += 1
    $iInfo = _WinAPI_GetLayeredWindowAttributes($hWnd, $iTransColor, $iTransparency)
    If @error Then Exit ConsoleWrite("ERROR:" & @extended & @CRLF)
    ConsoleWrite("N:" & $iCounter & " - Alpha Value: " & $iTransparency & " " & @CRLF)
Until $iCounter = 100

Func _WinAPI_GetLayeredWindowAttributes($hWnd, ByRef $i_transcolor, ByRef $Transparency, $asColorRef = False)
    $i_transcolor = -1
    $Transparency = -1
    Local $aResult = DllCall("user32.dll", "bool", "GetLayeredWindowAttributes", "hwnd", $hWnd, "dword*", $i_transcolor, "byte*", $Transparency, "dword*", 0)
    If @error Then Return SetError(@error, @extended, 0)
    If Not $asColorRef Then
        $aResult[2] = Int(BinaryMid($aResult[2], 3, 1) & BinaryMid($aResult[2], 2, 1) & BinaryMid($aResult[2], 1, 1))
    EndIf
    $i_transcolor = $aResult[2]
    $Transparency = $aResult[3]
    Return $aResult[4]
EndFunc   ;==>_WinAPI_GetLayeredWindowAttributes

Result:

N:1 - Alpha Value: 255 
N:2 - Alpha Value: 255 
N:3 - Alpha Value: 255 
N:4 - Alpha Value: 255 
N:5 - Alpha Value: 255 
N:6 - Alpha Value: 255 
N:7 - Alpha Value: 255 
N:8 - Alpha Value: 255 
N:9 - Alpha Value: 255 
N:10 - Alpha Value: 255 
N:11 - Alpha Value: 255 
N:12 - Alpha Value: 255 
N:13 - Alpha Value: 255 
N:14 - Alpha Value: 255 
N:15 - Alpha Value: 255 
N:16 - Alpha Value: 255 
N:17 - Alpha Value: 255 
N:18 - Alpha Value: 255 
N:19 - Alpha Value: 255 
N:20 - Alpha Value: 255 
N:21 - Alpha Value: 255 
N:22 - Alpha Value: 255 
N:23 - Alpha Value: 255 
N:24 - Alpha Value: 255 
N:25 - Alpha Value: 255 
N:26 - Alpha Value: 255 
N:27 - Alpha Value: 255 
N:28 - Alpha Value: 255 
N:29 - Alpha Value: 255 
N:30 - Alpha Value: 255 
N:31 - Alpha Value: 255 
N:32 - Alpha Value: 255 
N:33 - Alpha Value: 255 
N:34 - Alpha Value: 255 
N:35 - Alpha Value: 255 
N:36 - Alpha Value: 255 
N:37 - Alpha Value: 255 
N:38 - Alpha Value: 255 
N:39 - Alpha Value: 255 
N:40 - Alpha Value: 255 
N:41 - Alpha Value: 255 
N:42 - Alpha Value: 255 
N:43 - Alpha Value: 255 
N:44 - Alpha Value: 255 
N:45 - Alpha Value: 255 
N:46 - Alpha Value: 255 
N:47 - Alpha Value: 255 
N:48 - Alpha Value: 255 
N:49 - Alpha Value: 255 
N:50 - Alpha Value: 255 
N:51 - Alpha Value: 255 
N:52 - Alpha Value: 255 
N:53 - Alpha Value: 255 
N:54 - Alpha Value: 255 
N:55 - Alpha Value: 255 
N:56 - Alpha Value: 255 
N:57 - Alpha Value: 255 
N:58 - Alpha Value: 255 
N:59 - Alpha Value: 255 
N:60 - Alpha Value: 255 
N:61 - Alpha Value: 255 
N:62 - Alpha Value: 255 
N:63 - Alpha Value: 255 
N:64 - Alpha Value: 255 
N:65 - Alpha Value: 255 
N:66 - Alpha Value: 255 
N:67 - Alpha Value: 255 
N:68 - Alpha Value: 255 
N:69 - Alpha Value: 255 
N:70 - Alpha Value: 255 
N:71 - Alpha Value: 255 
N:72 - Alpha Value: 255 
N:73 - Alpha Value: 255 
N:74 - Alpha Value: 255 
N:75 - Alpha Value: 255 
N:76 - Alpha Value: 255 
N:77 - Alpha Value: 255 
N:78 - Alpha Value: 255 
N:79 - Alpha Value: 255 
N:80 - Alpha Value: 255 
N:81 - Alpha Value: 255 
N:82 - Alpha Value: 255 
N:83 - Alpha Value: 255 
N:84 - Alpha Value: 255 
N:85 - Alpha Value: 255 
N:86 - Alpha Value: 255 
N:87 - Alpha Value: 255 
N:88 - Alpha Value: 255 
N:89 - Alpha Value: 255 
N:90 - Alpha Value: 255 
N:91 - Alpha Value: 255 
N:92 - Alpha Value: 255 
N:93 - Alpha Value: 255 
N:94 - Alpha Value: 255 
N:95 - Alpha Value: 255 
N:96 - Alpha Value: 255 
N:97 - Alpha Value: 255 
N:98 - Alpha Value: 255 
N:99 - Alpha Value: 255 
N:100 - Alpha Value: 255

 

Edited by johnmcloud
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...