Jump to content
Sign in to follow this  

Copy in clipboard from last active window

Recommended Posts



I've tried several codes (using WinActive) but with no success. Below is a code which can be run if the path to Chrome (or another navigator) is changed as on your local computer (how can I have the path to work on any computer???).

Goal: a program to help search on the internet; from any program where are texts - just select the text, call the program, show a menu, choose something than automaticaly redirect on the web for the search.

Problem: copy in clipboard is not working perfect everytime and I don't know what to do... sometimes I mark text in MS Word (let's say, can be any application), call the program, but in clipboard is not copied the last marked text.

I guess GUISetState(@SW_HIDE,$hGui) is not enough. I guess before Send("^c") I need to be sure that the last active window (before was displayed the AutoIt program window) is active. But how???

(The code below can be used as a generic launcher with kind of floating menu. No? To me it look like that.)

Can anybody help? Thank you!

#Region;**** Directives created by AutoIt3Wrapper_GUI ****
#EndRegion;**** Directives created by AutoIt3Wrapper_GUI ****
#include <GUIConstantsEx.au3>
;#include <ButtonConstants.au3>
#include <WindowsConstants.au3>

Opt('MustDeclareVars', 1)


Func Example()
    Local $hGui, $msg
    Local $OptionsExit, $HelpBtn, $HelpDummy, $HelpContext, $Dictionar, $Google, $Despre
    Local $clip, $pos
    $pos = MouseGetPos()
    $hGui = GUICreate("Scurtaturi", 0, 0,-1,-1,$WS_POPUP,BitOR($WS_EX_TOPMOST,$WS_EX_TOOLWINDOW))
    WinMove("Scurtaturi", "", $pos[0]-3, $pos[1]-3)

; Create a dummy control and a contextmenu
    $HelpDummy = GUICtrlCreateDummy()
    $HelpContext = GUICtrlCreateContextMenu($HelpDummy)
    $Dictionar = GUICtrlCreateMenuItem("Dictionar", $HelpContext)
    $Google = GUICtrlCreateMenuItem("Google", $HelpContext)
    GUICtrlCreateMenuItem("", $HelpContext)
    $Despre = GUICtrlCreateMenuItem("Despre...", $HelpContext)

    ShowMenu($hGui, $msg, $HelpContext)

    While 1
        $msg = GUIGetMsg()
        Switch $msg
            Case $OptionsExit, $GUI_EVENT_CLOSE
            Case $HelpBtn
                ShowMenu($hGui, $msg, $HelpContext)
            Case $Despre
                MsgBox(64, "About...", "Searching tool.")
            Case $Dictionar
                $clip = ClipGet()
                Run('C:\Users\Radu\AppData\Local\Google\Chrome\Application\chrome.exe "http://hallo.ro/search.do?d=en&l=ro&type=both&query=' & $clip & '"')
            Case $Google
                $clip = ClipGet()
                Run('C:\Users\Radu\AppData\Local\Google\Chrome\Application\chrome.exe "http://www.google.ro/search?q=' & $clip & '"')
EndFunc ;==>Example2

; Show a menu in a given GUI window which belongs to a given GUI ctrl
Func ShowMenu($hWnd, $CtrlID, $nContextID)
    Local $arPos, $x, $y
    Local $hMenu = GUICtrlGetHandle($nContextID)
    $arPos = ControlGetPos($hWnd, "", $CtrlID)
    $x = $arPos[0]
    $y = $arPos[1] + $arPos[3]
    ClientToScreen($hWnd, $x, $y)
    TrackPopupMenu($hWnd, $hMenu, $x, $y)
EndFunc ;==>ShowMenu

; Convert the client (GUI) coordinates to screen (desktop) coordinates
Func ClientToScreen($hWnd, ByRef $x, ByRef $y)
    Local $stPoint = DllStructCreate("int;int")
    DllStructSetData($stPoint, 1, $x)
    DllStructSetData($stPoint, 2, $y)

    DllCall("user32.dll", "int", "ClientToScreen", "hwnd", $hWnd, "ptr", DllStructGetPtr($stPoint))
    $x = DllStructGetData($stPoint, 1)
    $y = DllStructGetData($stPoint, 2)
; release Struct not really needed as it is a local
    $stPoint = 0
EndFunc ;==>ClientToScreen

; Show at the given coordinates (x, y) the popup menu (hMenu) which belongs to a given GUI window (hWnd)
Func TrackPopupMenu($hWnd, $hMenu, $x, $y)
    DllCall("user32.dll", "int", "TrackPopupMenuEx", "hwnd", $hMenu, "int", 0, "int", $x, "int", $y, "hwnd", $hWnd, "ptr", 0)
EndFunc ;==>TrackPopupMenu
Edited by Radu

I just love AutoIt. But this do not help me enough to know it. I need this forums also ;).

Share this post

Link to post
Share on other sites

Try this:

1. after closing your popup, get the handle of the topmost window

2. while the window is not active...

3. try to activate it

4. *then* send Ctrl+C.

Local $topgui = WinGetHandle("")
        While Not WinActive($topgui)

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