Jump to content

Colored Button Reacts to the Enter key used in an Input box.


Go to solution Solved by Dan_555,

Recommended Posts

Posted (edited)

Hi, 

somehow this button reacts to the Enter from the input box. 

Try pressing enter in the input box, to see that it is not reacting.

Then click on the test button and then activate the input box and press enter few times. 

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <GuiEdit.au3>

#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Test", 207, 369)
$Inp1 = GUICtrlCreateInput("0", 2, 19, 121, 22)
GUICtrlSetFont($Inp1, 12, 700, 0, "Ubuntu Mono")

$BTN01 = GUICtrlCreateButton("TEST", 70, 0, 40, 16)
GUICtrlSetBkColor($BTN01, 0x00FF00)
GUICtrlSetFont($BTN01, 8, 700, 0, "Ubuntu Mono")

$Edit1 = GUICtrlCreateEdit("", 3, 86, 201, 281)
GUICtrlSetData($Edit1, "")
GUICtrlSetFont($Edit1, 10, 700, 0, "Consolas")
GUICtrlSetLimit($Edit1, 15000000)

GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
;"            "

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $BTN01
                GUICtrlSetData ($Edit1,"Test" & @CRLF,1)
    EndSwitch
WEnd

So far i have located the error. 

If you remove the GUICtrlSetBkColor($BTN01, 0x00FF00)  line, the script is working as intended.

Any ideas of why is this happening  or how to fix this to work as a colored button ?

Edited by Dan_555

Some of my script sourcecode

Posted
#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3> ;;; https://www.autoitscript.com/forum/topic/213492-button-reacts-to-the-enter-key-used-in-an-input-box/
#include <GuiEdit.au3>

Opt("GUIOnEventMode", 1)

#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Test", 207, 369)
GUISetOnEvent($GUI_EVENT_CLOSE, OnEvent_CLOSE)

$Inp1 = GUICtrlCreateInput("0", 2, 19, 121, 22)
GUICtrlSetFont($Inp1, 12, 700, 0, "Ubuntu Mono")
GUICtrlSetOnEvent(-1, OnEvent_Inp1)

$Edit1 = GUICtrlCreateEdit("", 3, 86, 201, 281)
GUICtrlSetData($Edit1, "")
GUICtrlSetFont($Edit1, 10, 700, 0, "Consolas")
GUICtrlSetLimit($Edit1, 15000000)
GUICtrlSetOnEvent(-1, OnEvent_Edit1)

$BTN1 = GUICtrlCreateButton("TEST", 70, 0, 40, 16)
GUICtrlSetFont($BTN1, 8, 700, 0, "Ubuntu Mono")
GUICtrlSetBkColor($BTN1, 0x00F000)
GUICtrlSetOnEvent(-1, OnEvent_BTN1)


GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
;"            "

While 1
    Sleep(100)
WEnd

;~  $nMsg = GUIGetMsg()
;~  Switch $nMsg
;~      Case $GUI_EVENT_CLOSE
;~          Exit
;~      Case $BTN1
;~          GUICtrlSetData($Edit1, "Test" & @CRLF, 1)
;~  EndSwitch
;~ WEnd


Func OnEvent_CLOSE()
    GUIDelete()
    Exit
EndFunc

Func OnEvent_Inp1()
    GUICtrlSetData($Edit1, "Inp1" & @CRLF, 1)
EndFunc

Func OnEvent_Edit1()
;~  GUICtrlSetData($Edit1, "Edit1" & @CRLF, 1)
EndFunc

Func OnEvent_BTN1()
    GUICtrlSetData($Edit1, "Test" & @CRLF, 1)
EndFunc

Also happens on event.

Follow the link to my code contribution ( and other things too ).
FAQ - Please Read Before Posting.
autoit_scripter_blue_userbar.png

  • Solution
Posted (edited)

Ok, i have found a 'workaround' for this problem.

The solution is to Create another colored button, which does nothing. I made one with the coordinates/size of 1,1,1,1 which makes it  invisible.

Then after using each colored button, add ControlClick to this button and it will catch the input/return instead.

  

#include <GUIConstantsEx.au3>

#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Test", 207, 280)
$Inp1 = GUICtrlCreateInput("0", 2, 19, 121, 22)
GUICtrlSetFont($Inp1, 12, 700, 0, "Ubuntu Mono")

$BTNNOTHING = GUICtrlCreateButton("TEST", 1,1, 1, 1)
GUICtrlSetBkColor($BTNNOTHING, 0x00FF00)

$BTN01 = GUICtrlCreateButton("TEST", 70, 0, 40, 16)
GUICtrlSetFont($BTN01, 8, 700, 0, "Ubuntu Mono")
GUICtrlSetBkColor($BTN01, 0x00FF00)

$Edit1 = GUICtrlCreateEdit("", 3, 86, 201, 181)
GUICtrlSetData($Edit1, "")
GUICtrlSetFont($Edit1, 10, 700, 0, "Consolas")
GUICtrlSetLimit($Edit1, 15000000)

GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
;"            "

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $BTN01
                GUICtrlSetData ($Edit1,"Test" & @CRLF,1)
                ControlClick($Form1,"",$BTNNOTHING)
    EndSwitch
WEnd

 

Edited by Dan_555

Some of my script sourcecode

  • Dan_555 changed the title to Colored Button Reacts to the Enter key used in an Input box.
Posted (edited)

I'm not sure if this was mentioned in the other thread, but after a bit of a play I found GUICtrlSetBkColor() is adding the $BS_DEFPUSHBUTTON style to the button. 

You can revoke it, but it breaks the custom drawing:

$iStyle = _WinAPI_GetWindowLong(GUICtrlGetHandle($idBtn),  $GWL_STYLE)
ConsoleWrite("Old Style: " & Hex($iStyle, 8) & @CRLF)
$iStyle = BitAnd(BitNOT($BS_DEFPUSHBUTTON), $iStyle)
_WinAPI_SetWindowLong(GUICtrlGetHandle($idBtn), $GWL_STYLE, $iStyle)
$iStyle = _WinAPI_GetWindowLong(GUICtrlGetHandle($idBtn),  $GWL_STYLE)
ConsoleWrite("New Style: " & Hex($iStyle, 8) & @CRLF)

Basically when you press enter in an edit without ES_WANTRETURN, it sends a WM_COMMAND a message that IDOK was clicked.  But if you have a default button defined, that gets sent in place of IDOK.. (Edit: to clarify,  this is expected behavior if you have a default button!)

As a side, when you change the text of the input box and press enter - autoit also sends a WM_COMMAND containing the ControlID of the edit, and a notification code of 0. This one isn't a normally sent by windows - so i guess its part of the mechanism for GuiGetMsg. 

Edited by MattyD
Posted (edited)

  It is curious that the button doesn't initially act like a "default" button though, despite having the style. Once the green button takes focus (clicking, or tabbing past it) it starts to act like a default button should (You just get IDOKs before then!)

Manually setting $DEFPUSHBUTTON (without the GuiCtrlSetBkCol) acts as you'd expect. So there's some funky stuff going on in there...

Nevermind, it seems you need to send DM_SETDEFID to the parent for the default button to take effect. But autoit obviously does that on your behalf..

#AutoIt3Wrapper_UseX64=Y

#include <WinAPI.au3>
#include <GuiConstants.au3>
#include <AutoItConstants.au3>
#include <WindowsConstants.au3>
#include <GuiEdit.au3>

Global $mEdtMsg[]
$mEdtMsg[0x0100] = "EN_SETFOCUS"
$mEdtMsg[0x0200] = "EN_KILLFOCUS"
$mEdtMsg[0x0300] = "EN_CHANGE"
$mEdtMsg[0x0400] = "EN_UPDATE"
$mEdtMsg[0x0500] = "EN_ERRSPACE"
$mEdtMsg[0x0501] = "EN_MAXTEXT"
$mEdtMsg[0x0601] = "EN_HSCROLL"
$mEdtMsg[0x0602] = "EN_VSCROLL"
$mEdtMsg[0x0700] = "EN_ALIGN_LTR_EC"
$mEdtMsg[0x0701] = "EN_ALIGN_RTL_EC"

Global $mBtnMsg[]
$mBtnMsg[0x0000] = "BN_CLICKED"
$mBtnMsg[0x0001] = "BN_PAINT"
$mBtnMsg[0x0002] = "BN_PUSHED"
$mBtnMsg[0x0003] = "BN_UNPUSHED"
$mBtnMsg[0x0004] = "BN_DISABLE"
$mBtnMsg[0x0005] = "BN_DBLCLK"
$mBtnMsg[0x0006] = "BN_SETFOCUS"
$mBtnMsg[0x0007] = "BN_KILLFOCUS"

Global $hGUI = GUICreate("Test GUI", 200, 220)
Global $idBtn1 = GUICtrlCreateButton("Button1", 4, 4, 96, 25)
Global $idBtn2 = GUICtrlCreateButton("Button2", 102, 4, 96, 25)
Global $idInput = GUICtrlCreateInput("Input", 4, 33, 192, 25)
Global $idEdit = GUICtrlCreateEdit("", 4, 62, 192, 150)

Global $mCtrlNames[]
$mCtrlNames[$IDOK] = "IDOK"
$mCtrlNames[$IDCANCEL] = "IDCANCEL"
$mCtrlNames[$idBtn1] = "Button1"
$mCtrlNames[$idBtn2] = "Button2"
$mCtrlNames[$idInput] = "Input"
$mCtrlNames[$idEdit] = "Edit"

$iStyle = _WinAPI_GetWindowLong(GUICtrlGetHandle($idBtn2), $GWL_STYLE)
ConsoleWrite("Original Style    : " & Hex($iStyle, 8) & @CRLF)

GuiCtrlSetBkColor($idBtn2, 0x00FF00)

$iStyle = _WinAPI_GetWindowLong(GUICtrlGetHandle($idBtn2), $GWL_STYLE)
ConsoleWrite("SetBkCol Style    : " & Hex($iStyle, 8) & @CRLF)

