Sign in to follow this  
Followers 0
NomadRJ

Function to slide up and down a GUI behind the taskbar.

5 posts in this topic

#1 ·  Posted (edited)

Guys, I've made a really simple function to show and hide the GUI sliding it up and down behind the taskbar.

Started to use it in one of my scripts. The app is only shown in the tray, and brings up when you click it. I wanted the window to be on the bottom right of the screen, so I used @DesktopWidth and @DesktopHeight, also used WinGetPos("classname=Shell_TrayWnd") to position it according to the taskbar size.

It's working fine, but (there's always a but, as a former girlfriend used to complaint :) ) I'm failing to add some extra functionality to it and really need your help.

What I am trying to do is to check if the window is either in front or in the back of the others (tried WinGetState, but did not work) when you click the tray icon. In case the window is visible, I would like to slide it down, otherwise I would like to bring it to front (now I'm only bringing it to front).

Can you guys advise me on what command I could use to accomplish this?

I'm including a sample script to clarify what I'm saying here.

#Include <Constants.au3>
#include <GUIConstants.au3>

Opt("TrayMenuMode",1) ; Default tray menu items (Script Paused/Exit) will not be shown.
Opt("TrayOnEventMode",1); Change to OnEvent mode 
Opt("GUIOnEventMode", 1); Change to OnEvent mode 
Opt("WinTitleMatchMode", 4); Advanced Window Descriptions

;== DEFINE LABEL AND CHECK DUPLACATE INSTANCE
$label = "SystemMonitor v0.01 beta"
If WinExists($label) Then Exit 
AutoItWinSetTitle($label)

;== DEFINE GUI STRUCTURE
$sizew = 218
$sizeh = 180
$TrayPos = WinGetPos("classname=Shell_TrayWnd"); $TrayPos[3] = altura do Tray
$posw = @DesktopWidth - $sizew - 6
$posh = @DesktopHeight - $sizeh - $TrayPos[3] - 22
$lnspace = 16

;== CREATE GUI INTERFACE
$Form1 = GUICreate($label, $sizew, $sizeh, $posw, $posh+$sizeh+50, Default, $WS_EX_TOOLWINDOW)
GUICtrlCreateGroup("System Information", 10, 8, 200, 130);Group Box
GUICtrlCreateLabel ("User name:", 18, 16+$lnspace, Default, Default)
GUICtrlCreateLabel (@UserName, 102, 16+$lnspace, 100, 15, $SS_RIGHT)
GUICtrlCreateLabel ("Computer name:", 18, 16+2*$lnspace, Default, Default)
GUICtrlCreateLabel (@ComputerName, 102, 16+2*$lnspace, 100, 15, $SS_RIGHT)
GUICtrlCreateLabel ("Free Space:", 18, 16+3*$lnspace, Default, Default)
$hdlabel = GUICtrlCreateLabel ("", 102, 16+3*$lnspace, 100, 15, $SS_RIGHT)
GUICtrlCreateLabel ("IP address (1):", 18, 16+4*$lnspace, Default, Default)
$ip1label = GUICtrlCreateLabel ("", 102, 16+4*$lnspace, 100, 15, $SS_RIGHT)
GUICtrlCreateLabel ("IP address (2):", 18, 16+5*$lnspace, Default, Default)
$ip2label = GUICtrlCreateLabel ("", 102, 16+5*$lnspace, 100, 15, $SS_RIGHT)
GUICtrlCreateLabel ("Logon server:", 18, 16+6*$lnspace, Default, Default)
$logonlabel = GUICtrlCreateLabel ("", 102, 16+6*$lnspace, 100, 15, $SS_RIGHT)
GUICtrlCreateGroup("", -99, -99, 1, 1);close group

;== SYSTRAY DEFINITIONS
TraySetIcon (@AutoItExe)
TraySetClick(16)
TraySetToolTip($label)
$aboutitem = TrayCreateItem("About")
TrayItemSetOnEvent($aboutitem,"_ShowAbout")
$shwindow = TrayCreateItem("ShowGUI")
TrayItemSetOnEvent($shwindow,"_ShowGUI")
$exititem = TrayCreateItem("Exit")
TrayItemSetOnEvent($exititem,"_CloseProgram")

;== GUI DEFINITIONS
$GUIactive = 0
TraySetOnEvent($TRAY_EVENT_PRIMARYUP,"_ShowGUI")
TraySetState()

GUISetOnEvent($GUI_EVENT_CLOSE, "_CloseGUI")

_RefreshStatus();1st Refresh

;== GUI LOOP
While 1
    Sleep(300000) ; Idle loop (300.000 miliseconds = 5 minutos)
    _RefreshStatus()
WEnd
Exit

;== GUI FUNCTIONS
Func _ShowAbout()
    Msgbox(64, "About", "LATG - "&$label)
EndFunc

Func _CloseProgram()
    If $GUIactive = 1 Then
        _SlideDown($label, $posw, $posh, $sizeh)
        Exit
    Else
        Exit
    EndIf
EndFunc

Func _ShowGUI()
    $TrayPos = WinGetPos("classname=Shell_TrayWnd"); $TrayPos[3] = altura do Tray
    $posw = @DesktopWidth - $sizew - 6
    $posh = @DesktopHeight - $sizeh - $TrayPos[3] - 22; -63 para janela normal
    If $GUIactive = 0 Then
        _SlideUp($label, $posw, $posh)
        $GUIactive = 1
        _RefreshStatus()
    Else
        WinActivate($label)
    EndIf
EndFunc

Func _CloseGUI()
    _SlideDown($label, $posw, $posh, $sizeh)
    $GUIactive = 0
EndFunc


;== GENERAL FUNCTIONS
Func _RefreshStatus()
    $hddfree = Round((DriveSpaceFree("c:\")*100)/DriveSpaceTotal("c:\"),1)
    GUICtrlSetData ($hdlabel, $hddfree & "%")
    GUICtrlSetData ($ip1label, @IPAddress1)
    GUICtrlSetData ($ip2label, @IPAddress2)
    GUICtrlSetData ($logonlabel, StringMid(@LogonServer,3,12))
EndFunc

; _SlideUp function to present window
Func _SlideUp($title, $x, $y)
    GUISetState (@SW_SHOW) 
    WinMove ($title, "", $x, $y, Default, Default, 2)
EndFunc

; _SlideDown function to close window
Func _SlideDown($title, $x, $y, $z)
    WinMove ($label, "", $x, $y+$z+30, Default, Default, 2)
    GUISetState (@SW_HIDE) 
EndFunc
Edited by NomadRJ

Share this post


Link to post
Share on other sites



Guys, I've made a really simple function to show and hide the GUI sliding it up and down behind the taskbar.

Started to use it in one of my scripts. The app is only shown in the tray, and brings up when you click it. I wanted the window to be on the bottom right of the screen, so I used @DesktopWidth and @DesktopHeight, also used WinGetPos("classname=Shell_TrayWnd") to position it according to the taskbar size.

It's working fine, but (there's always a but, as a former girlfriend used to complaint :) ) I'm failing to add some extra functionality to it and really need your help.

What I am trying to do is to check if the window is either in front or in the back of the others (tried WinGetState, but did not work) when you click the tray icon. In case the window is visible, I would like to slide it down, otherwise I would like to bring it to front (now I'm only bringing it to front).

Can you guys advise me on what command I could use to accomplish this?

I'm including a sample script to clarify what I'm saying here.

#Include <Constants.au3>
#include <GUIConstants.au3>

Opt("TrayMenuMode",1); Default tray menu items (Script Paused/Exit) will not be shown.
Opt("TrayOnEventMode",1); Change to OnEvent mode 
Opt("GUIOnEventMode", 1); Change to OnEvent mode 
Opt("WinTitleMatchMode", 4); Advanced Window Descriptions

;== DEFINE LABEL AND CHECK DUPLACATE INSTANCE
$label = "SystemMonitor v0.01 beta"
If WinExists($label) Then Exit 
AutoItWinSetTitle($label)

;== DEFINE GUI STRUCTURE
$sizew = 218
$sizeh = 180
$TrayPos = WinGetPos("classname=Shell_TrayWnd"); $TrayPos[3] = altura do Tray
$posw = @DesktopWidth - $sizew - 6
$posh = @DesktopHeight - $sizeh - $TrayPos[3] - 22
$lnspace = 16

;== CREATE GUI INTERFACE
$Form1 = GUICreate($label, $sizew, $sizeh, $posw, $posh+$sizeh+50, Default, $WS_EX_TOOLWINDOW)
GUICtrlCreateGroup("System Information", 10, 8, 200, 130);Group Box
GUICtrlCreateLabel ("User name:", 18, 16+$lnspace, Default, Default)
GUICtrlCreateLabel (@UserName, 102, 16+$lnspace, 100, 15, $SS_RIGHT)
GUICtrlCreateLabel ("Computer name:", 18, 16+2*$lnspace, Default, Default)
GUICtrlCreateLabel (@ComputerName, 102, 16+2*$lnspace, 100, 15, $SS_RIGHT)
GUICtrlCreateLabel ("Free Space:", 18, 16+3*$lnspace, Default, Default)
$hdlabel = GUICtrlCreateLabel ("", 102, 16+3*$lnspace, 100, 15, $SS_RIGHT)
GUICtrlCreateLabel ("IP address (1):", 18, 16+4*$lnspace, Default, Default)
$ip1label = GUICtrlCreateLabel ("", 102, 16+4*$lnspace, 100, 15, $SS_RIGHT)
GUICtrlCreateLabel ("IP address (2):", 18, 16+5*$lnspace, Default, Default)
$ip2label = GUICtrlCreateLabel ("", 102, 16+5*$lnspace, 100, 15, $SS_RIGHT)
GUICtrlCreateLabel ("Logon server:", 18, 16+6*$lnspace, Default, Default)
$logonlabel = GUICtrlCreateLabel ("", 102, 16+6*$lnspace, 100, 15, $SS_RIGHT)
GUICtrlCreateGroup("", -99, -99, 1, 1);close group

;== SYSTRAY DEFINITIONS
TraySetIcon (@AutoItExe)
TraySetClick(16)
TraySetToolTip($label)
$aboutitem = TrayCreateItem("About")
TrayItemSetOnEvent($aboutitem,"_ShowAbout")
$shwindow = TrayCreateItem("ShowGUI")
TrayItemSetOnEvent($shwindow,"_ShowGUI")
$exititem = TrayCreateItem("Exit")
TrayItemSetOnEvent($exititem,"_CloseProgram")

;== GUI DEFINITIONS
$GUIactive = 0
TraySetOnEvent($TRAY_EVENT_PRIMARYUP,"_ShowGUI")
TraySetState()

GUISetOnEvent($GUI_EVENT_CLOSE, "_CloseGUI")

_RefreshStatus();1st Refresh

;== GUI LOOP
While 1
    Sleep(300000); Idle loop (300.000 miliseconds = 5 minutos)
    _RefreshStatus()
WEnd
Exit

;== GUI FUNCTIONS
Func _ShowAbout()
    Msgbox(64, "About", "LATG - "&$label)
EndFunc

Func _CloseProgram()
    If $GUIactive = 1 Then
        _SlideDown($label, $posw, $posh, $sizeh)
        Exit
    Else
        Exit
    EndIf
EndFunc

Func _ShowGUI()
    $TrayPos = WinGetPos("classname=Shell_TrayWnd"); $TrayPos[3] = altura do Tray
    $posw = @DesktopWidth - $sizew - 6
    $posh = @DesktopHeight - $sizeh - $TrayPos[3] - 22; -63 para janela normal
    If $GUIactive = 0 Then
        _SlideUp($label, $posw, $posh)
        $GUIactive = 1
        _RefreshStatus()
    Else
        WinActivate($label)
    EndIf
EndFunc

Func _CloseGUI()
    _SlideDown($label, $posw, $posh, $sizeh)
    $GUIactive = 0
EndFunc


;== GENERAL FUNCTIONS
Func _RefreshStatus()
    $hddfree = Round((DriveSpaceFree("c:\")*100)/DriveSpaceTotal("c:\"),1)
    GUICtrlSetData ($hdlabel, $hddfree & "%")
    GUICtrlSetData ($ip1label, @IPAddress1)
    GUICtrlSetData ($ip2label, @IPAddress2)
    GUICtrlSetData ($logonlabel, StringMid(@LogonServer,3,12))
EndFunc

; _SlideUp function to present window
Func _SlideUp($title, $x, $y)
    GUISetState (@SW_SHOW) 
    WinMove ($title, "", $x, $y, Default, Default, 2)
EndFunc

; _SlideDown function to close window
Func _SlideDown($title, $x, $y, $z)
    WinMove ($label, "", $x, $y+$z+30, Default, Default, 2)
    GUISetState (@SW_HIDE) 
EndFunc
Maybe search for examples of "WindowFromPoint"

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

This is for checking if a Window has the focus, which it normally doesn't have if it is covered up AFAIK:

If WinActive($myWindowHandle) Then
...
EndIf
I guess you would put this code in the main loop.

But here is a better way to make toast, derived from Rob Saunder's work, ready to test:

#NoTrayIcon
; Authors:  Rob Saunders (admin@therks.com) & squirrely1
#region Init
Opt('WinTitleMatchMode', 4)
Opt('MustDeclareVars', 1)
Global Const $WS_BORDER = 0x00800000
Global Const $WS_POPUP = 0x80000000
Global Const $WS_EX_TOPMOST = 0x00000008
Global Const $WS_EX_TOOLWINDOW = 0x00000080
Global Const $WINANI_HIDE = 0x10000
Global Const $WINANI_LEFTTORIGHT = 0x40001
Global Const $WINANI_RIGHTTOLEFT = 0x40002
Global Const $WINANI_TOPTOBOTTOM = 0x40004
Global Const $WINANI_BOTTOMTOTOP = 0x40008
Global Const $WINANI_ALPHA = 0x80000
Global $hTaskbar
Global $iWinX
Global $iWinY
Global $sTaskbarPos
Global $aTaskbarPos
Global $iWinAniShow
Global $iWinAniHide
Global $oIE
Global $gui_Toast
Global $ob_Main
Global $sIniFile = @ScriptDir & '\toast.ini'
Global $iWinWidth = 156
Global $iWinHeight = 76
Global $iTimeOut = 7000
Global $sToastContent = @ScriptDir & '\Toast.html'
Global $conditions
$conditions =         "Temperature:  68°F"
$conditions &= @CR &"Humidity:  28%"
$conditions &= @CR &"Wind:  4 mph"
$conditions &= @CR &"Skies:  Partly cloudy"
$hTaskbar = WinGetHandle('classname=Shell_TrayWnd')
If @error Then
    $iWinX = @DesktopWidth - $iWinWidth
    $iWinY = @DesktopHeight - $iWinHeight
Else
    $sTaskbarPos = _TaskbarGetPos($hTaskbar, 1)
    $aTaskbarPos = WinGetPos($hTaskbar)
    Switch $sTaskbarPos
        Case 'top'
            $iWinX = @DesktopWidth - $iWinWidth
            $iWinY = $aTaskbarPos[1] + $aTaskbarPos[3]
            $iWinAniShow = $WINANI_TOPTOBOTTOM
            $iWinAniHide = BitOR($WINANI_BOTTOMTOTOP, $WINANI_HIDE)
        Case 'bottom'
            $iWinX = @DesktopWidth - $iWinWidth
            $iWinY = $aTaskbarPos[1] - $iWinHeight
            $iWinAniShow = $WINANI_BOTTOMTOTOP
            $iWinAniHide = BitOR($WINANI_TOPTOBOTTOM, $WINANI_HIDE)
        Case 'left'
            $iWinX = $aTaskbarPos[0] + $aTaskbarPos[2]
            $iWinY = @DesktopHeight - $iWinHeight
            $iWinAniShow = $WINANI_LEFTTORIGHT
            $iWinAniHide = BitOR($WINANI_RIGHTTOLEFT, $WINANI_HIDE)
        Case 'right'
            $iWinX = $aTaskbarPos[0] - $iWinWidth
            $iWinY = @DesktopHeight - $iWinHeight
            $iWinAniShow = $WINANI_RIGHTTOLEFT
            $iWinAniHide = BitOR($WINANI_LEFTTORIGHT, $WINANI_HIDE)
    EndSwitch
EndIf
#endregion Init
#region controls
$gui_Toast = GUICreate('Toast!', $iWinWidth, $iWinHeight, $iWinX, $iWinY, $WS_POPUP+$WS_BORDER, $WS_EX_TOPMOST+$WS_EX_TOOLWINDOW)
;GUISetBkColor(0xB5C2CE)
GUISetFont(10, 400, 0, "Tahoma")
$ob_Main = GUICtrlCreateLabel($conditions, 8, 5, $iWinWidth-16, $iWinHeight-10)
;GUICtrlSetBkColor($ob_Main, 0x000000)
;GUICtrlSetColor($ob_Main, 0xFFFFFF)
_AnimateWindow($gui_Toast, '', 360, $iWinAniShow)
GUISetState()
Dim $iTimer = TimerInit()
Dim $iWinAniHideTime = 500
While 1
    If TimerDiff($iTimer) > $iTimeOut Then
        ExitLoop
    EndIf
    Sleep(100)
WEnd
_AnimateWindow($gui_Toast, '', 640, $iWinAniHide)
#endregion controls
;===============================================================================
;Syntax:            _AnimateWindow( $vTitle [, $sText = '' [, $iTime = 1000 [, $iMethod = 0x80000 [, $vUser32Dll = 'user32.dll' ] ] ] ] )
;Parameter:     $sTitle : Title or handle to window.
;               $sText  : Text of window (optional).
;Return Value:  A two-item array: 
;                   [0] = Width offset
;                   [1] = Height offset
;Author:        Rob Saunders (admin@therks.com)
;User Calltip:  _AnimateWindow($vTitle, $sText = '', $iTime, $iMethod, $vUser32Dll)
;Example:       _AnimateWindow('Untitled - Notepad', '', 1000, BitOR($WINANI_HIDE, $WINANI_LEFTTORIGHT, $WINANI_TOPTOBOTTOM), $hUser32Dll)
;===============================================================================
Func _AnimateWindow($vTitle, $sText = '', $iTime = 1000, $iMethod = 0x80000, $vUser32Dll = 'user32.dll')
    Local $hWnd
    If IsHWnd($vTitle) Then
        $hWnd = $vTitle
    Else
        $hWnd = WinGetHandle($vTitle, $sText)
    EndIf
    DllCall($vUser32Dll, 'int', 'AnimateWindow', 'hwnd', $hWnd, 'int', $iTime, 'long', $iMethod)
EndFunc ;==>_AnimateWindow
;===============================================================================
;
; Description:      _TaskbarGetPos - Returns the position the user has placed their taskbar (top, left, right, or bottom)
; Syntax:           _TaskbarGetPos ( $h_Taskbar [, $i_ReturnType = 0 ] )
; Parameter(s):     $h_Taskbar - Handle of taskbar window.
;                   $b_ReturnString - If False (default) returns number. If True returns string.
; Return Value(s):  Position of taskbar, as follows:
;                       0, 'top'
;                       1, 'bottom'
;                       2, 'left'
;                       3, 'right'
; Author(s):        Rob Saunders (admin@therks.com)
;
;===============================================================================
Func _TaskbarGetPos($h_Taskbar, $b_ReturnString = False)
    Local $s_TaskbarPos
    Local $i_TaskbarPos
    Local $a_TaskbarPos = WinGetPos($h_Taskbar)
    If $a_TaskbarPos[2] >= @DesktopWidth Then
        If $a_TaskbarPos[1] < @DesktopHeight / 2 Then
            $s_TaskbarPos = 'top'
            $i_TaskbarPos = 0
        Else
            $s_TaskbarPos = 'bottom'
            $i_TaskbarPos = 1
        EndIf
    Else
        If $a_TaskbarPos[0] < @DesktopWidth / 2 Then
            $s_TaskbarPos = 'left'
            $i_TaskbarPos = 2
        Else
            $s_TaskbarPos = 'right'
            $i_TaskbarPos = 3
        EndIf
    EndIf
    If $b_ReturnString Then
        Return $s_TaskbarPos
    Else
        Return $i_TaskbarPos
    EndIf
EndFunc ;==>_TaskbarGetPos
I have been putting cream cheese on my toast lately, but it is showing up around my mid-section these days.

Dictum: "Put down the cream cheese and step away from the kitchen!"

Edited by Squirrely1

Das Häschen benutzt Radar

Share this post


Link to post
Share on other sites

Nice example squirrel! Thanks! The user32.dll function is smoother than the simple one I have created.

I just found out why my idea wasn't working. As soon as you click on the tray icon the main window loose the focus. So, doesn't matter what kind of test I do, It will never be selected when clicking the icon. LoL! :)

I'm wondering if there is still a way to know if the window is hidden by others.... :)

Share this post


Link to post
Share on other sites

I'm wondering if there is still a way to know if the window is hidden by others.

I think that the extended style $WS_EX_TOPMOST generally keeps the toast on top of all other windows.

Das Häschen benutzt Radar

Share this post


Link to post
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
Sign in to follow this  
Followers 0