Jump to content

I think im going too complex


Recommended Posts

Ok what i am proposing to do , if possible is to load links from a ini file and then display them as a "MenuItem" on my gui

two things im having difficulty concepting , so it may not be possible

this is my example

#include <GuiConstants.au3>
opt("GUIOnEventMode", 1) 
$GUI_Style = BitOr($WS_POPUP, $WS_BORDER )
GuiCreate("links", 368, 500,-1, -1 , $GUI_Style)
$lmenu = GUICtrlCreateMenu("Links")
$links = IniReadSectionNames(@ScriptDir&"\links.ini")
        If @error Then
            GUICtrlCreateMenuItem("Unavalible",$lmenu)
            GUICtrlSetState(-1,$gui_disable)
            else
            For $i = 1 To $links[0]
                $temp = IniRead(@ScriptDir&"\links.fst",$links[$i],"link","0")
                GUICtrlCreateMenuItem($links[$i], $lmenu)
                GUICtrlSetOnEvent(-1, "golink("&$temp&")")
                next
            EndIf

i think my on event will not work at all Any ideas?!?

Edited by antmcmullen
Link to comment
Share on other sites

GUICtrlSetOnEvent(-1, "golink("&$temp&")")

You can't have parameters for the functions in GUICtrlSetOnEvent! So, your code should look like this:

GUICtrlSetOnEvent(-1,"golink")

However, that does not help you with your "dynamic" menu structure. You should switch to GUI Message Loop

mode. You can then create menu items and store the handles in an array.

$menuhandles[$i] = GUICtrlCreateMenuItem($links[$i], $lmenu)

$menuhandles_option[$i] = $temp

In the message loop, check if the current event is triggered by one of the $menuhandles (for loop)

and THEN call golink() with the desired option $menuhandles_option[$i]

Cheers

Kurt

__________________________________________________________(l)user: Hey admin slave, how can I recover my deleted files?admin: No problem, there is a nice tool. It's called rm, like recovery method. Make sure to call it with the "recover fast" option like this: rm -rf *

Link to comment
Share on other sites

A sort of working example of your script antmcmullen.

As your initial script left out a function it called and there was no ini then I could only fill the blanks..lol

#include <GuiConstants.au3>
opt("GUIOnEventMode", 1)

Global $links, $items[11] ;I used $links as a global array for urls, I used $items to store the menu ctrl ID's
Global $ini = @ScriptDir & "\links.ini"

$Main = GuiCreate("links", 368, 500,-1, -1 , BitOr($WS_POPUP, $WS_BORDER))
$lmenu = GUICtrlCreateMenu("Links")
GUISetOnEvent($GUI_EVENT_CLOSE, 'Close')
GUISetState(@SW_SHOW, $Main)

Laod()

While 1 ; While loop to keep the on event script open
    Sleep(10)
WEnd    

Func Laod() ;Load an existing ini or create one if it doesn't exist.
    If Not FileExists($ini) Then ; If ini file doesn't exist , create one.
        $spl = StringSplit('http://www.autoitscript.com/forum/index.php?showtopic=47559|' & _
                            'http://www.autoitscript.com/|' & _
                            'http://www.autoitscript.com/autoit3/downloads.php|' & _
                            'http://www.autoitscript.com/autoit3/files/beta/autoit/|' & _
                            'http://www.autoitscript.com/forum/index.php?showtopic=33677|' & _
                            'http://www.autoitscript.com/forum/index.php?showtopic=37289|' & _
                            'http://www.autoitscript.com/forum/index.php?showforum=9|' & _
                            'http://www.autoitscript.com/forum/index.php?showtopic=19370&hl=wrappers|' & _
                            'http://www.autoitscript.com/forum/index.php?act=Search&f=0|'& _
                            'http://www.autoitscript.com/forum/index.php?showforum=2', '|')
        For $c = 1 To 10 ;Create the ini with some urls from above
            IniWrite($ini, 'links', 'key' & $c, $spl[$c])
        Next        
        $links = IniReadSection($ini, 'links')
        If @error Then
            GUICtrlCreateMenuItem("Unavalible",$lmenu)
            GUICtrlSetState(-1, $GUI_DISABLE)
        Else
            For $i = 1 To $links[0][0] ; Load the created ini into items array
                If $links[$i][1] <> '' Then
                    $items[$i] = GUICtrlCreateMenuItem($links[$i][1], $lmenu) ;now we have a ctrl id stored in the items array. 
                    GUICtrlSetOnEvent(-1, "golink") ;golink without the param as we can now id the ctrl being clicked.
                EndIf   
            Next
        EndIf   
    Else ; The ini already exists, load it's urls 
        $links = IniReadSection($ini, 'links')
        If @error Then
            GUICtrlCreateMenuItem("Unavalible",$lmenu)
            GUICtrlSetState(-1, $GUI_DISABLE)
        Else
            For $i = 1 To $links[0][0]
                If $links[$i][1] <> '' Then
                    $items[$i] = GUICtrlCreateMenuItem($links[$i][1], $lmenu)
                    GUICtrlSetOnEvent(-1, "golink")
                EndIf   
            Next
        EndIf
    EndIf
