Jump to content

testing control for lost focus


monte
 Share

Recommended Posts

Hello, I'm in the middle of creating a taskbar replacement for windows and am lost on how to test for a window losing focus. I've tried adlibenable and an if (in main()) with no luck (see commented lines). Any thoughts?

CODE
#include <GUIConstants.au3>

#include <Date.au3>

adlibenable("clockTimer", 100)

;adlibenable("goMenuTimer", 100)

$g_szVersion = "xpSkin"

If WinExists($g_szVersion) Then Exit

AutoItWinSetTitle($g_szVersion)

HotKeySet("{ESC}", "quit")

;create gui

$xpSkin = guicreate ("xpSkin", @desktopwidth / 4 * 3, @desktopheight/38.4, -1, @desktopheight - 25, BitOr($WS_POPUP,$WS_DLGFRAME,$WS_EX_TOOLWINDOW))

GUISetBkColor(0x000000)

$goButton = GUICtrlCreateLabel ("Go", 3, 3, 25)

GUICtrlSetBkColor($goButton, 0x000000)

GUICtrlSetColor($goButton, 0x00ff00)

$clockPos = controlgetpos("xpSkin", "", $xpSkin)

$clock = guictrlcreatelabel ("", $clockPos[2] - 50, -1 + 5, BitOr($SS_SIMPLE,$SS_SUNKEN))

$datePos = controlgetpos("", "", $clock)

GUICtrlSetTip($clock, _DateTimeFormat( _NowCalc(),1)) ;, $datePos[0] - 5, $datePos[1] - 5)

GUICtrlSetColor ($clock, 0x00ff00)

;main()

ControlHide ( "ClassName=Progman", "FolderView", "SysListView321")

ControlHide ( "ClassName=Shell_TrayWnd", "", "TrayClockWClass1")

ControlHide ( "ClassName=Shell_TrayWnd", "", "Button1")

ControlHide ( "ClassName=Shell_TrayWnd", "", "Button2")

ControlHide ( "ClassName=Shell_TrayWnd", "", "ToolbarWindow321")

ControlHide ( "ClassName=Shell_TrayWnd", "", "ToolbarWindow322")

ControlHide ( "ClassName=Shell_TrayWnd", "", "")

;display dialog on top of all windows

GUISetState (@SW_SHOW, $xpSkin)

winsetontop ("xpSkin", "", 1)

While 1

$msg = GUIGetMsg()

Select

Case $msg = $goButton

GoMenu()

EndSelect

;if NOT winactive("GoMenu") then

;GUIDelete($goMenu)

;GUISetState(@SW_ENABLE, $xpSkin)

;endif

sleep (1)

WEnd

func clockTimer()

GUICtrlSetData($clock, @HOUR & ":" & @MIN & ":" & @SEC)

endfunc

Func GoMenu()

GUISetState(@SW_DISABLE, $xpSkin)

$goMenu = guicreate ("GoMenu", 200, 500, (@desktopwidth/4)/2 - 2, @desktopheight/3.2, BitOr($WS_POPUP,$WS_DLGFRAME,$WS_EX_TOOLWINDOW))

GUISetBkColor(0x000000)

GUISetState (@SW_SHOW, $goMenu)

guiswitch($goMenu)

sleep(3000)

GUIDelete($goMenu)

GUISetState(@SW_ENABLE, $xpSkin)

endFunc

func goMenuTimer()

if NOT winactive("GoMenu") then

;GUIDelete($goMenu)

;GUISetState(@SW_ENABLE, $xpSkin)

endif

endfunc

Func quit()

Controlshow ( "ClassName=Shell_TrayWnd", "", "")

Controlshow ( "ClassName=Shell_TrayWnd", "", "TrayClockWClass1")

Controlshow ( "ClassName=Shell_TrayWnd", "", "Button1")

Controlshow ( "ClassName=Shell_TrayWnd", "", "Button2")

Controlshow ( "ClassName=Shell_TrayWnd", "", "ToolbarWindow321")

