examples for the titles of files:
2020.08.12 17.36.52.png
2020.12.05 01.46.08.png
2021.01.31 23.20.00.png
examples for the paths:
- each png (from 2020.08.12 00:00:00 to 2020.08.12 23:59:59) should be saved in the folder D:\Images\2020.08.12\
- each png (from 2020.08.13 00:00:00 to 2020.08.13 23:59:59) should be saved in the folder D:\Images\2020.08.13\
and so on
the folder D:\Images\ already exists (the folders like 2020.08.12 should be auto created) (i don't have this multiple auto creation now)
- the auto creation of screen-shots should be auto stopped in 30 minutes of idle period
- (when idle period is over) in 30 seconds: screen-shots should be auto created again
i am planning to add the following code to my main big script (which is the only ahk-script that is constantly running as an exe-file):
(please also read the short comments inside this code: 6 in the beginning and 3 in the end)
Code: Select all
SetTimer, MyAutoScreenShot, 30000 ; for some reason i only get the desired 60 seconds if i write 30000 in this line (and also 30000 in the next line)
SetTimer, MyIdle, 30000
return
MyIdle:
if (A_TimeIdle > 1800000) ; 30 minutes
{
SetTimer, MyAutoScreenShot, off
}
if (A_TimeIdle < 1800000) ; is this number correct here?
{
SetTimer, MyAutoScreenShot, on
}
return
q::run C:\WINDOWS\system32\calc.exe ; this line is only for the test (no need in #persistent)
MyAutoScreenShot:
{ ; is this line necessary?
pToken := Gdip_Startup()
OnExit, my-auto-ss-shutdown
folderPath := "D:\Images\Auto\" ; i tried something like "D:\Images\%A_YYYY%.%A_MM%.%A_DD%\" but unsuccessfully
fileName := A_YYYY "." A_MM "." A_DD " " A_Hour "." A_Min "." A_Sec ".png"
CaptureScreen:
pBitmap := Gdip_BitmapFromScreen()
saveFileTo := folderPath fileName
Gdip_SaveBitmapToFile(pBitmap, saveFileTo)
Gdip_DisposeImage(pBitmap)
return
my-auto-ss-shutdown:
Gdip_Shutdown(pToken)
Exitapp
return
Gdip_BitmapFromScreen(Screen=0, Raster="")
{
if (Screen = 0)
{
Sysget, x, 76
Sysget, y, 77
Sysget, w, 78
Sysget, h, 79
}
else if (SubStr(Screen, 1, 5) = "hwnd:")
{
Screen := SubStr(Screen, 6)
if !WinExist( "ahk_id " Screen)
return -2
WinGetPos,,, w, h, ahk_id %Screen%
x := y := 0
hhdc := GetDCEx(Screen, 3)
}
else if (Screen&1 != "")
{
Sysget, M, Monitor, %Screen%
x := MLeft, y := MTop, w := MRight-MLeft, h := MBottom-MTop
}
else
{
StringSplit, S, Screen, |
x := S1, y := S2, w := S3, h := S4
}
if (x = "") || (y = "") || (w = "") || (h = "")
return -1
chdc := CreateCompatibleDC(), hbm := CreateDIBSection(w, h, chdc), obm := SelectObject(chdc, hbm), hhdc := hhdc ? hhdc : GetDC()
BitBlt(chdc, 0, 0, w, h, hhdc, x, y, Raster)
ReleaseDC(hhdc)
pBitmap := Gdip_CreateBitmapFromHBITMAP(hbm)
SelectObject(chdc, obm), DeleteObject(hbm), DeleteDC(hhdc), DeleteDC(chdc)
return pBitmap
}
Gdip_SaveBitmapToFile(pBitmap, sOutput, Quality=75)
{
SplitPath, sOutput,,, Extension
if Extension not in BMP,DIB,RLE,JPG,JPEG,JPE,JFIF,GIF,TIF,TIFF,PNG
return -1
Extension := "." Extension
DllCall("gdiplus\GdipGetImageEncodersSize", "uint*", nCount, "uint*", nSize)
VarSetCapacity(ci, nSize)
DllCall("gdiplus\GdipGetImageEncoders", "uint", nCount, "uint", nSize, "uint", &ci)
if !(nCount && nSize)
return -2
Loop, %nCount%
{
Location := NumGet(ci, 76*(A_Index-1)+44)
if !A_IsUnicode
{
nSize := DllCall("WideCharToMultiByte", "uint", 0, "uint", 0, "uint", Location, "int", -1, "uint", 0, "int", 0, "uint", 0, "uint", 0)
VarSetCapacity(sString, nSize)
DllCall("WideCharToMultiByte", "uint", 0, "uint", 0, "uint", Location, "int", -1, "str", sString, "int", nSize, "uint", 0, "uint", 0)
if !InStr(sString, "*" Extension)
continue
}
else
{
nSize := DllCall("WideCharToMultiByte", "uint", 0, "uint", 0, "uint", Location, "int", -1, "uint", 0, "int", 0, "uint", 0, "uint", 0)
sString := ""
Loop, %nSize%
sString .= Chr(NumGet(Location+0, 2*(A_Index-1), "char"))
if !InStr(sString, "*" Extension)
continue
}
pCodec := &ci+76*(A_Index-1)
break
}
if !pCodec
return -3
if (Quality != 75)
{
Quality := (Quality < 0) ? 0 : (Quality > 100) ? 100 : Quality
if Extension in .JPG,.JPEG,.JPE,.JFIF
{
DllCall("gdiplus\GdipGetEncoderParameterListSize", "uint", pBitmap, "uint", pCodec, "uint*", nSize)
VarSetCapacity(EncoderParameters, nSize, 0)
DllCall("gdiplus\GdipGetEncoderParameterList", "uint", pBitmap, "uint", pCodec, "uint", nSize, "uint", &EncoderParameters)
Loop, % NumGet(EncoderParameters) ;%
{
if (NumGet(EncoderParameters, (28*(A_Index-1))+20) = 1) && (NumGet(EncoderParameters, (28*(A_Index-1))+24) = 6)
{
p := (28*(A_Index-1))+&EncoderParameters
NumPut(Quality, NumGet(NumPut(4, NumPut(1, p+0)+20)))
break
}
}
}
}
if !A_IsUnicode
{
nSize := DllCall("MultiByteToWideChar", "uint", 0, "uint", 0, "uint", &sOutput, "int", -1, "uint", 0, "int", 0)
VarSetCapacity(wOutput, nSize*2)
DllCall("MultiByteToWideChar", "uint", 0, "uint", 0, "uint", &sOutput, "int", -1, "uint", &wOutput, "int", nSize)
VarSetCapacity(wOutput, -1)
if !VarSetCapacity(wOutput)
return -4
E := DllCall("gdiplus\GdipSaveImageToFile", "uint", pBitmap, "uint", &wOutput, "uint", pCodec, "uint", p ? p : 0)
}
else
E := DllCall("gdiplus\GdipSaveImageToFile", "uint", pBitmap, "uint", &sOutput, "uint", pCodec, "uint", p ? p : 0)
return E ? -5 : 0
}
Gdip_DisposeImage(pBitmap)
{
return DllCall("gdiplus\GdipDisposeImage", "uint", pBitmap)
}
GetDCEx(hwnd, flags=0, hrgnClip=0)
{
return DllCall("GetDCEx", "uint", hwnd, "uint", hrgnClip, "int", flags)
}
CreateCompatibleDC(hdc=0)
{
return DllCall("CreateCompatibleDC", "uint", hdc)
}
CreateDIBSection(w, h, hdc="", bpp=32, ByRef ppvBits=0)
{
hdc2 := hdc ? hdc : GetDC()
VarSetCapacity(bi, 40, 0)
NumPut(w, bi, 4), NumPut(h, bi, 8), NumPut(40, bi, 0), NumPut(1, bi, 12, "ushort"), NumPut(0, bi, 16), NumPut(bpp, bi, 14, "ushort")
hbm := DllCall("CreateDIBSection", "uint" , hdc2, "uint" , &bi, "uint" , 0, "uint*", ppvBits, "uint" , 0, "uint" , 0)
if !hdc
ReleaseDC(hdc2)
return hbm
}
SelectObject(hdc, hgdiobj)
{
return DllCall("SelectObject", "uint", hdc, "uint", hgdiobj)
}
GetDC(hwnd=0)
{
return DllCall("GetDC", "uint", hwnd)
}
BitBlt(ddc, dx, dy, dw, dh, sdc, sx, sy, Raster="")
{
return DllCall("gdi32\BitBlt", "uint", dDC, "int", dx, "int", dy, "int", dw, "int", dh
, "uint", sDC, "int", sx, "int", sy, "uint", Raster ? Raster : 0x00CC0020)
}
ReleaseDC(hdc, hwnd=0)
{
return DllCall("ReleaseDC", "uint", hwnd, "uint", hdc)
}
Gdip_CreateBitmapFromHBITMAP(hBitmap, Palette=0)
{
DllCall("gdiplus\GdipCreateBitmapFromHBITMAP", "uint", hBitmap, "uint", Palette, "uint*", pBitmap)
return pBitmap
}
DeleteObject(hObject)
{
return DllCall("DeleteObject", "uint", hObject)
}
DeleteDC(hdc)
{
return DllCall("DeleteDC", "uint", hdc)
}
Gdip_Startup()
{
if !DllCall("GetModuleHandle", "str", "gdiplus")
DllCall("LoadLibrary", "str", "gdiplus")
VarSetCapacity(si, 16, 0), si := Chr(1)
DllCall("gdiplus\GdiplusStartup", "uint*", pToken, "uint", &si, "uint", 0)
return pToken
}
Gdip_Shutdown(pToken)
{
DllCall("gdiplus\GdiplusShutdown", "uint", pToken)
if hModule := DllCall("GetModuleHandle", "str", "gdiplus")
DllCall("FreeLibrary", "uint", hModule)
return 0
}
return ; is this line necessary?
} ; is this line necessary?
return ; is this line necessary?
the code also cannot create folders (depending on the date) and save the png-files inside folders (which have a corresponding dates in their titles)
i'm also not sure that everything is correct concerning the timers
so if you see any disadvantages (and if you know how to make this code perform all parts of my idea), feel free to post the correct/improved variant