Screen clipping

Post your working scripts, libraries and tools for AHK v1.1 and older
mankvl
Posts: 25
Joined: 23 Sep 2016, 03:58

Re: Screen clipping

06 Sep 2017, 03:55

I'am having problems with

Code: Select all

SCW_LBUTTONDOWN()
function, I added all SCW functions to my lib folder (file named SCW.ahk) when I run my main script it's included automatically as I understand. But it always runs that function after every hotkey. Maybe i need to have this function in the main script? or change smth to stop always being used?

Code: Select all

SCW_LBUTTONDOWN() {
   MouseGetPos,,, WinUMID
    WinGetTitle, Title, ahk_id %WinUMID%
   if Title = ScreenClippingWindow
   {
      PostMessage, 0xA1, 2,,, ahk_id %WinUMID%
      KeyWait, Lbutton
      CoordMode, mouse, Relative
      MouseGetPos, x,y
     XClose := SCW_Reg("G" A_Gui "#XClose"), YClose := SCW_Reg("G" A_Gui "#YClose")
      if (x > XClose and y < YClose)
      Gui %A_Gui%: Destroy
      return 1   ; confirm that click was on module's screen clipping windows
   }
}
the log always shows

Code: Select all

227: if (x > XClose &&  y < YClose)  
228: Gui,%A_Gui%: Destroy
229: Return,1 (2.06)
218: MouseGetPos,,,WinUMID
219: WinGetTitle,Title,ahk_id %WinUMID%
220: if Title = ScreenClippingWindow
231: } (2.81)
and this par never stops or returns

Code: Select all

218: MouseGetPos,,,WinUMID
219: WinGetTitle,Title,ahk_id %WinUMID%
220: if Title = ScreenClippingWindow
need4speed
Posts: 143
Joined: 22 Apr 2016, 06:50

Re: Screen clipping

19 Oct 2017, 04:31

How to set transparency of the screenshots?
In order to compare screenshots I overlay them and set the topmost semi-transparent.
Unfortunatelly this code works only with standard Windows but not with screenshots created by the ScreenClipping script:
Could anyone adjust the code?
Or even better, if more ppl are longing for transparent screenshots,
maybe someone could incorporate transparency natively in the main script?!? Cheers.
User avatar
king-of-hearts
Posts: 30
Joined: 01 Oct 2017, 06:29

Re: Screen clipping

21 Oct 2017, 08:12

I was searching this for days!! Thank you guys! So Awesome!

Im using Joe Glines version btw
https://autohotkey.com/boards/viewtopic.php?f=6&t=38707 - MS Access Manager - SQL Query: Incredible tool for MS Access/SQL Queries on the fly!!
User avatar
Delta Pythagorean
Posts: 627
Joined: 13 Feb 2017, 13:44
Location: Somewhere in the US
Contact:

Re: Screen clipping

23 Oct 2017, 07:50

I would upload my version of a screen clipping script but I'm afraid I'm at work :lol:

[AHK]......: v2.0.12 | 64-bit
[OS].......: Windows 11 | 23H2 (OS Build: 22621.3296)
[GITHUB]...: github.com/DelPyth
[PAYPAL]...: paypal.me/DelPyth
[DISCORD]..: tophatcat

need4speed
Posts: 143
Joined: 22 Apr 2016, 06:50

Re: Screen clipping

25 Oct 2017, 16:04

Hey Delta, what is your version about and is it any better than Joes?
User avatar
FanaticGuru
Posts: 1906
Joined: 30 Sep 2013, 22:25

Re: Screen clipping

25 Oct 2017, 17:00

Below is my version. I had more plans for it that I never implemented once I got it to the point that I started using it. For one I wanted to be able to minimize and restore clips but never got around to doing.

The main differences are:
  • dynamically created help menu
  • settings Gui
  • ability to create a PDF from clipping if you have Acrobat installed
  • right click menu for clippings (the ton of different hotkey combinations got too much to remember)
  • ability to save clippings and PDFs to a folder automatically
  • cleaned up a lot of the code and made it more modular
  • other stuff I probably forgot
I use this quite a bit.
The ability to just create a clip to 'memorize' something on the screen is handy.
Clipping and pasting images then in other programs like Outlook and Word.
Clipping to a PDF that is then easy to print with comments.

Code: Select all

; Screen Clipper
; Fanatic Guru
; 2017 02 25
; Version 1.00
;
; Use Mouse Drag to Clip Selection of Screen
;
;{-----------------------------------------------
;
; Credits:
;
; Learning one	:=Screen Clipping
;		https://autohotkey.com/boards/viewtopic.php?f=6&t=12088
; Joe Glines		:= added Clip, IMGUR, Email parameters to ScreenClip2Win()
; maestrith		:= IMGUR upload
;		https://autohotkey.com/board/topic/95521-ahk-11-upload-a-screen-capture-to-imgur/
; tervon 			:= closing of clip window
;
;}

; INITIALIZATION - ENVIROMENT
;{-----------------------------------------------
;
#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.
#SingleInstance force  ; Ensures that only the last executed instance of script is running
;}

