Jump to content

[SOLVED] Button on active window


johnmcloud
 Share

Recommended Posts

Hi guys,

I'd like to add a simple button on active window. This is what i have done:

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

HotKeySet("{Esc}", "_Exit")

Global $Width = 80
Global $Height = 30

$GUI = GUICreate("Test", $Width, $Height, 0, 0, $WS_POPUP, BitOR($WS_EX_TOOLWINDOW, $WS_EX_TOPMOST))
$Button = GUICtrlCreateButton("Button", 0, 0, $Width, $Height)
GUISetState(@SW_SHOW)

While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE
Exit
Case $Button
MsgBox(0, 0, "Test")
EndSwitch
Move()
WEnd

Func _Exit()
Exit 0
EndFunc ;==>_Exit

Func Move()
Local $size = WinGetPos("[active]")
Local $x = $size[0] + $size[2] - ($Width * 2.5)
Local $y = $size[1]
WinSetOnTop("Test", "", 1)
WinMove($GUI, "", $x, $y)
EndFunc ;==>Move

I have a couple of problem:

1) If no windows is selected, i see a phantom moving button :D you can easily see, just adjust the speed:

WinMove($GUI, "", $x, $y, 80, 30, 100)

I want to see the button only if a active windows exist, not over the desktop

2) The button not work, when you click on it...disappears

Please help me out to improve the script/resolve this problems,

Thanks ;)

EDIT: I have reposted the script, the autoit code have some problem

Edited by johnmcloud
Link to comment
Share on other sites

UPDATE: New Script:

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

HotKeySet("{Esc}", "_Exit")

Global $Width = 60
Global $Height = 25

$GUI = GUICreate("Test", $Width, $Height, 0, 0, $WS_POPUP, BitOR($WS_EX_TOOLWINDOW, $WS_EX_TOPMOST))
$Button = GUICtrlCreateButton("Button", 0, 0, $Width, $Height)
GUISetState(@SW_SHOWNOACTIVATE)

While 1
$nMsg = GUIGetMsg()
Switch $nMsg
  Case $GUI_EVENT_CLOSE
   Exit
  Case $Button
   MsgBox(0, 0, "Test")
EndSwitch
Move()
WEnd

Func _Exit()
Exit 0
EndFunc   ;==>_Exit

Func Move()
WinSetOnTop("Test", "", 1)
Local $size = WinGetPos("[active]")
Local $name = WinGetTitle("[active]")
Local $x = $size[0] + $size[2] - ($Width * 2.5)
Local $y = $size[1]
If $name <> "Test" Then
  WinMove($GUI, "", $x, $y)
EndIf
EndFunc   ;==>Move

Resolved:

1) Now button work ;)

2) No more phantom button moving

To Resolve:

1) Don't show button on desktop

2) Give to the last active window the focus, it lose

Please if someone know how to improve/resolve post here,

Thanks ;)

Edited by johnmcloud
Link to comment
Share on other sites

Hi,

It's not perfect, but this is how I would do it (without thinking how I could do it better) :

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <WinAPI.au3>

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

Global $Width = 60, $Height = 25, $hLastActiveWnd = -1, $hDesktopWnd = WinGetHandle("[TITLE:Program Manager;CLASS:Progman]")

$GUI = GUICreate("Test", $Width, $Height, 0, 0, $WS_POPUP, BitOR($WS_EX_TOOLWINDOW, $WS_EX_TOPMOST))
$Button = GUICtrlCreateButton("Button", 0, 0, $Width, $Height)
GUISetState()

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button
            MsgBox(64, "", "Test")
    EndSwitch

    Move()
    Sleep(50)
WEnd

Func _Exit()
    Exit
EndFunc   ;==>_Exit

Func Move()
    Local $hActiveWnd = WinGetHandle("[active]")

    If $hActiveWnd <> $hLastActiveWnd And $hActiveWnd <> $hDesktopWnd Then
        Local $aSize = WinGetClientSize($hActiveWnd)
        If Not IsArray($aSize) Then Return ;Exit Func

        _WinAPI_SetParent($GUI, $hActiveWnd)

        WinMove($GUI, "", $aSize[0] - ($Width * 2.5), 10)

        If Not WinActive($hActiveWnd) Then WinActivate($hActiveWnd)

        $hLastActiveWnd = $hActiveWnd
    EndIf
EndFunc   ;==>Move

Br, FireFox.

Link to comment
Share on other sites

Thanks FireFox, but work wroste then mine, i can't click on the button, if i reduce the size of the window the button will disappear etc..

Thanks for trying, for now i'll tring to make the focus to the previus windows when you click on the button, how to do?

Maybe with WinList() but the help doesn' help me ;)

Edited by johnmcloud
Link to comment
Share on other sites

I suggest you to create the button on every visible window :

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <WinAPI.au3>

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

Opt("GUIOnEventMode", 1)

Global $iWidth = 60, $iHeight = 25, $hDesktopWnd = WinGetHandle("[TITLE:Program Manager;CLASS:Progman]")

Global $aWinList = WinList()

For $iWnd = 1 To $aWinList[0][0]
    ; Only display visble windows that have a title
    If $aWinList[$iWnd][0] <> "" And IsVisible($aWinList[$iWnd][1]) Then
        If $aWinList[$iWnd][1] <> $hDesktopWnd Then
            _AttachGUI($aWinList[$iWnd][1])
        EndIf
    EndIf
Next

While 1
    Sleep(1000)
WEnd

Func _Exit()
    Exit
EndFunc   ;==>_Exit

