Jump to content

(button) state polling


Rozek
 Share

Recommended Posts

Hello!

Is it possible to "poll" the state of a button (while another event handler is running)?

In my concrete case, I have a button which triggers a (possibly lengthy) file search and I would like to provide a way to abort it.

Simply using a "Stop" button does not work as the search executes within an event handler.

Will I have to "unwrap" the search (i.e., exit the event handler as soon as possible, allowing other event handlers to execute, and run the search step-by-step between two GetMsg invocations) or is there a simpler way? Unwrapping the search would require some code reorganization which I would like to avoid...

Thanks in advance for any help!

Andreas Rozek

Edited by Rozek
Link to comment
Share on other sites

Maybe using an adlib function would serve your purpose? (One that changes some global parameter which you check from the search code.) Or a checkbox or something else you can easily check the state of in the search code so that you don't have to catch the actual input any more...

Roses are FF0000, violets are 0000FF... All my base are belong to you.

Link to comment
Share on other sites

Hmmm,

my question might not have been precise enough...what I would like to know is:

how do I find out that a given button has been pressed (if I can't do ordinary event processing using GUIGetMsg)?

Within my file search loop, it is quite easy to check the button's state at regular intervals (thus, adlib functionality is not needed) but I don't know how to retrieve that state information.

Right now, I make sure, that the button does not have the keyboard focus at the beginning of the loop and check the focused control later - as soon as my StopButton get's the focus, the loop is aborted. This approach is nothing but a poor hack, however, it works...

Is there a better approach?

Andreas Rozek

Link to comment
Share on other sites

I suggest catching WM_COMMAND message (a button sends BN_CLICKED notification to the window when pressed).

Example:

Global Const $WM_COMMAND = 0x0111
Global Const $BN_CLICKED = 0
Global $stop = False

$gui = GUICreate("", 200, 200)
$btn_start = GUICtrlCreateButton("Start", 50, 50, 100, 50)
$btn_stop = GUICtrlCreateButton("Stop", 50, 100, 100, 50)
$label = GUICtrlCreateLabel("", 50, 10, 100, 20)

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

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $btn_start
            work()
    EndSwitch
WEnd

Func work()
    $stop = False
    While 1
    Sleep(1000)
    GUICtrlSetData($label, "Working... "&@MIN&":"&@SEC)
    If $stop Then ExitLoop
    WEnd
    GUICtrlSetData($label, "")
EndFunc
Func MY_WM_COMMAND($hWnd, $Msg, $wParam, $lParam)
    $nNotifyCode    = BitShift($wParam, 16)
    $nID            = BitAnd($wParam, 0x0000FFFF)
    If $nID = $btn_stop And $nNotifyCode = $BN_CLICKED Then $stop = True
EndFunc
Edited by Siao

"be smart, drink your wine"

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