; INITIALIZATION - VARIABLES
;{-----------------------------------------------
;
 ;Obtain IMGUR API token value here: https://imgur.com/account/settings
Settings_Imgur_Client := "xxxxxxxxxxxxxxx"
Settings_SavePath := ".\Images - Screen Clipper\"
RelativePath(Settings_SavePath)
Settings_SavePathPDF := ".\PDF - Screen Clipper\"
RelativePath(Settings_SavePathPDF)
Settings_Ini := A_ScriptFullPath ": Screen Clipper.ini" ; Ini File in Script's Alternate Data Stream (requires NTFS)
Settings_Ini := A_ScriptDir "\Screen Clipper.ini" ; Normal Ini File

; Read Settings from Ini File
if FileExist(Settings_Ini)
{
	IniRead, Settings_Imgur_Client, %Settings_Ini%, Settings, Settings_Imgur_Client, %Settings_Imgur_Client%
	IniRead, Settings_SavePath, %Settings_Ini%, Settings, Settings_SavePath, %Settings_SavePath%
}

; Read Hotkeys from Script File
FileRead, Script, %A_ScriptFullPath%
Script :=  RegExReplace(Script, "ms`a)^\s*/\*.*?^\s*\*/\s*|^\s*\(.*?^\s*\)\s*")
Hotkeys := {}
Loop, Parse, Script, `n, `r
	if RegExMatch(A_LoopField,"^\s*(.*):`:.*`;\s*(.*)",Match)
	{
		if !RegExMatch(Match1,"(Shift|Alt|Ctrl|Win)")
		{
			StringReplace, Match1, Match1, +, Shift+
			StringReplace, Match1, Match1, <^>!, AltGr+
			StringReplace, Match1, Match1, <, Left, All
			StringReplace, Match1, Match1, >, Right, All 
			StringReplace, Match1, Match1, !, Alt+
			StringReplace, Match1, Match1, ^, Ctrl+
			StringReplace, Match1, Match1, #, Win+
		}
		Hotkeys.Push({"Hotkey":Match1, "Comment":Match2})
	}

;}

