Jump to content

cannot put if and func inside loop


Recommended Posts

Hi everyone, I am new here and might a bit ignorant in front of you, but please remember I have just started to learn.

Ok, so here is the problem :

My Idea was to create a process killer that loads in taskbar and when we left click it, it shows all the process running and if we click any process it kills the process, on right click it should show a menu of about, exit, etc. I tried both (Trayonevent and tray getmsg) but I could not assign functions in loop.

Here is the onevent code :

Opt("TrayMenuMode",1)   ;;changing option
Opt("TrayOnEventMode",1)    ;;here also
Global $prcs = ProcessList(), $val2 ;;getting process list and declaring some more var
Global $id[$prcs[0][0]]     ;;id for events

TraySetOnEvent($TRAY_EVENT_PRIMARYDOWN,"primary")
TraySetOnEvent($TRAY_EVENT_SECONDARYUP,"secondary")

Func primary()  ;;the primary function
    For $i = 1 To $prcs[0][0]
        $id[$prcs[$i][0]]=TrayCreateItem($prcs[$i][0])  ;;this works fine 
        TrayItemSetOnEvent($id[$prcs[$i][0]], "prckill" & ($prcs[$i][0]))   ;;generates error always
    Next
EndFunc

Func prckill( $param )  ;;here is the function that takes arguments 
    If ProcessExists($param) Then   ;;but all this doesn't work
        processclose($param)
    EndIf
EndFunc

Ok for the thing that doesn't work, I tried many other ways like

Func primary()  ;;the primary function
    For $i = 1 To $prcs[0][0]
        $id[$prcs[$i][0]]=TrayCreateItem($prcs[$i][0])  ;;this works fine 
        TrayItemSetOnEvent($id[$prcs[$i][0]], "prckill" & $i    ;;other version that i tried, but it still gives error
    Next
EndFunc

for $j=1 to $prcs[0][0]
    Func prckill & $j   ;;here is where i try to initalize all the function as many process are there but it always
    If ProcessExists[$J][0] Then    ;;says next missing endfunc missing or something else missing
        processclose[$j][0]     ;;even this doesn't work 
    EndIf   
EndFunc
Next

Also there are 2 or 3 more that i tired but they were wrecked too.

Can anyone please guide me what to how.

I think I made myself clear what I wanted to do, and how I started. :D

If anyone wants to know how I worked in traygetmsg() mode, I can post that thing too, but basically the idea and problem is same.

Please See what you can do.

Link to comment
Share on other sites

  • Moderators

RazorTorque4X,

Welcome to the AutoIt forum. :D

I am new here and might a bit ignorant in front of you, but please remember I have just started to learn

We all started there, so I would not worry too much! :)

A couple of instant problems with what you are trying to do:

1. You can only have 1 tray menu - you use TraySetClick to determine which mouse buttons open it - you cannot have separate menus for both mouse buttons. The answer is to use the tray menu for the short right-click menu and create your own GUI to deal with the process list.

2. You cannot use parameters in OnEvent mode (well, not without a deal of faffing around with a nice UDF from martin and I have never seen it done for the tray menu) - which makes your idea of passing the parameter to prckill a non-starter. :D However, this problem will go away if we go the route above, because then we can read our own GUI and not have to worry about it. :huggles:

Here is a short script which shows what I mean - left click on the icon for the GUI, right click for the tray menu:

#include <GUIConstantsEx.au3>
#include <Constants.au3>
#Include <GuiToolBar.au3>

Opt("TrayOnEventMode", 1) ; Use event trapping for tray menu
Opt("TrayMenuMode", 3) ; Default tray menu items will not be shown.

TrayCreateItem("About")
TrayItemSetOnEvent(-1, "On_About")
TrayCreateItem("")
TrayCreateItem("Exit")
TrayItemSetOnEvent(-1, "On_Exit")

TraySetState()

TraySetClick(8)

