Jump to content
guiltyking

Check RightClick on a Button

Recommended Posts

Is there any better solution to check right Click on a button rather than that one? Since could be there 100`s keys to process.

Topic is about to find out what is missing in AutoIT to handle contol ID`s of right Clicked Buttons amongst bunch of buttonkeys.

#include <GUIConstantsEx.au3> 

Opt("GUIOnEventMode", 1)

$hGUI = GUICreate("Test", 300, 300)
    GUISetOnEvent($GUI_EVENT_CLOSE, "bye")
    GUISetOnEvent($GUI_EVENT_SECONDARYUP, "_SecondaryUp")
    global $cButton[9]
        for $x=1 to 3
            $cButton[$x] = GUICtrlCreateButton("Check"&$x, 10, 10*($X*5), 80, 30)
            GUICtrlSetOnEvent(-1, "_Button")
        next 
        $x+=1
            $cButton[$x] = GUICtrlCreateButton("NoCheck", 10, 10*($X*5), 80, 30)
    GUISetState()
While Sleep(10)
WEnd 

Func _Button()
    $nTempId=GUICtrlRead(@GUI_CtrlId) ; can get ID from Left Clicked Button//
    $aCInfo = GUIGetCursorInfo($hGUI)
    MsgBox(0, "Pressed",  "LEFT "&$nTempId&":"&GUICtrlRead($aCInfo[4]),.5)
EndFunc 

Func _SecondaryUp()
    $aCInfo = GUIGetCursorInfo($hGUI)
    for $x=1 to 3                               ; my solutionprocess slower....
        If $aCInfo[4] = $cButton[$x] Then
            $nTempId=GUICtrlRead(@GUI_CtrlId) ;cannot get  real ID, but last one.
            $nTempId2=GUICtrlRead($cButton[$x]) ;can get  ID            
            MsgBox(0, "Pressed", "RIGHT   "&$nTempId&":<=?  True= "&$nTempId2,1)
        EndIf
    next 
EndFunc 

Func bye()
    Exit
EndFunc

 

Edited by guiltyking

Share this post


Link to post
Share on other sites
#include <GUIConstants.au3>
#include <GuiMenu.au3>
#include <WindowsConstants.au3>
#include <WinAPIShellEx.au3>

Global Enum $idSettings = 1000
Global $hMain = GUICreate("", 120, 50)
Global $btnMenu = GUICtrlCreateButton("Menu", 10, 10, 100, 30)
Global $hBtnMenu = GUICtrlGetHandle($btnMenu)
Global $pButtonProc = DllCallbackGetPtr(DllCallbackRegister("ButtonProc", "lresult", "hwnd;uint;wparam;lparam;uint_ptr;dword_ptr"))

GUISetState(@SW_SHOW, $hMain)

_WinAPI_SetWindowSubclass($hBtnMenu, $pButtonProc, 1000)

While (True)
    Switch (GUIGetMsg())
        Case $GUI_EVENT_CLOSE
            GUIDelete($hMain)
            Exit 0
    EndSwitch
WEnd

Func ButtonProc($hWndFrom, $iMsg, $wParam, $lParam, $iSubclassId, $pData)
    Switch ($hWndFrom)
        Case $hBtnMenu
            Switch ($iMsg)
                Case $WM_RBUTTONUP
                    Local $hMenuButton = _GUICtrlMenu_CreatePopup($MNS_NOCHECK)
                    _GUICtrlMenu_AddMenuItem($hMenuButton, "Settings", $idSettings)
                    _GUICtrlMenu_TrackPopupMenu($hMenuButton, $hWndFrom)
                    _GUICtrlMenu_DestroyMenu($hMenuButton)
            EndSwitch
    EndSwitch
    Return _WinAPI_DefSubclassProc($hWndFrom, $iMsg, $wParam, $lParam)
EndFunc   ;==>EditProc

 

Edited by InunoTaishou

Share this post


Link to post
Share on other sites

Typo, it was supposed to be Switch ($hWndFrom)

I don't know about better. I'm getting the handle of the button that was clicked from windows as well as every message that button can send to your gui.

Share this post


Link to post
Share on other sites

Here's a short method I've used now and then:

#include <GUIConstantsEx.au3>

