Jump to content
Sign in to follow this  
badapple89

Sending GUI "to back"

Recommended Posts

badapple89

This one hopefully is easier than my last post.

I have a small GUI window where a user puts in someones name and presses search and it does an AD lookup to retrieve that persons extension number.

Currently I have Opt("TrayOnEventMode",1) and when minimised the gui gets GuiSetState(@SW_HIDE) and sent to tray via TraySetState(1). They then click the tray icon to  GuiSetState(@SW_SHOW).

This is so they don't have to re-open each time and can easily launch via tray icon, and so there task-bar (the bar listing all open programs) isnt taken up all day when this is open.

And this works.

However I want it so when the user navigates away from GIU weather it be via the minimise button OR just clicking a part of the screen that is not the GUI (generally outlook or another open program). That the GUI is "sent to the back" of other open programs or if you like the exact opposite of "always on top" (just clearing the WinSetOnTop flag dosnt work).

I.E. The user would have to minimise all other open programs to see the GUI or click the tray icon to make it show "on top" again.

 

I have played around with a few things but cant seem to achieve:

1. Sending to back of all open programs .... GUISetState(@SW_DISABLE) then WinSetOnTop($mainwindow, "", 0) seems to kinda do it but I cant enable cleanly.

2. How to run anything when the user clicks away from GIU

3. ....and then when they click back on the window. Maybe $GUI_EVENT_PRIMARYDOWN? Depending what solution is to Question 1 + 2

3. Will skip the hiding of program on taskbar for now as I think I can achieve that via $WS_EX_TOOLWINDOW or child window.  

Any ideas welcome.

And hope I'm not posing too many threads.

Share this post


Link to post
Share on other sites
PhoenixXL

Code

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

Local Const $hGUI = GUICreate("Testing", 500, 300)
Local Const $ibTn = GUICtrlCreateButton("Go to Back", 10, 10)
GUISetState()

Do
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $ibTn
            SendWinToBack($hGUI)
    EndSwitch

Until $nMsg = $GUI_EVENT_CLOSE


Func SendWinToBack($hWnd)
        
        ;Default performs no work rather than just to complete the syntax of parameters
        ;$SWP_NOMOVE and $SWP_NOSIZE are required to make the GUI visible.
    Return _WinAPI_SetWindowPos($hWnd, $HWND_BOTTOM, Default, Default, Default, Default, BitOR($SWP_NOACTIVATE, $SWP_SHOWWINDOW, $SWP_NOMOVE, $SWP_NOSIZE ))
EndFunc   ;==>SendWinToBack

Regards :)

Edited by PhoenixXL

My code:

PredictText: Predict Text of an Edit Control Like Scite. Remote Gmail: Execute your Scripts through Gmail. StringRegExp:Share and learn RegExp.

Run As System: A command line wrapper around PSEXEC.exe to execute your apps scripts as System (LSA). Database: An easier approach for _SQ_LITE beginners.

MathsEx: A UDF for Fractions and LCM, GCF/HCF. FloatingText: An UDF for make your text floating. Clipboard Extendor: A clipboard monitoring tool. 

Custom ScrollBar: Scroll Bar made with GDI+, user can use bitmaps instead. RestrictEdit_SRE: Restrict text in an Edit Control through a Regular Expression.

Share this post


Link to post
Share on other sites
badapple89

Wow that's exactly what I want. Thanks for that (didn't expect the code)!!!!

Buuuuut any hints for point 2?

How can I run the function when the user clicks AWAY from the GUI?

I have struggled with this in the past.

Also could you explain why Return is used? The help file says its for a user defined function but were not using one?

Share this post


Link to post
Share on other sites
PhoenixXL

Also could you explain why Return is used? The help file says its for a user defined function but were not using one?

 

The return is used to show the success of the function,ReturnVal -  Success: True; Failure: False

For point 2, the easiest way would be check if the window is active or not 

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

Global Const $hGUI = GUICreate("Testing", 500, 300)
Local Const $ibTn = GUICtrlCreateButton("Go to Back", 10, 10)
Global Const $bFocusLost = GUICtrlCreateDummy()
AdlibRegister("CheckFocus")
GUISetState()

Do
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $ibTn, $bFocusLost
            SendWinToBack($hGUI)
    EndSwitch

Until $nMsg = $GUI_EVENT_CLOSE


Func SendWinToBack($hWnd)

    ;Default performs no work rather than just to complete the syntax of parameters
    ;$SWP_NOMOVE and $SWP_NOSIZE are required to make the GUI visible.
    Return _WinAPI_SetWindowPos($hWnd, $HWND_BOTTOM, Default, Default, Default, Default, BitOR($SWP_NOACTIVATE, $SWP_SHOWWINDOW, $SWP_NOMOVE, $SWP_NOSIZE))
