Page 7 of 16

Re: Screen clipping

Posted: 08 Dec 2017, 20:04
by Joe Glines
@rommmcek Thank you for working on this! What I don't understand is why I don't have similar issues. I have four monitors and all are at different resolutions. Any thoughts on why I don't have the same issue as Tank? Perhaps his graphics cards/drivers are the issue?

Re: Screen clipping

Posted: 09 Dec 2017, 02:03
by rommmcek
I'm actually working on correct capturing under MS Magnifier. Currently I'm able to do this if the upper left corner of the screen has 0, 0 coordinate values at 100%, 200%, 300%, ... magnification.
What I do is in fact displacing of the capturing point, calculated by ScreenClipping. This should work for every one.

I certainly don't know what causes Tank's problem, I made just a supposition it could be related to resolution difference (since resolution is "artificially" increased by magnifier), but of course I don't know what is the ratio between his values.

So if somebody experiences capturing down and left (Tank's report) of the GUI, he should increase x and decrease y values at the reported spot of the Gdip_BitmapFromScreen(Screen=0, Raster="") function and apply this only on the problematic monitor.

bye!

Re: Screen clipping

Posted: 13 Dec 2017, 12:07
by Sabestian Caine
rommmcek wrote:Just play with these two lines:
- v1:=237, v2:=207 (where to show)
- x:=237, y:=207, w:=683, h:=400 (what to capture)
P.s.: Please remove this: sx:=sx*2, sy:=sy*2 (just this, curly bracket remains!). This was blunder of mine!
thank u so much dear rommmcek...now i am able to clip the desired part of screen and i am also able to put it at the desired location on the screen also....
but there is one more problem-
as when i press f1 key then it shows the desired clip at desired location, which is ok..... for this my codes are these-

Code: Select all

f1::
SCW_ScreenClip2Win(clip:=1,email:=0,IMUGR:=0)
return
#include C:\My Documents\Scripts\ScreenClipping_block.ahk
but i want that when i press f3 key then it should clip some other part of screen, at some other location on screen. I tried to use these codes-

Code: Select all

f1::
SCW_ScreenClip2Win(clip:=1,email:=0,IMUGR:=0)
return

f3::
SCW_ScreenClip2Win(clip:=1,email:=0,IMUGR:=0)
return

#include C:\My Documents\Scripts\ScreenClipping_block.ahk


but, If i change "what to capture" and "where to show" coordinates in screenclipping_block.ahk script for f3 they, then it changes the desired clip and desired location of clip which was set by f1 key also. In other words f1 key and f3 key work same in the above codes. So please tell me how can i use SCW_ScreenClip2Win fucntion in screenclipping_block.ahk script for two or more than two keys for showing two different rectangles (images) of screen at two different parts (region) of screen? please help... thanks a lot...

Re: Screen clipping

Posted: 13 Dec 2017, 18:46
by rommmcek
Below global IMUGR_client:="XXXXX" put:

Code: Select all

global f1wtsv1, f1wtsv2, f1wtcx, f1wtcy, f1wtcw, f1wtch
global f2wtsv1, f2wtsv2, f2wtcx, f2wtcy, f2wtcw, f2wtch
global f3wtsv1, f3wtsv2, f3wtcx, f3wtcy, f3wtcw, f3wtch
HotKeys:

Code: Select all

^f1::
WinGetPos, f1wtcx, f1wtcy, f1wtcw, f1wtch, ScreenClippingWindow ahk_class AutoHotkeyGUI ; wtc - what to clip
SoundBeep
return
!f1::
WinGetPos, f1wtsv1, f1wtsv2,,, ScreenClippingWindow ahk_class AutoHotkeyGUI ; wts - where to show
SoundBeep
return

^f2::
WinGetPos, f2wtcx, f2wtcy, f2wtcw, f2wtch, ScreenClippingWindow ahk_class AutoHotkeyGUI ; wtc - what to clip
SoundBeep
return
!f2::
WinGetPos, f2wtsv1, f2wtsv2,,, ScreenClippingWindow ahk_class AutoHotkeyGUI ; wts - where to show
SoundBeep
return

^f3::
WinGetPos, f3wtcx, f3wtcy, f3wtcw, f3wtch, ScreenClippingWindow ahk_class AutoHotkeyGUI ; wtc - what to clip
SoundBeep
return
!f3::
WinGetPos, f3wtsv1, f3wtsv2,,, ScreenClippingWindow ahk_class AutoHotkeyGUI ; wts - where to show
SoundBeep
return

f1::
f2::
f3::
#Lbutton::SCW_ScreenClip2Win(clip:=1,email:=0,IMUGR:=0) ; Win+left click mouse=auto copy to clipboard
Where to show:

Code: Select all

if (A_ThisHotkey = "f1")
	v1:=f1wtsv1, v2:=f1wtsv2                        
else if (A_ThisHotkey = "f2")
	v1:=f2wtsv1, v2:=f2wtsv2             
else if (A_ThisHotkey = "f3")
	v1:=f3wtsv1, v2:=f3wtsv2             
What to clip:

Code: Select all

if (A_ThisHotkey = "f1")
    x:=f1wtcx, y:=f1wtcy, w:=f1wtcw, h:=f1wtch
else if (A_ThisHotkey = "f2")
	x:=f2wtcx, y:=f2wtcy, w:=f2wtcw, h:=f2wtch
else if (A_ThisHotkey = "f3")
	x:=f3wtcx, y:=f3wtcy, w:=f3wtcw, h:=f3wtch

Re: Screen clipping

Posted: 14 Dec 2017, 12:39
by Sabestian Caine
rommmcek wrote:Below global IMUGR_client:="XXXXX" put:

Code: Select all

global ClipWidth, ClipHight
HotKeys:

Code: Select all

f1::
f2::
f3::SCW_ScreenClip2Win(clip:=1,email:=0,IMUGR:=0)
Where to show:

Code: Select all

f (A_ThisHotkey = "f1")
	v1:=0, v2:=0                        ; upper left corner
else if (A_ThisHotkey = "f2")
	v1:=A_ScreenWidth-ClipWidth, v2:=0  ; upper right corner
else if (A_ThisHotkey = "f3")
	v1:=0, v2:=A_ScreenHeight-ClipHight  ; lower left corner
What to clip:

Code: Select all

if (A_ThisHotkey = "f1")
	x:=300, y:=200, w:=ClipWidth:=100, h:=ClipHight:=50
else if (A_ThisHotkey = "f2")
	x:=500, y:=400, w:=ClipWidth:=150, h:=ClipHight:=75
else if (A_ThisHotkey = "f3")
	x:=700, y:=600, w:=ClipWidth:=200, h:=ClipHight:=100

Thank you very much dear rommmcek.... this is really what i wanted..... you are really great...

sir one more thing i want to know just for knowledge purpose-
can't we add "what to clip" and "where to show" coordinates within the function itself for better compatibility.
please see below a fictitious example-

Code: Select all

f1::
SCW_ScreenClip2Win( x:=237, y:=207, w:=683, h:=400, v1:=237, v2:=207)
return

f2::
SCW_ScreenClip2Win( x:=244, y:=214, w:=6847, h:=350, v1:=145, v2:=652)
return

#include C:\My Documents\Scripts\ScreenClipping_block.ahk
if the above can happen then it would be very easy to use the script. Please guide.... thanks a lot...

Re: Screen clipping

Posted: 15 Dec 2017, 16:19
by rommmcek
I've updated post above, just custom edit variables in Hotkey(s). (see post below)
P.s.: Now the native #Lbutton Hotkey is preserved.

Re: Screen clipping

Posted: 17 Dec 2017, 01:31
by rommmcek
New update!
Script will work as original! Besides you can optionally define e.g. f1 where to clip & where to show via ^f1 & !f1
Example for f1:
1. Hold down Win and drag with mouse desired clip.
2. Press ^f1 to define where to clip
3. Move the ScreenClippingWindow with mouse to the desired position and press !f1 to define where to show
4. Press Esc to destroy ScreenClippingWindow
5. Press f1 to clip (desired portion) and show it at desired positon

P.s: To save time download attachment (6)

Re: Screen clipping

Posted: 17 Dec 2017, 14:56
by burque505
I've been using Joe Glines's script for quite a while now, rommmcek's updates are cool. Thanks a million, you guys! :clap:

In the spoiler is my little tweak with the ability to pick a folder to save in. (I don't like the default folder select dialog, and I know there's an updated version somewhere that I remember fiddling with in a VB script a year or so ago, but I can't find it now. Arrrgh...)

1) Edit line 4 to suit (global folderVar := "C:\Users\" . A_UserName . "\Desktop\AHK\ClipShots\"). This is the default save folder.
2) Edit line 86 to suit (^#s::FileSelectFolder, folderVar, *C:\Users\%A_UserName%\Desktop\AHK\ClipShots\,3).
3) Change lines 46,47, and 48 to suit your clipping, emailing and IMGUR preferences. You probably won't want to use mine.

A) Ctrl+Shift+S saves to the folder in 'folderVar'.
B) Ctrl+Win+S brings up the folder select dialog for the 'save' folder.
C) Ctrl+Win+Q displays a message box with the current save folder.
Spoiler
Thanks again, guys.
Regards,
burque505

