Jump to content
Sign in to follow this  

Floating window selection

Recommended Posts


hello to all,

search for a long time to extract code from wonderfull CaptureIt without result.

Its 'Selection capture' is exactly what i need to help my user to archive, after cut, tons of images.

want this resizable window to build small tool to snapshot portion of images that we have....

can anyone can give me an example of this type of selection window ?

thank you for any help,


Share this post

Link to post
Share on other sites

Try this:

#include <GDIPlus.au3>
#include <ScreenCapture.au3>

Opt("MustDeclareVars", 1)

Global $hBitmap, $hImage
Global $sString="  Created with AutoIt  "

; Initialize GDI+ library

; Capture screen
$hBitmap  = _ScreenCapture_Capture(@MyDocumentsDir & '\AutoItImage.bmp')

; Show image
Run("MSPaint.exe " & '"' & @MyDocumentsDir & '\AutoItImage.bmp"')

Tell me if it was of any help :)

You can also specify the area of the screen you want to capture.

And if you just want to take a small piece of that script, just search for "selection" in the source file :)

Edited by Kiti

Share this post

Link to post
Share on other sites

From http://www.autoitscript.com/forum/index.ph...st&p=529021

I extracted the following

#include <WinAPI.au3>
#include <GDIPlus.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <String.au3>
#include <misc.au3>
#include <ScreenCapture.au3>

;==========  Instructions ====
;Press Spacebar then press Ecs key to exit
; Press Ctrl key    - Makes Bitmap of the window, sub-window or desktop the mouse is over. If
;                           the mouse is over the transparent resizable rectangle, the area under
;                           the rectangle is used to make the bitmap.
; ========> End of Instructions ===
Global $GUI, $CopyWin, $PixelArray, $CopyWin, $e, $d, $PixelsReady = 0, $Start = 0
Global Const $WM_LBUTTONDOWN = 0x0201 ; Drag Window 1 of 3 addin

Opt("GUIResizeMode", 1)
Opt("WinTitleMatchMode", 2)
$GUI = GUICreate("Template", 18, 18, -1, -1, BitOR($WS_POPUP, $WS_SIZEBOX, $WS_CLIPCHILDREN), $WS_EX_TOPMOST)
WinSetTrans("Template", "", 100)

GUIRegisterMsg($WM_LBUTTONDOWN, "_WinMove") ; Drag Window 2 of 3 addin

While Not _IsPressed("1B") ;Esc to Exit
    If ((_IsPressed("12") And _IsPressed("01")) Or ($Start = 0)) Then ;Alt + Left Mouse Button - Start rectangle
        $Start = 1
        GUISetState(@SW_SHOW, $GUI)
        While (Not _IsPressed("20")) ; Spacebar
            $CopyWin = WinGetPos("Template")
            If _IsPressed("11") Then Save2BmpShift() ; Ctrl key
    If _IsPressed("11") Then Save2BmpShift() ; Shift key Makes bitmap

Func Save2BmpShift()
    Local $aMPos, $GuisInfo
    $aMPos = MouseGetPos()
    $GuisInfo = _WinInfoFromPoint($aMPos[0], $aMPos[1])
    If $GuisInfo[0] = "Template" Then
        $apos = WinGetPos("Template")
        _ScreenCapture_Capture("temp.bmp", $apos[0], $apos[1], $apos[0] + $apos[2], $apos[1] + $apos[3], False)
        If $GuisInfo[1] = $GuisInfo[3] Or $GuisInfo[3] = "Shell_TrayWnd" Then
            $hWnd = $GuisInfo[2]
            $apos = WinGetPos("[ACTIVE]")
            $tPoint = DllStructCreate("int X;int Y")
            DllStructSetData($tPoint, "X", $apos[0])
            DllStructSetData($tPoint, "Y", $apos[1])
            DllCall("User32.dll", "int", "ScreenToClient", "hwnd", $hWnd, "ptr", DllStructGetPtr($tPoint))
            ;_WinAPI_ScreenToClient($hWnd, $tPoint)
            $x = DllStructGetData($tPoint, "X")
            $y = DllStructGetData($tPoint, "Y")
            $Width = $apos[2]
            $Height = $apos[3]
            $hWnd = $GuisInfo[2]
            $pos = ControlGetPos($GuisInfo[4], "", _WinAPI_GetWindowLong($GuisInfo[2], 0xFFFFFFF4))
            $x = 0
            $y = 0
            $Width = $pos[2]
            $Height = $pos[3]
        $hWnd = $GuisInfo[2]
        $hDC = _WinAPI_GetDC($hWnd)
        $memDC = _WinAPI_CreateCompatibleDC($hDC)
        $memBmp = _WinAPI_CreateCompatibleBitmap($hDC, $Width, $Height)
        _WinAPI_SelectObject($memDC, $memBmp)
