Jump to content

[SOLVED] Generate unique ID for items...


Recommended Posts

I've already fixed this, posted my new problem at the bottom...

And here is a copy

Haha, nice to hear you've learned something :idea:

I already found another way around, indeed with using the WM_COMMAND and it now works!

But I have another question right now:

The script automatically generates menuitems using:

_GUICtrlMenu_InsertMenuItem ( $hMenu, 0, $MenuResult, $miArray[$i] )

Where it says "HERE", I need an unique ID for every item, how is this possible?

Thanks in advance,

Rawox

Edited by Rawox
Link to comment
Share on other sites

While the parent GUI is hidden, messages from the context menu item are not getting to GuiGetMsg():

#include <GuiConstants.au3>

Global $f_ShowGUI = True, $hMain, $Button, $Menu, $Config

$hMain = GUICreate("Test", 200, 200)
$Button = GUICtrlCreateButton("test", 10, 10)
$Menu = GUICtrlCreateContextMenu($Button)
GUICtrlCreateMenuItem("", $Menu)
$Config = GUICtrlCreateMenuItem("Configuration", $Menu)
GUISetState(@SW_SHOW)

HotKeySet("^!v", "_showMenu") ; Popup context menu
HotKeySet("^!s", "_showGUI") ; Show/Hide GUI

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Config
            ConsoleWrite("Debug:  GuiGetMsg() = $Config" & @LF)
    EndSwitch
WEnd

Func _showMenu()
    ControlClick("Test", "", $Button, "menu")
EndFunc   ;==>_showMenu

Func _showGUI()
    $f_ShowGUI = Not $f_ShowGUI
    If $f_ShowGUI Then
        GUISetState(@SW_SHOW, $hMain)
    Else
        GUISetState(@SW_HIDE, $hMain)
    EndIf
EndFunc   ;==>_showMenu

You might have to GuiRegisterMsg() for the appropriate notification and handle it yourself. I don't know which message you would look for on a context menu item. But searching the forum for some examples might turn it up.

:idea:

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

I had the right idea, register WM_COMMAND:

#include <GuiConstants.au3>
#include <WindowsConstants.au3>

Global $f_ShowGUI = True, $hMain, $Button, $Menu, $Config

$hMain = GUICreate("Test", 200, 200)
ConsoleWrite("$hMain = " & $hMain & @CRLF)
GUICtrlCreateLabel("Hit Ctrl-Alt-s to show/hide the GUI", 10, 10, 180, 20)
GUICtrlCreateLabel("Hit Ctrl-Alt-v to trigger context menu", 10, 50, 180, 20)
$Button = GUICtrlCreateButton("TEST", 50, 150, 100, 30)
ConsoleWrite("$Button = " & $Button & @CRLF)
$Menu = GUICtrlCreateContextMenu($Button)
ConsoleWrite("$Menu = " & $Menu & @CRLF)
GUICtrlCreateMenuItem("", $Menu)
$Config = GUICtrlCreateMenuItem("Configuration", $Menu)
ConsoleWrite("$Config = " & $Config & @CRLF)

GUIRegisterMsg($WM_COMMAND, "_WM_COMMAND")

GUISetState(@SW_SHOW)

HotKeySet("^!v", "_showMenu") ; Popup context menu
HotKeySet("^!s", "_showGUI") ; Show/Hide GUI

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
WEnd

Func _showMenu()
    ControlClick("Test", "", $Button, "menu")
EndFunc   ;==>_showMenu

Func _showGUI()
    $f_ShowGUI = Not $f_ShowGUI
    If $f_ShowGUI Then
        GUISetState(@SW_SHOW, $hMain)
    Else
        GUISetState(@SW_HIDE, $hMain)
    EndIf
EndFunc   ;==>_showMenu