Re: Screen clipping

Posted: 12 Jan 2018, 18:49
by goji
This is amazing and works well on the installed AHK version. However it doesn't with the portable one. Which is unfortunately what i use.. And what i wish to keep using. I can see the drag area showing up, but in the end, no file saved, no windows, etc.. Just not working. I'd like to understand why : am i missing something or it just can work on a portable version? Let me know, thanks :)

PS: I'd also like to remove the outlook part.. ^^

Re: Screen clipping

Posted: 25 Mar 2018, 15:02
by kczx3
I am trying to get better at working with classes in AHK. So I took a stab at converting this to a class. I wanted to figure out how to use a Resize handler on the screen shot GUIs to adjust the border to the size but don't have that much experience with GDI+. Also, it doesn't seem that there is a good way in AHK v1.1 to use a class method as a GUI OnSize handler. Criticism is welcome for learning.

Code: Select all

#SingleInstance Force
#Include <gdip_all>

; Options, all are optional as the class will define defaults in an option isn't found
opts := {}
opts.selColor := "Green"
opts.selTrans := 100
opts.innerBorderColor := "ffDEDEDE"
opts.outerBorderColor := "ffffffff"

; initialize the class
screenClipper := new ScreenClipper(opts)
Return

/*
Based on ScreenClip2Win by LearningOne - https://autohotkey.com/boards/viewtopic.php?t=12088
Thanks to tic (Tariq Porter) for his GDI+ Library
https://github.com/tariqporter/Gdip
*/
class ScreenClipper {
    ; Object to store clip windows in
    clips := {}
    
