Attach.ahk is causing my images to flicker. The following code is fully functional. Press the up and down arrow keys to view the images. On some of the images after it is loaded it flickers.
Comment out Gosub SetAttach and there is absolutely no flicker.
The image also flickers while the window is being resized manually with the mouse.
Code:
#Include Attach.ahk
RegRead, A_PicturesSF, HKCU, Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders, My Pictures
currentpath = %a_picturesSF%
;currentpath = c:\windows\system32
Gui 1: Font, s10,
Gui 1: Add, Text,HwndHandle1 x460 y10,Attach.ahk is causing the image to flicker.`nComment out Gosub SetAttach and the flicker is gone.
Gui 1: Add, Picture ,HwndHandle2 x430 y70 vPicture
Gui 1: Add, ListBox,HwndHandle3 vmylistbox gLoadPictureWithDelay x16 y30 w375 h650 Hscroll1300 Sort
Gui 1: +resize
Gui 1: show, w914 h710,ListBox
winget,PMID,ID,ListBox
Gosub PopulateListbox
Control, choose , 1, listbox1, ahk_id %PMID%
Gosub SetAttach
Return
SetAttach:
Attach("OnAttach")
RT = p r2
Attach(Handle1, RT)
Attach(Handle2, RT)
Attach(Handle3, RT)
Return
PopulateListbox:
Loop, %currentpath%\*.bmp
{
GuiControl, 1:-Redraw, MyListBox
GuiControl, 1:, MyListBox, %A_LoopFilename%
GuiControl, 1:+Redraw, MyListBox
}
return
LoadPictureWithDelay:
SetTimer, PictureScroll, -50
Return
PictureScroll:
Guicontrolget, MyListBox, 1:
SplitPath,MyListbox,,,CheckExt
if (CheckExt = "jpg" or CheckExt = "bmp" or CheckExt = "tif" or CheckExt = "ico" or CheckExt = "cur" or CheckExt = "ani" or CheckExt = "png" or CheckExt = "wmf" or CheckExt = "emf" or CheckExt = "gif")
{
mylistbox1 = %currentpath%\%MyListbox%
ImageData_Load(ImageData, MyListbox1)
ImageData_GetSize(ImageData, Width, Height)
;thanks to Razlin for suggestions from http://www.autohotkey.com/forum/viewtopic.php?p=215638#215638 for the following code
GuiControl, 1:Hide, static2,
if (Width < 493 and Height < 493) ;position picture properly
{
xvar := (493-width)/2+405
yvar := (493-height)/2+95
GuiControl,1:,Picture, *w%width% *h%height% %currentpath%\%MyListbox%
ControlMove, static2, %xvar%, %yvar%, , , ahk_id %PMID%
}
Else
{
if Width = %Height%
{
GuiControl,1:,Picture, *w493 *h-1 %currentpath%\%MyListbox%
GuiControlGet, Pic, 1:Pos, static2
xvar := (493-picW)/2+407
hvar := (493-pich)/2+100
ControlMove, static2, %xvar%, %hvar%, , , ahk_id %PMID%
}
if Width > %Height%
{
GuiControl,1:,Picture, *w493 *h-1 %currentpath%\%MyListbox%
GuiControlGet, Pic, 1:Pos, static2
xvar := (493-picW)/2+407
hvar := (493-pich)/2+100
ControlMove, static2, %xvar%, %hvar%, , , ahk_id %PMID%
}
if Width < %Height%
GuiControl,1:,Picture, *w-1 *h493 %currentpath%\%MyListbox%
GuiControlGet, Pic, 1:Pos, static2
if picw < 493
{
xvar := (493-picW)/2+407
hvar := (493-pich)/2+100
ControlMove, static2, %xvar%, %hvar%, , , ahk_id %PMID%
}
GuiControl, 1:show, static2,
}
}
Attach(PMID) ;reset attach
return
;thanks to Lexikos, following code found at http://www.autohotkey.com/forum/topic28334.html&highlight=imagedataload+byref+imagedata+imagefile
ImageData_Load(ByRef ImageData, ImageFile)
{
static PixelFormat32bppARGB = 0x26200a
, ImageLockModeRead = 0x1
, ImageLockModeUserInputBuf = 0x4
; Initialize GDI+. Doing this here rather than at script
; startup is more convenient, at the cost of ~15ms.
VarSetCapacity(GdiplusStartupInput, 16, 0), NumPut(1, GdiplusStartupInput)
GdiplusModule := DllCall("LoadLibrary", "str", "Gdiplus")
if GdiplusModule = 0
return false, ErrorLevel:="GDIPLUS NOT FOUND"
r := DllCall("Gdiplus\GdiplusStartup", "uint*", GdipToken, "uint", &GdiplusStartupInput, "uint", 0)
if r != 0
return false, ErrorLevel:=r
; Convert the filename to a unicode string.
VarSetCapacity(wImageFile, StrLen(ImageFile)*2+1)
DllCall("MultiByteToWideChar", "uint", 0, "uint", 0, "str", ImageFile, "int", -1, "uint", &wImageFile, "int", StrLen(ImageFile)+1)
; Load the image.
r := DllCall("Gdiplus\GdipCreateBitmapFromFile", "uint", &wImageFile, "uint*", bitmap)
if r != 0
return false, ErrorLevel:=r
; Get the image's size.
DllCall("Gdiplus\GdipGetImageWidth", "uint", bitmap, "uint*", width)
DllCall("Gdiplus\GdipGetImageHeight", "uint", bitmap, "uint*", height)
; Make room for a BitmapData structure and the image data.
VarSetCapacity(ImageData, 24 + width * height * 4, 0)
; Fill the BitmapData structure with details of the desired image format.
NumPut(width, ImageData, 0, "UInt")
NumPut(height, ImageData, 4, "UInt")
NumPut(width * 4, ImageData, 8, "Int") ; Stride
NumPut(PixelFormat32bppARGB, ImageData, 12, "Int") ; PixelFormat
NumPut(&ImageData + 24, ImageData, 16, "UInt") ; Scan0
; Rect specifies the image region to lock.
VarSetCapacity(rect, 16, 0)
NumPut(width, rect, 8)
NumPut(height, rect, 12)
; Lock the image and fill ImageData.
r := DllCall("Gdiplus\GdipBitmapLockBits"
, "uint", bitmap
, "uint", &rect
, "uint", ImageLockModeRead | ImageLockModeUserInputBuf
, "int", PixelFormat32bppARGB
, "uint", &ImageData)
if r = 0 ; Status.Ok ; "LockBits and UnlockBits must be used as a pair."
DllCall("Gdiplus\GdipBitmapUnlockBits", "uint", bitmap, "uint", &ImageData)
; Delete the bitmap (image in memory).
DllCall("Gdiplus\GdipDisposeImage", "uint", bitmap)
; Uninitialize GDI+.
DllCall("Gdiplus\GdiplusShutdown", "uint", GdipToken)
DllCall("FreeLibrary", "uint", GdiplusModule)
return r=0, ErrorLevel:=r
}
ImageData_GetSize(ByRef ImageData, ByRef Width, ByRef Height)
{
Width := NumGet(ImageData, 0)
Height := NumGet(ImageData, 4)
}
return
GuiEscape:
GuiClose:
exitapp
Any ideas on how to use Attach.ahk and avoid the flicker?
thanks
DataLife