EndFunc 

Func golink()
    For $s = 1 To $links[0][0]
        Select
            Case @GUI_CtrlId = $items[$s] ; Ctrl ID being clicked.
                ShellExecute($links[$s][1]) ;Run the corresponding url from links array. 
        EndSelect
    Next
EndFunc 

Func Close()
    Exit
EndFunc

Cheers

Edit: This will load upto 10 url menu items that will launch using shell execute.

If there's no ini then it will be created (in the script directory).

Edited by smashly
Link to comment
Share on other sites

hmm, now you tell us... lol :rolleyes:

I just used the same method I was using and replaced the Key with a Name.

Didn't require much change to do this.

eg:

[links]

Google=http://www.google.com

AutoIt=http://www.autoitscript.com/

.............

So the Key name is the Menu Item Name.

The value is what's launched

#include <GuiConstants.au3>
opt("GUIOnEventMode", 1)

Global $links, $items ;I used $links as a global array for urls, I used $items to store the menu ctrl ID's
Global $ini = @ScriptDir & "\links.ini"

$Main = GuiCreate("links", 368, 500,-1, -1 , BitOr($WS_POPUP, $WS_BORDER))
$lmenu = GUICtrlCreateMenu("Links")
GUISetOnEvent($GUI_EVENT_CLOSE, 'Close')
GUISetState(@SW_SHOW, $Main)

Laod()

While 1 ; While loop to keep the on event script open
    Sleep(10)
WEnd   

Func Laod() ;Load an existing ini or create one if it doesn't exist.
    If Not FileExists($ini) Then ; If ini file doesn't exist , create one.
        $spn = StringSplit('Google|AutoIt|Yahoo|Hotmail|Gmail|MSN|MSFN|RyanVM|Doom9|MaxConsole', '|')
        $spl = StringSplit('http://www.google.com|http://www.autoitscript.com/|http://www.yahoo.com/|' & _
                            'http://www.hotmail.com/|http://www.gmail.com/|http://www.msn.com/|' & _
                            'http://www.msfn.org|http://www.ryanvm.net/msfn/|http://www.doom9.org/|'& _
                            'http://www.maxconsole.com/', '|')
        For $c = 1 To 10 ;Create the ini with names and urls from above
            IniWrite($ini, 'links', $spn[$c], $spl[$c])
        Next
        $spn = 0
        $spl = 0
        LoadLinks()
    Else ; The ini already exists, load it's urls
        LoadLinks()
    EndIf   
EndFunc 

Func LoadLinks()
    $links = IniReadSection($ini, 'links')
    If @error Then
        GUICtrlCreateMenuItem("Unavalible",$lmenu)
        GUICtrlSetState(-1, $GUI_DISABLE)
    Else
        Global $items[$links[0][0] +1] ;Set the menu $items array to the size of ini entry count, unlimited menu entries.
        For $i = 1 To $links[0][0]
            If $links[$i][1] <> '' Then
                $items[$i] = GUICtrlCreateMenuItem($links[$i][0], $lmenu)
                GUICtrlSetOnEvent(-1, "golink")
            EndIf   
        Next
    EndIf
EndFunc 

Func golink()
    For $s = 1 To $links[0][0]
        Select
            Case @GUI_CtrlId = $items[$s] ; Ctrl ID being clicked.
                ShellExecute($links[$s][1]) ;Run the corresponding url from links array.
        EndSelect
    Next
EndFunc 