; INITIALIZATION - GUI
;{-----------------------------------------------
;
Menu, tray, icon, %A_WinDir%\system32\mmcndmgr.dll,106
;******************Tray Menu items***********
Menu, Tray, NoStandard ;removes default options
Menu, Tray, Add , Help, Help ;can doubleclick main
Menu, Tray, Default, Help ;sets to default
Menu, Tray, Add, Settings
Menu, Tray, Add, 
Menu, Tray, Add, Suspend
Menu, Tray, Add, Reload
Menu, Tray, Add, Edit
Menu, Tray, Add, Exit

Gui, +AlwaysOnTop +resize
Gui, Help:Default
Gui, Color, aqua
for Index, Element in Hotkeys
{
	Gui, Font, Bold
	Loop, Parse, % Element.Hotkey, +
	{
		If  (A_LoopField = "Win")
		{
			Gui, Font, cBlue
			xx := 40
		}
		else if (A_LoopField = "Alt")
		{
			Gui, Font, cRed
			xx := 75
		}
		else if (A_LoopField = "Ctrl")
		{
			Gui, Font, cGreen
			xx := 110
		}
		else if (A_LoopField = "Shift")
		{
			Gui, Font, cYellow
			xx := 5
		}
		else
		{
			Gui, Font, Bold cBlack
			xx := 145
		}
		if (A_Index = 1)
			Gui, Add, Text, x%xx% , % Format("{:Ts}", A_LoopField)
		else
			Gui, Add, Text, x%xx% yp , % Format("{:Ts}", A_LoopField)
	}
	Gui, Font
	Gui, Add, Text, yp x200, % Element.Comment
}

Gui, Settings:Default
Gui, Font, s10 bold
Gui, Add, Text, , Enter Path to Save Clips:
Gui, Font
Gui, Add, Edit, w500 vSettings_SavePath, %Settings_SavePath%
Gui, Add, Button, gSettings_ButtonPath yp x510 w22 h22 hwndIcon
GuiButtonIcon(Icon, "shell32.dll", 46, "s20")
Gui, Add, Text, x8, Starting ".\" indicates to start Relative Path in current folder.
Gui, Add, Text, yp+15,   Each additional "." indicates to start Relative Path up one folder
Gui, Add, Text, yp+15,   Example:  .\Images         (will store images in a subfolder of the working folder named "Images" )
Gui, Font, bold
Gui, Add, Text, yp+40, Enter Imgur Client ID:
Gui, Font
Gui, Add, Edit, yp-4 x140 w125 vSettings_Imgur_Client, %Settings_Imgur_Client%
Gui, Add, Button, yp+50 x100 w120 gSettings_ButtonSave, SAVE Settings
Gui, Add, Button, yp x300 w120 gSettings_ButtonUse Default, USE Settings

Menu, Context_Clip, add, COPY:  &Clipboard, Context_Clip_Handler
Menu, Context_Clip, add, COPY:  Clipboard (with Border), Context_Clip_Handler
Menu, Context_Clip, add
Menu, Context_Clip, add, COPY:  &PDF, Context_Clip_Handler
Menu, Context_Clip, add, COPY:  PDF (with Border), Context_Clip_Handler
Menu, Context_Clip, add, COPY:  P&DF - Saved, Context_Clip_Handler
Menu, Context_Clip, add, COPY:  PDF - Saved (with Border), Context_Clip_Handler
Menu, Context_Clip, add
Menu, Context_Clip, add, COPY:  &File, Context_Clip_Handler
Menu, Context_Clip, add, COPY:  File (with Border), Context_Clip_Handler
Menu, Context_Clip, add, COPY:  File && Email, Context_Clip_Handler
Menu, Context_Clip, add, COPY:  File && Imgur, Context_Clip_Handler
Menu, Context_Clip, add, COPY:  File`, Imgur && Email, Context_Clip_Handler
Menu, Context_Clip, add
Menu, Context_Clip, add
Menu, Context_Clip, add, CLOSE:  Clip Image, Context_Clip_Handler

;}

; BEGINNING OF AUTO-EXECUTE
;{-----------------------------------------------
;

;
;}-----------------------------------------------
; END OF AUTO-EXECUTE

; HOTKEYS
;{-----------------------------------------------
;
#Lbutton::		;	<-- Clip Image Only
	SCW_ScreenClip2Win(SetClipboard:=false)
return

#^Lbutton::	;	<-- Clip Image and Copy to Clipboard
	SCW_ScreenClip2Win(SetClipboard:=true)
return

#+Lbutton::	;	<-- Clip Image, Copy to Clipboard, Create PDF
	SCW_ScreenClip2Win(SetClipboard:=true)
	WaitReleaseAllModifiers() 
	Clipboard2Acrobat()
return

#!Lbutton::		;	<-- Clip Image and Save to File
	Hwnd := SCW_ScreenClip2Win(SetClipboard:=false)
	SCW_Win2File(Settings_SavePath,Hwnd)
return

#^!Lbutton::	;	<-- Clip Image, Copy to Clipboard, and Save to File
	Hwnd := SCW_ScreenClip2Win(SetClipboard:=true)
	SCW_Win2File(Settings_SavePath,Hwnd)
return

#^!+Lbutton::	;	<-- Clip Image, Copy to Clipboard, Save to File, and Create PDF
	Hwnd := SCW_ScreenClip2Win(SetClipboard:=true)
	SCW_Win2File(Settings_SavePath,Hwnd)
	WaitReleaseAllModifiers() 
	Clipboard2Acrobat()
return


#IfWinActive, ScreenClipperWindow ahk_class AutoHotkeyGUI
	^c::				;	<-- (Screen Clipper) : Copy Active Clip to Clipboard
		SCW_Win2Clipboard()
	return
	+^c::			;	<-- (Screen Clipper) : Copy Active Clip to Clipboard with Border
		SCW_Win2Clipboard(0)
	return
	^s::				;	<-- (Screen Clipper) : Save Active Clip to File
		SCW_Win2File(Settings_SavePath)
	return
	+^s::			;	<-- (Screen Clipper) : Save Active Clip to File with Border
		SCW_Win2File(Settings_SavePath,,0)
	return
	#e::				;	<-- (Screen Clipper) : Save Active Clip to File and Email
		File := SCW_Win2File(Settings_SavePath)
		Email_AttachFile(File)
	return
	#i::				;	<-- (Screen Clipper) : Save Active Clip to File and Post to IMGUR
		File := SCW_Win2File(Settings_SavePath)
		URL := Imgur_Post(File, Settings_Imgur_Client)
		Clipboard := URL
	return
	#^i::				;	<-- (Screen Clipper) : Save Active Clip to File, Post to IMGUR and Email
		File := SCW_Win2File(Settings_SavePath)
		URL := Imgur_Post(File, Settings_Imgur_Client)
		Email_AttachFile(File, URL)
	return
	RButton::	;	<-- (Screen Clipper) : Single for Menu, Double to Close Active Clip
		KeyWait, RButton			; wait release
		KeyWait, RButton, D T0.2	; and pressed again within 0.2 seconds
		if ErrorLevel ; timed-out (only a single press)
			Menu, Context_Clip, Show
		else
			WinClose, A
	return
	Esc::				;	<-- (Screen Clipper) : Close Active Clip
		WinClose, A
	return
#IfWinActive
;}

; SUBROUTINES
;{-----------------------------------------------
;

;}

; SUBROUTINES - GUI
;{-----------------------------------------------
;
Settings_ButtonPath:
	FileSelectFolder, Settings_SavePath, *%Settings_SavePath%, 3, Select Folder for Saved Image
	GuiControl, Settings:, Settings_SavePath, %Settings_SavePath%\
return
Settings_ButtonUSE:
	Gui, Settings:Submit
return
Settings_ButtonSAVE:
	Gui, Settings:Submit
	IniWrite, %Settings_Imgur_Client%, %Settings_Ini%, Settings, Settings_Imgur_Client
	IniWrite, %Settings_SavePath%, %Settings_Ini%, Settings, Settings_SavePath
return
HelpGuiEscape:
HelpGuiClose:
	Gui, Help:Hide
Return

;~ Context Menu for Clip Window
Context_Clip_Handler:
		if (A_ThisMenuItemPos = 16) ; Close Clip Image
			WinClose, A
		Sleep 350	; Context Menu selection fades out and needs time to disappear before Clipping
		if  (A_ThisMenuItemPos = 1)		; Clipboard
			SCW_Win2Clipboard()
		else if (A_ThisMenuItemPos = 2)	; Clipboard with Border
			SCW_Win2Clipboard(0)
		else if (A_ThisMenuItemPos = 4) ; Clipboard to PDF
		{
			SCW_Win2Clipboard()
			Clipboard2Acrobat()
		}
		else if (A_ThisMenuItemPos = 5) ; Clipboard with Border to PDF
		{
			SCW_Win2Clipboard(0)
			Clipboard2Acrobat()
		}
		else if (A_ThisMenuItemPos = 6) ; Clipboard to PDF and Save
		{
			SCW_Win2Clipboard()
			Clipboard2Acrobat(Settings_SavePathPDF)
		}
		else if (A_ThisMenuItemPos = 7) ; Clipboard with Border to PDF and Save
		{
			SCW_Win2Clipboard(0)
			Clipboard2Acrobat(Settings_SavePathPDF)
		}
		else if (A_ThisMenuItemPos = 9) ; File
			SCW_Win2File(Settings_SavePath)
		else if (A_ThisMenuItemPos = 10) ; File with Border
			SCW_Win2File(Settings_SavePath,,0)
		else if (A_ThisMenuItemPos = 11) ; File & Email
		{
			File := SCW_Win2File(Settings_SavePath)
			Email_AttachFile(File)
		}
		else if (A_ThisMenuItemPos = 12) ; File & Imgur
		{
			File := SCW_Win2File(Settings_SavePath)
			URL := Imgur_Post(File, Settings_Imgur_Client)
			Clipboard := URL
		}
		else if (A_ThisMenuItemPos = 13) ; File, Imgur & Email
		{
			File := SCW_Win2File(Settings_SavePath)
			URL := Imgur_Post(File, Settings_Imgur_Client)
			Email_AttachFile(File, URL)
		}
		
	;~ MsgBox You selected %A_ThisMenuItemPos% - %A_ThisMenuItem% from the menu %A_ThisMenu%.
return

;~ Tray Menu
Help:
	Gui,Help:Show, , Help
return
Settings:
	Gui,Settings:Show, , Settings
return
Suspend:
	Suspend
Return
Reload:
	Reload
Return
Edit:
	Edit
Return
Exit:
	ExitApp
Return
;}

; FUNCTIONS
;{-----------------------------------------------
;
;{vvvvv SCW Functions vvvvv
/*
[module/script] ScreenClip2Win
Author:      Learning one
Thanks:      Tic, HotKeyIt

Creates always on top layered windows from screen clippings. Click in upper right corner to close win. Click and drag to move it.
Uses Gdip.ahk by Tic.

#Include ScreenClip2Win.ahk    						; by Learning one
;=== Short documentation ===
SCW_ScreenClip2Win(SetClipboard:=0) 		; creates always on top window from screen clipping. Click and drag to select area.
SCW_DestroyAllClipWins()								; destroys all screen clipping windows.
SCW_Win2Clipboard(DeleteBorders:=1)			; copies window to clipboard. By default, removes borders. To keep borders, specify "SCW_Win2Clipboard(0)"
SCW_SetUp(Options="")		; you can change some default options in Auto-execute part of script. Syntax: "<option>.<value>"
	StartAfter - module will start to consume GUIs for screen clipping windows after specified GUI number. Default: 80
	MaxGuis - maximum number of screen clipping windows. Default: 6
	BorderAColor - Default: ff6666ff (ARGB format)
	BorderBColor - Default: ffffffff (ARGB format)
	DrawCloseButton - on/off draw "Close Button" on screen clipping windows. Default: 0 (off)
	AutoMonitorWM_LBUTTONDOWN - on/off automatic monitoring of WM_LBUTTONDOWN message. Default: 1 (on)
	SelColor - selection color. Default: Yellow
	SelTrans - selection transparency. Default: 80

	Example:   SCW_SetUp("MaxGuis.30 StartAfter.50 BorderAColor.ff000000 BorderBColor.ffffff00")

;=== Avoid OnMessage(0x201, "WM_LBUTTONDOWN") collision example===
Gui, Show, w200 h200
SCW_SetUp("AutoMonitorWM_LBUTTONDOWN.0")   ; turn off auto monitoring WM_LBUTTONDOWN
OnMessage(0x201, "WM_LBUTTONDOWN")   ; manualy monitor WM_LBUTTONDOWN
Return

^Lbutton::SCW_ScreenClip2Win()   ; click & drag
Esc::ExitApp

#Include Gdip.ahk      ; by Tic
#Include ScreenClip2Win.ahk      ; by Learning one
WM_LBUTTONDOWN() {
	if SCW_LBUTTONDOWN()   ; LBUTTONDOWN on module's screen clipping windows - isolate - it's module's buissines
		return
	else   ; LBUTTONDOWN on other windows created by script
		MsgBox,,, You clicked on script's window not created by this module,1
}
*/

SCW_Version()
{
	return 1.02
}

SCW_DestroyAllClipWins()
{
	MaxGuis := SCW_Reg("MaxGuis"), StartAfter := SCW_Reg("StartAfter")
	Loop, %MaxGuis%
	{
		StartAfter++
		Gui %StartAfter%: Destroy
	}
}

SCW_SetUp(Options="")
{
   if !(Options = "")
	{
		Loop, Parse, Options, %A_Space%
		{
			Field := A_LoopField
			DotPos := InStr(Field, ".")
			if (DotPos = 0)   
				Continue
			var := SubStr(Field, 1, DotPos-1)
			val := SubStr(Field, DotPos+1)
			if var in StartAfter,MaxGuis,AutoMonitorWM_LBUTTONDOWN,DrawCloseButton,BorderAColor,BorderBColor,SelColor,SelTrans
				%var% := val
		}
	}

	SCW_Default(StartAfter,80), SCW_Default(MaxGuis,6)
	SCW_Default(AutoMonitorWM_LBUTTONDOWN,1), SCW_Default(DrawCloseButton,0)
	SCW_Default(BorderAColor,"ff6666ff"), SCW_Default(BorderBColor,"ffffffff")
	SCW_Default(SelColor,"Yellow"), SCW_Default(SelTrans,80)
	SCW_Reg("MaxGuis", MaxGuis), SCW_Reg("StartAfter", StartAfter), SCW_Reg("DrawCloseButton", DrawCloseButton)
	SCW_Reg("BorderAColor", BorderAColor), SCW_Reg("BorderBColor", BorderBColor)
	SCW_Reg("SelColor", SelColor), SCW_Reg("SelTrans",SelTrans)
	SCW_Reg("WasSetUp", 1)
	if AutoMonitorWM_LBUTTONDOWN
		OnMessage(0x201, "SCW_LBUTTONDOWN")
}

SCW_ScreenClip2Win(SetClipboard:=0) 
{
	static c
	if !(SCW_Reg("WasSetUp"))
		SCW_SetUp()
	StartAfter := SCW_Reg("StartAfter"), MaxGuis := SCW_Reg("MaxGuis"), SelColor := SCW_Reg("SelColor"), SelTrans := SCW_Reg("SelTrans")
	c++
	if (c > MaxGuis)
		c := 1
	GuiNum := StartAfter + c
	Area := SCW_SelectAreaMod("g" GuiNum " c" SelColor " t" SelTrans)
	StringSplit, v, Area, |
	if (v3 < 10 and v4 < 10)   ; too small area
		return
	pToken := Gdip_Startup()
	if pToken =
	{
		MsgBox, 64, GDI+ error, GDI+ failed to start. Please ensure you have GDI+ on your system.
		return
	}
	Sleep, 100
	pBitmap := Gdip_BitmapFromScreen(Area)
	if (SetClipboard=1)
		Gdip_SetBitmapToClipboard(pBitmap)
	hwnd := SCW_CreateLayeredWinMod(GuiNum,pBitmap,v1,v2, SCW_Reg("DrawCloseButton"))
	Gdip_DisposeImage(pBitmap)
	Gdip_Shutdown("pToken")
	return hwnd
}

SCW_SelectAreaMod(Options="")
{
	CoordMode, Mouse, Screen
	MouseGetPos, MX, MY
	loop, parse, Options, %A_Space%
	{
		Field := A_LoopField
		FirstChar := SubStr(Field,1,1)
		if FirstChar contains c,t,g,m
		{
			StringTrimLeft, Field, Field, 1
			%FirstChar% := Field
		}
	}
	c := (c = "") ? "Blue" : c, t := (t = "") ? "50" : t, g := (g = "") ? "99" : g
	Gui %g%: Destroy
	Gui %g%: +AlwaysOnTop -caption +Border +ToolWindow +LastFound
	WinSet, Transparent, %t%
	Gui %g%: Color, %c%
	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 %g%: Show, x%X% y%Y% w%w% h%h% NA
	}
	Gui %g%: Destroy
	MouseGetPos, MXend, MYend
	If ( MX > MXend )
		temp := MX, MX := MXend, MXend := temp
	If ( MY > MYend )
		temp := MY, MY := MYend, MYend := temp
	Return MX "|" MY "|" w "|" h
}

SCW_CreateLayeredWinMod(GuiNum,pBitmap,x,y,DrawCloseButton=0)
{
	static CloseButton := 16
	BorderAColor := SCW_Reg("BorderAColor"), BorderBColor := SCW_Reg("BorderBColor")

	Gui %GuiNum%: -Caption +E0x80000 +LastFound +ToolWindow +AlwaysOnTop +OwnDialogs
	Gui %GuiNum%: Show, Na, ScreenClipperWindow
	hwnd := WinExist()

	Width := Gdip_GetImageWidth(pBitmap), Height := Gdip_GetImageHeight(pBitmap)
	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" BorderAColor, 3), pPen2 := Gdip_CreatePen("0x" BorderBColor, 1)
	if DrawCloseButton
	{
		Gdip_DrawRectangle(G, pPen1, 1+Width-CloseButton+3, 1, CloseButton, CloseButton)
		Gdip_DrawRectangle(G, pPen2, 1+Width-CloseButton+3, 1, CloseButton, CloseButton)
	}
	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)
	SCW_Reg("G" GuiNum "#HWND", hwnd)
	SCW_Reg("G" GuiNum "#XClose", Width+6-CloseButton)
	SCW_Reg("G" GuiNum "#YClose", CloseButton)
	Return hwnd
}

SCW_LBUTTONDOWN()
{
	MouseGetPos,,, WinUMID
	WinGetTitle, Title, ahk_id %WinUMID%
	if Title = ScreenClipperWindow
	{
		PostMessage, 0xA1, 2,,, ahk_id %WinUMID%
		KeyWait, Lbutton
		CoordMode, mouse, Relative
		MouseGetPos, x,y
		XClose := SCW_Reg("G" A_Gui "#XClose"), YClose := SCW_Reg("G" A_Gui "#YClose")
		if (x > XClose and y < YClose)
			Gui %A_Gui%: Destroy
		return 1   ; confirm that click was on module's screen clipping windows
	}
}

SCW_Reg(variable, value="")
{
	static
	if (value = "")
		return kxucfp%variable%pqzmdk
	else
		kxucfp%variable%pqzmdk = %value%
}

SCW_Default(ByRef Variable,DefaultValue)
{
	if (Variable="")
		Variable := DefaultValue
}

SCW_Win2Clipboard(DeleteBorders:=1, Hwnd := "")
{
	/*   ;   does not work for layered windows
	ActiveWinID := WinExist("A")
	pBitmap := Gdip_BitmapFromHWND(ActiveWinID)
	Gdip_SetBitmapToClipboard(pBitmap)
	*/
	if !Hwnd
		WinGet, Hwnd, ID, A
	WinGetPos, X, Y, W, H,  ahk_id %Hwnd%
	if DeleteBorders
		X+=3, Y+=3, W-=6, H-=6
	pToken := Gdip_Startup()
	pBitmap := Gdip_BitmapFromScreen(X "|" Y "|" W "|" H)
	Gdip_SetBitmapToClipboard(pBitmap)
	Gdip_Shutdown("pToken")
}

SCW_Win2File(SavePath,Hwnd := "",DeleteBorders:=1) 
{
	IfNotExist, %SavePath%
		FileCreateDir, %SavePath%
	if !Hwnd
		WinGet, Hwnd, ID, A
	WinGetPos, X, Y, W, H,  ahk_id %Hwnd%
	if DeleteBorders
		X+=3, Y+=3, W-=6, H-=6
	pToken := Gdip_Startup()
	pBitmap := Gdip_BitmapFromScreen(X "|" Y "|" W "|" H)
	FormatTime, TimeStamp ,, yyyy_MM_dd @ HH_mm_ss 
	FileName := TimeStamp " (" w "x" h  ").PNG"
	Gdip_SaveBitmapToFile(pBitmap, SavePath FileName)
	Gdip_DisposeImage(pBitmap)
	Gdip_Shutdown("pToken")
	return SavePath FileName
}
;}^^^^^ SCW Functions ^^^^^

Email_AttachFile(File, URL:="")
{
	try
		IsObject(MailItem := ComObjActive("Outlook.Application").CreateItem(olMailItem:=0)) ; Get the Outlook application object if Outlook is open
	catch
		MailItem  := ComObjCreate("Outlook.Application").CreateItem(olMailItem:=0) ; Create if Outlook is not open
	MailItem.BodyFormat := (olFormatHTML:=2)
	;~ MailItem.TO :="[email protected]"
	;~ MailItem.CC :="[email protected]"
	FormatTime, TimeStamp , % RegExReplace(File, "^.*\\|[_ @]|\(.*$"), dddd MMMM d, yyyy h:mm:ss tt
	MailItem.Subject :="Screen shot taken : " (TimeStamp) ; Subject line of email
	HTMLBody := "
		<H2 style='BACKGROUND-COLOR: red'><br></H2> 
		<HTML>Please find attached the screenshot taken on " TimeStamp "<br><br>"
	if URL
		HTMLBody .= "
			<span style='color:black'>The image can also be accessd here: <a href=""" (URL) """>" (URL) "</a>  <br><br></span>"
	HTMLBody .= "</HTML>"
	MailItem.HTMLBody := HTMLBody
	MailItem.Attachments.Add(File)
	MailItem.Display 
}

Imgur_Post(File, Client)
{
	Http := ComObjCreate("WinHttp.WinHttpRequest.5.1")
	Img := ComObjCreate("WIA.ImageFile")
	Img.LoadFile(File)
	/* 	; Converts Image to JPG
	IP := ComObjCreate("WIA.ImageProcess")
	IP.Filters.Add(IP.FilterInfos("Crop").FilterID)
	IP.Filters.Add(IP.FilterInfos("Convert").FilterID)
	IP.Filters(2).Properties("FormatID").Value:="{B96B3CAE-0728-11D3-9D7B-0000F81EF32E}"
	IP.Filters(2).Properties("Quality").Value:=85
	Img := IP.Apply(Img)
	*/
	Data := Img.FileData.BinaryData
	Http.Open("POST","https://api.imgur.com/3/upload")
	Http.SetRequestHeader("Authorization","Client-ID " Client)
	Http.SetRequestHeader("Content-Length",size)
	Http.Send(Data)
	Codes:=Http.ResponseText
	split=":"
	RegExMatch(Codes, "U)link" split "(.*)" Chr(34), Match)
	URL:=RegExReplace(Match1, "\\")
	Return URL
}

Clipboard2Acrobat(SavePathPDF:="")		; Adobe Acrobat must be installed
{
	App := ComObjCreate("AcroExch.App")
	App.Show()
	App.MenuItemExecute("ImageConversion:Clipboard")
	if SavePathPDF
	{
		IfNotExist, %SavePathPDF%
			FileCreateDir, %SavePathPDF%
		FormatTime, TimeStamp ,, yyyy_MM_dd @ HH_mm_ss 
		FileName := TimeStamp ".PDF"
		AVDoc := App.GetActiveDoc()
		PVDoc := AVDoc.GetPDDoc()
		PDSaveIncremental		:= 0x0000   ;/* write changes only */ 
		PDSaveFull						:= 0x0001   ;/* write entire file */ 
		PDSaveCopy					:= 0x0002   ;/* write copy w/o affecting current state */
		PDSaveLinearized			:= 0x0004   ;/* write the file linearized for */
		PDSaveBinaryOK			:= 0x0010   ;/* OK to store binary in file */
		PDSaveCollectGarbage	:= 0x0020   ;/* perform garbage collection on */
		PVDoc.save(PDSaveFull|PDSaveLinearized, SavePathPDF FileName)
	}
}

RelativePath(ByRef Path)
{
	RegExMatch(Path,"^(\.*)\\",M), R := StrLen(M1)
	if (R=1) 
		Path := A_ScriptDir SubStr(Path,R+1)
	else if (R>1)
		Path := SubStr(A_ScriptDir,1,InStr(A_ScriptDir,"\",,0,R-1)) SubStr(Path,R+2)
	return Path
}

GuiDefaultFont() { ; by SKAN (modified by just me)
   VarSetCapacity(LF, szLF := 28 + (A_IsUnicode ? 64 : 32), 0) ; LOGFONT structure
   If DllCall("GetObject", "Ptr", DllCall("GetStockObject", "Int", 17, "Ptr"), "Int", szLF, "Ptr", &LF)
      Return {Name: StrGet(&LF + 28, 32), Size: Round(Abs(NumGet(LF, 0, "Int")) * (72 / A_ScreenDPI), 1)
            , Weight: NumGet(LF, 16, "Int"), Quality: NumGet(LF, 26, "UChar")}
   Return False
}

WaitReleaseAllModifiers() 
{ 
   List := "LWin|RWin|LAlt|RAlt|LControl|RControl|LShift|RShift" 
   while !Done
	{
		Done := true
		Loop Parse, list, | 
			if (GetKeyState(A_LoopField))
				Done := false
	Sleep 50		
	}
} 
FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks
User avatar
Delta Pythagorean
Posts: 627
Joined: 13 Feb 2017, 13:44
Location: Somewhere in the US
Contact:

Re: Screen clipping

27 Oct 2017, 07:24

This is my version of screen clipping:

Code: Select all

/*
	|===============================================================================|
	|	Hotkeys:																	|
	|		Win + LButton:															|
	|			Press and Hold to take an area of the screen you decide the size!	|
	|		Win + Shift + LButton:													|
	|			Press once and create a fullscreen screenshot.						|
	|===============================================================================|
*/

; Depending on where your installation of the GDip functions are...
#Include <GDip>
; #Include %A_ScriptDir%\GDip.ahk
; #Include %A_ScriptDir%\Lib\GDip.ahk

#SingleInstance, Force
#NoEnv

SetWorkingDir, % A_Desktop
OnExit, Exit

If (!pToken := Gdip_Startup()) {
	MsgBox, 48
		, GDIPlus Error
		, GDIPlus failed to start. Please ensure you have GDIPlus in your system.
	ExitApp
}
Return

#+LButton::
	Image := Gdip_Bitmapfromscreen(0 "|" 0 "|" A_ScreenWidth "|" A_ScreenHeight)
	Gdip_Savebitmaptofile(Image, A_Desktop "\" A_MM "-" A_DD "-" A_YYYY "_Fullscreen.png")
	Gdip_DisposeImage(Image)
	Return

#LButton::
	Mouse1 := MouseGetPos()
	SetTimer, Overlay, 10
	KeyWait, LButton
	Mouse2 := MouseGetPos()
	SetTimer, Overlay, off
	Gui, Screen:Hide

	Img			:= {}
	Width		:= (Mouse2.X > Mouse1.X) ? (Mouse2.X - Mouse1.X) : (Mouse1.X - Mouse2.X)
	Height		:= (Mouse2.Y > Mouse1.Y) ? (Mouse2.Y - Mouse1.Y) : (Mouse1.Y - Mouse2.Y)
	Img.x		:= (Mouse2.X > Mouse1.X) ? (Mouse1.X) : (Mouse2.X)
	Img.y		:= (Mouse2.Y > Mouse1.Y) ? (Mouse1.Y) : (Mouse2.Y)
	Image := Gdip_Bitmapfromscreen(Img.X "|" Img.Y "|" Width "|" Height)
	Gdip_Savebitmaptofile(Image, A_Desktop "\" A_MM "-" A_DD "-" A_YYYY ".png")
	Gdip_DisposeImage(Image)
	Width := Height := Img := ""
	Return

Overlay:
	T := MouseGetPos()
	ttx := (T.x > Mouse1.x) ? (Mouse1.x) : (T.x)
	tty := (T.y > Mouse1.y) ? (Mouse1.y) : (T.y)
	ttw := (T.x > Mouse1.x) ? (T.x - Mouse1.x) : (Mouse1.x - T.x)
	tth := (T.y > Mouse1.y) ? (T.y - Mouse1.y) : (Mouse1.y - T.y)
	Gui, Screen:Show, x%ttx% y%tty% w%ttw% h%tth% NA
	If (GetKeyState("Esc")) {
		SetTimer, Overlay, Off
		Exit
	}
	Return

Exit:
	For Index, Gui in ["Screen:"] ; You can set this to as many Gui's that you have, just do: "Name:"
		Gui, % Gui "Destroy"
	Gdip_Shutdown(pToken)
	ExitApp
	Return

MouseGetPos(Options := 3) {
	MouseGetPos, X, Y, Win, Ctrl, % Options
	Return, {X: X, Y: Y, Win: Win, Ctrl: Ctrl}
}
The script should explain most of it...

[AHK]......: v2.0.12 | 64-bit
[OS].......: Windows 11 | 23H2 (OS Build: 22621.3296)
[GITHUB]...: github.com/DelPyth
[PAYPAL]...: paypal.me/DelPyth
[DISCORD]..: tophatcat

need4speed
Posts: 143
Joined: 22 Apr 2016, 06:50

Re: Screen clipping

28 Oct 2017, 16:19

thanks for posting, i prefer though the OP script.
unfortunately changing the screenshot transparency does not work, why?

Code: Select all

^!RButton::
WinGet, currentTransparency, Transparent, A
if (currentTransparency = 150)
{
    WinSet, Transparent, OFF, A
}
else
{
    WinSet, Transparent, 150, A
}
return
User avatar
Delta Pythagorean
Posts: 627
Joined: 13 Feb 2017, 13:44
Location: Somewhere in the US
Contact:

Re: Screen clipping

29 Oct 2017, 15:16

need4speed wrote:thanks for posting, i prefer though the OP script.
unfortunately changing the screenshot transparency does not work, why?
Changing the transparency of the window, doesn't change the transparency of the screenshot. That, you'd have to use GDip to change it.

[AHK]......: v2.0.12 | 64-bit
[OS].......: Windows 11 | 23H2 (OS Build: 22621.3296)
[GITHUB]...: github.com/DelPyth
[PAYPAL]...: paypal.me/DelPyth
[DISCORD]..: tophatcat

euras
Posts: 429
Joined: 05 Nov 2015, 12:56

Re: Screen clipping

03 Nov 2017, 02:50

Joe Glines wrote:@tervon - Thanks for those ideas! I added them to mine (not that clicking in upper right is a pain, but definitely nice to have the other options)

I also added the saving option/function you had (although I might monkey with the date_format of the file)
Cool adds! Thanks!
:dance:
Hi Joe,
I'm trying to adapt your code to my own, but I get a problem. I changed hotkey in your code to Mbutton::SCW_ScreenClip2Win(clip:=1,email:=0) and try to automate those click and drags in another code like:

Code: Select all

MouseClickDrag, Middle, 267, 115, 392, 250, 15
but it doesn't work. If I do it manually, then your code works and clips the screen. If I try to automate that clipping, nothing happens.. Where is the problem?
User avatar
Joe Glines
Posts: 770
Joined: 30 Sep 2013, 20:49
Location: Dallas
Contact:

Re: Screen clipping

03 Nov 2017, 05:47

Hi euras,
Sorry, I'm not understanding what you're doing. What I recommend is you first just change the hotkey and see if it still triggers the rectangle that captures the screen. (I tried "Mbutton::SCW_ScreenClip2Win(clip:=1,email:=0)" and it worked for me. I don't understand what you're doing with MouseClickDrag.
Regards,
Joe
Sign-up for the 🅰️HK Newsletter

ImageImageImageImage:clap:
AHK Tutorials:Web Scraping | | Webservice APIs | AHK and Excel | Chrome | RegEx | Functions
Training: AHK Webinars Courses on AutoHotkey :ugeek:
YouTube

:thumbup: Quick Access Popup, the powerful Windows folders, apps and documents launcher!
euras
Posts: 429
Joined: 05 Nov 2015, 12:56

Re: Screen clipping

03 Nov 2017, 06:17

Joe Glines wrote:Hi euras,
Sorry, I'm not understanding what you're doing. What I recommend is you first just change the hotkey and see if it still triggers the rectangle that captures the screen. (I tried "Mbutton::SCW_ScreenClip2Win(clip:=1,email:=0)" and it worked for me. I don't understand what you're doing with MouseClickDrag.
Regards,
Joe
sorry, I maybe not explained it well. I start your code as a separate code but I have changed activating combination to Mbutton::SCW_ScreenClip2Win(clip:=1,email:=0). If I click & hold middle mouse button and move the mouse, then I select area which I want to clip and get that picture in my Clippboard. So far so good. But then I start another code which has the function MouseClickDrag so I set coordination's which area I want to select and get a picture of that area. But it looks like MouseClickDrag and the real mouse click and drag is not the same thing, because MouseClickDrag does not activate your code. So I get nothing here...
User avatar
Joe Glines
Posts: 770
Joined: 30 Sep 2013, 20:49
Location: Dallas
Contact:

Re: Screen clipping

03 Nov 2017, 06:29

I don't use mouseclickdrag... I'd just research that command and see where people have reported a similar issue.

Did you test your mouseclickdrag w/o using my script? Are you sure you're calling it correctly? Try changing it to left click (and update my script to use left click).
Sign-up for the 🅰️HK Newsletter

ImageImageImageImage:clap:
AHK Tutorials:Web Scraping | | Webservice APIs | AHK and Excel | Chrome | RegEx | Functions
Training: AHK Webinars Courses on AutoHotkey :ugeek:
YouTube

:thumbup: Quick Access Popup, the powerful Windows folders, apps and documents launcher!
euras
Posts: 429
Joined: 05 Nov 2015, 12:56

Re: Screen clipping

03 Nov 2017, 06:48

Joe Glines wrote:I don't use mouseclickdrag... I'd just research that command and see where people have reported a similar issue.

Did you test your mouseclickdrag w/o using my script? Are you sure you're calling it correctly? Try changing it to left click (and update my script to use left click).
I already did it with Left click, but it doesn't work.
Why I'm using MouseClickDrag? Let's say there is a code, which goes to some pages and clip some areas from those pages. Since I know which areas the code should clip, I can automate those mouse movements with MouseClickDrag instead of doing it self. But the problem is that MouseClickDrag command doesn't awake your code...
Maybe I don't know how to awake other command from other code? Right now I just activate both codes (your and mine) and add a line in my code MouseClickDrag, middle. Is it not enough? Should I connect those two codes somehow?
User avatar
Joe Glines
Posts: 770
Joined: 30 Sep 2013, 20:49
Location: Dallas
Contact:

Re: Screen clipping

03 Nov 2017, 07:09

Well, depending how savvy you are, you could "butcher" my code (which is really someone else's) and bypass having the hotkey entirely. I know I saw other scripts that already were set up for this. It is probably a smarter approach than trying to get the two to work together. Basically in your first code you'd just pass the coordinates you want to grab to the screen-clipping code.
Sign-up for the 🅰️HK Newsletter

ImageImageImageImage:clap:
AHK Tutorials:Web Scraping | | Webservice APIs | AHK and Excel | Chrome | RegEx | Functions
Training: AHK Webinars Courses on AutoHotkey :ugeek:
YouTube

:thumbup: Quick Access Popup, the powerful Windows folders, apps and documents launcher!
need4speed
Posts: 143
Joined: 22 Apr 2016, 06:50

Re: Screen clipping

10 Nov 2017, 05:14

Changing the transparency of the window, doesn't change the transparency of the screenshot. That, you'd have to use GDip to change it.
Isn't Gdip already incorporated in the script?

another thing. although border is turned off

Code: Select all

^c::SCW_Win2Clipboard(0)  ; copy selected win to clipboard  Change to (1) if want border
^s:: SCW_Win2File(0)  ;save selected clipping on desktop as timestamp named .png  ; this was submited by tervon
sometimes the border is still visible when copied/saved.

anyone else having this issue? :wave:
and anyone willig to fix the bug? :roll:
User avatar
Joe Glines
Posts: 770
Joined: 30 Sep 2013, 20:49
Location: Dallas
Contact:

Re: Screen clipping

10 Nov 2017, 05:54

In the initial script I created I worked through and found each particular function from GDIP that was used and pulled it into my script. (so the file size was smaller and I didn't have to tell people to include the library).

I think Delta Pythagorean point is that you'd have to make adjustments to the transparency by using GDIP (which means you might need to include the library and make some additional changes).

As for the border being visible- I used to notice that but I believe when you paste it, the border isn't there so I never cared to track it down.
Sign-up for the 🅰️HK Newsletter

ImageImageImageImage:clap:
AHK Tutorials:Web Scraping | | Webservice APIs | AHK and Excel | Chrome | RegEx | Functions
Training: AHK Webinars Courses on AutoHotkey :ugeek:
YouTube

:thumbup: Quick Access Popup, the powerful Windows folders, apps and documents launcher!
need4speed
Posts: 143
Joined: 22 Apr 2016, 06:50

Re: Screen clipping

10 Nov 2017, 17:16

the border is visible sometimes, in this case after saving the file to the desktop then pasting it:
Image
User avatar
FanaticGuru
Posts: 1906
Joined: 30 Sep 2013, 22:25

Re: Screen clipping

10 Nov 2017, 18:54

need4speed wrote:the border is visible sometimes, in this case after saving the file to the desktop then pasting it:
You might try my version. It seems like I remember streamlining some stuff having to do with the border. Been months since I looked at the code through.

FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks
User avatar
tank
Posts: 3122
Joined: 28 Sep 2013, 22:15
Location: CarrolltonTX
Contact:

Re: Screen clipping

15 Nov 2017, 10:16

Very cool, I take dozens of screen clips a day with snippet but will use this instead
We are troubled on every side‚ yet not distressed; we are perplexed‚
but not in despair; Persecuted‚ but not forsaken; cast down‚ but not destroyed;
Telegram is the best way to reach me
https://t.me/ttnnkkrr
If you have forum suggestions please submit a
Check Out WebWriter

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: No registered users and 139 guests