; Set left click to act as Play/Pause or New
TraySetOnEvent($TRAY_EVENT_PRIMARYUP, "On_List")

Global $iIcon_X, $iIcon_Y

; Find the tray icon
_Locate_Icon()

; Create and hide the GUI
GUICreate("Process List", 200, 200, $iIcon_X - 200, $iIcon_Y - 240)
GUISetState(@SW_HIDE)

While 1

    Switch GUIGetMsg()
        ; Pressing either [_] or [X] rehides the GUI
        Case $GUI_EVENT_CLOSE, $GUI_EVENT_MINIMIZE
            GUISetState(@SW_HIDE)
    EndSwitch

WEnd


Func On_List()

    ; Show the GUI
    GUISetState(@SW_SHOW)
    ; Obviously a lot more to go here!!!

EndFunc



Func _Locate_Icon   ()

    ; Find taskbar
    Local $aPos = WinGetPos("[Class:Shell_TrayWnd]", "")
    ; Start icon position calculation
    $iIcon_X = $aPos[0]
    $iIcon_Y = $aPos[1]
    ; Get systray handle and position
    Local $hSysTray_Handle = ControlGetHandle("[Class:Shell_TrayWnd]", "", "[Class:ToolbarWindow32;Instance:1]")
    $aPos = ControlGetPos("[Class:Shell_TrayWnd]", "", "[Class:ToolbarWindow32;Instance:1]")
    If @error Then Return
    ; Continue icon position calculation
    $iIcon_X += $aPos[0]
    $iIcon_Y += $aPos[1]
    ; Get systray item count
    Local $iSystray_ButCount = _GUICtrlToolbar_ButtonCount($hSysTray_Handle)
    If $iSystray_ButCount = 0 Then Return
    ; Look for tooltip by finding track title
    For $iSystray_ButtonNumber = 0 To $iSystray_ButCount - 1
        Local $sText = _GUICtrlToolbar_GetButtonText($hSysTray_Handle, $iSystray_ButtonNumber)
        If StringInStr($sText, "ProcessKiller") = 1 Then
            ; Get coords of icon
            $aPos = _GUICtrlToolbar_GetButtonRect($hSysTray_Handle, $iSystray_ButtonNumber)
            ; Finalise icon position calculation
            $iIcon_X += $aPos[0]
            $iIcon_Y += $aPos[1]
            ExitLoop
        EndIf
        If $iSystray_ButtonNumber = $iSystray_ButCount - 1 Then Return 1
    Next

    Return 0

EndFunc

Func On_About()
    MsgBox(0, "About", "Whatever")
EndFunc

Func On_Exit()
    Exit
EndFunc

Lots of work for you to do yet. You will need to get the processes into a List/ListView within the GUI, detect a click, kill the process, etc. But that should give you a good start and you know where we are if you run into problems. :

Have fun.

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

RazorTorque4X,

Welcome to the AutoIt forum. :D

We all started there, so I would not worry too much! :D

A couple of instant problems with what you are trying to do:

1. You can only have 1 tray menu - you use TraySetClick to determine which mouse buttons open it - you cannot have separate menus for both mouse buttons. The answer is to use the tray menu for the short right-click menu and create your own GUI to deal with the process list.

2. You cannot use parameters in OnEvent mode (well, not without a deal of faffing around with a nice UDF from martin and I have never seen it done for the tray menu) - which makes your idea of passing the parameter to prckill a non-starter. :) However, this problem will go away if we go the route above, because then we can read our own GUI and not have to worry about it. :huggles:

Here is a short script which shows what I mean - left click on the icon for the GUI, right click for the tray menu:

#include <GUIConstantsEx.au3>
#include <Constants.au3>
#Include <GuiToolBar.au3>

Opt("TrayOnEventMode", 1) ; Use event trapping for tray menu
Opt("TrayMenuMode", 3) ; Default tray menu items will not be shown.