EndFunc   ;==>SendWinToBack

Func CheckFocus()
    If WinActive($hGUI) = 0 Then GUICtrlSendToDummy($bFocusLost)
EndFunc   ;==>CheckFocus

The following code using messages is the more recommended way

#include <WinAPI.au3>
#include <Constants.au3>
#include <GUIConstants.au3>
#include <WindowsConstants.au3>

Local Const $hGUI = GUICreate("Testing", 500, 300)
Local Const $ibTn = GUICtrlCreateButton("Go to Back", 10, 10)
GUIRegisterMsg($WM_ACTIVATE, "WM_ACTIVATE")
GUISetState()

Do
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $ibTn
            SendWinToBack($hGUI)
    EndSwitch

Until $nMsg = $GUI_EVENT_CLOSE

Func SendWinToBack($hWnd)

    ;Default performs no work rather than just to complete the syntax of parameters
    ;$SWP_NOMOVE and $SWP_NOSIZE are required to make the GUI visible.
    Return _WinAPI_SetWindowPos($hWnd, $HWND_BOTTOM, Default, Default, Default, Default, BitOR($SWP_NOACTIVATE, $SWP_SHOWWINDOW, $SWP_NOMOVE, $SWP_NOSIZE))
EndFunc   ;==>SendWinToBack

Func WM_ACTIVATE($hWnd, $iMsg, $wParam, $lParam)

    Local Const $iState = _WinAPI_LoWord($wParam)
    If $iState = 0 Then ConsoleWrite("Window DeActivated, Sending to the Back? " & SendWinToBack($hWnd) & @CRLF)

    Return 0

EndFunc   ;==>WM_ACTIVATE

Regards ;)

Edited by PhoenixXL

My code:

PredictText: Predict Text of an Edit Control Like Scite. Remote Gmail: Execute your Scripts through Gmail. StringRegExp:Share and learn RegExp.

Run As System: A command line wrapper around PSEXEC.exe to execute your apps scripts as System (LSA). Database: An easier approach for _SQ_LITE beginners.

MathsEx: A UDF for Fractions and LCM, GCF/HCF. FloatingText: An UDF for make your text floating. Clipboard Extendor: A clipboard monitoring tool. 

Custom ScrollBar: Scroll Bar made with GDI+, user can use bitmaps instead. RestrictEdit_SRE: Restrict text in an Edit Control through a Regular Expression.

Share this post


Link to post
Share on other sites
FireFox

Or WM_ACTIVATE.


 

OS : Win XP SP2 (32 bits) / Win 7 SP1 (64 bits) / Win 8 (64 bits) | Autoit version: latest stable / beta.
Hardware : Intel(R) Core(TM) i5-2400 CPU @ 3.10Ghz / 8 GiB RAM DDR3.

My UDFs : Skype UDF | TrayIconEx UDF | GUI Panel UDF | Excel XML UDF | Is_Pressed_UDF

My Projects : YouTube Multi-downloader | FTP Easy-UP | Lock'n | WinKill | AVICapture | Skype TM | Tap Maker | ShellNew | Scriptner | Const Replacer | FT_Pocket | Chrome theme maker

My Examples : Capture toolIP Camera | Crosshair | Draw Captured Region | Picture Screensaver | Jscreenfix | Drivetemp | Picture viewer

My Snippets : Basic TCP | Systray_GetIconIndex | Intercept End task | Winpcap various | Advanced HotKeySet | Transparent Edit control

 

Share this post


Link to post
Share on other sites
PhoenixXL

At the same time : FireFox posted and I edited :P

Edited by PhoenixXL

My code:

PredictText: Predict Text of an Edit Control Like Scite. Remote Gmail: Execute your Scripts through Gmail. StringRegExp:Share and learn RegExp.

Run As System: A command line wrapper around PSEXEC.exe to execute your apps scripts as System (LSA). Database: An easier approach for _SQ_LITE beginners.

MathsEx: A UDF for Fractions and LCM, GCF/HCF. FloatingText: An UDF for make your text floating. Clipboard Extendor: A clipboard monitoring tool. 

Custom ScrollBar: Scroll Bar made with GDI+, user can use bitmaps instead. RestrictEdit_SRE: Restrict text in an Edit Control through a Regular Expression.

Share this post


Link to post
Share on other sites
badapple89

See this is the stuff that no matter how many help files I read would never work out or find WM_ACTIVATE. Thanks FireFox and Phoenix (so many fire references)
All ruining sweet including hiding the taskbar (just using a child window)