$hGui = GUICreate("Test GUI", 300, 200)
$send = GUICtrlCreateLabel("Test Label", 20, 40, 100, 40)
GUISetState()

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $send
            MsgBox(0, "Label", "Clicked", 1)
        Case $GUI_EVENT_SECONDARYUP
            $aCur = GUIGetCursorInfo($hGui)
            If IsArray($aCur) Then
                If $aCur[4] = $send Then
                    MsgBox(0, "Label", "Right Clicked", 1)
                EndIf
            EndIf
    EndSwitch
WEnd

Sometimes the method just depends on what other processing you need to include.

Share this post


Link to post
Share on other sites

examine my code, it restricts right click on only selected buttons.

And GUIGetCursorInfo($hGui) fails on multiple keys. therefore you have to loop like mine:)

Edited by guiltyking

Share this post


Link to post
Share on other sites

I'm curious: what's the advantage of restricting over ignoring?

But, like I said, it depends on what other processing you need.

Regardless, I can imagine that your topic title will result in a lot of people looking for a minimal method ... and maybe the complicated ones, too.

Share this post


Link to post
Share on other sites
Posted (edited)
On 4/12/2016 at 7:59 PM, qwert said:

Here's a short method I've used now and then:

#include <GUIConstantsEx.au3>

$hGui = GUICreate("Test GUI", 300, 200)
$send = GUICtrlCreateLabel("Test Label", 20, 40, 100, 40)
GUISetState()

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $send
            MsgBox(0, "Label", "Clicked", 1)
        Case $GUI_EVENT_SECONDARYUP
            $aCur = GUIGetCursorInfo($hGui)
            If IsArray($aCur) Then
                If $aCur[4] = $send Then
                    MsgBox(0, "Label", "Right Clicked", 1)
                EndIf
            EndIf
    EndSwitch
WEnd

Sometimes the method just depends on what other processing you need to include.

Wow! That is almost what I am need. Can you describe me what that array is doing, how to understand its behaviour?

UPD: is it possible to use multiple (included) switches/cases for better understanding?

Edited by krasnoshtan

Share this post


Link to post
Share on other sites
Posted (edited)
24 minutes ago, krasnoshtan said:

...Can you describe me what that array is doing, how to understand its behaviour?..

Okay, just got it. Every time right mouse button is released above the GUI, the check is performed - is there are any defined elements was right clicked.

Edited by krasnoshtan

Share this post


Link to post
Share on other sites

Maybe this :

#include <GUIConstants.au3>

$hGui = GUICreate("Test GUI", 300, 200)
$lab1 = GUICtrlCreateLabel("Test Label", 20, 40, 100, 25, $SS_SUNKEN)
$but1 = GUICtrlCreateButton("Button1", 20, 70, 100, 25)
$lab2 = GUICtrlCreateLabel("Test Label 2", 20, 100, 100, 25, $SS_SUNKEN)
$but2 = GUICtrlCreateButton("Button2", 20, 130, 100, 25)

GUISetState()

While 1
  Switch GUIGetMsg()
    Case $GUI_EVENT_CLOSE
      Exit
    Case $lab1
      MsgBox(0, "Label", "Clicked", 2)
    Case $GUI_EVENT_SECONDARYUP
      $aCur = GUIGetCursorInfo($hGui)
      If Not IsArray($aCur) Then ContinueLoop
      Switch $aCur[4]
        Case $lab1
          MsgBox(0, "Label 1", "Right Clicked", 2)
        Case $but1
          MsgBox(0, "Button 1", "Right Clicked", 2)
        Case $lab2
          MsgBox(0, "Label 2", "Right Clicked", 2)
        Case $but2
          MsgBox(0, "Button 2", "Right Clicked", 2)
      EndSwitch
  EndSwitch
WEnd

 

Share this post


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

Maybe this :

#include <GUIConstants.au3>

$hGui = GUICreate("Test GUI", 300, 200)
$lab1 = GUICtrlCreateLabel("Test Label", 20, 40, 100, 25, $SS_SUNKEN)
$but1 = GUICtrlCreateButton("Button1", 20, 70, 100, 25)
$lab2 = GUICtrlCreateLabel("Test Label 2", 20, 100, 100, 25, $SS_SUNKEN)
$but2 = GUICtrlCreateButton("Button2", 20, 130, 100, 25)

GUISetState()

