Jump to content
Sign in to follow this  

Failsafe state/style detection ($WS_CAPTION)

Recommended Posts

After some research because I ran into some issue, I discovered the following:

Usually, if you want to check a GUI/button/checkbox/... style or state you simply do it via

If BitAnd(GUICtrlGetState($idCheckbox), $GUI_CHECKED) Then
In most cases this is a reliable method. Not for values like $WS_CAPTION.
#include <GUIConstants.au3>
$hGUI = GUICreate('Test', Default, Default, -1, -1, $WS_CAPTION) ;0x00C00000 (= 0x00400000 + 0x00800000)

_GUIHasCaption($WS_DLGFRAME) ;0x00400000
_GUIHasCaption($WS_BORDER) ;0x00800000

Func _GUIHasCaption($iStyle = 0)
    For $i = 1 To 2
        Local $aStyle = GUIGetStyle($hGUI), $bCaption = False
        If BitAND($aStyle[0], $WS_CAPTION) Then $bCaption = True
        $bCaption2 = (BitAND($aStyle[0], $WS_CAPTION) = $WS_CAPTION)
        MsgBox(0, ($i = 1 ? 'Caption' : 'No Caption + 0x' & Hex($iStyle)), _
        'GUI has caption: ' & $bCaption & ' (detection method 1)' & @CRLF & _
        'GUI has caption: ' & $bCaption2 & ' (detection method 2)')
        GUISetStyle(BitXOR($aStyle[0], $WS_CAPTION, $iStyle))
As you can see, the method (1) most people use doesn't work with styles and states using Hex values greater than 0x8 (0xA- 0xF) or in other words: values which already share the bits (1, 2, 4, 8) of smaller values. 0x00C00000 ($WS_CAPTION) = 0x00800000 + 0x00400000 so checking a style for $WS_CAPTION, BitAnd() also returns a positive number if there is anything >= 0x00400000 (=$WS_DLGFRAME) in it, because bit 4 is in 0x4 to 0x7 and bit 8 in 0x8 to 0xF.

Via method 2 you can make sure to always get the correct result: BitAnd($iCheck, $iWanted, ..., $iN) = $iWanted returns true, only if $iWanted is inside of $iCheck (and all other $iN's if neccessary).

I just wondered why this info was nowhere to read. I had a hard time finding out why some functions in my script(s) didn't always behave as expected, because of that. I had to figure it out myself and refresh my knowledge about bits and bytes, so it wasn't that bad :)

Thats why I wanted to share this info, so it can also help others.

I tried to explain this here in detail, but I failed because I'm no native English speaker. I deleted my bad explanation, so If anyone else wants to explain this behavior with details about bit-operations so it is easier to understand, I'd be pleased if you'd share your knowledge here.

Edited by Synix

Share this post

Link to post
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
Sign in to follow this  

  • Create New...