;~ Func _WinAPI_BitBlt($hDestDC, $iXDest, $iYDest, $iWidth, $iHeight, $hSrcDC, $iXSrc, $iYSrc, $iROP)
        _WinAPI_BitBlt($memDC, 0, 0, $Width, $Height, $hDC, $x, $y, $SRCCOPY)
        $hImage = _GDIPlus_BitmapCreateFromHBITMAP($memBmp)
        _GDIPlus_ImageSaveToFile($hImage, 'temp.bmp')
        _WinAPI_ReleaseDC($hWnd, $hDC)
EndFunc   ;==>Save2BmpShift

;Return :   $GuisInfo[0] = Window Text
;         $GuisInfo[1] = Class Name
;         $GuisInfo[2] = Handle of windowWnd
;         $GuisInfo[3] = Owner Class Name
;         $GuisInfo[4] = Handle of Owner Window
; A modified Siao Function
Func _WinInfoFromPoint($nX, $nY)
    Local $tStrBuff, $pStrBuff, $aRet, $hWnd, $Info[5]
    $tStrBuff = DllStructCreate("char[150]")
    $pStrBuff = DllStructGetPtr($tStrBuff)
    $aRet = DllCall("user32.dll", "hwnd", "WindowFromPoint", "uint", $nX, "uint", $nY)
    $Info[2] = $aRet[0] ;$hWnd
    $aRet = DllCall("user32.dll", "int", "GetClassName", "hwnd", $Info[2], "ptr", $pStrBuff, "int", 100)
    $Info[1] = DllStructGetData($tStrBuff, 1) ; ClassName
    DllStructSetData($tStrBuff, 1, "")
    DllCall("user32.dll", "int", "GetWindowText", "hwnd", $Info[2], "ptr", $pStrBuff, "int", 100)
    $Info[0] = DllStructGetData($tStrBuff, 1) ; Window Text
    DllStructSetData($tStrBuff, 1, "")
    $aRet = DllCall("user32.dll", "hwnd", "GetAncestor", "hwnd", $Info[2], "uint", 2) ;$GA_ROOT = 2
    $Info[4] = $aRet[0] ; Handle of Owner Window
    $aRet = DllCall("user32.dll", "int", "GetClassName", "hwnd", $Info[4], "ptr", $pStrBuff, "int", 100)
    $Info[3] = DllStructGetData($tStrBuff, 1) ;Owner ClassName
    DllStructSetData($tStrBuff, 1, "")
    ;ConsoleWrite("text= " &$Info[0] & @CRLF)
    Return $Info
EndFunc   ;==>_WinInfoFromPoint

; Drag Window 3 of 3 addin
Func _WinMove($hWnd, $Command, $wParam, $lParam)
    If BitAND(WinGetState($hWnd), 32) Then Return $GUI_RUNDEFMSG
    ;DllCall("user32.dll", "long", "SendMessage", "hwnd", $HWnd, "int", $WM_SYSCOMMAND, "int", 0xF009, "int", 0)
    DllCall("user32.dll", "int", "SendMessage", "hWnd", $hWnd, "int", $WM_NCLBUTTONDOWN, "int", $HTCAPTION, "int", 0)
    Return 1
EndFunc   ;==>_WinMove

Read instructions near top of script

You may be able to adapt the above script to suit your needs.

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