check/uncheck Button controls (BM_SETCHECK v. BM_CLICK)

Get help with using AutoHotkey and its commands and hotkeys
User avatar
jeeswg
Posts: 6821
Joined: 19 Dec 2016, 01:58
Location: UK

check/uncheck Button controls (BM_SETCHECK v. BM_CLICK)

29 Sep 2019, 12:16

INTRO
- ControlSetChecked (AHK v2) and Control Check/Uncheck (AHK v1), check/uncheck(tick/untick) the checkbox in a Button control.
- The functions/subcommands, which use BM_CLICK in the source code, have the side effect of *focusing* the control.
- BM_SETCHECK does not have this side effect.
- [EDIT:] Presumably BM_CLICK has some advantage over BM_SETCHECK, perhaps it's more likely to work, otherwise why was it chosen?
- [EDIT:] In the tests I did, BM_SETCHECK worked, so any examples where BM_SETCHECK doesn't work would be useful.

QUESTION
- I'm posting in case anyone has any examples where ControlSetChecked (or Control Check/Uncheck) works, but where BM_SETCHECK doesn't work.
- Note: ticking/unticking a checkbox could conceivably fail in two ways:
- (1) complete failure: there is no change in appearance, and the window doesn't register the change
- (2) partial failure: there is a change in appearance (the tick is added/removed), however, the window does not seem to have registered the change [perhaps in such cases, a way to notify the control/window could be found]

- My 2 use cases were the Notepad Find dialog, and my own AutoHotkey GUIs, BM_SETCHECK worked fine in both cases.
- Thanks for reading.

Code: Select all

q:: ;toggle checkbox via BM_CLICK (ControlSetChecked)
;e.g. test on Notepad Find dialog
;note: has the side effect of focusing the checkbox
ControlGet, hCtl, Hwnd,, Button2, A
ControlGet, vIsChecked, Checked,,, % "ahk_id " hCtl
vSubCmd := vIsChecked ? "Uncheck" : "Check"
Control, % vSubCmd,,, % "ahk_id " hCtl ;uses BM_CLICK in the source code
return

w:: ;toggle checkbox via BM_SETCHECK
;e.g. test on Notepad Find dialog
;note: has the benefit of not focusing the checkbox
ControlGet, hCtl, Hwnd,, Button2, A
ControlGet, vIsChecked, Checked,,, % "ahk_id " hCtl
SendMessage, 0xF1, % !vIsChecked, 0,, % "ahk_id " hCtl ;BM_SETCHECK := 0xF1
return
Last edited by jeeswg on 29 Sep 2019, 19:08, edited 1 time in total.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
rommmcek
Posts: 694
Joined: 15 Aug 2014, 15:18

Re: check/uncheck Button controls (BM_SETCHECK v. BM_CLICK)

29 Sep 2019, 18:54

I'm using something like this:

Code: Select all

;ControlGet, hCtl, Hwnd,, Button3, A ; wrong
    winGet, hCtl, ID, A
    ControlGet, state, Checked,, Button3, % "ahk_id " hCtl
    while (state && A_Index < 50) {
        ControlClick, Button3, % "ahk_id " hCtl
        ControlGet, state, Checked,, Button3, % "ahk_id " hCtl
        Sleep, 25 ; if in "hurry" comment the sleep
    }
It works like a lightning, I don't remember of last fail!

P.s. off topic: OMG 4 24...
Last edited by rommmcek on 30 Sep 2019, 05:54, edited 2 times in total.
User avatar
jeeswg
Posts: 6821
Joined: 19 Dec 2016, 01:58
Location: UK

Re: check/uncheck Button controls (BM_SETCHECK v. BM_CLICK)

29 Sep 2019, 19:11

- @rommmcek: What I'm interested in is *clicking* a control, without *focusing* the control. If you use ControlSetChecked or ControlClick, they have the side effect of changing the keyboard focus to that control. BM_SETCHECK ticks/unticks the checkbox, without focusing the checkbox.
- Btw you could check over your script, to make it function correctly.
- Btw 2 I don't understand the PS.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
lexikos
Posts: 6652
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

Re: check/uncheck Button controls (BM_SETCHECK v. BM_CLICK)

29 Sep 2019, 19:49

Compare.
Sets the check state of a radio button or check box.
Source: BM_SETCHECK message - Windows applications | Microsoft Docs
Simulates the user clicking a button. This message causes the button to receive the WM_LBUTTONDOWN and WM_LBUTTONUP messages, and the button's parent window to receive a BN_CLICKED notification code.
Source: BM_CLICK message - Windows applications | Microsoft Docs
Applications cannot respond to the change in state if they are not notified. Think Gui Submit/GuiControlGet (pass) vs. g-label (fail).

But actually, there aren't any commands that use BM_CLICK. We post WM_LBUTTONDOWN/UP directly.
rommmcek
Posts: 694
Joined: 15 Aug 2014, 15:18

Re: check/uncheck Button controls (BM_SETCHECK v. BM_CLICK)

29 Sep 2019, 19:50

Yeah, I get it, but you can store current focus and change it afterwards (nobody will notice). >> It's a pseudo code <<

"P.s." is intentionally convoluted!
User avatar
jeeswg
Posts: 6821
Joined: 19 Dec 2016, 01:58
Location: UK

Re: check/uncheck Button controls (BM_SETCHECK v. BM_CLICK)

29 Sep 2019, 19:57

- @lexikos: Thanks for the info.
- Hmm. I suppose that: if we use BM_SETCHECK to toggle the Notepad Find dialog 'Match case' checkbox, Notepad isn't notified, but when we click 'Find Next' to search, Notepad is notified (about that button), and I'm guessing that the code is written such that it retrieves the checkbox state at that point.
- You're right, BM_CLICK isn't used. I was confused because it's *mentioned* 3 times in script_autoit.cpp (although isn't used).
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA

Return to “Ask For Help”

Who is online

Users browsing this forum: Bing [Bot], chinagreenelvis, Chunjee, Google [Bot], jmainardi, malaysoni, SuperFoobar, Zoanthario and 189 guests