Jump to content

Is there a way to check if the button got clicked with the right mouse or left mouse?


Info
 Share

Recommended Posts

Someone will come along and tell you there is a better way to do it, they always do......but this works for me and I find it simple enough.

CODE
#include <GUIConstantsEx.au3>
#include <Array.au3>

Global $buts
dim $buts[5]
Example()

Func Example()
    Local $Button_1, $Button_2, $msg
    $example=GUICreate("My GUI Button", 300,100,-1,-1); will create a dialog box that when displayed is centered
    $left=10
For $i=1 to UBound($buts)-1
    $buts[$i] = GUICtrlCreateButton("Button "&$i, $left, 30, 50,50)
    $left=$left+60
Next
    GUISetState()  

   ; Run the GUI until the dialog is closed
    While 1
        $hover=GUIGetCursorInfo(WinGetHandle($example,""))
        For $i=1 to UBound($buts)-1
            if $hover[4]= $buts[$i] and $hover[2]=1 then MsgBox(0,"Button "&$i&" pressed","left button")
            if $hover[4]= $buts[$i] and $hover[3]=1 then MsgBox(0,"Button "&$i&" pressed","right button")
        Next
        $msg = GUIGetMsg()
        Select
            Case $msg = $GUI_EVENT_CLOSE
                Exit 0
        EndSelect
        Sleep(50)
    WEnd
EndFunc  ;==>Example
Link to comment
Share on other sites

That does the job, if not a little wasteful on CPU cycles. Here's mine.

#include <GuiConstantsEx.au3>

$gui = GUICreate("GUI", 200, 200)
$button1 = GUICtrlCreateButton("Button1", 10, 10)
$button2 = GUICtrlCreateButton("Button2", 70, 10)

ConsoleWrite("button1 id:  " & $button1 & @CRLF)
ConsoleWrite("button2 id:  " & $button2 & @CRLF)

GUISetState()

Do
    $msg = GUIGetMsg()
    Switch $msg
        Case $button1
            ConsoleWrite("left-click button1" & @CRLF)
        Case $button2
            ConsoleWrite("left-click button2" & @CRLF)
        Case $GUI_EVENT_SECONDARYDOWN
            $iID = GUIGetCursorInfo($gui)
            ConsoleWrite("right-click id:  " & $iID[4] & @CRLF)
    EndSwitch
Until ($msg = $GUI_EVENT_CLOSE)

Now I think that's about it for free code for you today. Time to go learn something on your own. You'll notice that blank 'help me' requests are frowned upon. Show some effort, what you've tried, and that you're interested in learning something instead of being spoon fed, and you're more likely to get some friendly help.

Edited by wraithdu
Link to comment
Share on other sites

Cause I feel like showing off this evening.

#include <GuiConstantsEx.au3>
#include <WinAPI.au3>
#include <ButtonConstants.au3>
#include <WindowsConstants.au3>

$fRightClick = False

$gui = GUICreate("GUI", 200, 200)
$button1 = GUICtrlCreateButton("Button1", 10, 10)
$button2 = GUICtrlCreateButton("Button2", 70, 10)

GUISetState()

Do
    $msg = GUIGetMsg()
    Switch $msg
        Case $button1
            If $fRightClick Then
                ConsoleWrite("right-click button1" & @CRLF)
            Else
                ConsoleWrite("left-click button1" & @CRLF)
            EndIf
            $fRightClick = False
        Case $button2
            If $fRightClick Then
                ConsoleWrite("right-click button2" & @CRLF)
            Else
                ConsoleWrite("left-click button2" & @CRLF)
            EndIf
            $fRightClick = False
        Case $GUI_EVENT_SECONDARYDOWN
            $iID = GUIGetCursorInfo($gui)
            If $iID[4] <> 0 Then
                $fRightClick = True
                $word = _WinAPI_MakeLong($iID[4], $BN_CLICKED)
                _SendMessage($gui, $WM_COMMAND, $word, GUICtrlGetHandle($iID[4]))
            EndIf
    EndSwitch
Until ($msg = $GUI_EVENT_CLOSE)
Link to comment
Share on other sites

Why do you think I've done nothing else but asking for "free codes"?

Because you and Melba showed some in my first topic?

Just to let you know, I don't use any of the codes you both gave me.

Thank you for this code.

Edited by Info
Link to comment
Share on other sites

@Info

I say that because in neither topic did you show any of your own code or propose anything that you tried (even if you failed). The only code you posted was skeleton at best, and you event asked if M23 could 'plug' his example into it. That's frstrating at best to those of us that regularly help out with examples. Show some initiative.

@picea892

Loops are fine when necessary, but inefficient in this context. Not only does the loop itself use more CPU, there's a Sleep() in there that may kinda 'lag' a response, and GuiGetMsg() itself has some 'sleep' built into it when there are no messages in the queue (approx 15ms) so will lag the loop some more. I'm not saying it's bad, just there are better alternatives. Why does everyone have to take criticism so negatively and personal?

Edited by wraithdu
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...