Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

[function] Attach 1.1


  • Please log in to reply
62 replies to this topic
DataLife
  • Members
  • 1022 posts
  • Last active: Nov 27 2015 01:09 AM
  • Joined: 27 Apr 2008
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.

#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
Check out my scripts.  (MyIpChanger) (XPSnap) (SavePictureAs) All my scripts are tested on Windows 7, AutoHotkey 32 bit Ansi unless otherwise stated.

majkinetor
  • Moderators
  • 4512 posts
  • Last active: May 20 2019 07:41 AM
  • Joined: 24 May 2006
The image doesn't flicker here. You set up "r2" parameter which is "delayed redraw" - the picture control will get redrawn only when resizing is finished (actually, after 100ms so it can happen before if you resize very slow).

If you omit 2 from "r2", it will flicker.

In the case of "r2" picture doesn't flicker but it does get messed up while resizing.

That is all normal and by design. I don't think there is a way to fix this and its also present in other functions dealing with Attach-like functionality.

BTW, this

Loop, %currentpath%\*.bmp 
 { 
  GuiControl, 1:-Redraw, MyListBox 
  GuiControl, 1:, MyListBox, %A_LoopFilename% 
  GuiControl, 1:+Redraw, MyListBox 
 }

Should be:

GuiControl, 1:-Redraw, MyListBox 
Loop, %currentpath%\*.bmp 
   GuiControl, 1:, MyListBox, %A_LoopFilename% 
GuiControl, 1:+Redraw, MyListBox

Posted Image

DataLife
  • Members
  • 1022 posts
  • Last active: Nov 27 2015 01:09 AM
  • Joined: 27 Apr 2008

The image doesn't flicker here. You set up "r2" parameter which is "delayed redraw" - the picture control will get redrawn only when resizing is finished (actually, after 100ms so it can happen before if you resize very slow).

If you omit 2 from "r2", it will flicker.

In the case of "r2" picture doesn't flicker but it does get messed up while resizing.

That is all normal and by design. I don't think there is a way to fix this and its also present in other functions dealing with Attach-like functionality.

Apparently attach.ahk refreshes the picture even if it has not been resized.

When I choose a new image to display with the arrow keys I get flicker. Is there a way to tell attach.ahk not to refresh if the gui has not changed sizes?

Also, can you take a look my previous post about text not resizing with attach.ahk.

It is the last post here <!-- m -->http://www.autohotke... ... c&start=30<!-- m -->

thanks
DataLife
Check out my scripts.  (MyIpChanger) (XPSnap) (SavePictureAs) All my scripts are tested on Windows 7, AutoHotkey 32 bit Ansi unless otherwise stated.

majkinetor
  • Moderators
  • 4512 posts
  • Last active: May 20 2019 07:41 AM
  • Joined: 24 May 2006

Apparently attach.ahk refreshes the picture even if it has not been resized.


That can't be true. Attach is OnMessage handler for WM_SIZE message.

Since your sample doesn't work here (I don't have anything in the list box nor when I make it so nothing happens when I use arrows). You need to create simple 10 line sample that shows the problem.

Also, can you take a look my previous post about text not resizing with attach.ahk.

That works here. U should change the attach definition to code given bellow (it works with your code nevertheless)
SetAttach:
	;Attach("OnAttach")        ;this is not needed, see docs when its used.
	Attach(Handle1, "w.5 h1/3 r")      ;use r instead of r2. r2 is used when control is flickering (i.e. picture and similar). 
	Attach(Handle2, "p r")
Return

So the code that works here is:


#Include Attach.ahk
	Gui 1: font, s14, MS sans serif
	Gui 1: Add, Text,HwndHandle1 , Attach ahk does not resize text controls
	Gui 1: Add, Button,HwndHandle2, Demo
	Gui 1: +Resize
	Gui 1: show, AutoSize, Need to resize text
		
	Attach(Handle1, "w.5 h1/3 r")
	Attach(Handle2, "p r")
Return

