Jump to content

Need help with focus problem


jftuga
 Share

Recommended Posts

Hi,

I am writing a small app that sits on the desktop, but I am trying to make as unobtrusive as possible. The program sits in the upper right corner of the screen with a very low transparency. If you hover over it, the program records the current window handle (the one that has focus) and then calls WinActivate() to gain focus & increases the transparency. If you hover out (ie move the mouse) of the program's "bounding box" the transparency returns to very low and the previous window regains focus. The problem is that my program uses dropdown combo boxes. When navigating through one of the choices with the mouse, the mouse leaves the "bounding box", thus losing focus which is not the desired effect of the program. I have commented the entire program, including where the problem occurs. I could really use some help with the OnAdLib() function. Any help would be greatly appreciated.

I am attaching the code as well as pasting it.

Thanks,

-John

#cs

RDP / VNC Manager
April 28, 2005
-John Taylor

Desktop applet to facilitate connecting to a varity of RDP & VNC hosts. Inspired by
SearchBar - http://www.autoitscript.com/forum/index.php?showtopic=9359&st=0&p=65838

#ce

Dim $Group[3]; number of RDP Groups
$Group[0]= "dc|clstrA|clstrB|backupsrv|sqlsrv"
$Group[1]= "custservice|custdatabase|nas"
$Group[2]= "test|web1|web2|printsrv|scansrv"

$Default_Trans = 40 ; Transparency - 20 is barley visible, 200 is very visible, default is 3 (hidden), debug is 40
$High_Trans = 200
$Trans_Toggle = 0
$PrevWin = ""; Window handle if previous window, will return focus to this window once mouse leaves "bounding box"
$TopWin_Y = 615 ; the y coordinate where the window will be placed - default is 615
$w = 72 ; the width of each combo box, make this the width of the longest server name, default is 72
$offset = 1 ; width between 2 combo boxes (distance between where one ends & the next one begins)

$TopWin_Width = (Ubound($Group) * $w) + (Ubound($Group) * $offset); for each $Group
$TopWin_Width = $TopWin_Width + ( $w + $offset ); for VNC
$TopWin_Width = $TopWin_Width + (20 + $offset); for close button

#include <GUICONSTANTS.au3>
#notrayicon
Opt ("GUIOnEventMode", 1)

$ProgramName = "RDP Manager"
If WinExists($ProgramName) Then
    WinActivate($ProgramName)
    WinSetTrans($ProgramName, "", $High_Trans)
    MsgBox(0, "Error", $ProgramName & " is already running.  Quit that instance first and then restart.")
    WinSetTrans($ProgramName, "", $Default_Trans)
    Exit
EndIf

$GUIStyle = $WS_POPUP
$GUIStyleEx = BitOR($WS_EX_TOOLWINDOW, $WS_EX_TOPMOST)

$TopWin = GUICreate($ProgramName, $TopWin_Width, 20, $TopWin_Y, -2, $GUIStyle, $GUIStyleEx)
GUISetOnEvent($GUI_EVENT_CLOSE,"OnExit")
$slots = WinGetPos($ProgramName)
$BoundingBox_A = $slots[0]
$BoundingBox_B = $slots[1]
$BoundingBox_X = $slots[2] + $BoundingBox_A
$BoundingBox_Y = $slots[3] + $BoundingBox_B


$x = 0
Dim $EventId[Ubound($Group)]
for $i = 0 to Ubound($Group) - 1
    $EventId[$i] = GUICtrlCreateCombo("", $x, 0, $w)
    GUICtrlSetOnEvent($EventId[$i], "Run_RDP")
    GUICtrlSetData(-1,$Group[$i])
    $x = $x + $w + $offset
Next

; Append VNC Combo Box, combining all groups
$all_groups = $Group[0]
for $j = 1 to Ubound($Group) - 1
    $all_groups = $all_groups & "|" & $Group[$j]
Next
$VncId = GUICtrlCreateCombo("", $x, 0, $w)
GUICtrlSetOnEvent($VncId, "Run_VNC")
GUICtrlSetData(-1,$all_groups)
$x = $x + $w + $offset

GUICtrlCreateButton("X", $x, 0, 20, 20, $BS_BITMAP)
GUICtrlSetOnEvent(-1, "OnExit")
GUICtrlSetFont(-1, 12, -1, "bold")
;GUICtrlSetImage ( -1, "C:\Windows\system32\x.bmp" )
WinSetTrans($TopWin, "", $Default_Trans)
GUISetState()

;MsgBox(0,"Bounding Box", $BoundingBox_A & " : " & $BoundingBox_B & " : " & $BoundingBox_X & " : " & $BoundingBox_Y)

AdlibEnable ( "OnAdLib", 500 )
while( 1 )
    sleep ( 1250 )
wend