I rewrote the above code to use GUIOnEventMode as this is what I always have used (and just wanted to see if I could convert it ;))
Always thought that not using it would use more CPU as help says if GUIOnEventMode is not used the GUI is continually polled. But comparing these scripts they run the same. The help file also says its down to personal preference and the needs of the script.
Any reason for me NOT to use OnEventMode.

Also I had trouble with this before and never worked it out.

If _Singleton("Ext. Search", 1) = 0 Then
Maximize()
EndIf
Func Maximize()
    GUISetState(@SW_RESTORE)
    GuiCtrlSetState($inpSearch, $GUI_FOCUS)
EndFunc

But I think it wiggs out because $GUI_FOCUS is not a declared. But how do I declare it properly?

Will post whole script when done as I think its pretty handy ... If your users extension numbers are stored in there AD profile that is.

Share this post


Link to post
Share on other sites
PhoenixXL
But I think it wiggs out because $GUI_FOCUS is not a declared. But how do I declare it properly?

 

Include the header file, "<GUIConstantsEx.au3>"

As far, I prefer using MessageLoop since it makes the code inside the loop and much of the code(no. of lines) reduce.

Simultaneously there is not much difference in the output perceived from both the different ways.


My code:

PredictText: Predict Text of an Edit Control Like Scite. Remote Gmail: Execute your Scripts through Gmail. StringRegExp:Share and learn RegExp.

Run As System: A command line wrapper around PSEXEC.exe to execute your apps scripts as System (LSA). Database: An easier approach for _SQ_LITE beginners.

MathsEx: A UDF for Fractions and LCM, GCF/HCF. FloatingText: An UDF for make your text floating. Clipboard Extendor: A clipboard monitoring tool. 

Custom ScrollBar: Scroll Bar made with GDI+, user can use bitmaps instead. RestrictEdit_SRE: Restrict text in an Edit Control through a Regular Expression.

Share this post


Link to post
Share on other sites
badapple89

Naw its already in there ... or have I included it wrong? :geek:
The function runs as expected when called normally, its only when called from _Singleton that it cant run (and can I only test _Singleton  once compiled)
 
This is the whole code btw. Some of its a little messy, but I think its pretty good for only learning this year.
 
For others that may stumble upon this
May need to change some of the AD fields it looks up or use a custom array.
Also need the extra AD.au3 file (search the forums as I cant remember who made it :ermm: )
 
 

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Icon=H:\Scripts\AutoIT\Ext. Search\Phone.ico
#AutoIt3Wrapper_Res_SaveSource=Y
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
 
 
#include <WinAPI.au3>
#include <GUIConstantsEx.au3>
#include <AD.au3>
#include <Array.au3>
#include <GuiListView.au3>
#include <GuiButton.au3>
#include <WindowsConstants.au3>
#include <Constants.au3>
#include <Misc.au3>
 
;If already running then
If _Singleton("Ext. Search", 1) = 0 Then
 MsgBox (0, "Ext. Search", "Already open!" &@CRLF& "Maybe its hiding down next to the time.")
 Exit
EndIf
 
;Set GUI
Opt("GUIOnEventMode", 1) ; Change to OnEvent mode
Opt("TrayOnEventMode",1)
Opt("TrayMenuMode",1)
opt("GUIEventOptions",1)
 
 
$guiSettings = GUICreate('', -1, -1, -1, -1, -1,BitOR($WS_EX_LAYERED, $WS_EX_TOOLWINDOW))
GUiSetState()
$mainwindow = GUICreate("Ext. Search", 240, 150, -1, -1, -1, -1, $guiSettings)
 
;Call functions on actions
GUISetOnEvent($GUI_EVENT_CLOSE, "On_Close")
GUISetOnEvent($GUI_EVENT_MINIMIZE, "Minimize")
TraySetOnEvent($TRAY_EVENT_PRIMARYUP,"Maximize")
GUIRegisterMsg($WM_ACTIVATE, "WM_ACTIVATE")
 
;Search Input
Global $inpSearch = GUICtrlCreateInput("", 30, 15, 100)
 
;Search button
$butSearch = GUICtrlCreateButton("Search", 140, 13, 60, 25, $BS_DEFPUSHBUTTON)
GUICtrlSetTip(-1, "Enter name or initials", "", "", 1)
GUICtrlSetOnEvent($butSearch, "Search")
 
;VNC button
;Modify names to show to other users
If @UserName = @UserName then
$butVNC = GUICtrlCreateButton("VNC", 205, 13, 30, 25)
GUICtrlSetTip(-1, "Modify .au3 to show to other users", "", "", 1)
GUICtrlSetOnEvent($butVNC, "VNC")
EndIf
 
