Eambo

Help Understanding How to Interrupt a GUI Function without OnEvent

5 posts in this topic

Hi Everyone,

I know this question has been asked before, and even has a shiny tutorial page - but I'm having issues understanding the logic and just how this works. I was hoping some kind hearted hero might be able to help me understand things a bit better.

 

Problem: I have a "Stop" button that I want to interrupt functions at any time when pressed.

Scenario: I'm not using OnEvent

 

At the minute, I have the following code. I know that this works for closing at any time, so I was hoping to utilise something similar when my button is pressed:

 

Func On_WM_SYSCOMMAND($hWnd, $msg, $wParam, $lParam)
    Local Const $SC_MOVE = 0xF010
    Local Const $SC_SIZE = 0xF000
    Local Const $SC_CLOSE = 0xF060

         If BitAND($wParam, 0x0000FFFF) =  $BtnStop Then Exit

    Switch BitAND($wParam, 0xFFF0)
        Case $SC_MOVE
            ConsoleWrite("WM_Move detected" & @CRLF)
        Case $SC_SIZE
            ConsoleWrite("WM_Size detected" & @CRLF)
        Case $SC_CLOSE
            ConsoleWrite("WM_Close intercepted" & @CRLF)
            Exit
            Return -15
    EndSwitch

         Return $GUI_RUNDEFMSG

EndFunc

 

As you can imagine, the move/size/close cases all work awesomely - but I'm not having much luck with $BtnStop. The tutorial has mentions of using flags, but I was hoping I could circumvent that entirely if possible.

Anyone have any ideas on why this won't work who'd be willing to explain it to me? Or if it *is* possible, what I'm doing wrong?

Thank you!

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

For button messages you must use WM_COMMAND  :)

GUIRegisterMsg($WM_COMMAND, "WM_COMMAND")
;...
Func WM_COMMAND($hWnd, $Msg, $wParam, $lParam)
 #forceref $hWnd, $Msg, $lParam
 Switch BitAND($wParam, 0x0000FFFF)
    Case $my_button
        $stop =  1  ;<< use this to stop a loop (example)
     ;   Exit
 EndSwitch
  Return 'GUI_RUNDEFMSG'
EndFunc

 

Edited by mikell
1 person likes this

Share this post


Link to post
Share on other sites
12 minutes ago, mikell said:

For button messages you must use WM_COMMAND  :)

GUIRegisterMsg($WM_COMMAND, "WM_COMMAND")
;...
Func WM_COMMAND($hWnd, $Msg, $wParam, $lParam)
 #forceref $hWnd, $Msg, $lParam
 Switch BitAND($wParam, 0x0000FFFF)
    Case $my_button
        $stop =  1  ;<< use this to stop a loop (example)
     ;   Exit
 EndSwitch
  Return 'GUI_RUNDEFMSG'
EndFunc

 

Hi Mikell,

 

Thank you for the help! I may be doing something extremely silly, and my apologies if so. So using your example, I've tweaked as follows:

 

GUIRegisterMsg($WM_SYSCOMMAND, "On_WM_SYSCOMMAND")
GUIRegisterMsg($WM_COMMAND, "WM_COMMAND")
...
$BtnStop = GUICtrlCreateButton("Stop Action", 424, 136, 161, 65)
...

Func On_WM_SYSCOMMAND($hWnd, $msg, $wParam, $lParam)
    Local Const $SC_MOVE = 0xF010
    Local Const $SC_SIZE = 0xF000
    Local Const $SC_CLOSE = 0xF060



    Switch BitAND($wParam, 0xFFF0)
        Case $SC_MOVE
            ConsoleWrite("WM_Move detected" & @CRLF)
        Case $SC_SIZE
            ConsoleWrite("WM_Size detected" & @CRLF)
        Case $SC_CLOSE
            ConsoleWrite("WM_Close intercepted" & @CRLF)
            FileDelete(@TempDir & "\Heimerdinger_OriginalSkin.jpg")
            Exit
            Return -15
    EndSwitch

         Return $GUI_RUNDEFMSG

EndFunc 

Func WM_COMMAND($hWnd, $Msg, $wParam, $lParam)
 #forceref $hWnd, $Msg, $lParam
Switch BitAND($wParam, 0x0000FFFF)
    Case $BtnStop
        MsgBox(0,0,1)
        Return
        EndSwitch
  Return 'GUI_RUNDEFMSG'
EndFunc

 

Unfortunately now when compiling I get this:

 

"C:\Users\eambo\Desktop\Multitool\Multitool.au3" (389) : ==> Variable used without being declared.:
Case $BtnStop
Case ^ ERROR

I've tried a few things to remedy this, but none give me the desired results. I've tried commenting out the first "GUIRegisterMsg" incase they were conflicting, as well as declaring $BtnStop inside this function itself just out of interest - but it still does not show the messagebox I was trying to test.

 

I've tried even declaring $BtnStop as global just out of interest, but it isn't playing nice. Any idea where I'm going wrong with this one? Thank you for the help thus far!

Share this post


Link to post
Share on other sites

Please try creating the button before the GuiSetState()

1 person likes this

Share this post


Link to post
Share on other sites
3 minutes ago, mikell said:

Please try creating the button before the GuiSetState()

I have no idea why that was a thing, but it absolutely worked. You rock - thank you so much!

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