TrayCreateItem("About")
TrayItemSetOnEvent(-1, "On_About")
TrayCreateItem("")
TrayCreateItem("Exit")
TrayItemSetOnEvent(-1, "On_Exit")

TraySetState()

TraySetClick(8)

; Set left click to act as Play/Pause or New
TraySetOnEvent($TRAY_EVENT_PRIMARYUP, "On_List")

Global $iIcon_X, $iIcon_Y

; Find the tray icon
_Locate_Icon()

; Create and hide the GUI
GUICreate("Process List", 200, 200, $iIcon_X - 200, $iIcon_Y - 240)
GUISetState(@SW_HIDE)

While 1

    Switch GUIGetMsg()
        ; Pressing either [_] or [X] rehides the GUI
        Case $GUI_EVENT_CLOSE, $GUI_EVENT_MINIMIZE
            GUISetState(@SW_HIDE)
    EndSwitch

WEnd


Func On_List()

    ; Show the GUI
    GUISetState(@SW_SHOW)
    ; Obviously a lot more to go here!!!

EndFunc



Func _Locate_Icon   ()

    ; Find taskbar
    Local $aPos = WinGetPos("[Class:Shell_TrayWnd]", "")
    ; Start icon position calculation
    $iIcon_X = $aPos[0]
    $iIcon_Y = $aPos[1]
    ; Get systray handle and position
    Local $hSysTray_Handle = ControlGetHandle("[Class:Shell_TrayWnd]", "", "[Class:ToolbarWindow32;Instance:1]")
    $aPos = ControlGetPos("[Class:Shell_TrayWnd]", "", "[Class:ToolbarWindow32;Instance:1]")
    If @error Then Return
    ; Continue icon position calculation
    $iIcon_X += $aPos[0]
    $iIcon_Y += $aPos[1]
    ; Get systray item count
    Local $iSystray_ButCount = _GUICtrlToolbar_ButtonCount($hSysTray_Handle)
    If $iSystray_ButCount = 0 Then Return
    ; Look for tooltip by finding track title
    For $iSystray_ButtonNumber = 0 To $iSystray_ButCount - 1
        Local $sText = _GUICtrlToolbar_GetButtonText($hSysTray_Handle, $iSystray_ButtonNumber)
        If StringInStr($sText, "ProcessKiller") = 1 Then
            ; Get coords of icon
            $aPos = _GUICtrlToolbar_GetButtonRect($hSysTray_Handle, $iSystray_ButtonNumber)
            ; Finalise icon position calculation
            $iIcon_X += $aPos[0]
            $iIcon_Y += $aPos[1]
            ExitLoop
        EndIf
        If $iSystray_ButtonNumber = $iSystray_ButCount - 1 Then Return 1
    Next

    Return 0

EndFunc

Func On_About()
    MsgBox(0, "About", "Whatever")
EndFunc

Func On_Exit()
    Exit
EndFunc

Lots of work for you to do yet. You will need to get the processes into a List/ListView within the GUI, detect a click, kill the process, etc. But that should give you a good start and you know where we are if you run into problems. :

Have fun.

M23

Whoa! That's a lot's of code for a newbie like me but thanks very much for taking time, there are other post here way after mine, but they have a lots of replies, why just cause I am new ? Anyway, that 's gonna take me atleast half a day to figure out and another half to make it what I want, then I'll get back again here.

Really thanks for helping.

Now I'll take a break to digest all this and then come back. bye :D

Link to comment
Share on other sites

  • Moderators

RazorTorque4X,

thanks very much for taking time [...] why just cause I am new ?

No, because you had an interesting question and taken the trouble to try and write some code yourself. That is a pretty winning combination in my book. :D

Looking forward to our next meeting.

M23

P.S. When you reply please use the "Add Reply" button at the top and bottom of the page rather then the "Reply" button in the post itself. That way you do not get the contents of the previous post quoted in your reply and the whole thread becomes easier to read.

Edit: Typign!

Edited by Melba23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

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