Func Run_RDP()
    $host = GUICtrlRead( @GUI_CTRLID )
    $cmd = "C:\Windows\System32\mstsc.exe /v:" & $host & " /w:800 /h:600"
;$cmd = "C:\Windows\System32\mstsc.exe c:\WINDOWS\system32\rdp.rdp /v:" & $host
;MsgBox(0,"Debug", $cmd )
    Run( $cmd, "" )
EndFunc

Func Run_VNC()
    $host = GUICtrlRead( @GUI_CTRLID )
    $cmd = '"C:\Program Files\TightVNC-unstable\vncviewer.exe" ' & $host
;MsgBox(0,"Debug", $cmd )
    Run( $cmd, "" )
EndFunc

Func OnExit()
    exit
EndFunc
    
Func OnAdLib()
    $pos = MouseGetPos()
    if $pos[0] > $BoundingBox_A AND $pos[0] < $BoundingBox_X AND $pos[1] > $BoundingBox_B AND $pos[1] < $BoundingBox_Y Then
        WinSetTrans($TopWin, "", $High_Trans)
        $slots = WinList()
    ; Find the name and handle of the window that currenlty has focus, is there an easier way to do this?
    ; place this info into $PrevWin, and then get window focus with WinActivate()
        For $i = 1 to $slots[0][0]
            if BitAnd( WinGetState($slots[$i][1]),8 ) Then
                if $ProgramName = $slots[$i][0] Then
                    ExitLoop
                endif
                $PrevWin = $slots[$i][1]
            ;MsgBox(0,"PrevWin", $slots[$i][0] & " : " & $PrevWin)
                ExitLoop
            endif
        Next
        WinActivate($ProgramName)
        $Trans_Toggle=1
    Else
        if 1 == $Trans_Toggle Then
            WinSetTrans($TopWin, "", $Default_Trans)

        ; since we have left the "bounding box", return focus to the window that previously had it
        ; here is the problems
        ; 1) drop down boxes are outside of the "bounding box"
        ; 2) moving the mouse over and then out again works fine, but if you select a host, but 
        ;   do not click on it, it will still try to run rdp or vnc

            if "" <> $PrevWin Then
            ;MsgBox(0,"PrevWin", $PrevWin)
                WinActivate($PrevWin)
            EndIf
            $Trans_Toggle = 0
        endif
    endif
Endfunc

RDP_Manager.au3

Link to comment
Share on other sites

2) moving the mouse over and then out again works fine, but if you select a host, but do not click on it, it will still try to run rdp or vnc

I can help with that, I also answered this in another topic, here : http://www.autoitscript.com/forum/index.php?showtopic=10827

Look at my 2nd post.

With the window active stuff, try this code, I took out your OnAdlib() Function code and replace with a "WinActive" code by Xenogis, it also has a more smoother transparency transition.

#cs

RDP / VNC Manager
April 28, 2005
-John Taylor

Desktop applet to facilitate connecting to a varity of RDP & VNC hosts. Inspired by
SearchBar - http://www.autoitscript.com/forum/index.php?showtopic=9359&st=0&p=65838

#ce

Dim $Group[3]; number of RDP Groups
$Group[0]= "dc|clstrA|clstrB|backupsrv|sqlsrv"
$Group[1]= "custservice|custdatabase|nas"
$Group[2]= "test|web1|web2|printsrv|scansrv"

Dim $Active

$Default_Trans = 40; Transparency - 20 is barley visible, 200 is very visible, default is 3 (hidden), debug is 40
$High_Trans = 200
$Trans_Toggle = 0
$PrevWin = ""; Window handle if previous window, will return focus to this window once mouse leaves "bounding box"
$TopWin_Y = 615; the y coordinate where the window will be placed - default is 615
$w = 72; the width of each combo box, make this the width of the longest server name, default is 72
$offset = 1; width between 2 combo boxes (distance between where one ends & the next one begins)

$TopWin_Width = (Ubound($Group) * $w) + (Ubound($Group) * $offset); for each $Group
$TopWin_Width = $TopWin_Width + ( $w + $offset ); for VNC
$TopWin_Width = $TopWin_Width + (20 + $offset); for close button

#include <GUICONSTANTS.au3>
#notrayicon
Opt ("GUIOnEventMode", 1)

$ProgramName = "RDP Manager"
If WinExists($ProgramName) Then
    WinActivate($ProgramName)
    WinSetTrans($ProgramName, "", $High_Trans)
    MsgBox(0, "Error", $ProgramName & " is already running.  Quit that instance first and then restart.")
    WinSetTrans($ProgramName, "", $Default_Trans)
    Exit
EndIf

$GUIStyle = $WS_POPUP
$GUIStyleEx = BitOR($WS_EX_TOOLWINDOW, $WS_EX_TOPMOST)