Controlshow ( "ClassName=Shell_TrayWnd", "", "ToolbarWindow322")

Controlshow ( "ClassName=Progman", "", "SysListView321")

exit

EndFunc

Link to comment
Share on other sites

  • Moderators

Your post is kind of confusing, the title says "control" yet the post says "window", so I went off the code and took a guess this is what you are looking for, I marked what I did or added with ;######################

CODE
#include <GUIConstants.au3>
#include <Date.au3>
Global Const $WM_ACTIVATEAPP = 0x01C;######################
AdlibEnable("clockTimer", 100)
;adlibenable("goMenuTimer", 100)
$g_szVersion = "xpSkin"
If WinExists($g_szVersion) Then Exit
AutoItWinSetTitle($g_szVersion)
HotKeySet("{ESC}", "quit")
;create gui
$xpSkin = GUICreate("xpSkin", @DesktopWidth / 4 * 3, @DesktopHeight / 38.4, -1, @DesktopHeight - 25, BitOR($WS_POPUP, $WS_DLGFRAME, $WS_EX_TOOLWINDOW))
GUISetBkColor(0x000000)
$goButton = GUICtrlCreateLabel("Go", 3, 3, 25)
GUICtrlSetBkColor($goButton, 0x000000)
GUICtrlSetColor($goButton, 0x00ff00)
$clockPos = ControlGetPos("xpSkin", "", $xpSkin)
$clock = GUICtrlCreateLabel("", $clockPos[2] - 50, -1 + 5, BitOR($SS_SIMPLE, $SS_SUNKEN))
$datePos = ControlGetPos("", "", $clock)
GUICtrlSetTip($clock, _DateTimeFormat(_NowCalc(), 1)) ;, $datePos[0] - 5, $datePos[1] - 5)
GUICtrlSetColor($clock, 0x00ff00)
;main()
ControlHide("ClassName=Progman", "FolderView", "SysListView321")
ControlHide("ClassName=Shell_TrayWnd", "", "TrayClockWClass1")
ControlHide("ClassName=Shell_TrayWnd", "", "Button1")
ControlHide("ClassName=Shell_TrayWnd", "", "Button2")
ControlHide("ClassName=Shell_TrayWnd", "", "ToolbarWindow321")
ControlHide("ClassName=Shell_TrayWnd", "", "ToolbarWindow322")
ControlHide("ClassName=Shell_TrayWnd", "", "")
;display dialog on top of all windows
GUIRegisterMsg($WM_ACTIVATEAPP, "_MY_WM_ACTIVATEAPP");######################
GUISetState(@SW_SHOW, $xpSkin)
WinSetOnTop("xpSkin", "", 1)
While 1
    $msg = GUIGetMsg()
    Select
        Case $msg = $goButton
            GoMenu()
    EndSelect
    ;if NOT winactive("GoMenu") then
    ;GUIDelete($goMenu)
    ;GUISetState(@SW_ENABLE, $xpSkin)
    ;endif
WEnd
Func clockTimer()
    GUICtrlSetData($clock, @HOUR & ":" & @MIN & ":" & @SEC)
EndFunc   ;==>clockTimer
Func GoMenu()
    GUISetState(@SW_DISABLE, $xpSkin)
    $goMenu = GUICreate("GoMenu", 200, 500, (@DesktopWidth / 4) / 2 - 2, @DesktopHeight / 3.2, BitOR($WS_POPUP, $WS_DLGFRAME, $WS_EX_TOOLWINDOW))
    GUISetBkColor(0x000000)
    GUISetState(@SW_SHOW, $goMenu)
    GUISwitch($goMenu)
    Sleep(3000)
    GUIDelete($goMenu)
    GUISetState(@SW_ENABLE, $xpSkin)
EndFunc   ;==>GoMenu
Func goMenuTimer()
    If Not WinActive("GoMenu") Then
        ;GUIDelete($goMenu)
        ;GUISetState(@SW_ENABLE, $xpSkin)
    EndIf