;Clear $BS_DEFPUSHBUTTON
;~ $iStyle = BitAnd(BitNOT($BS_DEFPUSHBUTTON), $iStyle)
;~ ConsoleWrite("Def Cleared Style : " & Hex($iStyle, 8) & @CRLF)
;~ $iStyle = _WinAPI_SetWindowLong(GUICtrlGetHandle($idBtn2), $GWL_STYLE, $iStyle)

$iStyle = _WinAPI_GetWindowLong(GUICtrlGetHandle($idBtn2), $GWL_STYLE)
ConsoleWrite("Final Style       : " & Hex($iStyle, 8) & @CRLF)

GUIRegisterMsg($WM_COMMAND, "WM_COMMAND")
GUISetState()

Local $iMsg
While 1
    $iMsg = GUIGetMsg()
    Switch $iMsg
        Case $GUI_EVENT_CLOSE
            ExitLoop
    EndSwitch
WEnd

GUIDelete($hGUI)

Func WM_COMMAND($hWnd, $iMsg, $wParam, $lParam)
    Local $iNotifCode = _WinAPI_HiWord($wParam)
    Local $iCtrlId = _WinAPI_LoWord($wParam)
    Local $hCtrl = $lParam
    Local $sCode

    Switch _WinAPI_GetClassName($lParam)
        Case "Edit"
            If MapExists($mEdtMsg, $iNotifCode) Then $sCode = $mEdtMsg[$iNotifCode]
        Case "Button"
            If MapExists($mBtnMsg, $iNotifCode) Then $sCode = $mBtnMsg[$iNotifCode]
    EndSwitch

    ConsoleWrite(StringFormat("%-20s %-10s %-20s Hwnd: %-10s \r\n", _
            "WM_COMMAND", _
            MapExists($mCtrlNames, $iCtrlId) ? $mCtrlNames[$iCtrlId] : $iCtrlId,  _
            $sCode ? $sCode : Hex($iNotifCode, 4), _
            $lParam))

    Return $GUI_RUNDEFMSG
EndFunc
Edited by MattyD
Posted

@MattyD Yes very funky.  Moreover if you check the style you see something unexpected.

#include <WinAPI.au3>
#include <GUIConstantsEx.au3>
#include <ButtonConstants.au3>
#include <GuiButton.au3>
#include <WindowsConstants.au3>

ConsoleWrite(Hex($WS_CHILD) & "/" & Hex($WS_TABSTOP) & "/" & Hex($WS_VISIBLE) & "/" & Hex($BS_NOTIFY) & "/" & Hex($BS_OWNERDRAW) & @CRLF)

$Form1 = GUICreate("Test", 207, 280)

$idBtn = GUICtrlCreateButton("Non UDF", 10, 10, 100, 25)
$iStyle = _WinAPI_GetWindowLong(GUICtrlGetHandle($idBtn),  $GWL_STYLE)
ConsoleWrite("before: " & Hex($iStyle, 8) & @CRLF)
GUICtrlSetBkColor(-1, 0x00FF00)
$iStyle = _WinAPI_GetWindowLong(GUICtrlGetHandle($idBtn),  $GWL_STYLE)
ConsoleWrite("after: " & Hex($iStyle, 8) & @CRLF)

$hBtn = _GUICtrlButton_Create($Form1, "UDF", 10, 40, 100, 25, $BS_OWNERDRAW)
$iStyle = _WinAPI_GetWindowLong($hBtn,  $GWL_STYLE)
ConsoleWrite("udf: " & Hex($iStyle, 8) & @CRLF)

$idBtn2 = GUICtrlCreateButton("Non UDF 2", 10, 70, 100, 25, $BS_OWNERDRAW)
$iStyle = _WinAPI_GetWindowLong(GUICtrlGetHandle($idBtn2),  $GWL_STYLE)
ConsoleWrite("after 2: " & Hex($iStyle, 8) & @CRLF)

GUISetState(@SW_SHOW)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
WEnd

Setting color of standard button seems to set BS_OWNERDRAW (not $BS_DEFPUSHBUTTON)

But trying to set BS_OWNERDRAW on standard button seems to set $BS_DEFPUSHBUTTON instead.

 

 

Posted

Oh ok that's a rookie mistake on my end -  I was treating it like a bitfield, which we obviously shouldn't be doing!  

$BS_OWNERDRAW = 0x0B which contains a 1 ($BS_DEFPUSHBUTTON).  I reckon that control proc is probably making the same mistake. (looking for BS_DEFPUSHBUTTON when the button is selected, then sending DM_SETDEFID)

Posted (edited)
40 minutes ago, Nine said:

But trying to set BS_OWNERDRAW on standard button seems to set $BS_DEFPUSHBUTTON instead.

Maybe its trying to clear 0x08 / 0x02 (BS_CHECKBOX, BS_RADIOBUTTON) given its a push button?? that leaves the 1 at the end.. just speculating.

0x0B (BS_OWNERDRAW) = 0x08 + 0x02 + 0x01

Edit: ok BS_RADIOBUTTON value was wrong too... apologies, need to go to sleep!

Edited by MattyD

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