    ; How many clips we currently have
    guiCount := 0
    
    ; Default options
    static defaultOpts := {"selColor": "yellow", "selTrans": 50, "innerBorderColor": "ff6666ff", "outerBorderColor": "ffffffff"}
    
    __New(opts) {
        try {
            Menu, Tray, Icon, % SubStr(A_ScriptName, 1, -4) ".ico"
        }
        
        ; Validate the passed options, assign defaults if an option wasn't passed
        this.opts := this.validateOpts(opts)
        
        ; Not sure on how exactly this should be handled to inform the user
        if (!this.pToken := this.startGdip()) {
            throw Exception("GDI+ failed to initialize", "ScreenClipper.__New")
            return false
        }
        
        ; Bind the LButton Down event so we can move the window
        this.WM_LBUTTONDOWN_Bound := this.OnMessage.bind(this)
        OnMessage(0x201, this.WM_LBUTTONDOWN_Bound)
        
        ; Assign our class method to a var so we can bind it to the Windows + LButton Hotkey
        takeScreenClip := this.takeScreenClip.bind(this)
        Hotkey, #Lbutton, % takeScreenClip
        
        ; Assign our class method to a var so we can bind it to both Escape and RButton for closing the active screen clip
        removeScreenClip := this.removeScreenClip.bind(this)
        Hotkey, IfWinActive, ScreenClippingWindow ahk_class AutoHotkeyGUI
        HotKey, Esc, % removeScreenClip
        HotKey, RButton, % removeScreenClip
    }
    
    ; Shutdown GDI+
    __Delete() {
        if (this.pToken) {
            Gdip_shutdown(this.pToken)
        }
    }
    
    ; Validate and merge passed opts to default ones
    validateOpts(opts) {        
        for defK, defV in this.defaultOpts {
            if (!opts.HasKey(defK) || opts[defK] = "") {
                opts[defK] := defV
            }
        }
        
        return opts
    }
    