While 1
  Switch GUIGetMsg()
    Case $GUI_EVENT_CLOSE
      Exit
    Case $lab1
      MsgBox(0, "Label", "Clicked", 2)
    Case $GUI_EVENT_SECONDARYUP
      $aCur = GUIGetCursorInfo($hGui)
      If Not IsArray($aCur) Then ContinueLoop
      Switch $aCur[4]
        Case $lab1
          MsgBox(0, "Label 1", "Right Clicked", 2)
        Case $but1
          MsgBox(0, "Button 1", "Right Clicked", 2)
        Case $lab2
          MsgBox(0, "Label 2", "Right Clicked", 2)
        Case $but2
          MsgBox(0, "Button 2", "Right Clicked", 2)
      EndSwitch
  EndSwitch
WEnd

 

yeah, that's clearer, thanks!

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

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By GillesMaisonneuve
      Good morning,
       
      I am trying to align a two-line text in a button so that it would be centered in the button but can't succeed to do it for the vertical part.
      Can someone tell me what I do wrong ?
      Here is my code:
      $btn3 = GUICtrlCreateButton("Execute " & @LF & " file 1", 100, 65, 60, 50, _ BitOr($BS_CENTER,$BS_BOTTOM,$BS_MULTILINE)) and here is the resulting layout I get in my window.
      I tried to follow the help, made a lot of different parameter combinations (here it's `$BS_BOTTOM`, but I tried `$BS_VCENTER` and `$BS_TOP`), still get the same result : vertical alignement is a no go for me.
      Can someone help ?
      TIA
      Gilles

    • By Pike
      Hey Everyone, I haven't been here in a while and recently picked up my old project. Still an FNG!
      To the point: I want to be able to push the button and either play an mp3, open a file, or open an exe program (all with the same button). The problem is that I don't know how to code for that at "case 1"
              Case 1                                                                                                                                             
                  Local $sRead = IniRead("config.ini", "Config", "One", "Default")                                
                  SoundPlay($sRead, @HotKeyPressed)                                                                     <-------------------------------
                  Run($sRead, @HotKeyPressed)                                                                                 <------------------------------- This 'Run' doesn't work even by itself!
      See code below for layout. If you need more information I will attach more code or answer any questions. Any help is greatly appreciated.
       
      Much Respect,
      Pike
       
      Func _HandleButton() $iClickCount += 1 $iTimer = TimerInit() AdlibRegister("CheckButtonPress", $CheckTime) EndFunc Func CheckButtonPress() If TimerDiff($iTimer) < $CheckTime Or $iTimer = 0 Then Return Switch $iClickCount Case 1 Local $sRead = IniRead("config.ini", "Config", "One", "Default") SoundPlay($sRead, @HotKeyPressed) Run($sRead, @HotKeyPressed) Case 2 MsgBox(0, "INFORMATION:", "Choose the file you want designated to this button") Local $sFileOpenDialog = FileOpenDialog(0, @ScriptDir & "\", "All (*.*)", $FD_FILEMUSTEXIST + $FD_MULTISELECT) If @error Then MsgBox($MB_SYSTEMMODAL, "", "No file(s) were selected.") EndIf Local $sFileName = InputBox("INFORMATION:", "Please Name Your File?" & @CRLF _ & @CRLF _ & "6 Characters or Less", "") IniWrite(@ScriptDir & "\config.ini", "Config", "One", $sFileOpenDialog) IniWrite(@ScriptDir & "\config.ini", "Config", "NameOne", $sFileName) EndSwitch $iClickCount = 0 AdlibUnRegister() EndFunc  

    • By misioooo
      Hi!
       
      I am writing some GUI and i need to use images as buttons, but with text on them (text is from INI file).
      Part with button looks like this:
      $ankieta1 = GUICtrlCreateButton($txtAnk1, 100,350,765,164) GUICtrlSetFont(-1, 22, 800, -1, "Arial") ;GUICtrlSetColor(-1,0xffffff) $hImageBtn_1 = _GUIImageList_Create(765, 164) _GUIImageList_AddBitmap($hImageBtn_1, "c:\path\to\ankieta_clean.bmp") _GUICtrlButton_SetImageList($ankieta1, $hImageBtn_1, 4) GUICtrlSetOnEvent(-1, "On_Ankieta1") The problem - if i uncomment GUICtrlSetColor, color of text changes, but button is just plain gray (no image). How i can change text color not touching image on a button here?
    • By XinYoung
      Quick question: How can I click this "Select All" button in IE? There's no ID, and the href is "#"  
      Am I looking at the wrong html code?
      Am I looking
      Thank you all in advance!
    • By XinYoung
      Hello again 
      I need to click this Submit button, but it's in a lightbox and has no ID (i think) and I can't figure out a way to target it. _IEFormSubmit doesn't work.

       
      Thank you
×
×
  • Create New...