$TopWin = GUICreate($ProgramName, $TopWin_Width, 20, $TopWin_Y, -2, $GUIStyle, $GUIStyleEx)
GUISetOnEvent($GUI_EVENT_CLOSE,"OnExit")
$slots = WinGetPos($ProgramName)
$BoundingBox_A = $slots[0]
$BoundingBox_B = $slots[1]
$BoundingBox_X = $slots[2] + $BoundingBox_A
$BoundingBox_Y = $slots[3] + $BoundingBox_B


$x = 0
Dim $EventId[Ubound($Group)]
for $i = 0 to Ubound($Group) - 1
    $EventId[$i] = GUICtrlCreateCombo("", $x, 0, $w)
    GUICtrlSetOnEvent($EventId[$i], "Run_RDP")
    GUICtrlSetData(-1,$Group[$i])
    $x = $x + $w + $offset
Next

; Append VNC Combo Box, combining all groups
$all_groups = $Group[0]
for $j = 1 to Ubound($Group) - 1
    $all_groups = $all_groups & "|" & $Group[$j]
Next
$VncId = GUICtrlCreateCombo("", $x, 0, $w)
GUICtrlSetOnEvent($VncId, "Run_VNC")
GUICtrlSetData(-1,$all_groups)
$x = $x + $w + $offset

GUICtrlCreateButton("X", $x, 0, 20, 20, $BS_BITMAP)
GUICtrlSetOnEvent(-1, "OnExit")
GUICtrlSetFont(-1, 12, -1, "bold")
;GUICtrlSetImage ( -1, "C:\Windows\system32\x.bmp" )
WinSetTrans($TopWin, "", $Default_Trans)
GUISetState()

;MsgBox(0,"Bounding Box", $BoundingBox_A & " : " & $BoundingBox_B & " : " & $BoundingBox_X & " : " & $BoundingBox_Y)

AdlibEnable ( "OnAdLib", 500 )
while( 1 )
    sleep ( 1250 )
wend

Func Run_RDP()
    $host = GUICtrlRead( @GUI_CTRLID )
    $cmd = "C:\Windows\System32\mstsc.exe /v:" & $host & " /w:800 /h:600"
;$cmd = "C:\Windows\System32\mstsc.exe c:\WINDOWS\system32\rdp.rdp /v:" & $host
;MsgBox(0,"Debug", $cmd )
    Run( $cmd, "" )
EndFunc

Func Run_VNC()
    $host = GUICtrlRead( @GUI_CTRLID )
    $cmd = '"C:\Program Files\TightVNC-unstable\vncviewer.exe" ' & $host
;MsgBox(0,"Debug", $cmd )
    Run( $cmd, "" )
EndFunc

Func OnExit()
    exit
EndFunc
    
Func OnAdLib()
   If $Active = 1 And Not WinActive($TopWin) Then
      For $N = 200 To 40 Step - 5
         WinSetTrans($TopWin, "", $N)
         Sleep(10)
      Next
      $Active = 0
   ElseIf $Active = 0 Then
      If WinActive($TopWin) Then
         For $N = 40 To 200 Step + 5
            WinSetTrans($TopWin, "", $N)
            Sleep(10)
         Next
         $Active = 1
      EndIf
   EndIf
EndFunc
Edited by Burrup

qq

Link to comment
Share on other sites

Thanks Burrup.

I combined our code to achieve my desired outcome. Since I really want to have the transparency set to 5 which is invisible, the program must have the ability to FadeIn() when the mouse hovers over the bounding box -- otherwise you would not be able to see where to click to gain focus.

Here are my changes.

Thanks again!

-John

Func FadeOut()
    for $i = $High_Trans to $Default_Trans step - 5
        WinSetTrans($TopWin, "", $i)
        sleep(10)
    next
endfunc
    
Func FadeIn()
    for $i = $Default_Trans to $High_Trans step + 5
        WinSetTrans($TopWin, "", $i)
        sleep(10)
    next
endfunc
    
Func OnAdLib()
; going out of focus
    If $Active = 1 And Not WinActive($TopWin) Then
        $pos = MouseGetPos()
        if $pos[0] > $BoundingBox_A AND $pos[0] < $BoundingBox_X AND _
                        $pos[1] > $BoundingBox_B AND $pos[1] < $BoundingBox_Y Then
            return
        endif

        FadeOut()
        $Active = 0

; coming into focus
    ElseIf $Active = 0 Then
        $pos = MouseGetPos()
        if $pos[0] > $BoundingBox_A AND $pos[0] < $BoundingBox_X AND _
                        $pos[1] > $BoundingBox_B AND $pos[1] < $BoundingBox_Y Then
            FadeIn()
            $Active = 1
            return
        endif

        If WinActive($TopWin) Then
            FadeIn()
            $Active = 1
        EndIf
    EndIf
EndFunc
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...