    ; Takes a screenshot
    takeScreenClip() {
        coords := this.selectAreaMod()
        ; Too small
        if (coords.w < 10 && coords.h < 10) {
            return false
        }
        
        ; Gdip_BitmapFromScreen takes a pipe delimited string of coords, unfortunately
        pBitmap := Gdip_BitmapFromScreen(coords.x "|" coords.y "|" coords.w "|" coords.h)
        
        ; Create the GUI to display the screen shot
        hwnd := this.createClipWin(pBitmap, coords.x, coords.y)
        
        ; Assign the new GUI's hwnd to our instance clips object
        ; We store the gui number as the value for destroying it later
        this.clips[hwnd] := this.guiCount
        return
    }
    
    ; Called in response to hitting the Escape key or right mouse button
    ; Conditional upon a ScreenClippingWindow being the active window
    removeScreenClip() {
        ; Get the hwnd of the active ScreenClippingWindow
        hwnd := WinExist("A")
        
        guiNum := this.clips[hwnd]
        this.clips.Delete(hwnd), this.guiCount--
        Gui, %guiNum%: Destroy
    }
    
    startGdip() {
        if (!pToken := Gdip_Startup()) {
            ; MsgBox, 64, GDI+ error, GDI+ failed to start. Please ensure you have GDI+ on your system.
            return false
        }
        return pToken
    }
    
    ; Method called in response to Windows key + LButton to define the screenshot area
    ; Causes a semi-transparent window to appear to display the selected size
    selectAreaMod() {
        CoordMode, Mouse, Screen
        MouseGetPos, MX, MY
        
        Gui Selector: +AlwaysOnTop -Caption +Border +ToolWindow +LastFound
        WinSet, Transparent, % this.opts.selTrans
        Gui Selector: Color, % this.opts.selColor
        Hotkey := RegExReplace(A_ThisHotkey,"^(\w* & |\W*)")
        While, GetKeyState(Hotkey, "p") {
            Sleep, 10
            MouseGetPos, MXend, MYend
            w := abs(MX - MXend), h := abs(MY - MYend)
            X := (MX < MXend) ? MX : MXend
            Y := (MY < MYend) ? MY : MYend
            Gui Selector: Show, x%X% y%Y% w%w% h%h% NA
        }
        Gui Selector: Destroy
        MouseGetPos, MXend, MYend
        If (MX > MXend) {
            temp := MX, MX := MXend, MXend := temp
        }
        If (MY > MYend) {
            temp := MY, MY := MYend, MYend := temp
        }
        Return { x: MX, y: MY, w: w, h: h }
    }
    
    ; Creates a GUI to display a bitmap via GDI+
    ; Heavily borrowed from SCW_ScreenClip2Win by LearningOne
    createClipWin(pBitmap, x, y) {
        guiNum := ++this.guiCount
        
        Width := Gdip_GetImageWidth(pBitmap), Height := Gdip_GetImageHeight(pBitmap)
        Gui, %guiNum%:New, % "-Caption +E0x80000 +LastFound +AlwaysOnTop +OwnDialogs +Border +Resize +Label" this.__class "_On +MaxSize" width - 10 "x" height - 10
        Gui, %guiNum%: Show, Na, ScreenClippingWindow
        hwnd := WinExist()

        hbm := CreateDIBSection(Width + 6, Height + 6), hdc := CreateCompatibleDC(), obm := SelectObject(hdc, hbm)
        G := Gdip_GraphicsFromHDC(hdc), Gdip_SetSmoothingMode(G, 4), Gdip_SetInterpolationMode(G, 7)

        Gdip_DrawImage(G, pBitmap, 3, 3, Width, Height)
        Gdip_DisposeImage(pBitmap)

        pPen1 := Gdip_CreatePen("0x" this.opts.innerBorderColor, 3)
        pPen2 := Gdip_CreatePen("0x" this.opts.outerBorderColor, 1)
        Gdip_DrawRectangle(G, pPen1, 1, 1, Width + 3, Height + 3)
        Gdip_DrawRectangle(G, pPen2, 1, 1, Width + 3, Height + 3)
        Gdip_DeletePen(pPen1), Gdip_DeletePen(pPen2)
        
        UpdateLayeredWindow(hwnd, hdc, x - 3, y - 3, Width + 6, Height + 6)
        SelectObject(hdc, obm), DeleteObject(hbm), DeleteDC(hdc), Gdip_DeleteGraphics(G)
        Return hwnd
    }
    
