Sign in to follow this  
Followers 0
Info

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

11 posts in this topic

?

Share this post


Link to post
Share on other sites



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

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

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

Share this post


Link to post
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)

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

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

Share this post


Link to post
Share on other sites

I suppose 0% CPU usage is quite high.

Share this post


Link to post
Share on other sites

@picea892

Your code uses up more cpu cycles and therefore uses more memory. It isn't a very high user, but there is not reason to waste cpu if you don't have to.


[font="Verdana"] [size="2"]"[/size][/font]Failure is not an option -- it comes packaged with Windows"[font="Verdana"][size="2"] Gecko Web Browser[/size][/font][font="Verdana"][size="2"], [/size][/font][font="Verdana"][size="2"]Yahtzee![/size][/font][font="Verdana"][size="2"], Toolbar Launcher (like RocketDock)[/size][/font][font="Verdana"][size="2"]Internet Blocker, Simple Calculator, Local Weather, Easy GDI+ GUI [/size][/font][font="Verdana"][size="2"]Triangle Solver, TCP File Transfer, [/size][/font][font="Verdana"][size="2"]Valuater's Autoit Wrappers[/size][/font][font="Verdana"][size="3"][size="2"][size="2"]OOP In AutoIt[/size][/size][/size][/font][font="Verdana"][size="2"][size="1"]Using Windows XP SP3, 1GB RAM, AMD Athlon Processor @ 2.1 GHzCheck me out at gadgets.freehostrocket.com[/size][/size][/font]

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

@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

Share this post


Link to post
Share on other sites

Your right on both counts Wraithdu

1) Any amount of efficiency should be sought after

2) Can`t take this stuff personal. I`m here to learn after all.

Picea

Share this post


Link to post
Share on other sites

Picea, your signature is amazing.

Share this post


Link to post
Share on other sites
:)

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