-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.ahk
127 lines (103 loc) · 3.64 KB
/
main.ahk
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#NoEnv
SendMode Input
SetWorkingDir %A_ScriptDir%
#SingleInstance force
#MaxThreadsPerHotkey 2
#Include Gdip.ahk
#Include WinGetPosEx.ahk
#Include utility.ahk
; ========================================
; start gdip
gdipToken := Gdip_Startup()
OnExit, gdipShutdown
; read settings
settingsFile := A_ScriptDir . "\settings.ini"
defaultFolderPath := A_ScriptDir . "\screenshots\"
bindHotkey := ReadSettings("bindHotkey", "^F11")
folderPath := ReadSettings("outputFolderPath", defaultFolderPath)
filenameFormat := ReadSettings("filenameFormat", "yyyyMMdd_HHmmss")
imageFormat := ReadSettings("imageFormat", "png")
imageQuality := ReadSettings("imageQuality", 95)
includeBorder := ReadSettings("includeWindowBorder", 0)
shutterSoundPath := ReadSettings("shutterSoundPath", "")
postProcess := ReadSettings("postProcess", "")
; fix the folder path
folderPath := NeutralizePathLastSlash(folderPath)
isFolderPathRelative := DllCall("shlwapi\PathIsRelative", "str", folderPath)
If (isFolderPathRelative) {
folderPath := RelToAbs(A_ScriptDir, folderPath)
folderPath := NeutralizePathLastSlash(folderPath)
}
; check if folder is missing
If (!InStr(FileExist(folderPath), "D")) {
; if its default folder, lets create it
If (folderPath == defaultFolderPath) {
FileCreateDir, %defaultFolderPath%
}
Else {
MsgBox, Please create the output folder, or the images will not be created.
}
}
; ========================================
mainRoutine:
Hotkey, %bindHotkey%, gdipScreenshot
Return
gdipScreenshot:
; get active window stats
currentWindow := WinExist("A")
WinGetPosEx(currentWindow, winPosX, winPosY, winWidth, winHeight)
GetClientSize(currentWindow, clientWidth, clientHeight)
If (includeBorder == 0) {
borderSize := (winWidth - clientWidth) / 2
borderSize := Floor(borderSize)
If (borderSize < 0) {
borderSize := 0
}
titleSize := (winHeight - clientHeight) - (2 * borderSize)
titleSize := Floor(titleSize)
If (titleSize < 0) {
titleSize := 0
}
winPosX := winPosX + borderSize
winPosY := winPosY + borderSize + titleSize
winWidth := clientWidth
winHeight := clientHeight
}
Else {
If (winPosX < 0) {
winWidth := winWidth + (winPosX * 2)
winPosX := 0
}
If (winPosY < 0) {
winHeight := winHeight + (winPosY * 2)
winPosY := 0
}
}
; prepare position
gdipPos := JoinArray([winPosX, winPosY, winWidth, winHeight], "|")
; prepare path
FormatTime, currentFilenameFormat, A_Now, %filenameFormat%
currentFilename := currentFilenameFormat . "." . imageFormat
savePath := folderPath . currentFilename
; let the black magic run
gdipImage := Gdip_BitmapFromScreen(gdipPos)
Gdip_SaveBitmapToFile(gdipImage, savePath, imageQuality)
Gdip_DisposeImage(gdipImage)
If (postProcess != "") {
; post process with variables
postProcess := StrReplace(postProcess, "{$imgDir}", folderPath)
postProcess := StrReplace(postProcess, "{$imgName}", currentFilename)
postProcess := StrReplace(postProcess, "{$imgFullPath}", savePath)
postProcess := StrReplace(postProcess, "{$imgBasename}", currentFilenameFormat)
postProcess := StrReplace(postProcess, "{$imgExt}", imageFormat)
Run, %comspec% /c %postProcess%, , Hide
}
; play the shutter sound
If (FileExist(shutterSoundPath)) {
SoundPlay, %shutterSoundPath%
}
Return
gdipShutdown:
Gdip_Shutdown(gdipToken)
ExitApp
Return