;Results list window
$hListView = GUICtrlCreateListView("", 10, 50, 218, 80)
; Add columns ro results list
_GUICtrlListView_AddColumn($hListView, "Name", 100)
_GUICtrlListView_AddColumn($hListView, "Initials", 50)
_GUICtrlListView_AddColumn($hListView, "Ext.", 45)
 
;Show GUI as hidden by default
GUISetState(@SW_SHOW)
 
;Get list of users so search can work
Getlist()
 
;Idle around only needed for main GUI
While 1
Sleep(1000)
WEnd
 
Func Getlist()
;Open connection to AD
_AD_Open()
 
;Users OU
$sOU = "OU=Users,OU=Wallmans,DC=wallmans,DC=local"
 
;Get follwoing details for all objects in OU
global $aObjects = _AD_GetObjectsInOU($sOU, "(physicalDeliveryOfficeName=*)", 2, "sAMAccountName,displayname,pager,telephonenumber")
If @error > 0 Then
MsgBox(64, "Active Directory", "No OUs could be found")
Else
;_ArrayDisplay($aObjects, "Active Directory Functions - Example 1 - Objects in OU '" & $sOU & "'")
EndIf
 
;For results loop through backards (or else index error)
For $i = $aObjects[0][0] to 0 step -1
;See if member of North or south
If _AD_IsMemberOf("400 South",$aObjects[$i][0]) +  _AD_IsMemberOf("400 North",$aObjects[$i][0]) < 1 then
;If not then delete
_ArrayDelete($aObjects, $i)
Endif
Next
 
;close AD Connection
_AD_Close()
Endfunc
 
Func Search()
 
;Clear previous search results
_GUICtrlListView_DeleteAllItems(GUICtrlGetHandle($hListView))
 
;Get text from input box
$Search = GUICtrlRead($inpSearch)
 
;Name array as it will error if _ArrayFindAll returns 0
Dim $aName[1]
;Search all objects on name feild for text entered
$aName = _ArrayFindAll($aObjects, $Search, 0, 0, 0, 1, 1)
 
;Name array as it will error if _ArrayFindAll returns 0
Dim $aInitial[1]
;Search all objects on pager (initials) feild for initials
$aInitial = _ArrayFindAll($aObjects, $Search, 0, 0, 0, 1, 2)
 
;If nothing was found in name
If UBound($aName) = 0 Then
;Then rename $aInitial as $aName so next part of code can work
$aName = $aInitial
Else
;If results found in both then join arrays as $aName
_ArrayConcatenate($aName, $aInitial)
EndIf
 
;Testing
;_ArrayDisplay($aName, "Combined")
 
;If we have results then continue
If UBound($aName) > 0 Then
 
;Array for just usernames - only used for VNC connection
Global $aUName[UBound($aName)]
 
;Name new aray of search results
Dim $aSearch[UBound($aName)][3]
 
;Fill new arrays
For $i = 0 To UBound($aName) - 1
;Username to seperate array
$aUName[$i] = $aObjects[$aName[$i]][0]
 
;Name
$aSearch[$i][0] = $aObjects[$aName[$i]][1]
;Initials
$aSearch[$i][1] = $aObjects[$aName[$i]][2]
;Format phone number to extion number
$iPhone = StringMid($aObjects[$aName[$i]][3], 13, 3)
;Extention number
$aSearch[$i][2] = "2" & $iPhone
Next
 
;Populate new aray into result list $hListView
_GUICtrlListView_AddArray($hListView, $aSearch)
 
Else
;If no results list the message into $hListView
GUICtrlCreateListViewItem("No Results", $hListView)
EndIf
EndFunc   ;==>Search
 
Func VNC()
;Get list number selected
$vSelected =  _GUICtrlListView_GetSelectedIndices($hListView, False)
;Get that username from $aSearch array
Run("\\adl-file\logs$\VNC\" & $aUName[$vSelected] & ".bat")
Minimize()
EndFunc
 
Func Minimize()
$hWnd = $mainwindow
Return _WinAPI_SetWindowPos($hWnd, $HWND_BOTTOM, Default, Default, Default, Default, BitOR($SWP_NOMOVE, $SWP_NOSIZE ))
EndFunc
 
Func Maximize()
GUISetState(@SW_RESTORE)
GuiCtrlSetState($inpSearch, $GUI_FOCUS)
EndFunc
 
Func On_Close()
Exit ; If it was this GUI - we exit
EndFunc   ;==>On_Close
 
Func WM_ACTIVATE($hWnd, $iMsg, $wParam, $lParam)
    If _WinAPI_LoWord($wParam) = 0 Then Minimize()
    Return 0
EndFunc   ;==>WM_ACTIVATE


 

EDIT: Added correct code wrapping ;)

Edited by badapple89

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  

×