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

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



    • rcmaehl
      By rcmaehl
      A UDF with Extended Functions for Window Management
       
      Notes:
      Fixes WinGetClassList's barbaric returning of a @LF separated string instead of an array.
       
      Potential Uses:
      Automating applications that change their controls' handles/classes on each launch (e.g. half of Cisco's programs)
       
      Functions:
      _WinGetClassList
      _WinGetClassNNList
      _WindowGetHandleList
      _WindowGetHandleListFromPos
       
      Download: 
      WindowEx.zip  (v0.4)
       
      Changelog:
      10/04/2016 (v0.4): _WinGetClassNNList Fixed : Not Returning an Index when using $2D_ARRAY _WinGetClassNNList Fixed : Not Properly returning $aArray[x][1] on Classes with instances > 9 when using $2D_ARRAY 10/03/2016 (v0.3): _WinGetClassList Added : Exactly the same as WinGetClassList but returns a more civilized Array _WinGetClassNNList Added : Returns Classes and their instances in either a 1D or 2D array depending on Flags _WindowGetHandleList Renamed: _WinGetHandleList SCRIPT BREAKING! _WindowGetHandleListFromPos Renamed: _WinGetHandleListFromPos SCRIPT BREAKING! 10/01/2016 (v0.2): WindowsExConstants.au3 Added : Flags in _WindowGetHandleListFromPos _WindowGetHandleListFromPos Removed: ConsoleWrite left in during debug _WindowGetHandleListFromPos Added : Flag for if part of a Control is at $X, $Y return it as well. 10/01/2016 (v0.1): _WindowGetHandleList Added : Retrieves the handles of classes from a window. _WindowGetHandleListFromPos Added : Retrieves the handles of classes at a specific position from a window. Known and Reported Bugs:
      None reported To Do:
      To Be Decided. Opinions welcome! Upcoming Changes:
      To Be Decided.
    • Pike
      By Pike
      Hello Everyone,
      New Guy here. This is a two-part question, hopefully easy enough though. I know it's ugly coding, this is because I have only a couple hours worth of coding experience and could really use some help.
      1) When I single click the button I want one action (like playing a sound clip) and when I double click the button I want a different action (such as opening dialogue box to ask for soundclip directory address). At the bottom of the script I made a comment which one I would like single click versus double click.
      2) I can not get the FileOpenDialogue to save the file address to the config.ini file at the 'One' position on the .ini.
      #include <ButtonConstants.au3> #include <GUIConstantsEx.au3> #include <WindowsConstants.au3> #include <MsgBoxConstants.au3> #include <FileConstants.au3> #include <AutoItConstants.au3> #include <Sound.au3> Opt('MustDeclareVars', 1) Opt("GUIOnEventMode", 1) Global $OneID _Main() Func _Main() GUICreate("Form1", 156, 86, 281, 276) $OneID = GUICtrlCreateButton("Button", 15, 17, 122, 37) GUICtrlSetOnEvent($OneID, "OnOne") GUISetOnEvent($GUI_EVENT_CLOSE, "OnExit") GUISetState() While 1 While 1 Sleep(1000) WEnd WEnd EndFunc ;---------------Functions------------- While 1 Sleep(100) WEnd Func OnOne() ; ******************Single Click for this action**************** $OneID = IniRead("config.ini", "Config", "One", $OneID) If $OneID = True Then SoundPlay($OneID, @HotKeyPressed) Else ; *******Double Click for this action*****And Help Saving File Address to config.ini 'One'*********** $OneID = FileOpenDialog("Select file", @WorkingDir, "All (*.*)") IniWrite("config.ini", "Config", "One", $OneID) $OneID = InputBox("Enter Information", "Short Name for File?", "") IniWrite("config.ini", "Config", "NameOne", $OneID) EndIf EndFunc Any help would be greatly appreciated, and if you need anymore clarity as to what exactly I'm trying to do, please do not hesitate to ask. Thanks so much!
      Pike
    • FrancescoDiMuro
      By FrancescoDiMuro
      Good morning everyone

      I was playing a little bit with "Screen Capture" UDF, and I was trying to make a "Window" capture, but, since I made a GUI which through I fire the event "Capture", my GUI is captured as well, and I don't want to
      This is the line of code that makes the capture:
       
      _ScreenCapture_CaptureWnd($strScreenCaptureFileName, $objActiveWindow, 0, 0, -1, -1, False) And these are the lines of code which select the "active" window:
       
      Local $objCurrentWindow = 9999 If _IsPressed("01") Then $objCurrentWindow = WinGetHandle("[ACTIVE]") If $objCurrentWindow <> $objMyGUI Then $objActiveWindow = $objCurrentWindow EndIf EndIf Sorry If I made stupid mistakes
      Thanks in advance.

      Francesco
    • kcvinu
      By kcvinu
      Hi all,
      I have a button created with _GUICtrlButton_Create function. I know that this function returns a handle of button instead of an ID. So i have obtain control ID by calling _WinAPI_GetDlgCtrlID function. After that i have tried the GUICtrlSetBkColor function. But it didnt worked. Any suggestions will be appreciated. Thanks in advance. 
      Note : - The window which carries this button is not created with native gui create function. Instead, it is created with CreateWindowEx api function.
×