EndFunc   ;==>goMenuTimer
Func _MY_WM_ACTIVATEAPP($hwnd, $msg, $wParam, $lParam);######################
    Local $nNotifyCode = BitShift($wParam, 16)
    Local $nID = BitAND($wParam, 0xFFFF)
    Local $hCtrl = $lParam
    If $nID = 0 Then
        ;Window has lost focus
        MsgBox(64, 'Info', 'Your window has lost focus.')
        quit()
    EndIf
    Return 1
EndFunc   ;==>_MY_WM_ACTIVATEAPP
Func quit()
    ControlShow("ClassName=Shell_TrayWnd", "", "")
    ControlShow("ClassName=Shell_TrayWnd", "", "TrayClockWClass1")
    ControlShow("ClassName=Shell_TrayWnd", "", "Button1")
    ControlShow("ClassName=Shell_TrayWnd", "", "Button2")
    ControlShow("ClassName=Shell_TrayWnd", "", "ToolbarWindow321")
    ControlShow("ClassName=Shell_TrayWnd", "", "ToolbarWindow322")
    ControlShow("ClassName=Progman", "", "SysListView321")
    Exit
EndFunc   ;==>quit

Edit:

Added codebox tags

Edit2:

P.S. Don't forget Opt('WinTitleMatchMode', 4) since you are using classname= :whistle:

Edited by SmOke_N

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

Thanks smoke_n, and sorry about the control/window specification, I suppose I'm not familiar yet with autoit symantecs. This code works great, though I cannot make $goMenu (the window that appears when you press go)disappear if it has lost focus. My changes are marked with ;######################

Basically, I changed func goMenu() to include a paramater testing for creation or deletion of the window.

#include <GUIConstants.au3>
#include <Date.au3>
Global Const $WM_ACTIVATEAPP = 0x01C
AdlibEnable("clockTimer", 100)
;adlibenable("goMenuTimer", 100)

$g_szVersion = "xpSkin"
If WinExists($g_szVersion) Then Exit
AutoItWinSetTitle($g_szVersion)

HotKeySet("{ESC}", "quit")

;create gui
$xpSkin = GUICreate("xpSkin", @DesktopWidth / 4 * 3, @DesktopHeight / 38.4, -1, @DesktopHeight - 25, BitOR($WS_POPUP, $WS_DLGFRAME, $WS_EX_TOOLWINDOW))
GUISetBkColor(0x000000)

$goButton = GUICtrlCreateLabel("Go", 3, 3, 25)
GUICtrlSetBkColor($goButton, 0x000000)
GUICtrlSetColor($goButton, 0x00ff00)

$clockPos = ControlGetPos("xpSkin", "", $xpSkin)
$clock = GUICtrlCreateLabel("", $clockPos[2] - 50, -1 + 5, BitOR($SS_SIMPLE, $SS_SUNKEN))

$datePos = ControlGetPos("", "", $clock)
GUICtrlSetTip($clock, _DateTimeFormat(_NowCalc(), 1));, $datePos[0] - 5, $datePos[1] - 5)
GUICtrlSetColor($clock, 0x00ff00)

;main()
ControlHide("ClassName=Progman", "FolderView", "SysListView321")
ControlHide("ClassName=Shell_TrayWnd", "", "TrayClockWClass1")
ControlHide("ClassName=Shell_TrayWnd", "", "Button1")
ControlHide("ClassName=Shell_TrayWnd", "", "Button2")
ControlHide("ClassName=Shell_TrayWnd", "", "ToolbarWindow321")
ControlHide("ClassName=Shell_TrayWnd", "", "ToolbarWindow322")
ControlHide("ClassName=Shell_TrayWnd", "", "")

;display dialog on top of all windows
GUIRegisterMsg($WM_ACTIVATEAPP, "_MY_WM_ACTIVATEAPP")
GUISetState(@SW_SHOW, $xpSkin)
WinSetOnTop("xpSkin", "", 1)

