Jump to content

Popup Menu on Label by left click


Go to solution Solved by jguinch,

Recommended Posts

Making my first steps in AutoIt3 started reading various guides and examples. It started Monday and ended my first project - training to Saturday. :(  (I try to make sense out of the variables, how functions work, various codes and Ah! This GUI!)

My question is:

Can I create a popup menu on a label by LEFT clicking?

The program that I have made I have created three context menu which work fine but with right click.

For specialist of code I would like to give me some advice on how to write code. Looking at my code looks like newspaper! Generally, studying AutoIt3 met much more concise code. There are points in my code that I could improve?

Any suggestion is welcome

#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#Region ### START Koda GUI section ### Form=c:\users\bacilic\desktop\guess the number\clickonlabel.kxf
$ClickOnLabel = GUICreate("Change value by click on label", 475, 174, 193, 130)
$LabelFirstNumber = GUICtrlCreateLabel("First Number:", 32, 32, 99, 24)
GUICtrlSetFont(-1, 12, 400, 0, "MS Sans Serif")
$LabelSecondNumber = GUICtrlCreateLabel("Second Number:", 8, 64, 123, 24)
GUICtrlSetFont(-1, 12, 400, 0, "MS Sans Serif")
$LabelProcess = GUICtrlCreateLabel("Process:", 56, 96, 65, 24)
GUICtrlSetFont(-1, 12, 400, 0, "MS Sans Serif")
$LabelCalculate = GUICtrlCreateLabel("Calculate", 328, 128, 90, 28)
GUICtrlSetFont(-1, 14, 800, 0, "MS Sans Serif")
GUICtrlSetTip(-1, "Click to become the operation")
GUICtrlSetCursor(-1, 0)
$LabelResult = GUICtrlCreateLabel("Result", 320, 40, 110, 65)
GUICtrlSetFont(-1, 26, 400, 0, "Segoe Print")
GUICtrlSetTip(-1, "Ηere will display the result")

; First Number choose label
$LabelChooseFirstNumber = GUICtrlCreateLabel("Choose number...", 136, 32, 145, 24)
GUICtrlSetFont(-1, 12, 800, 0, "MS Sans Serif")
GUICtrlSetColor(-1, 0x0000FF)
GUICtrlSetTip(-1, "Choose the first number")
GUICtrlSetCursor(-1, 0)

;Popup Menu for First Number
$LabelChooseFirstNumberContext = GUICtrlCreateContextMenu($LabelChooseFirstNumber)
$MenuItemFirst3 = GUICtrlCreateMenuItem("Just: 3", $LabelChooseFirstNumberContext)
$MenuItemFirstRandom = GUICtrlCreateMenuItem("Random from 1 to 100", $LabelChooseFirstNumberContext)
$MenuItemFirstSelect = GUICtrlCreateMenuItem("Select...", $LabelChooseFirstNumberContext)

; Second Number choose label
$LabelChooseSecondNumber = GUICtrlCreateLabel("Choose number...", 136, 64, 145, 24)
GUICtrlSetFont(-1, 12, 800, 0, "MS Sans Serif")
GUICtrlSetColor(-1, 0x0000FF)
GUICtrlSetTip(-1, "Choose the secont number")
GUICtrlSetCursor(-1, 0)

; Menu Second number
$LabelChooseSecondNumbercontext = GUICtrlCreateContextMenu($LabelChooseSecondNumber)
$MenuItemSecond3 = GUICtrlCreateMenuItem("Just: 3", $LabelChooseSecondNumbercontext)
$MenuItemSecondRandom = GUICtrlCreateMenuItem("Random from 1 to 100", $LabelChooseSecondNumbercontext)
$MenuItemSecondSelect = GUICtrlCreateMenuItem("Select...", $LabelChooseSecondNumbercontext)

; Process choose label
$LabelChooseProcess = GUICtrlCreateLabel("Choose process...", 136, 96, 148, 24)
GUICtrlSetFont(-1, 12, 800, 0, "MS Sans Serif")
GUICtrlSetColor(-1, 0x0000FF)
GUICtrlSetTip(-1, "Chose mathematical operation you want to be between two numbers")
GUICtrlSetCursor(-1, 0)

; Menu of Process
$LabelChooseProcesscontext = GUICtrlCreateContextMenu($LabelChooseProcess)
$MenuItemAddition = GUICtrlCreateMenuItem("Addition (+)", $LabelChooseProcesscontext)
$MenuItemDeduction = GUICtrlCreateMenuItem("Deduction (-)", $LabelChooseProcesscontext)
$MenuItemMultiplication = GUICtrlCreateMenuItem("Multiplication (*)", $LabelChooseProcesscontext)
$MenuItemDivision = GUICtrlCreateMenuItem("Division (/)", $LabelChooseProcesscontext)

Dim $ClickOnLabel_AccelTable[1][2] = [["{F5}", $MenuItemFirstRandom]]
GUISetAccelerators($ClickOnLabel_AccelTable)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

$FirstNumber = 0
$SecondNumber = 0
$ResultNumber = 0
$math = ""
$Error = ""

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $LabelChooseFirstNumber ;Popup menu when left-click on First Number
            _ShowMenu(GUICtrlGetHandle($LabelChooseFirstNumberContext))
        Case $LabelChooseSecondNumber ;Popup menu when left-click on Second Number
            _ShowMenu(GUICtrlGetHandle($LabelChooseSecondNumbercontext))
        Case $LabelChooseProcess ; Popup menu when left-click on Calculate
            _ShowMenu(GUICtrlGetHandle($LabelChooseProcesscontext))
        Case $MenuItemFirst3
            $FirstNumber = 3
            chooseFirstNumberBlue()
        Case $MenuItemFirstRandom
            $FirstNumber = Random(1, 100, 1)
            chooseFirstNumberBlue()
        Case $MenuItemFirstSelect
            $FirstNumber = InputBox("Give a Number", "Input a number:", "", "", 190, 130)
            ; Check if press Cancel button or input number not is number
            If @error = 1 or not StringIsDigit($FirstNumber) Then
                GUICtrlSetData($LabelChooseFirstNumber, "You must set a number!")
                GUICtrlSetFont($LabelChooseFirstNumber, 9, 800, 0, "MS Sans Serif")
                GUICtrlSetColor($LabelChooseFirstNumber, 0xFF0000)
            Else
                chooseFirstNumberBlue()
            EndIf
        Case $MenuItemSecond3
            $SecondNumber = 3
            chooseSecondNumberBlue()
        Case $MenuItemSecondRandom
            $SecondNumber = Random(1, 100, 1)
            chooseSecondNumberBlue()
        Case $MenuItemSecondSelect
            $SecondNumber = InputBox("Give a Number", "Input a number:", "", "", 190, 130)
            If @error = 1 Or Not StringIsDigit($SecondNumber) Then
                $LabelChooseSecondNumber = GUICtrlCreateLabel("You must set a number!", 136, 64, 145, 24)
                GUICtrlSetFont(-1, 9, 800, 0, "MS Sans Serif")
                GUICtrlSetColor(-1, 0xFF0000)
            Else
                chooseSecondNumberBlue()
            EndIf
        Case $MenuItemAddition
            $math = "Addition (+)"
            chooseProcessBlue()
        Case $MenuItemDeduction
            $math = "Deduction (-)"
            chooseProcessBlue()
        Case $MenuItemMultiplication
            $math = "Multiplication (*)"
            chooseProcessBlue()
        Case $MenuItemDivision
            $math = "Division (/)"
            chooseProcessBlue()
        Case $LabelCalculate
            If GUICtrlRead($LabelChooseFirstNumber) = "Choose number..." Or GUICtrlRead($LabelChooseFirstNumber) = "You must set a number!" Then
                $LabelResult = GUICtrlCreateLabel("Without numbers not be operation!", 320, 40, 110, 65)
                GUICtrlSetFont(-1, 11, 400, 0, "Segoe Print")
                GUICtrlSetColor(-1, 0xFF0000)
            ElseIf GUICtrlRead($LabelChooseSecondNumber) = "Choose number..." Or GUICtrlRead($LabelChooseSecondNumber) = "You must set a number!" Then
                $LabelResult = GUICtrlCreateLabel("Without numbers not be operation!", 320, 40, 110, 65)
                GUICtrlSetFont(-1, 11, 400, 0, "Segoe Print")
                GUICtrlSetColor(-1, 0xFF0000)
            Else
                Calculate()
                If @error = 1 Then
                    $LabelResult = GUICtrlCreateLabel("Not set operator!", 320, 40, 110, 65)
                    GUICtrlSetFont(-1, 11, 400, 0, "Segoe Print")
                    GUICtrlSetColor(-1, 0xFF0000)
                ElseIf @error = 2 Then
                    $LabelResult = GUICtrlCreateLabel("Impossible to divide by zero!", 320, 40, 110, 65)
                    GUICtrlSetFont(-1, 11, 400, 0, "Segoe Print")
                    GUICtrlSetColor(-1, 0xFF0000)
                Else
                    $LabelResult = GUICtrlCreateLabel($ResultNumber, 320, 40, 110, 65)
                    GUICtrlSetFont(-1, 26, 400, 0, "Segoe Print")
                EndIf
            EndIf
    EndSwitch
WEnd

Func Calculate()
    If $math = "" Then SetError(1)
    Select
        Case $math = "Addition (+)"
            $ResultNumber = $FirstNumber + $SecondNumber
        Case $math = "Deduction (-)"
            $ResultNumber = $FirstNumber - $SecondNumber
        Case $math = "Multiplication (*)"
            $ResultNumber = $FirstNumber * $SecondNumber
        Case $math = "Division (/)"
            If $SecondNumber = 0 Then SetError(2)
            $ResultNumber = $FirstNumber / $SecondNumber
    EndSelect
    Return $ResultNumber
EndFunc   ;==>Calculate

Func _ShowMenu($hMenu)
    DllCall("user32.dll", "int", "TrackPopupMenuEx", "hwnd", $hMenu, "int", 0, "int", MouseGetPos(0), "int", MouseGetPos(1), "hwnd", $ClickOnLabel, "ptr", 0)
EndFunc   ;==>_ShowMenu

Func chooseFirstNumberBlue()
    ; Change Color and font to First Number label
    GUICtrlSetData($LabelChooseFirstNumber, $FirstNumber)
    GUICtrlSetFont($LabelChooseFirstNumber, 12, 800, 0, "MS Sans Serif")
    GUICtrlSetColor($LabelChooseFirstNumber, 0x0000FF)
EndFunc   ;==>chooseFirstNumberBlue

Func chooseSecondNumberBlue()
    ; Change Color and font to Second Number label
    GUICtrlSetData($LabelChooseSecondNumber, $SecondNumber)
    GUICtrlSetFont($LabelChooseSecondNumber, 12, 800, 0, "MS Sans Serif")
    GUICtrlSetColor($LabelChooseSecondNumber, 0x0000FF)
EndFunc   ;==>chooseSecondNumberBlue

Func chooseProcessBlue()
    GUICtrlSetData($LabelChooseProcess, $math)
    GUICtrlSetFont($LabelChooseProcess, 12, 800, 0, "MS Sans Serif")
    GUICtrlSetColor($LabelChooseProcess, 0x0000FF)
EndFunc   ;==>chooseProcessBlue

Label Select Popup Menu.au3

Edited by Bacilic
Link to comment
Share on other sites

  • Solution

Here is one way :

#include <GUIConstantsEx.au3>

Global $hMain = GUICreate("Context Menu")
$id_Label = GUICtrlCreateLabel("Click me !", 10, 10)

$id_Context = GUICtrlCreateContextMenu($id_Label)
$id_MenuItem1 = GUICtrlCreateMenuItem("Item 1", $id_Context)
$id_MenuItem2 = GUICtrlCreateMenuItem("Item 2", $id_Context)

GUISetState()

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $id_Label
            _ShowMenu( GUICtrlGetHandle($id_Context) )
    EndSwitch
WEnd


Func _ShowMenu($hMenu)
    DllCall("user32.dll", "int", "TrackPopupMenuEx", "hwnd", $hMenu, "int", 0, "int", MouseGetPos(0), "int", MouseGetPos(1), "hwnd", $hMain, "ptr", 0)
EndFunc
Link to comment
Share on other sites

Thank you jguinch for your answer.

Integrate _ShowMenu in my code and I made some improvements to writing .The only problem I encountered is that sometimes when changing the text of the Label pop-up menu does not work with left click.

Of course I try to figure out the function of _ShowMenu($hMenu):

Func _ShowMenu($hMenu)
    DllCall("user32.dll", "int", "TrackPopupMenuEx", "hwnd", $hMenu, "int", 0, "int", MouseGetPos(0), "int", MouseGetPos(1), "hwnd", $ClickOnLabel, "ptr", 0)
EndFunc   ;==>_ShowMenu

but that's another story.

A new problem that I encountered in debugging the code is how to check if the user has entered number (integer or decimal). I use Νot StringIsDigit but it only checks for integers. In decimal sent the message: "You must set a number!"

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