Jump to content
Sign in to follow this  
johnmcloud

[SOLVED] Button on active window

Recommended Posts

johnmcloud

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

Share this post


Link to post
Share on other sites
johnmcloud

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

Share this post


Link to post
Share on other sites
FireFox

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.


 

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
johnmcloud

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

Share this post


Link to post
Share on other sites
FireFox

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

 

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
IanN1990

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

Share this post


Link to post
Share on other sites
johnmcloud

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

Share this post


Link to post
Share on other sites
IanN1990

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.

Share this post


Link to post
Share on other sites
johnmcloud

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.

Sure, why not. I'll post the script when is all completed ;)

Share this post


Link to post
Share on other sites
kylomas

johnmcloud,

This is all very interesting, but, what is it good for?

kylomas


Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites
IanN1990

@John So, were you ever able to get it all finished and polished nicely ?

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  

  • Similar Content

    • nooneclose
      By nooneclose
      Hello again, I am in phase two of my help bot. I cannot figure out how to properly code Autoit to click on a webpage button using the I.E UDF. I want to click on the button that I have attached as an image. This button does not have a name but it has an ID. Here is the HTML code for this button that I found using chromes developer inspector.
      HTML Code: 
      <img role="button" tabindex="-1" id="toolactions_INSERT-tbb_image" src="nav_icon_insertkey.gif" alt="New Work Order CTRL+ALT+I" draggable="false"> Here is the code that I have so far:
      ;******************************************************************************* ; Opens IE and fill out a New Workorder ;******************************************************************************* ; Start IE at the ** Tracking Application in ****** $oIE = _IECreate("https:I_am_Bound_by_my_company_to_not_reveal_this_webpage") ; Wait for a browser page load to complete _IELoadWait($oIE) Sleep(6000) ; Store the field names where the important data will be sent Local $nWorkOrderB = _IEGetObjById($oIE, "toolactions_INSERT-tbb_image") _IEAction($nWorkOrderB, "click")  I want my program to click on the button that looks like a piece of paper with a blue # 
      (For those wondering, I am allowed to send images just not the URL.) 
      As always any help would be appreciated. 

    • MJ36
      By MJ36

      How to click this button ? 
      I Try 
       
      func test () $oCorrectObj = "" $tags = $oIE.document.GetElementsByTagName("button") For $tag in $tags $class_value = $tag.GetAttribute("type") If string($class_value) = "buttont" Then $oCorrectObj = $tag MsgBox(0, "Level: ", "Level found :)") Else MsgBox(16, "Failed", "Not found.") ExitLoop EndIf Next If IsObj ( $oCorrectObj ) Then _IEAction ($oCorrectObj, "click") EndIF  
      and
       
      func klik () Local $oFound = "" Local $oBtns = _IETagNameGetCollection($oIE, "span") For $oBtn In $oBtns If String($oBtn.classname) = "yt-uix-button-content" Then $oFound = $oBtn EndIf Next _IEAction ($oFound, "click") EndFunc  
      Please Help 
      Can you click it by name?
    • faustf
      By faustf
      i guys , like title anyone  have idea if is possible , and if yes if  have example to post ?  i find in forum and in google but  i dont know   how can start  to create  button with icon over toolbar near  clock or is  goog before language layout .
      sorry i not post any script but i dont know  where to start
      thankz
      at  all
    • caramen
      By caramen
      Hmmm i am sorry  i had simular problem but i cant find back post. 
       
      How to hide a gui button ? 
       
      I Want to dodge the variable non déclaréd when i make button in
      if statment 
       
      thx before 
    • bbProg
      By bbProg
      Hi,
       
      how can I wait activate for windows with information as in the photos attached?
      Class is not being recognized by the script.
       
      thanks in advanced.



×