I hope that you are aware that you must also resize window down, not only left (so that Button doesn't cover the Text control).

BTW, latest Attach is always in the Forms framework.
Posted Image

DataLife
  • Members
  • 1022 posts
  • Last active: Nov 27 2015 01:09 AM
  • Joined: 27 Apr 2008
thanks majkinetor for helping me.

I will work on a short script to demonstrate the flicker issue and test it on more then 1 PC.

What version of windows are you using?

I am using Winxp, autohotkey_L 1.0.92.00 and attach.ahk 1.1

The following code demonstrates what I want Attach.ahk to do with text controls.

This works great but I can not figure out how to get the text to resize when the height and width are both adjusted.

;#Include Attach.ahk
   Gui 1: font, s42, MS sans serif
   Gui 1: Add, Text,HwndHandle1 , Change height of Gui - Can Attach.ahk do this
   Gui 1: font, s12, MS sans serif
   Gui 1: Add, Button,HwndHandle2, Demo
   Gui 1: +Resize
   Gui 1: show, AutoSize, Need to resize text
 ;Attach(Handle1, "w.5 h1/3 r")
 ;Attach(Handle2, "p r")
Return
GuiSize:
WinGetPos,,,w,h,Need to resize text
fontsize := (h * .10 )
Gui 1: font, s%Fontsize%,
GuiControl,1: font,static1
return
Thanks
DataLife
Check out my scripts.  (MyIpChanger) (XPSnap) (SavePictureAs) All my scripts are tested on Windows 7, AutoHotkey 32 bit Ansi unless otherwise stated.

majkinetor
  • Moderators
  • 4512 posts
  • Last active: May 20 2019 07:41 AM
  • Joined: 24 May 2006
No.
Attach can't do that.
That includes changing the font size and its not in the domain of control dimensions.
Posted Image

DataLife
  • Members
  • 1022 posts
  • Last active: Nov 27 2015 01:09 AM
  • Joined: 27 Apr 2008
Attach(Handle1, "p r2") works very good to eliminate image flicker when manually resizing the Gui. But there still is image flicker when clicking on a listbox item or scrolling thru the images using the arrow keys. Mostly seen in WinXp.

Attach(Handle1, "p r") almost completely eliminates image flicker while scrollling the images, but causes very bad image flicker when manually resizing the Gui. Also, mostly seen WinXp.

The question is how to eliminate image flicker while scrolling the images and when resizing the Gui manually.

The following code works very good in WinXp and Vista. Although, there was not much flicker in Vista to begin with.

#Include Attach.ahk
Gui 1: Add, Picture,HwndHandle1 x430 y30 vPicture
Gui 1: Add, ListBox,HwndHandle2 vmylistbox gPictureScroll x16  y30 w375 h650 Hscroll1300 Sort
Gui 1: +resize
Gui 1: show, w800 h600,ListBox
loop, c:\Windows\*.bmp               ;winxp wallpaper
  GuiControl, 1:, ListBox1, %A_LoopFileLongPath%
Loop, c:\Windows\Web\Wallpaper\*.jpg ;vista wallpaper
  GuiControl, 1:, ListBox1, %A_LoopFileLongPath%
Attach(Handle1, "p r2")    ;---------start with "p r2"-------------
Attach(Handle2, "p r2")
Return
PictureScroll:
Attach(Handle1, "p r")  ;---------change to "p r"-------only while scrolling
Guicontrolget, MyListBox, 
GuiControl, 1:Hide, static1,
GuiControl,1:,Picture, *w300 *h-1 %MyListbox%
GuiControl, 1:Show, static1,
Attach() ;reset attach 
Attach(Handle1, "p r2") ;-------reset back to "p r2"----to be able to resize Gui without image flicker
return

Based on the way attach.ahk is written does it make sense that the image flicker is greatly reduced this way?

DataLife
Check out my scripts.  (MyIpChanger) (XPSnap) (SavePictureAs) All my scripts are tested on Windows 7, AutoHotkey 32 bit Ansi unless otherwise stated.

majkinetor
  • Moderators
  • 4512 posts
  • Last active: May 20 2019 07:41 AM
  • Joined: 24 May 2006
Your code doesn't flicker here when resizing.
It does flicker when changing the image but that has nothing to do with Attach - it flickers the same when I remove it.

"r" will flicker more in any case. The only difference between r and r2 is that r redraws Asap, while r2 redraws after 100ms.

To avoid flicker, you should create 2 picture controls, one hidden in the background. Then when you load picture you should load it in the hidden control, and switch the controls.
Posted Image

DataLife
  • Members
  • 1022 posts
  • Last active: Nov 27 2015 01:09 AM
  • Joined: 27 Apr 2008

No.
Attach can't do that.
That includes changing the font size and its not in the domain of control dimensions.


I used changing font size because that was the only way I could demostrate what I wanted the text to do.

Here is an example by HotKeyIt that changes the size of the text with GuiControl, Move


56Width:=A_ScreenWidth -300
56Height:=A_ScreenHeight -100
56Font1:=A_ScreenWidth/4
56Font2:=A_ScreenWidth/15
MoreTextYPosition := A_ScreenHeight - 200
Gui 56: font, s%56Font1% c000066 , Arial
Gui 56: Color, White
Gui 56: +Caption +AlwaysOnTop +center +Resize
Gui 56: Add, Text, x0 w%56Width% y0 h%56Height% center hwndAText, Text
Gui 56: Font, s%56Font2%
Gui 56: Add, Text, x0 w%56Width% y500 h%MoreTextYPosition% center hwndBText, More Text
Gui 56: Show, , Resize Me

56GuiSize:
56Font1:=A_GuiWidth/6
Gui 56: font, s%56Font1%
GuiControl, Move, Static1, x0 w%A_GuiWidth% y0 center
GuiControl, Font, Static1
GuiControlGet, Static1, Pos
56Height := Static1W / 2
If A_GuiWidth>A_GuiHeight/1.5
   56Font2:=A_GuiWidth/10
else
   56Font2:=A_GuiHeight/10
MoreTextYPosition := A_GuiHeight/1.4
Gui 56: font, s%56Font2%
GuiControl, Move, Static2, x0 w%A_GuiWidth% y%MoreTextYPosition% center
GuiControl, Font, Static2

Gui 56: Show, , Resize Me
Return
56GuiClose:
ExitApp
Found at http://www.autohotke...ght=resize text
Can Attach.ahk resize the text control similar to the way HotKeyIt done it?

DataLife
Check out my scripts.  (MyIpChanger) (XPSnap) (SavePictureAs) All my scripts are tested on Windows 7, AutoHotkey 32 bit Ansi unless otherwise stated.

majkinetor
  • Moderators
  • 4512 posts
  • Last active: May 20 2019 07:41 AM
  • Joined: 24 May 2006
Again, that functionality although useful, is not part of Attach's domain.

However, you can implement above code in Forms framework like this:

_()
	hForm := Form_New("m5.5 Font='s16, Arial' +Resize w500 h200")
	hText := Form_Add(hForm, "Text", "AutoHotkey Community Forum Index", "center", "Align T", "Attach p")  ;use Align to position controls on top of each other
	hEdit := Form_Add(hForm, "Edit", "dummy", "r2", "Align F", "Attach p")
	
	Attach("OnAttach")

	Form_Show()
return

OnAttach() {
	global 
	fontSize := Win_Get(hText, "Rh") // 2   ;get the Rectangle Height of the hText control
	fontDef  = s%fontSize%, Arial
	
	oldFont1 := Font(hText, fontDef )
	DllCall("DeleteObject", "Uint", oldFont1)

/*
	oldFont2 := Font(hEdit, fontDef)
	DllCall("DeleteObject", "Uint", oldFont2)

	;This doesn't work right, font is quickly returned back.
	;When DeleteObject is not used, memory leak is happening.
	;Didn't look into it.
*/
}

#include _Forms.ahk

It doesn't have to be forms framework tho, I just CBB to write all equivalent AHK code. If you don't want to use Forms, you can at least get Font module to make font changing simple.
Posted Image

DataLife
  • Members
  • 1022 posts
  • Last active: Nov 27 2015 01:09 AM
  • Joined: 27 Apr 2008
thanks. I will look into your suggestions.
Check out my scripts.  (MyIpChanger) (XPSnap) (SavePictureAs) All my scripts are tested on Windows 7, AutoHotkey 32 bit Ansi unless otherwise stated.

majkinetor
  • Moderators
  • 4512 posts
  • Last active: May 20 2019 07:41 AM
  • Joined: 24 May 2006
BTW, none of the mainstream anchoring libraries like Attach (Visual Studio, Delphi etc...) don't support font resizing, only control resizing. Its up to control to handle its internals once it receives WM_SIZE message. The best variant, IMO, is to subclass Text control and implement WM_SIZE procedure (using Win_Subclass function). Then you don't have to think about this and you could just add the control normally and connect it to Attach.
Posted Image

DataLife
  • Members
  • 1022 posts
  • Last active: Nov 27 2015 01:09 AM
  • Joined: 27 Apr 2008
majkinetor,
I would like to get your permission to include attach.ahk in my source code.

I am currently using #include attach.ahk but want to make my script as easy as possible to use.

It would be much easier if I included attach.ahk at the bottom of my source code and notate Attach.ahk by majkinetor.

Can I get your permission to do that?
thanks
DataLife
Check out my scripts.  (MyIpChanger) (XPSnap) (SavePictureAs) All my scripts are tested on Windows 7, AutoHotkey 32 bit Ansi unless otherwise stated.

majkinetor
  • Moderators
  • 4512 posts
  • Last active: May 20 2019 07:41 AM
  • Joined: 24 May 2006
sure
Posted Image

DataLife
  • Members
  • 1022 posts
  • Last active: Nov 27 2015 01:09 AM
  • Joined: 27 Apr 2008

sure

thanks
DataLife
Check out my scripts.  (MyIpChanger) (XPSnap) (SavePictureAs) All my scripts are tested on Windows 7, AutoHotkey 32 bit Ansi unless otherwise stated.