Func _WM_COMMAND($hWnd, $Msg, $wParam, $lParam)
    Local $wParamLo = BitAND($wParam, 0x0000FFFF)
    Local $wParamHi = BitShift($wParam, 16)
    $wParamHi = BitAND($wParamHi, 0x0000FFFF)

    ConsoleWrite("Debug:  _WM_COMMAND:" & @CRLF & _
            @TAB & "$hWND = " & $hWND & _
            @TAB & "$Msg = " & $Msg & @CRLF & _
            @TAB & "$wParam = " & $wParam & "; $wParamHi = " & $wParamHi & "; $wParamLo = " & $wParamLo & @CRLF & _
            @TAB & "$lParam = " & $lParam & @CRLF)

    If $hWND = $hMain Then
        If $wParamLo = $Config Then
            If $f_ShowGUI Then
                ConsoleWrite("Debug:  You clicked 'Configuration' on the context menu while the GUI was visible!" & @CRLF)
            Else
                ConsoleWrite("Debug:  You clicked 'Configuration' on the context menu while the GUI was hidden!" & @CRLF)
            EndIf
        EndIf
    EndIf

    Return $GUI_RUNDEFMSG
EndFunc

Hope that helps. I learned something!

:idea:

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

Haha, nice to hear you've learned something :idea:

I already found another way around, indeed with using the WM_COMMAND and it now works!

But I have another question right now:

The script automatically generates menuitems using:

_GUICtrlMenu_InsertMenuItem ( $hMenu, 0, $MenuResult, "HERE" )

Where it says "HERE", I need an unique ID for every item, how is this possible?

Thanks in advance,

Rawox

#include <GuiMenu.au3>
#include <GuiConstantsEx.au3>
#include <WinAPI.au3>
#include <WindowsConstants.au3>
#include <Array.au3>
#Include <Misc.au3>

Opt ( "MustDeclareVars", 1 )
HotKeySet("^!v", "_showMenu")
AdLibRegister ( "_checkClip" )
_Singleton ( @ScriptName )

Global      $Title =        "RightClip"
Global      $Version =      "0.3"
Global      $Author =       "Rawox"
Global      $IniDir =       @ScriptDir & "\Config.ini"
Global      $hGUI, $idConfig, $ClipHist = "", $sDelim = "[||]", $Result, $i, $MenuLen = 30, $MenuResult, $Test
Global      $itArray[1], $miArray[1]
            $itArray[0] = 0
            $miArray[0] = 0

$hGUI =     GUICreate ( $Title, 400, 300 )
            GUISetState(@SW_HIDE)

            GUIRegisterMsg ( $WM_COMMAND, "WM_COMMAND" )
            GUIRegisterMsg ( $WM_CONTEXTMENU, "WM_CONTEXTMENU" )

Do

Until 1 = 2

Func _showMenu()
    WM_CONTEXTMENU ( $hGUI )
EndFunc

Func _checkClip()
    ;ConsoleWrite ( ClipGet ( ) & @CRLF )
    If ClipGet ( ) <> $ClipHist AND ClipGet ( ) <> "" Then
        $ClipHist = ClipGet ( )
        $itArray[0] = $itArray[0] + 1
        $Result = StringReplace ( $ClipHist, @CRLF, "|[CRLF]|" )
        $Result = StringReplace ( $Result, @CR, "|[CR]|" )
        $Result = StringReplace ( $Result, @LF, "|[LF]|" )
        $Result = StringReplace ( $Result, @TAB, "|[TAB]|" )
        _ArrayAdd ( $itArray, $Result )
        IniWrite ( $IniDir, "Array", "itArray", _ArrayToString ( $itArray, $sDelim ) )
    EndIf
EndFunc

Func WM_COMMAND ( $hWnd, $iMsg, $iwParam, $ilParam )
    Switch $iwParam
        Case $idConfig
            _WinAPI_ShowMsg("Configuration")
    EndSwitch
EndFunc

Func WM_CONTEXTMENU ( $iwParam )
    Local $hMenu

    $hMenu = _GUICtrlMenu_CreatePopup ( )
    $itArray = StringSplit ( IniRead ( $IniDir, "Array", "itArray", "" ), $sDelim, 3 )
    If $itArray[0] <> 0 Then
        $i = 0
        Do
            $i += 1
            $MenuResult = StringReplace ( $itArray[$i], "|[CRLF]|", " " )
            $MenuResult = StringReplace ( $MenuResult, "|[CR]|", " " )
            $MenuResult = StringReplace ( $MenuResult, "|[LF]|", " " )
            $MenuResult = StringReplace ( $MenuResult, "|[TAB]|", "" )
            If StringLen ( $MenuResult ) > $MenuLen Then $MenuResult = StringLeft ( $MenuResult, 30 )
            _GUICtrlMenu_InsertMenuItem ( $hMenu, 0, $MenuResult, "HERE" )
        Until $i == $itArray[0]
    EndIf
    _GUICtrlMenu_InsertMenuItem ( $hMenu, 1, "Configuration", $idConfig )
    _GUICtrlMenu_InsertMenuItem ( $hMenu, 1, "", 0 )
    _GUICtrlMenu_TrackPopupMenu ( $hMenu, $iwParam )
    _GUICtrlMenu_DestroyMenu ( $hMenu )
    Return True