Func _AttachGUI($hWnd)
    Local $aSize = WinGetClientSize($hWnd)
    If Not IsArray($aSize) Then Return ;Exit Func

    $GUI = GUICreate("Test", $iWidth, $iHeight, $aSize[0] - $iWidth - 10, 10, $WS_CHILD, BitOR($WS_EX_TOOLWINDOW, $WS_EX_TOPMOST), $hWnd)
    GUISetOnEvent($GUI_EVENT_CLOSE, "_Exit")

    GUICtrlCreateButton("Button", 0, 0, $iWidth, $iHeight)
    GUICtrlSetOnEvent(-1, "_toto")

    GUISetState(@SW_SHOW, $GUI)
    _WinAPI_SetParent($GUI, $hWnd) ;GUICreate parent param does not work on all wnds
EndFunc   ;==>_AttachGUI

Func IsVisible($handle)
    If BitAND(WinGetState($handle), 2) Then
        Return 1
    Else
        Return 0
    EndIf
EndFunc   ;==>IsVisible

Func _toto()
    MsgBox(64, "", "toto")
EndFunc   ;==>_toto

Then you can check for every new wnd (add attached wnds handle into an array)

Edit : You should add the SciTE wnd to the exclude list, it does not work properly (or find another way).

Br, FireFox.

Edited by FireFox
Link to comment
Share on other sites

My little attempt at your problem

It no longer displays a active button on desktop, taskbar or menu. I only have windows 7 so i donno if it will work on other OS's.

One idea i did have is maybe. On the active-window, u get the style of the window "and if the style is one with a windows border, min, max, close. Then to add your button"

Here is the code for just the desktop problem + improving perfomnce load on cpu.

#Notrayicon ;Makes the script start faster, easier for me to test and i hate tray icons :)
#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <Array.au3>

local $HandleStore
local $ActiveSizeStore[2]
local $Temp ;This is used to stop it moving the GUI tones, when its not needed. Mostly wasted on good pcs, but i perfer programing with performance in  mind.
local $ArrayOfActiveWindows[1]

$GUI = GUICreate("Test", 60, 25, 0, 0, $WS_POPUP, BitOR($WS_EX_TOOLWINDOW, $WS_EX_TOPMOST))
$Button = GUICtrlCreateButton("Button", 0, 0, 60, 25)
GUISetState(@SW_SHOWNOACTIVATE)

While 1
$nMsg = GUIGetMsg()
Switch $nMsg
  Case $GUI_EVENT_CLOSE
   Exit
Case $Button
   _ArrayDisplay($ArrayOfActiveWindows)
  ; MsgBox(0, 0, "Test")
EndSwitch
Move()
WEnd

Func Move()
$ActiveHandle = WinGetHandle("[Active]")
$ActiveTitle = WinGetTitle($ActiveHandle)
If $ActiveTitle = "Test" or $ActiveTitle = "Start menu" or $ActiveTitle = "Program Manager" or StringRight(WinGetText($ActiveHandle), 21) > "Running applications" Then Return 0 ;Stops It being shown on desktop or taskbar

If Not($HandleStore = $ActiveHandle) Then
   For $i = 0 to ubound($ArrayOfActiveWindows)-1
 If $ArrayOfActiveWindows[$i] = $ActiveHandle then
_ArrayDelete($ArrayOfActiveWindows,$i)
ExitLoop
 EndIf
   Next
   _ArrayAdd($ArrayOfActiveWindows, $ActiveHandle)
WinSetOnTop("Test", "", 1) ;See top comment on why i have done this as wellif
ConsoleWrite("A")
EndIf

$HandleStore = $ActiveHandle 

$ActiveSize = WinGetPos($ActiveHandle)
If IsArray($ActiveSize) Then
   If Not($ActiveSize[0] = $ActiveSizeStore[0]) or Not($ActiveSize[1] = $ActiveSizeStore[1]) Then $Temp = 2
   If $Temp > 0 Then
 WinMove($GUI, "", $ActiveSize[0] + $ActiveSize[2]-150, $ActiveSize[1]) ;I know this looks ulgy, and i did try and better lookink away but it causes "lag" where this is no lag while still being kind 2 the cpu as its only run 3 times per any movement rather then unlimited.
 $Temp -= 1
   EndIf
$ActiveSizeStore = $ActiveSize
EndIf
EndFunc   ;==>Move

**Edit Fixed a small problem by when the button became active, it causes a endless loop of moving the button :>

**Edit Newer version with Array support for rememebring Focus windows (not finished yet, it can only remember not act) still working on it.

Edited by IanN1990
Link to comment
Share on other sites

Nice IanN1990, there is only one problem, the focus didn't come to the previus windows.

On my first script i have add the WinList() and WinActivate() for the focus problem and work fine.

But not work on your. The focus it's the major problem, if example i want to create a "Copy" button ( Send("^c") ) need to do:

1) Click on "copy" button

2) Give focus to the previus window ( ==>_WinPrevious )

3) Send("^c")

With my original script work, but don't know on your script where to add this.

Thanks

EDIT: Solved, i have removed the array part, now work very well. Thanks guys, without your help without you I would not have been able to resolve ;)

Edited by johnmcloud
Link to comment
Share on other sites

Yea, i was working on the array part but then got called away with family bussniness and didn't get a chance to finish it *which is also why it was only half finished, all it did was record which windows you had been to, and kept your most active windows at the top of the array list" :( I am sry about that but i am glad at lest one part was helpful to you :)

Is there any chance you could post your finished product, as i would like to have a look. I was have thinking of using it on my pc as well.

Link to comment
Share on other sites

  • 3 weeks later...

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

×
×
  • Create New...