Sign in to follow this  
Followers 0
AdmiralAlkex

How to get a button react when right-clicked?

11 posts in this topic

#1 ·  Posted (edited)

I am creating a GUI in AutoIt and want something that looks like a button and that runs a function when right-clicked.

How can I do this without constant checking in my "main-loop"??

I am using OnEventMode and are running 3.2.12.0

Edited by AdmiralAlkex

Share this post


Link to post
Share on other sites



I just wanted to say that I manage to do it by using GUISetOnEvent with $GUI_EVENT_SECONDARYDOWN and $GUI_EVENT_SECONDARYUP and then checking if the button is under the mouse with GUIGetCursorInfo.

If there is a more elegant solution please do tell me!

Share this post


Link to post
Share on other sites

Achilles

Try using GUIRegisterMsg with WM_RBUTTONDOWN

Wrong, because:

MSDN

The WM_RBUTTONDOWN message is posted when the user presses the right mouse button while the cursor is in the client area of a window

Accordingly not works with the any control.

AdmiralAlkex

If there is a more elegant solution please do tell me!

I don`t know about elegance of this solution, but...

#include <GuiConstantsEx.au3>
#include <Constants.au3>
#include <WinAPI.au3>

Global Const $WM_RBUTTONUP = 0x0205

$hGui = GUICreate("Button right click demo", 200, 100)

$button = GUICtrlCreateButton("start", 60, 40, 75, 23)

GUISetState()

$wProcHandle = DllCallbackRegister("_WindowProc", "ptr", "hwnd;uint;wparam;lparam")

$wProcOld = _WinAPI_SetWindowLong(GUICtrlGetHandle($button), $GWL_WNDPROC, DllCallbackGetPtr($wProcHandle))

While GUIGetMsg() <> -3
WEnd

GUIDelete($hGui)
DllCallbackFree($wProcHandle)

Func _WindowProc($hWnd, $Msg, $wParam, $lParam)
    Switch $hWnd
        Case GUICtrlGetHandle($button)
            Switch $Msg
                Case $WM_RBUTTONUP
                    ConsoleWrite("!> Right mouse click" & @LF)
            EndSwitch
    EndSwitch
    
    Local $aRet = DllCall("user32.dll", "int", "CallWindowProc", "ptr", $wProcOld, _
                          "hwnd", $hWnd, "uint", $Msg, "wparam", $wParam, "lparam", $lParam)
    Return $aRet[0]
EndFunc

Share this post


Link to post
Share on other sites

Ok this works fine for me too, but maybe i am too nooby for that dll thing..how does it look with a few more buttons 3 or 4?

Share this post


Link to post
Share on other sites

Ok this works fine for me too, but maybe i am too nooby for that dll thing..how does it look with a few more buttons 3 or 4?

See where there's a Case GUICtrlGetHandle($button), replace that (or add a new one) with Case GUICtrlGetHandle($yourOtherButton)... Then you'll need to add all same stuff under it...

My Programs[list][*]Knight Media Player[*]Multiple Desktops[*]Daily Comics[*]Journal[/list]

Share this post


Link to post
Share on other sites

I also need to add

$wProcOld = _WinAPI_SetWindowLong(GUICtrlGetHandle($button2), $GWL_WNDPROC, DllCallbackGetPtr($wProcHandle))

But thanks for help

Share this post


Link to post
Share on other sites

I like my solution better and it can easely be adapted for more buttons. See this example:

#include <GUIConstantsEx.au3>

Opt("GUIOnEventMode", 1)

Global $ClickedButton

$GUI = GUICreate("A gui")

$ButtonMinimize = GUICtrlCreateButton("Right-click me to minimize", 5, 15)

GUISetOnEvent($GUI_EVENT_SECONDARYDOWN, "_GuiEvents")
GUISetOnEvent($GUI_EVENT_SECONDARYUP, "_GuiEvents")
GUISetState()

While 1
    Sleep(10)
WEnd

Func _GuiEvents()
    Switch @GUI_CtrlId
        Case $GUI_EVENT_SECONDARYDOWN
            $Cursor = GUIGetCursorInfo($GUI)
            If IsArray($Cursor) Then $ClickedButton = $Cursor[4]
        Case $GUI_EVENT_SECONDARYUP
            $Cursor = GUIGetCursorInfo($GUI)
            If IsArray($Cursor) And $ClickedButton = $ButtonMinimize And $Cursor[4] = $ButtonMinimize Then
                GUISetState(@SW_MINIMIZE, $GUI)
            EndIf
    EndSwitch
EndFunc

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

But with this code i got the problem that none of the menus or buttons work anymore when leftclicked.

Rasims works too, but its very laggy for the mass of buttons i got. Is it possible to just call it when the right button is pressed (without _IsPressed because it would call it muliple times)

Edited by Arterie

Share this post


Link to post
Share on other sites

Arterie

Global Const $WM_RBUTTONUP = 0x0205

Share this post


Link to post
Share on other sites

But with this code i got the problem that none of the menus or buttons work anymore when leftclicked.

Rasims works too, but its very laggy for the mass of buttons i got. Is it possible to just call it when the right button is pressed (without _IsPressed because it would call it muliple times)

What the hell are you talking about? My code doesn't interfere with leftclicks :D and with one/multiple Switch...Case...EndSwitch it would barely use any CPU at all ;)

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  
Followers 0