    ; Allows moving of Screen Clipping window by left clicking and dragging
    OnMessage(wParam, lParam, msg, hwnd) {
        if (this.clips[hwnd]) {
            PostMessage, 0xA1, 2,,, % "ahk_id " hwnd
            return 1   ; confirm that click was on module's screen clipping windows
        }
    }
}

; Would like to be able to resize the border when the GUI size is changed
; Then when Ctrl + C is pressed, only the size of the GUI is copied instead of the whole screenshot
ScreenClipper_OnSize(hwnd, eventInfo, w, h) {
    ;WinGetPos, x, y, , , % "ahk_id " hwnd
    ;pBitmap := Gdip_BitmapFromHWND(hwnd)
    ;hdc := GetDC(hwnd)
    ;pBitmap2 := Gdip_CreateBitmap(w, h), G2 := Gdip_GraphicsFromImage(pBitmap2)
    ;Gdip_DrawImage(G2, pBitmap, 0, 0, w, h, x # 3, y + 3, w - 3, h - 3)
    ;Gdip_DeleteGraphics(G2)
    ;UpdateLayeredWindow(hwnd, hdc, x-3, y-3, Width+6, Height+6)
}


Re: Screen clipping

Posted: 22 Apr 2018, 06:32
by SanyaIV
Lately I've been having issues with this script. Basically sometimes it doesn't copy the data into the clipboard. Example: I do a screenshot using WinKey + LeftMouse, paste it into Paint (just to check clipboard content) and it's fine, I take another one and paste, and it's not the new one but the old one, I take yet another one and paste, now it says no data that paint can use, and take yet another one after that and it'll work again. This happens ~50% of the time and I don't know why. It also happens with the one rommmcek uploaded. I don't really know anything about AHK scripting, I just know that I absolutely love this script, or at least used to when it worked reliably. Does anyone happen to know how to fix this?

Re: Screen clipping

Posted: 22 Apr 2018, 07:03
by kczx3
Does mine do the same?

Re: Screen clipping

Posted: 22 Apr 2018, 16:08
by burque505
SanyaIV, I had some similar (if not related) issues, not with these screen clipping scripts, but with pasting in general. They are gone, thank goodness, after rebooting and running CCleaner.

It appears that, at least for my system (Win7 64-bit), the Office 2016 clipboard was interfering with pasting into RichEdit fields in other applications (those that use msftedit.dll), and also with copying from those applicatons.

Occasionally I could even see the desired image being pasted just for milliseconds, then the old image would be pasted instead. It also corrupted some RTF documents, adding "ghost" images that would not display. This happened with Wordpad, LibreOffice Writer, and Word.

Might it be possible you have some Office application running when this behavior occurs?

Regards,
burque505

Re: Screen clipping

Posted: 24 Apr 2018, 01:45
by SanyaIV
kczx3 wrote:Does mine do the same?
Just tried it and I don't think I understand how to use it, it doesn't seem to copy the contents of the screenshot into clipboard.
burque505 wrote:SanyaIV, I had some similar (if not related) issues, not with these screen clipping scripts, but with pasting in general. They are gone, thank goodness, after rebooting and running CCleaner.

It appears that, at least for my system (Win7 64-bit), the Office 2016 clipboard was interfering with pasting into RichEdit fields in other applications (those that use msftedit.dll), and also with copying from those applicatons.

Occasionally I could even see the desired image being pasted just for milliseconds, then the old image would be pasted instead. It also corrupted some RTF documents, adding "ghost" images that would not display. This happened with Wordpad, LibreOffice Writer, and Word.

Might it be possible you have some Office application running when this behavior occurs?

Regards,
burque505
No Office applications running that I know of.

Re: Screen clipping

Posted: 24 Apr 2018, 09:53
by kczx3
Guess I hadn't incorporated that functionality yet. Sorry.

Re: Screen clipping

Posted: 24 Apr 2018, 21:35
by rommmcek
Looks like you're having problems outside the ScreenClipping tool.
As a workaround try replacing Send, !{PrintScreen} ; Active Win's client area to Clipboard with:

Code: Select all

   Clipboard:=""
   loop, 4 {  ;increase number of loops to ensure better realiability (e.g. under CPU load)
   ;SoundBeep, 8000 ; uncomment this line to determine number of loops needed (high pitched beep)
   Send, !{PrintScreen} ; Active Win's client area to Clipboard
   ClipWait, .3, 1 ; increase ".3" for slow computers
   if !ErrorLevel
    break
  }

Re: Screen clipping

Posted: 03 May 2018, 03:03
by SanyaIV
rommmcek wrote:Looks like you're having problems outside the ScreenClipping tool.
As a workaround try replacing Send, !{PrintScreen} ; Active Win's client area to Clipboard with:

Code: Select all

   Clipboard:=""
   loop, 4 {  ;increase number of loops to ensure better realiability (e.g. under CPU load)
   ;SoundBeep, 8000 ; uncomment this line to determine number of loops needed (high pitched beep)
   Send, !{PrintScreen} ; Active Win's client area to Clipboard
   ClipWait, .3, 1 ; increase ".3" for slow computers
   if !ErrorLevel
    break
  }
Been trying it for a while but seems it didn't fix it. =/ Still a lottery if the screenshot gets added to clipboard.

Re: Screen clipping

Posted: 03 May 2018, 23:47
by rommmcek
This is pretty discouraging. You should proceed like this:

1. Manually
- close (if possible all) running Apps on your computer (fresh restarted computer would be still better)
- run explorer
- run clipbrd (or any other similar App to monitor clipboard content) and resize it to less then half screen width/hight
- activate explorer and resize it so you can see the Viewer at the same time
- hit Alt+PrintScreen and monitor the Viewer
- repeat a few times with different programs in place of explorer

2. Programatically
- still running Viewer and explorer (other apps closed as much as possible)
- run TestCopyWin.ahk
- activate resized explorer
- hit Ctrl+i and monitor the Viewer
- repeat a few times with different programs in place of explorer

3. ScreenClipping
- still running Viewer and explorer (other apps closed as much as possible)
- run ScreenClipping and test it while monitoring Viewer

4. Report results

Re: Screen clipping

Posted: 06 May 2018, 10:19
by SanyaIV
rommmcek wrote:This is pretty discouraging. You should proceed like this:

1. Manually
- close (if possible all) running Apps on your computer (fresh restarted computer would be still better)
- run explorer
- run clipbrd (or any other similar App to monitor clipboard content) and resize it to less then half screen width/hight
- activate explorer and resize it so you can see the Viewer at the same time
- hit Alt+PrintScreen and monitor the Viewer
- repeat a few times with different programs in place of explorer

2. Programatically
- still running Viewer and explorer (other apps closed as much as possible)
- run TestCopyWin.ahk
- activate resized explorer
- hit Ctrl+i and monitor the Viewer
- repeat a few times with different programs in place of explorer

3. ScreenClipping
- still running Viewer and explorer (other apps closed as much as possible)
- run ScreenClipping and test it while monitoring Viewer

4. Report results
All of them worked correctly, except the application clipbrd which at certain times would bug out and stop updating until relaunching it. Really not sure here, it seems like it's working correctly now. For the sake of it I just launched the pre-edit one and it almost instantly stopped updating the clipboard. The post-edit one seems to be holding up now but at the same time there was that time earlier when it didn't update clipboard, I'm guessing my system is just either unstable or some other application is messing with things. Either way, post-edit is holding up well enough for everyday use at least, I'm satisfied with this.

Edit: :facepalm: So I thought I had a shortcut set up in my startup folder but turns out it was the actual old file, so whenever I restarted I would get the pre-edit version rather than the post-edit, that would explain things. In other words, the post-edit version most likely works 100%!

Re: Screen clipping

Posted: 31 May 2018, 00:59
by Stamimail
@Joe Glines
I suggest to change the
Rbutton:: winclose, A
to middle button (close the window like a middle click on a tab).
Use RightClick for ContextMenu with options.
Sorry I don't know how to implement this myself (still a newbie).

Is there someone can solve the problem of the incompatibility between the yellow selection range and the cursor position? To be able to use it, no matter what the DPI is?