4626
General Software Discussion / Re: RunInTrayMod 1.2
« Last post by MilesAhead on December 11, 2011, 11:19 AM »RunInTrayMod 1.2 Source cleaned up a bit more.
; RunInTrayMod - modified version of RunInTray utility found here:
; http://www.cellsworth.com/news/computer-related/35-uncategorizied/96-runintray.html
;
; The main modification to RunInTray is on detection of the window.
; A handle is obtained and used in Hide Show and Close operations.
; This way the client may change window caption and still respond
; to Hide, Show, and Close commands.
;
; Also Close is used instead of Kill to close the client app.
;
Global Const $TRAY_EVENT_PRIMARYUP = -8
Global Const $tagPOINT = "long X;long Y"
Global Const $tagGUID = "dword Data1;word Data2;word Data3;byte Data4[8]"
Global Const $HGDI_ERROR = Ptr(-1)
Global Const $INVALID_HANDLE_VALUE = Ptr(-1)
Global Const $KF_EXTENDED = 0x0100
Global Const $KF_ALTDOWN = 0x2000
Global Const $KF_UP = 0x8000
Global Const $LLKHF_EXTENDED = BitShift($KF_EXTENDED, 8)
Global Const $LLKHF_ALTDOWN = BitShift($KF_ALTDOWN, 8)
Global Const $LLKHF_UP = BitShift($KF_UP, 8)
AutoItSetOption("WinTitleMatchMode", -3)
AutoItSetOption("TrayOnEventMode", 1) ; Use event trapping for tray menu
AutoItSetOption("TrayMenuMode", 3) ; Default tray menu items will not be shown.
Const $delayMin = 1, $delayMax = 60
; call EmptyWorkingSet once a minute
Const $MemCountMax = 60
Global $Dir = "", $App = "", $handle = 0, $title = "", $memCount = 0, $delay = 0, $hasDelay = False
$iMsg = "Usage: " & @CRLF & @CRLF & "RunInTrayMod [ -d=n ] ProgramPath WorkingDir [ WindowTitle ]" & @CRLF
$iMsg &= " ( -d=n is seconds delay : Valid range for n is " & $delayMin & " to " & $delayMax & " )" & @CRLF & @CRLF
$iMsg &= "If any params contain space(s) wrap them in double quotes" & @CRLF & @CRLF
$iMsg &= "If App will not go to Tray specify exact Window Title as last param" & @CRLF & @CRLF
$iMsg &= "( If App leaves Icon in Taskbar when Trayed, it is not compatible )"
Switch $CmdLine[0]
Case 4
If Not StringInStr($CmdLine[1], "-d=") Then _ShowUsage($iMsg)
$delay = Number(StringMid($CmdLine[1], 4))
If $delay < $delayMin Or $delay > $delayMax Then $delay = 0
$App = $CmdLine[2]
$Dir = $CmdLine[3]
$title = $CmdLine[4]
Case 3
If StringInStr($CmdLine[1], "-d=") Then
$delay = Number(StringMid($CmdLine[1], 4))
If $delay < $delayMin Or $delay > $delayMax Then $delay = 0
$App = $CmdLine[2]
$Dir = $CmdLine[3]
Else
$App = $CmdLine[1]
$Dir = $CmdLine[2]
$title = $CmdLine[3]
EndIf
Case 2
If StringInStr($CmdLine[1], "-d=") Then _ShowUsage($iMsg)
$App = $CmdLine[1]
$Dir = $CmdLine[2]
Case Else
_ShowUsage($iMsg)
EndSwitch
$proc_Name = _FileNoPath($App)
$do_Kill = True
While $delay
TraySetToolTip(String($delay) & " Sec. to Launch " & _FileBaseName($App))
Sleep(1000)
$delay -= 1
WEnd
ShellExecute($App, "", $Dir)
Sleep(1000)
If $title <> "" Then
$handle = WinWait($title, "", 4)
If $handle = 0 Then
_ShowError("Could not get Handle for Window:" & @CRLF & @CRLF & $title)
EndIf
EndIf
If $handle = 0 Then
$handle = WinGetHandle(_WinGetByPID($proc_Name))
If $handle = "" Then
$iMsg = "Could Not Get Handle To: " & $proc_Name & @CRLF & @CRLF
$iMsg &= "Try adding Window Title to Command Line or Shortcut Target line"
_ShowError($iMsg)
EndIf
EndIf
TraySetIcon($App)
TraySetClick(8)
$hTray_Show_Item = TrayCreateItem("Hide")
$hTray_Donate_Item = TrayCreateItem("Donate")
TrayCreateItem("")
TrayCreateItem("Exit")
TrayItemSetOnEvent(-1, "On_Exit")
TrayItemSetOnEvent($hTray_Show_Item, "To_Tray")
TrayItemSetOnEvent($hTray_Donate_Item, "Donate")
TraySetOnEvent($TRAY_EVENT_PRIMARYUP, "To_Tray")
TraySetToolTip(_FileBaseName($App))
To_Tray()
While 1
Sleep(1000)
If Not ProcessExists($proc_Name) Then
$do_Kill = False
Exit
EndIf
$memCount += 1
If $memCount >= $MemCountMax Then
_EmptyWorkingSet()
$memCount = 0
EndIf
WEnd
Func On_Exit()
If $do_Kill Then WinClose(_WinAPI_GetWindowText($handle))
Exit
EndFunc ;==>On_Exit
Func To_Tray()
If TrayItemGetText($hTray_Show_Item) = "Hide" Then
_WinAPI_ShowWindow($handle, @SW_HIDE)
TrayItemSetText($hTray_Show_Item, "Show")
Else
_WinAPI_ShowWindow($handle, @SW_SHOW)
TrayItemSetText($hTray_Show_Item, "Hide")
EndIf
EndFunc ;==>To_Tray
Func Donate()
ShellExecute("http://www.favessoft.com/donate.html")
EndFunc ;==>Donate
;-------------- Helper Functions Stripped from Includes ------------
Func _WinAPI_GetWindowText($hWnd)
Local $aResult = DllCall("user32.dll", "int", "GetWindowTextW", "hwnd", $hWnd, "wstr", "", "int", 4096)
If @error Then Return SetError(@error, @extended, "")
Return SetExtended($aResult[0], $aResult[2])
EndFunc ;==>_WinAPI_GetWindowText
Func _WinAPI_ShowWindow($hWnd, $iCmdShow = 5)
Local $aResult = DllCall("user32.dll", "bool", "ShowWindow", "hwnd", $hWnd, "int", $iCmdShow)
If @error Then Return SetError(@error, @extended, False)
Return $aResult[0]
EndFunc ;==>_WinAPI_ShowWindow
Func _EmptyWorkingSet()
Local $result = DllCall("psapi.dll", 'int', 'EmptyWorkingSet', 'long', -1)
Return $result[1]
EndFunc ;==>_EmptyWorkingSet
Func _FileBaseName($path)
If $path = "" Then Return ""
If StringLen($path) < 3 Then Return ""
Local $pos = StringInStr($path, "\", 0, -1)
$pos += 1
Local $tmp = StringMid($path, $pos)
Return StringLeft($tmp, StringInStr($tmp, ".", 0, -1) - 1)
EndFunc ;==>_FileBaseName
Func _FileNoPath($path)
Return StringMid($path, StringInStr($path, "\", 0, -1) + 1)
EndFunc ;==>_FileNoPath
Func _ScriptBaseName()
Return StringLeft(@ScriptName, (StringInStr(@ScriptName, ".") - 1))
EndFunc ;==>_ScriptBaseName
Func _ShowError($errorMsg, $title = "", $quit = True, $timeOut = 0)
If $title = "" Then $title = _ScriptBaseName()
MsgBox(0x1010, $title, $errorMsg, $timeOut)
If $quit Then Exit
EndFunc ;==>_ShowError
Func _ShowUsage($usageMsg, $title = "", $quit = True, $timeOut = 0)
If $title = "" Then $title = _ScriptBaseName()
MsgBox(0x1040, $title, $usageMsg, $timeOut)
If $quit Then Exit
EndFunc ;==>_ShowUsage
Func _WinGetByPID($iPID, $nArray = 1)
If IsString($iPID) Then $iPID = ProcessExists($iPID)
Local $aWList = WinList(), $sHold
For $iCC = 1 To $aWList[0][0]
If WinGetProcess($aWList[$iCC][1]) = $iPID And _
BitAND(WinGetState($aWList[$iCC][1]), 2) Then
If $nArray Then Return $aWList[$iCC][0]
$sHold &= $aWList[$iCC][0] & Chr(1)
EndIf
Next
If $sHold Then Return StringSplit(StringTrimRight($sHold, 1), Chr(1))
Return SetError(1, 0, 0)
EndFunc ;==>_WinGetByPID
; http://www.cellsworth.com/news/computer-related/35-uncategorizied/96-runintray.html
;
; The main modification to RunInTray is on detection of the window.
; A handle is obtained and used in Hide Show and Close operations.
; This way the client may change window caption and still respond
; to Hide, Show, and Close commands.
;
; Also Close is used instead of Kill to close the client app.
;
Global Const $TRAY_EVENT_PRIMARYUP = -8
Global Const $tagPOINT = "long X;long Y"
Global Const $tagGUID = "dword Data1;word Data2;word Data3;byte Data4[8]"
Global Const $HGDI_ERROR = Ptr(-1)
Global Const $INVALID_HANDLE_VALUE = Ptr(-1)
Global Const $KF_EXTENDED = 0x0100
Global Const $KF_ALTDOWN = 0x2000
Global Const $KF_UP = 0x8000
Global Const $LLKHF_EXTENDED = BitShift($KF_EXTENDED, 8)
Global Const $LLKHF_ALTDOWN = BitShift($KF_ALTDOWN, 8)
Global Const $LLKHF_UP = BitShift($KF_UP, 8)
AutoItSetOption("WinTitleMatchMode", -3)
AutoItSetOption("TrayOnEventMode", 1) ; Use event trapping for tray menu
AutoItSetOption("TrayMenuMode", 3) ; Default tray menu items will not be shown.
Const $delayMin = 1, $delayMax = 60
; call EmptyWorkingSet once a minute
Const $MemCountMax = 60
Global $Dir = "", $App = "", $handle = 0, $title = "", $memCount = 0, $delay = 0, $hasDelay = False
$iMsg = "Usage: " & @CRLF & @CRLF & "RunInTrayMod [ -d=n ] ProgramPath WorkingDir [ WindowTitle ]" & @CRLF
$iMsg &= " ( -d=n is seconds delay : Valid range for n is " & $delayMin & " to " & $delayMax & " )" & @CRLF & @CRLF
$iMsg &= "If any params contain space(s) wrap them in double quotes" & @CRLF & @CRLF
$iMsg &= "If App will not go to Tray specify exact Window Title as last param" & @CRLF & @CRLF
$iMsg &= "( If App leaves Icon in Taskbar when Trayed, it is not compatible )"
Switch $CmdLine[0]
Case 4
If Not StringInStr($CmdLine[1], "-d=") Then _ShowUsage($iMsg)
$delay = Number(StringMid($CmdLine[1], 4))
If $delay < $delayMin Or $delay > $delayMax Then $delay = 0
$App = $CmdLine[2]
$Dir = $CmdLine[3]
$title = $CmdLine[4]
Case 3
If StringInStr($CmdLine[1], "-d=") Then
$delay = Number(StringMid($CmdLine[1], 4))
If $delay < $delayMin Or $delay > $delayMax Then $delay = 0
$App = $CmdLine[2]
$Dir = $CmdLine[3]
Else
$App = $CmdLine[1]
$Dir = $CmdLine[2]
$title = $CmdLine[3]
EndIf
Case 2
If StringInStr($CmdLine[1], "-d=") Then _ShowUsage($iMsg)
$App = $CmdLine[1]
$Dir = $CmdLine[2]
Case Else
_ShowUsage($iMsg)
EndSwitch
$proc_Name = _FileNoPath($App)
$do_Kill = True
While $delay
TraySetToolTip(String($delay) & " Sec. to Launch " & _FileBaseName($App))
Sleep(1000)
$delay -= 1
WEnd
ShellExecute($App, "", $Dir)
Sleep(1000)
If $title <> "" Then
$handle = WinWait($title, "", 4)
If $handle = 0 Then
_ShowError("Could not get Handle for Window:" & @CRLF & @CRLF & $title)
EndIf
EndIf
If $handle = 0 Then
$handle = WinGetHandle(_WinGetByPID($proc_Name))
If $handle = "" Then
$iMsg = "Could Not Get Handle To: " & $proc_Name & @CRLF & @CRLF
$iMsg &= "Try adding Window Title to Command Line or Shortcut Target line"
_ShowError($iMsg)
EndIf
EndIf
TraySetIcon($App)
TraySetClick(8)
$hTray_Show_Item = TrayCreateItem("Hide")
$hTray_Donate_Item = TrayCreateItem("Donate")
TrayCreateItem("")
TrayCreateItem("Exit")
TrayItemSetOnEvent(-1, "On_Exit")
TrayItemSetOnEvent($hTray_Show_Item, "To_Tray")
TrayItemSetOnEvent($hTray_Donate_Item, "Donate")
TraySetOnEvent($TRAY_EVENT_PRIMARYUP, "To_Tray")
TraySetToolTip(_FileBaseName($App))
To_Tray()
While 1
Sleep(1000)
If Not ProcessExists($proc_Name) Then
$do_Kill = False
Exit
EndIf
$memCount += 1
If $memCount >= $MemCountMax Then
_EmptyWorkingSet()
$memCount = 0
EndIf
WEnd
Func On_Exit()
If $do_Kill Then WinClose(_WinAPI_GetWindowText($handle))
Exit
EndFunc ;==>On_Exit
Func To_Tray()
If TrayItemGetText($hTray_Show_Item) = "Hide" Then
_WinAPI_ShowWindow($handle, @SW_HIDE)
TrayItemSetText($hTray_Show_Item, "Show")
Else
_WinAPI_ShowWindow($handle, @SW_SHOW)
TrayItemSetText($hTray_Show_Item, "Hide")
EndIf
EndFunc ;==>To_Tray
Func Donate()
ShellExecute("http://www.favessoft.com/donate.html")
EndFunc ;==>Donate
;-------------- Helper Functions Stripped from Includes ------------
Func _WinAPI_GetWindowText($hWnd)
Local $aResult = DllCall("user32.dll", "int", "GetWindowTextW", "hwnd", $hWnd, "wstr", "", "int", 4096)
If @error Then Return SetError(@error, @extended, "")
Return SetExtended($aResult[0], $aResult[2])
EndFunc ;==>_WinAPI_GetWindowText
Func _WinAPI_ShowWindow($hWnd, $iCmdShow = 5)
Local $aResult = DllCall("user32.dll", "bool", "ShowWindow", "hwnd", $hWnd, "int", $iCmdShow)
If @error Then Return SetError(@error, @extended, False)
Return $aResult[0]
EndFunc ;==>_WinAPI_ShowWindow
Func _EmptyWorkingSet()
Local $result = DllCall("psapi.dll", 'int', 'EmptyWorkingSet', 'long', -1)
Return $result[1]
EndFunc ;==>_EmptyWorkingSet
Func _FileBaseName($path)
If $path = "" Then Return ""
If StringLen($path) < 3 Then Return ""
Local $pos = StringInStr($path, "\", 0, -1)
$pos += 1
Local $tmp = StringMid($path, $pos)
Return StringLeft($tmp, StringInStr($tmp, ".", 0, -1) - 1)
EndFunc ;==>_FileBaseName
Func _FileNoPath($path)
Return StringMid($path, StringInStr($path, "\", 0, -1) + 1)
EndFunc ;==>_FileNoPath
Func _ScriptBaseName()
Return StringLeft(@ScriptName, (StringInStr(@ScriptName, ".") - 1))
EndFunc ;==>_ScriptBaseName
Func _ShowError($errorMsg, $title = "", $quit = True, $timeOut = 0)
If $title = "" Then $title = _ScriptBaseName()
MsgBox(0x1010, $title, $errorMsg, $timeOut)
If $quit Then Exit
EndFunc ;==>_ShowError
Func _ShowUsage($usageMsg, $title = "", $quit = True, $timeOut = 0)
If $title = "" Then $title = _ScriptBaseName()
MsgBox(0x1040, $title, $usageMsg, $timeOut)
If $quit Then Exit
EndFunc ;==>_ShowUsage
Func _WinGetByPID($iPID, $nArray = 1)
If IsString($iPID) Then $iPID = ProcessExists($iPID)
Local $aWList = WinList(), $sHold
For $iCC = 1 To $aWList[0][0]
If WinGetProcess($aWList[$iCC][1]) = $iPID And _
BitAND(WinGetState($aWList[$iCC][1]), 2) Then
If $nArray Then Return $aWList[$iCC][0]
$sHold &= $aWList[$iCC][0] & Chr(1)
EndIf
Next
If $sHold Then Return StringSplit(StringTrimRight($sHold, 1), Chr(1))
Return SetError(1, 0, 0)
EndFunc ;==>_WinGetByPID

Recent Posts