EndFunc
Edited by Rawox
Link to comment
Share on other sites

Sorry about the bump then :idea:

But I need unique variables. Like $var1, $var2 for every item.

Or is there another way? Because using WM_COMMAND I use $iwParam and gave the configuration $idConfig but now I need to have a variable for every item...?

Func WM_COMMAND ( $hWnd, $iMsg, $iwParam, $ilParam )
    Switch $iwParam
        Case $idConfig
            _WinAPI_ShowMsg("Configuration")
    EndSwitch
EndFunc
Link to comment
Share on other sites

But I need unique variables. Like $var1, $var2 for every item.

Or is there another way? Because using WM_COMMAND I use $iwParam and gave the configuration $idConfig but now I need to have a variable for every item...?

Keep the control IDs in an array. You can _ArraySearch() for the control ID in your WM_COMMAND function.

:idea:

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

That's sounds pretty straightforward :idea:

Well, it's harder then I thought... could you please give me an example?

I tried this:

WM_COMMAND Function:

Func WM_COMMAND ( $hWnd, $iMsg, $iwParam, $ilParam )
    ;_ArraySearch ( $menuArray,
    Switch $iwParam
        Case $idConfig
            _WinAPI_ShowMsg("Configuration")
    EndSwitchEndFunc

Func WM_CONTEXTMENU ( $iwParam )
    Local $hMenu

    $hMenu = _GUICtrlMenu_CreatePopup ( )
    $itArray = StringSplit ( IniRead ( $IniDir, "Array", "itArray", "" ), $sDelim, 3 )
    If $itArray[0] <> 0 Then
        $i = 0
        Do
            $i += 1
            $MenuResult = StringReplace ( $itArray[$i], "|[CRLF]|", " " )
            $MenuResult = StringReplace ( $MenuResult, "|[CR]|", " " )
            $MenuResult = StringReplace ( $MenuResult, "|[LF]|", " " )
            $MenuResult = StringReplace ( $MenuResult, "|[TAB]|", "" )
            If StringLen ( $MenuResult ) > $MenuLen Then $MenuResult = StringLeft ( $MenuResult, 30 )
            _GUICtrlMenu_InsertMenuItem ( $hMenu, 0, $MenuResult, $menuArray[$i] )
        Until $i == $itArray[0]
    EndIf
    _GUICtrlMenu_InsertMenuItem ( $hMenu, 1, "Configuration", $idConfig )
    _GUICtrlMenu_InsertMenuItem ( $hMenu, 1, "", 0 )
    _GUICtrlMenu_TrackPopupMenu ( $hMenu, $iwParam )
    _GUICtrlMenu_DestroyMenu ( $hMenu )
    Return True
EndFunc

but gave me this error:

>Running:(3.3.6.1):C:\Program Files\AutoIt3\autoit3.exe "H:\Scripts\RightClip\RightClip.au3"    
H:\Scripts\RightClip\RightClip.au3 (71) : ==> Array variable has incorrect number of subscripts or subscript dimension range exceeded.:
_GUICtrlMenu_InsertMenuItem ( $hMenu, 0, $MenuResult, $menuArray[$i] )
_GUICtrlMenu_InsertMenuItem ( $hMenu, 0, $MenuResult, ^ ERROR
->13:23:09 AutoIT3.exe ended.rc:1
>Exit code: 1    Time: 2.758

And of course I declared

Edited by Rawox
Link to comment
Share on other sites

Drop the goofy increment of $i in a Do/Until loop. Just do a simple loop:

For $i = 1 To Ubound($menuArray) - 1

     ; ... do stuff with $menuArray[$i]

Next

:idea:

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
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...