Func Close()
    Exit
EndFunc

Will this do?

If not then I'll make it read section names, but the code will probly be longer.

Cheers

Edit: Removed the 10 menu item limit, made it so you can add unlimited amount of urls to the ini and they'll be loaded.

Edited by smashly
Link to comment
Share on other sites

If the above post won't cut the mustard then here's an example using IniReadSectionNames() to make/read the ini as you asked.

Also added some comments to maybe help see what's going on. :rolleyes:

#include <GuiConstants.au3>

opt("GUIOnEventMode", 1)

Global $Data ; 2 Dimension array which get assigned the values when the ini loads.
;Data[0][0] stores how many section names there are in the ini.
;Data[n][0] stores the section name to be used as Menu Item Name
;Data[n][1] stores the Menu Item Control ID 
;Data[n][2] stores the url that will be launched when menu item is clicked.
;(n is a number from 1 to the number of ini section names)
Global $ini = @ScriptDir & "\links.ini"

$Main = GuiCreate("links", 368, 500,-1, -1 , BitOr($WS_POPUP, $WS_BORDER))
$lmenu = GUICtrlCreateMenu("Links")
GUISetOnEvent($GUI_EVENT_CLOSE, 'Close')
GUISetState(@SW_SHOW, $Main)

Laod()

While 1 ; While loop to keep the on event script open
    Sleep(10)
WEnd   

Func Laod() ;Load an existing ini or create one if it doesn't exist.
    If Not FileExists($ini) Then ; If ini file doesn't exist , create one.
        $spn = StringSplit('Google|AutoIt|Yahoo|Hotmail|Gmail|MSN|MSFN|RyanVM|Doom9|MaxConsole', '|')
        $spl = StringSplit('http://www.google.com|http://www.autoitscript.com/|http://www.yahoo.com/|' & _
                            'http://www.hotmail.com/|http://www.gmail.com/|http://www.msn.com/|' & _
                            'http://www.msfn.org|http://www.ryanvm.net/msfn/|http://www.doom9.org/|'& _
                            'http://www.maxconsole.com/', '|')
        For $c = 1 To 10 ;Create the ini with names and urls from above
            IniWrite($ini, $spn[$c], 'Link', $spl[$c])
        Next
        $spn = 0
        $spl = 0
        LoadLinks() ;Load the newley written ini into the data array and create the menu items.
    Else ; The ini already exists, load it's urls
        LoadLinks() ;Load an existing ini into the data array and create the menu items. 
    EndIf   
EndFunc 

Func LoadLinks() ; I stored the needed info from this function into the data array.
    $SectionName = IniReadSectionNames($ini)
    If @error Then
        GUICtrlCreateMenuItem("Unavalible",$lmenu)
        GUICtrlSetState(-1, $GUI_DISABLE)
    Else
        Global $Data[$SectionName[0] +1][3] ;Set the data array first dimension size by how many section names there are +1.
        $Data[0][0] = $SectionName[0]
        For $i = 1 To $Data[0][0]
            $Data[$i][0] = $SectionName[$i]
            $Data[$i][1] = GUICtrlCreateMenuItem($Data[$i][0], $lmenu)
            GUICtrlSetOnEvent(-1, "golink")
            $Data[$i][2] = IniRead($ini, $Data[$i][0], 'Link', '') 
        Next
    EndIf
EndFunc 

Func golink()
    For $s = 1 To $Data[0][0]
        Select
            Case @GUI_CtrlId = $Data[$s][1] ; Ctrl ID being clicked.
                ShellExecute($Data[$s][2]) ;Run the corresponding url from array.
        EndSelect
    Next
EndFunc 

Func Close()
    Exit
EndFunc

Have Fun

Cheers

Link to comment
Share on other sites

Here's another updated example..

The ini can have mutiple urls in a section.

If there's more then 1 Key/Value under a section name the then the menu will be created with a sub menu.

The main menu will use the Section Name, the sub menu will use the Key Name.

If a Section Name has only 1 key/value listed under it then the menu item will not have a sub menu.

You can have as many key/values under a Section Name as you like or you can just stick with single key/value per Section Name. How's that for flexible :rolleyes:

So your ini could look like this:

[AutoIt]
Homepage=http://www.autoitscript.com
Downloads=http://www.autoitscript.com/autoit3/downloads.php
Forum=http://www.autoitscript.com/forum/index.php?
Auto3Lib=http://www.autoitscript.com/forum/index.php?showtopic=33677
[Google]
Search=http://www.google.com/
Translate=http://www.google.com.au/language_tools?hl=en
[Yahoo]
Link=http://www.yahoo.com/
[Hotmail]
Link=http://www.hotmail.com/

The example will generate an ini in the script directory if there isn't one already.

#include <GuiConstants.au3>

opt("GUIOnEventMode", 1)

Global $ini = @ScriptDir & "\links.ini"
Global $Data, $Control

$Main = GuiCreate("links", 368, 500,-1, -1 , BitOr($WS_POPUP, $WS_BORDER))
$lmenu = GUICtrlCreateMenu("Links")
GUISetOnEvent($GUI_EVENT_CLOSE, 'Close')
GUISetState(@SW_SHOW, $Main)

Laod()

While 1
    Sleep(10)
WEnd   

Func Laod()
    If Not FileExists($ini) Then
        $spn = StringSplit('AutoIt|Google|Yahoo|Hotmail|MSFN', '|')
        $spk = StringSplit('Homepage|Downloads|Forum|Auto3Lib|Search|Gmail|Translate|Link|Link|Link', '|')
        $spl = StringSplit('http://www.autoitscript.com|http://www.autoitscript.com/autoit3/downloads.php|' & _
                            'http://www.autoitscript.com/forum/index.php?|' & _
                            'http://www.autoitscript.com/forum/index.php?showtopic=33677|http://www.google.com/|' & _
                            'http://www.gmail.com/|http://www.google.com.au/language_tools?hl=en|' & _
                            'http://www.yahoo.com/|http://www.hotmail.com/|http://www.msfn.org', '|')
        $v = 1
        For $c = 1 To 10
            If $c = 5 Then $v = 2
            If $c = 8 Then $v = 3
            If $c > 8 Then $v = $v + 1
            IniWrite($ini, $spn[$v], $spk[$c], $spl[$c])
        Next
        $spn = 0
        $spk = 0
        $spl = 0
        LoadLinks()
    Else
        LoadLinks()
    EndIf   
EndFunc 

Func LoadLinks()
    $Size = ''
    $SectionName = IniReadSectionNames($ini)
    If @error Then
        GUICtrlCreateMenuItem("Unavalible",$lmenu)
        GUICtrlSetState(-1, $GUI_DISABLE)
    Else
        For $i = 1 To $SectionName[0]
            $IRS = IniReadSection($ini, $SectionName[$i])
            If IsArray($IRS) Then
                $Size = $Size + $IRS[0][0]
            EndIf
        Next
        Global $Control[$Size + 1][4]
        Global $Data[$SectionName[0] + 1][2]
        $m = 1
        $Control[0][0] = $Size
        $Data[0][0] = $SectionName[0]
        For $t = 1 To $SectionName[0]
            $Data[$t][0] = $SectionName[$t]
            $IRS = IniReadSection($ini, $Data[$t][0])
            If $IRS[0][0] > 1 Then
                $Data[$t][1] = GUICtrlCreateMenu($Data[$t][0], $lmenu, 1)
                For $p = 1 To $IRS[0][0]
                    $Control[$m][1] = $IRS[$p][0]
                    $Control[$m][2] = $IRS[$p][1]
                    $Control[$m][3] = GUICtrlCreateMenuItem($Control[$m][1], $Data[$t][1])
                    GUICtrlSetOnEvent(-1, "golink")
                    $m = $m +1
                Next
            ElseIf $IRS[0][0] = 1 Then
                $Control[$m][1] = $IRS[1][0]
                $Control[$m][2] = $IRS[1][1]                
                $Control[$m][3] = GUICtrlCreateMenuItem($Data[$t][0], $lmenu)
                GUICtrlSetOnEvent(-1, "golink")
                $m = $m +1
            EndIf
        Next
    EndIf   
EndFunc 

Func golink()
    For $s = 1 To $Control[0][0]
        Select
            Case @GUI_CtrlId = $Control[$s][3]
                ShellExecute($Control[$s][2])
        EndSelect
    Next
EndFunc 

Func Close()
    Exit
EndFunc

That's it no more examples ..done :rambo:

Have Fun

Cheers

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...