Jump to content
GordonNT

Check if user is on desktop

Recommended Posts

Hi !

I would like to know when the user is switching on desktop, this is actually what I have :

#include <Array.au3>

$title = WinGetTitle("[active]")

Func onAppSwitch( $t )
    If $t == "" Then
        MsgBox($MB_SYSTEMMODAL, $title, "You are on desktop")
    EndIf
EndFunc

While 1
    Sleep( 250 )
    local $pTitle = WinGetTitle("[active]")
    If Not ( $pTitle == $title ) Then
        $title = $pTitle
        onAppSwitch( $pTitle )
    EndIf
Wend

But the problem is that the desktop is not the only window that has " " as title. Should I do that with the handle ? Well I don't really have clue about that.

Thanks for the help !

Share this post


Link to post
Share on other sites

Hi, thanks for your help @Nine

This is what I have now, thanks to you

#include <MsgBoxConstants.au3>
#include <WinAPISysWin.au3>

While 1
    Sleep( 250 )
    local $pHandle = WinGetHandle( "[ACTIVE]" )
    local $dHandle = _WinAPI_GetDesktopWindow( )
    If ($dHandle == $pHandle) Then
        MsgBox($MB_SYSTEMMODAL, "h", "You are on desktop")
    EndIf
Wend

Unfortunatly, it doesn't work, I can't find out why, could you help me again ?

Share this post


Link to post
Share on other sites

The _WinAPI_GetDesktopWindow is returning the wrong handle, not sure what handle it's returning, but it's not the Desktop.

Try this, it works.

#include <MsgBoxConstants.au3>
#include <WinAPISysWin.au3>
Global $pHandle,$dHandle = WinGetHandle("Program Manager")

While 1
    Sleep(250)
    $pHandle = WinGetHandle("[ACTIVE]")
    If ($dHandle = $pHandle) Then
        MsgBox($MB_SYSTEMMODAL, "h", "You are on desktop")
    EndIf
WEnd

 


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

@GordonNT, I forgot that by default, the handle you get when all apps are minimized is the handle of explorer.exe (on win7 at least).  So the best way to ensure you got the right handle use this :

WinMinimizeAll ()
Sleep (500)
Global $pHandle,$dHandle = WinGetHandle("[ACTIVE]")

While 1
    Sleep(250)
    $pHandle = WinGetHandle("[ACTIVE]")
    If ($dHandle = $pHandle) Then
        MsgBox($MB_SYSTEMMODAL, "h", "You are on desktop")
    EndIf
WEnd

 

 

Share this post


Link to post
Share on other sites
Global $pHandle,$dHandle = WinGetHandle("[ACTIVE]")

That will only work if the desktop is the active window the first time this is ran, which is highly unlikely.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

@BrewManNH Hi, thanks for your help

I tried you code but it doesn't work 😕  (i'm on win10, maybe it's different)

 

@Nine I tried your code as well but it always show me the msgbox even when I switch window 

Share this post


Link to post
Share on other sites
Posted (edited)

I'm on Windows 10 and it's working ok for me.  Try putting some consolewrites in there to see what handle you're getting.

#include <MsgBoxConstants.au3>
#include <WinAPISysWin.au3>
Global $pHandle, $dHandle = WinGetHandle("Program Manager")
ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $dHandle = ' & $dHandle & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console
;~  shows the handle of the desktop
While 1
    Sleep(250)
    $pHandle = WinGetHandle("[ACTIVE]")
    ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $pHandle = ' & $pHandle & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console
;~  shows the handle of the active window
    If ($dHandle = $pHandle) Then
        MsgBox($MB_SYSTEMMODAL, "h", "You are on desktop")
    EndIf
WEnd

You have to click on the desktop to get this to activate.

The title of the Program Manager (the desktop) might be different in other languages, you can use its class if the title doesn't work.

Edited by BrewManNH

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites
Posted (edited)

Ok that is my best effort

#include <MsgBoxConstants.au3>
#include <WinAPISysWin.au3>
#include <Process.au3>
#include <WinAPIProc.au3>
#include <Array.au3>


HotKeySet("{ESC}", "_Exit")
; HotKeySet("{F1}", "InitWindows")
Local $d1Handle = WinGetHandle ("Program Manager"),$pHandle
WinMinimizeAll ()
Local $d2Handle = WinGetHandle("[ACTIVE]")
InitWindows ()

While 1
    Sleep(1000)
    $pHandle = WinGetHandle("[ACTIVE]")
  If $d1Handle = $pHandle or $d2Handle = $pHandle Then
        If IsOnDesktop () then MsgBox($MB_SYSTEMMODAL, "h", "You are on desktop")
    EndIf
  ;ConsoleWrite (WinGetState ($d1Handle) & "/" & $d1Handle & "/" & $d2Handle & "/" & $pHandle & @CRLF)
WEnd

Func _Exit()
    Exit
EndFunc   ;==>_Exit

Func InitWindows ()
Global $aList = WinList ()

  _ArrayDelete ($aList,0)
  Redim $aList[ubound($aList)][3]
  For $i = 1 to UBound($aList)-1
    $aList[$i][2] = WinGetState ($aList[$i][1])
  Next

  _ArraySort ($aList, 0, 0, 0, 1)
  ;_ArrayDisplay ($aList)

EndFunc

Func IsOnDesktop ()
Local $aWin = WinList (), $iWin

  For $i = 1 to UBound($aWin)-1
    if $aWin[$i][0] = "" Then ContinueLoop
    $iWin = _ArrayBinarySearch ($aList,$aWin[$i][1],0,0,1)
    If @error Then
      If IsVisible ($aWin[$i][1]) then Return False
      ContinueLoop
    EndIf
    If $aWin[$i][1] = $d1Handle or $aWin[$i][1] = $d2Handle then ContinueLoop
    ; ConsoleWrite ($iWin & @CRLF)
    if $aList [$iWin][2] = WinGetState ($aWin[$i][1]) Then ContinueLoop
    if IsVisible ($aWin[$i][1]) then Return False
  Next
  Return True

EndFunc

Func IsVisible ($hWnd)
Local $iState = WinGetState ($hWnd)

  If not BitAnd($iState,$WIN_STATE_VISIBLE) then return False
  return not BitAnd($iState,$WIN_STATE_MINIMIZED)

EndFunc

Working great on Win7.  If there is any windows in front of desktop, it returns False.  You have to be perfectly clear above Desktop.

Edited by Nine

Share this post


Link to post
Share on other sites

@BrewManNH You code work well but I prefer the Nine's one 'cause it doesn't need a click on the desktop, but thanks for your support !

 

@Nine Thanks a lot for your help, it works really fine, I just added something to your code because it didn't work very well, just a mouseClick with left button after the winMinimizeAll. I really appreciate your help and I'll try to understand the code

 

I've got my answer, thanks !

Share this post


Link to post
Share on other sites
On 4/5/2019 at 6:28 PM, GordonNT said:

@BrewManNH You code work well but I prefer the Nine's one 'cause it doesn't need a click on the desktop

 

On 4/5/2019 at 6:28 PM, GordonNT said:

I just added something to your code because it didn't work very well, just a mouseClick with left button after the winMinimizeAll.

How is that different than my code?


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

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

×
×
  • Create New...