Sign in to follow this  
Followers 0
Will66

Capture Screen Shot of a Control

2 posts in this topic

#1 ·  Posted (edited)

I put this little demo together because I had the need to take a screen shot of an embedded IE that is built from an embedded Wysiwig editor. If that makes no sense see: wysiwig editor.

The idea is to take a screen shot of a control on a Gui. Takes screen shots of Buttons, pics and ie tested so far.

You'll need captdll.dll in your scripts directory or edit its path.

Workaround: To get the capture of embeded ie to work, i had to place a dummy picture control: $testPic = GUICtrlCreatePic("",$IE_Left, $IE_Top, $IE_Width, $IE_Height) because I could'nt get the co-ordinates as it wanted to grab the entire gui.

#include <GUIConstants.au3>
#include<Array.au3>
#include<File.au3>
#include <IE.au3>
Opt("GUIOnEventMode", 1)  ; Change to OnEvent mode
Dim $oIE,$GUIActiveX, $testPic,$html,$myedit

$gui=GUICreate("Capture Controls", 680, 580, (@DesktopWidth - 640) / 2, (@DesktopHeight - 580) / 2, $WS_OVERLAPPEDWINDOW + $WS_VISIBLE + $WS_CLIPSIBLINGS)
GUISetOnEvent($GUI_EVENT_CLOSE, "CLOSEClicked")

buildButtons()
GUISetState ()


While 1
  Sleep(10)  ; Idle around
WEnd


Func buildButtons()
   
        GUICtrlCreateButton("Button 1", 10,10,100,100)
        GUICtrlSetOnEvent(-1, "Button1_clicked")
    
        GUICtrlCreateButton("Button 2", 130,10,100,100)
        GUICtrlSetOnEvent(-1, "Button1_clicked")
        
        GUICtrlCreatePic(@Systemdir & "\oobe\images\merlin.gif",10,150, 0,0)
        GUICtrlSetOnEvent(-1, "Button1_clicked")
        
        GUICtrlCreateButton("Capture IE", 130,150,100,50)
        GUICtrlSetOnEvent(-1, "IEButton_clicked")
        
        $init_html='<center><div align="center" style="border:1px solid blue;width:200px;height:200px;color:green">'
        $init_html&='This is a div <p><img src="http://www.autoitscript.com/autoit3/files/graphics/autoit9_wall_thumb.jpg">'
        $init_html&='</p></div></center>'
        
        $myedit=GUICtrlCreateEdit ($init_html, 250,10,320,100,$ES_AUTOVSCROLL+$WS_VSCROLL)

        
        GUICtrlCreateButton("Write Html", 280,120,100,20)
        GUICtrlSetOnEvent(-1, "WriteButton_Clicked")
        
        _IEErrorHandlerRegister ()
        $oIE = _IECreateEmbedded ()
        Dim $IE_Left=10
        Dim $IE_Top=250
        Dim $IE_Width=400
        Dim $IE_Height=200      
        
        $testPic = GUICtrlCreatePic("",$IE_Left, $IE_Top, $IE_Width, $IE_Height)
        $GUIActiveX=GUICtrlCreateObj($oIE, $IE_Left, $IE_Top, $IE_Width, $IE_Height)
        _IENavigate ($oIE, "about:blank")
        
        Write_Html("Click on a Control to take its snapshot! <p><font color=red>image is saved in this scripts directory</font><p>Edit my Text or html in the edit box, click wtite html button, then click the Capture IE button")
    EndFunc
    
Func WriteButton_Clicked()
    $answer = GUICtrlRead($myedit)
Write_Html($answer) 
EndFunc
    
func Write_Html($html)
    $temphtml = ""  
    $temphtml  = "<HTML>"  & @CR
    $temphtml &= "<HEAD>"  & @CR
    $temphtml &= "<TITLE></TITLE>" & @CR
    $temphtml &= "</HEAD>"  & @CR
    $temphtml &= "<BODY style=""border:0px;margin:0px;"" scroll=""no"">" & @CR
    $temphtml &= $html & @CR
    $temphtml &= "</BODY>"  & @CR
    $temphtml &= "</HTML>"  & @CR

;   msgbox(0,"",$temphtml)
    _IEDocWriteHTML ($oIE, $temphtml)
    _IEAction ($oIE, "refresh")
EndFunc
    
Func IEButton_clicked()
_IELoadWait ($oIE)
$handle = ControlGetHandle($gui, "", $testPic)
capture_control($handle)
EndFunc

Func Button1_clicked()  
    $handle = @GUI_CTRLHANDLE
    capture_control($handle)
EndFunc

Func capture_control($handle)
        $pos = ControlGetPos($gui, "", $handle)
        ;MsgBox(0, "Window Stats:", "POSx: " & $pos[0] & ",POSy: " & $pos[1] & " SIZE: Width: " & $pos[2] & ",Height: " & $pos[3] )
        
            If @error Then 
            MsgBox(0,"ERROR","No Handle found")
            Exit
            EndIf

            $lpRect = DllStructCreate("int;int;int;int")
            $ret = DllCall("user32.dll","int","GetWindowRect","hwnd",$handle,"ptr",DllStructGetPtr($lpRect))
                        ;0x77d4b57c                    0x0000b57c 373               (0x175) 
            If @error Then
            MsgBox(0,"","DllCall error")
            Exit
            EndIf

            Dim $coords[4]

            $coords[0] = DllStructGetData($lpRect,1)
            $coords[1] = DllStructGetData($lpRect,2)
            $coords[2] = DllStructGetData($lpRect,3)
            $coords[3] = DllStructGetData($lpRect,4)

            ;_ArrayDisplay($coords,"Coordinates")
            ; Fist parameter - filename, next four: left, top, width, height. Last one - jpeg quality.
            DllCall(@ScriptDir & "\captdll.dll", "int", "CaptureRegion", "str", "dump_partial.jpg", "int", $coords[0], "int", $coords[1], "int", $pos[2], "int", $pos[3], "int", 95)
            MsgBox(0,"","dump_partial.jpg saved") 
    
EndFunc



Func CLOSEClicked()
      Exit   
EndFunc

Feed back is appreciated and any ideas on fixing the Workaround problem

Edited by Will66

Share this post


Link to post
Share on other sites



Yeah, that's usefull. :whistle:

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  
Followers 0