While 1
  $msg = GUIGetMsg() 
  Select
    Case $msg = $goButton
    GoMenu(0);######################
  EndSelect
WEnd

Func clockTimer()
  GUICtrlSetData($clock, @HOUR & ":" & @MIN & ":" & @SEC)
EndFunc;==>clockTimer

Func GoMenu($focus);######################
  $goMenu = guicreate ("GoMenu", 200, 500, (@desktopwidth/4)/2 - 2, @desktopheight/3.2, BitOr($WS_POPUP,$WS_DLGFRAME,$WS_EX_TOOLWINDOW)) 
   GUICtrlSetBkColor($goMenu, 0x000000)
   GUICtrlSetColor($goMenu, 0x00ff00)

  if NOT $focus = 1 then
    GUISetState(@SW_DISABLE, $xpSkin)
    GUISetState (@SW_ENABLE, $goMenu)
    GUISetState (@SW_SHOW, $goMenu)
    guiswitch($goMenu)
  endif

  if $focus = 1 then
    GUISetState(@SW_DISABLE, $goMenu)
    GUISetState(@SW_HIDE, $goMenu)
    GUISetState (@SW_ENABLE, $xpSkin)
    guiswitch($xpSkin)
  endif
EndFunc;==>GoMenu

Func _MY_WM_ACTIVATEAPP($hwnd, $msg, $wParam, $lParam)
  Local $nNotifyCode = BitShift($wParam, 16)
  Local $nID = BitAND($wParam, 0xFFFF)
  Local $hCtrl = $lParam
  If $nID = 0 Then
   ;Window has lost focus
     ;MsgBox(64, 'Info', 'Your window has lost focus.');######################
     ;quit();######################
      GoMenu(1);######################
  EndIf
  Return 1
EndFunc;==>_MY_WM_ACTIVATEAPP

Func quit()
  ControlShow("ClassName=Shell_TrayWnd", "", "")
  ControlShow("ClassName=Shell_TrayWnd", "", "TrayClockWClass1")
  ControlShow("ClassName=Shell_TrayWnd", "", "Button1")
  ControlShow("ClassName=Shell_TrayWnd", "", "Button2")
  ControlShow("ClassName=Shell_TrayWnd", "", "ToolbarWindow321")
  ControlShow("ClassName=Shell_TrayWnd", "", "ToolbarWindow322")
  ControlShow("ClassName=Progman", "", "SysListView321")
  Exit
EndFunc;==>quit
Link to comment
Share on other sites

I've found a solution...instead of using a label and window, I'm going to use GUICtrlCreateContextMenu. Thanks for all your help smoke_n. By the way, how in the world was I suppose to find / learn about your suggestion? Thanks! :

$WM_ACTIVATEAPP = 0x01C
GUIRegisterMsg($WM_ACTIVATEAPP, "_MY_WM_ACTIVATEAPP")
Func _MY_WM_ACTIVATEAPP($hwnd, $msg, $wParam, $lParam)
  Local $nNotifyCode = BitShift($wParam, 16)
  Local $nID = BitAND($wParam, 0xFFFF)
  Local $hCtrl = $lParam
  If $nID = 0 Then
  ;Window has lost focus
    ;MsgBox(64, 'Info', 'Your window has lost focus.');######################
    ;quit();######################
      GoMenu(1);######################
  EndIf
  Return 1
EndFunc;==>_MY_WM_ACTIVATEAPP
Link to comment
Share on other sites

  • Moderators

Thanks for all your help smoke_n. By the way, how in the world was I suppose to find / learn about your suggestion? Thanks! :

:) My suggestion? :whistle:

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

  • Moderators

your suggested solution / code changes.

No worries... that's me watching a lot of others using GUIRegisterMsg() and using APIViewer2004.exe to play with the all the fun things in it... you should see some of these guys ... gafrost/eltorro and others that play with the function often and what they have shown the ability to be had from them.

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

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