Jump to content

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

AHK Functions :: InCache() - Cache List of Recent Items


  • Please log in to reply
172 replies to this topic
polyethene
  • Members
  • 5519 posts
  • Last active: May 17 2015 06:39 AM
  • Joined: 26 Oct 2012

Can you please explain the difference between Signed and Unsigned Integer. That is one thing I have not been able to figure out

Basically signed integers can have negative values whereas unsigned ones are absolute. Two's compliment explains binary representations, you can use my toBin/toInt functions to experiment: e.g. toInt(111110000001) unsigned = 3969, toInt(111110000001, true) signed = -127.

autohotkey.com/net Site Manager

 

Contact me by email (polyethene at autohotkey.net) or message tidbit


SKAN
  • Administrators
  • 9115 posts
  • Last active:
  • Joined: 26 Dec 2005
Oh! Thanks Titan .. I forgot about that topic completely..

:)

Edit: Thanks for that wikipedia link too .. :D
kWo4Lk1.png

PhiLho
  • Moderators
  • 6850 posts
  • Last active: Jan 02 2012 10:09 PM
  • Joined: 27 Dec 2005
Thanks, Titan, for this starter.
A simple example: 16bit unsigned integers can go from 0 to 65535.
16bit signed integers go from -32768 to 32767 (IIRC).
So a number above 32767 used as signed number is seen as negative. Take its absolute value or cast it to a bigger number, and you have a wrong number!
It can make subtle bugs...
Posted Image vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2")

SKAN
  • Administrators
  • 9115 posts
  • Last active:
  • Joined: 26 Dec 2005

- A simple example: 16bit unsigned integers can go from 0 to 65535.
- 16bit signed integers go from -32768 to 32767 (IIRC).
- a number above 32767 used as signed number is seen as negative.
- Take its absolute value or cast it to a bigger number, and you have a wrong number!


Thats is very clear.. Thanks for the useful info. I know now why I should use UInt .. I was using Int because it was shorter :oops: .
kWo4Lk1.png

Laszlo
  • Moderators
  • 4713 posts
  • Last active: Mar 31 2012 03:17 AM
  • Joined: 14 Feb 2005
An explanation one step deeper: In computers machine words contain a number (n) of bits: b[0], b[1]…b[n-1], which are ON or OFF. When operations are performed on these words, they are interpreted as characters, integers, decimal numbers, addresses, etc. When they are interpreted as integers, there are two common conventions:
- Unsigned integers. Bits are interpreted as 0/1 binary digits: b[i] * 2**i, so a bit sequence of 1010 means 2**3+2**1 = 10. Number = SUM(i=0..n-1, b[i] * 2**i). The smallest unsigned integer is 0, the largest one is 1+2+4+…=2**n - 1.
- Two's complement signed integers. Bits are interpreted as 0/1 binary digits except the leftmost one b[n-1], which is interpreted as a negative binary digit, -b[n-1]*2**(n-1): Number = SUM(i=0..n-2, b[i] * 2**i) - b[n-1] * 2**(n-1). A bit sequence 10001100 interpreted as 8-bit signed number is -2**7 + 2**3 + 2**2 = -128+8+4 = -116. The smallest signed integer is "1000…" = -2**(n-1), the largest one is "0111…" = 2**(n-1) - 1.

When you choose INT or UINT (or CHAR, or FLOAT…), you tell the computer how to interpret the bunch of bits in the corresponding machine word.

SKAN
  • Administrators
  • 9115 posts
  • Last active:
  • Joined: 26 Dec 2005
Thank you Sir. :D
kWo4Lk1.png

SKAN
  • Administrators
  • 9115 posts
  • Last active:
  • Joined: 26 Dec 2005

ChooseColor()

Credit:
This standalone version of ChooseColor is based on majkinetor's work .. Thanks!
Scripts & Functions Topic : ChooseColor wrapper

Also refer:
Ask-for-Help Topic : How to call the standard Choose-Color-dialog? - Micha


ChooseColor() is a 45 line standalone wrapper function to call the built-in windows ChooseColor Dialog. This version of ChooseColor wrapper additionally saves the Custom Colors ( if any selected ) to the default file named CUSTOMCOLOR.BIN into the Windows folder.

Usage : ChooseColor( Color, hPar, ccFile)

[*:26f3nn0f]The function returns the hex color code ( in R G B format ) or -1 if the dialog was cancelled with close button or if escape was pressed.

[*:26f3nn0f]The first parameter may contain a valid color code ( in R G B format ) to initialise a default selected color on the dialog.

[*:26f3nn0f]The dialog opens @ coordinates 0,0 relative to the window ID passed as the parameter 2. If parameter 2 is null/void, Desktop is assumed to be the parent window.

[*:26f3nn0f]Only 16 different colors can be saved as Custom Colors. Parameter 3 facilitates to specify a different filename so that additional Custom color files can be maintained to overcome this limit.MSDN Reference : ChooseColor Function, CHOOSECOLOR Structure

The function along with a test demo:

[color=#808080]Gui, Add, Button, x200 y275 w150 gChangeBG, Change BackGround Color
Gui, Show, , ChooseColor() Demo
Return

ChangeBG:
 GuiColor := ChooseColor( GuiColor, WinExist() )
 IfGreaterOrEqual,GuiColor,0, Gui,Color,%GuiColor%
Return

GuiClose:
 ExitApp
Return[/color]

ChooseColor( Color=0x0, hPar=0x0, ccFile="")  { 
Color := SubStr(Color,1,2)="0x" ? Color : "0x" Color      ; Check & Prefix Color with "0x"
VarSetCapacity(CHOOSECOLOR, 36, 0) , mainPtr := (&CHOOSECOLOR)     ; Create Main structure
DllCall( "RtlFillMemory", UInt,mainPtr+0, Int,1, UChar,36 ) ; Insert size of the Structure

hPar := WinExist(ahk_id %hPar%) ; Validate the passed Parent window ID 
; Break Parent window ID into 4 bytes
H1 := hPar>>0 & 255,   H2 := hPar>>8 & 255,   H3 := hPar>>16 & 255,   H4 := hPar>>24 & 255
Loop 4                       ; Insert Parent window ID to CHOOSECOLOR structure @ Offset 4
  DllCall( "RtlFillMemory", UInt,mainPtr+3+A_Index, Int,1, UChar,H%A_Index% )

; Break Color into R,G and B values
RGB1 := Color>>16 & 255,    RGB2 := Color>>8  & 255,    RGB3 := Color>>0  & 255
Loop 3                      ; Insert R,G and B values to CHOOSECOLOR structure @ Offset 12
  DllCall( "RtlFillMemory", UInt,mainPtr+11+A_Index, Int,1, UChar,RGB%A_Index% )

; CustomColors ( CUS1 will be primary array and CUS2 will be a copy to detect any change )
VarSetCapacity(CUS1, 64, 0),   aPtr := (&CUS1),   VarSetCapacity(CUS2, 64, 0)
IfEqual,ccFile,, SetEnv,ccFile,%A_WinDir%\CUSTOMCOLOR.BIN ; Assign default save filename
IfExist,%ccFile%,  FileRead,CUS1, *m64 %ccFile%           ; Array CUS1 will be overwritten
Loop 64                                                   ; Copy data from CUS1 to CUS2
 oS:=A_Index-1, DllCall( "RtlFillMemory", UInt,&CUS2+oS, Int,1, UChar,*(&CUS1+oS) )
A1 := aPtr>>0 & 255,   A2 := aPtr>>8 & 255,   A3 := aPtr>>16 & 255,   A4 := aPtr>>24 & 255
Loop 4        ; Insert pointer to Custom colors array to CHOOSECOLOR structure @ Offset 16
   DllCall( "RtlFillMemory", UInt,mainPtr+15+A_Index, Int,1, UChar,A%A_Index% )

; Insert Integer 259 @ Offset 21 (259 is CC_RGBINIT + CC_FULLOPEN + CC_ANYCOLOR )
DllCall( "RtlFillMemory", UInt,mainPtr+20, Int,1,UChar,3  ) ; CC_RGBINIT=1 + CC_FULLOPEN=2
DllCall( "RtlFillMemory", UInt,mainPtr+21, Int,1,UChar,1  ) ; CC_ANYCOLOR=256

If ! DllCall("comdlg32\ChooseColorA", str, CHOOSECOLOR) OR errorLevel   ; Call ChooseColor
     Return -1            ; and return -1 in case of an error or if no color was selected.

Loop 64 ; Compare data CUS2 and CUS1, if custom color changed, then save array to BIN file
  If ( *(&CUS1+A_Index-1) != *(&CUS2+A_Index-1) )   {       ; Check byte by byte
       h := DllCall( "_lcreat", Str,ccFile, Int,0 )         ; Overwrite/create file
            DllCall( "_lwrite", UInt,h, Str,CUS1, Int,64 )  ; write the array,
            DllCall( "_lclose", UInt,h )                    ; close the file,
            Break                                           ; break the loop.
                                                    }  
Hex := "123456789ABCDEF0",   RGB := mainPtr+11 
Loop 3 ; Extract nibbles directly from main structure and convert it into Hex (initd. abv)
  HexColorCode .=  SubStr(Hex, (*++RGB >> 4), 1) . SubStr(Hex, (*RGB & 15), 1)
Return HexColorCode ; finally ... phew!
}

Note: This function looks very cryptic as I prefer my functions to be standalone/self-contained. I hope InsertInteger() / ExtractInteger are built into AHK in near future.

:)



** Compact Version ** 2008-06-17

ChooseColor( Clr=0x0, Par=0x0 )  {
 Hex:="123456789ABCDEF0",     VarSetCapacity(CC,36,0), RGB:=&CC+11, VarSetCapacity(D,64,0)
 NumPut(36,CC,0), NumPut(Par,CC,4), NumPut(Clr,CC,12), NumPut(&D,CC,16), NumPut(259,CC,20)
 If ! DllCall( "comdlg32\ChooseColorA", str,CC ) OR ErrorLevel
   Return -1
 Loop 3 
   HexCC .=  SubStr(Hex, (*++RGB >> 4), 1) . SubStr(Hex, (*RGB & 15), 1)
Return HexCC 
}


SKAN
  • Administrators
  • 9115 posts
  • Last active:
  • Joined: 26 Dec 2005

ShellFileOperation() - Basic Wrapper

A shell function that can be called to Copy / Move / Delete / Rename File(s). Displays an animated progress bar if the task is time consuming. Progress bar is useful when operating on slower media like Digital Cameras or when dealing with huge files like video.
I have wrapped only the basic functionality. Hopefully, I should be updating it in near future. For customised needs refer MSDN.

MSDN Reference:
SHFileOperation Function
SHFILEOPSTRUCT Structure
How To Use the Animated Copy Functions in Windows 95/98/Me
Parameters:

The first parameter can be only one of the following:

FO_MOVE   := 0x1
FO_COPY   := 0x2
FO_DELETE := 0x3
FO_RENAME := 0x4

The Second / Third parameters should be strings containing path to Source and Destination
Source may contain Wildcards but not Target
Source can be a list of files delimited with pipe. ( The wrapper function replaces pipe characters with NULL )
Always use fullpath and not relative.

The fouth parameter can contain a combination of values for which please refer MSDN

Usage Examples:

ShellFileOperation( 0x2, A_AhkPath, A_DeskTop )                     ; Copy a Single file
ShellFileOperation( 0x2, "C:\Program Files\AutoHotkey", A_DeskTop ) ; Copy the whole folder

Advanced Example: ( Along with the wrapped Shell Function )

The following code performs a recursive scan on the Temporary Internet Files folder and creates a pipe delimited list of .gif files and passes it to the wrapper function which copies the files to the script's subfolder named Gif_Files.

[color=black]SetBatchLines -1
SetWorkingDir, %A_ScriptDir%

imgPath := A_Temp . "orary Internet Files"
Loop, %imgPath%\*.gif, 0 ,1
     Source .= A_LoopFileLongPath "|"

Target := A_ScriptDir "\Gif_Files"

flags := ( FOF_RENAMEONCOLLISION := 0x8 ) | ( FOF_NOCONFIRMMKDIR := 0x200 ) 

ShellFileOperation( 0x2, Source, Target, flags )

Return[/color]


ShellFileOperation( fileO=0x0, fSource="", fTarget="", flags=0x0, ghwnd=0x0 )     {

 If ( SubStr(fSource,0) != "|" )
      fSource := fSource . "|"

 If ( SubStr(fTarget,0) != "|" )
      fTarget := fTarget . "|"

 fsPtr := &fSource
 Loop, % StrLen(fSource)
  If ( *(fsPtr+(A_Index-1)) = 124 ) 
      DllCall( "RtlFillMemory", UInt, fsPtr+(A_Index-1), Int,1, UChar,0 ) 

 ftPtr := &fTarget
 Loop, % StrLen(fTarget)
  If ( *(ftPtr+(A_Index-1)) = 124 ) 
      DllCall( "RtlFillMemory", UInt, ftPtr+(A_Index-1), Int,1, UChar,0 ) 

 VarSetCapacity( SHFILEOPSTRUCT, 30, 0 )                 ; Encoding SHFILEOPSTRUCT
 NextOffset := NumPut( ghwnd, &SHFILEOPSTRUCT )          ; hWnd of calling GUI
 NextOffset := NumPut( fileO, NextOffset+0    )          ; File operation
 NextOffset := NumPut( fsPtr, NextOffset+0    )          ; Source file / pattern
 NextOffset := NumPut( ftPtr, NextOffset+0    )          ; Target file / folder
 NextOffset := NumPut( flags, NextOffset+0, 0, "Short" ) ; options

 DllCall( "Shell32\SHFileOperationA", UInt,&SHFILEOPSTRUCT )
 
Return NumGet( NextOffset+0 )
}

The flag FOF_NOCONFIRMMKDIR supresses the user confirmation for subfolder creation and FOF_RENAMEONCOLLISION takes care of renaming similar filenames.

FOF_RENAMEONCOLLISION: For example I recursively scanned for README.TXT in drive C: and I had a list of 667 files. When copied to the same folder with FOF_RENAMEONCOLLISION, The 1st file was README.TXT, 2nd file was Copy of README.TXT, 3rd file was Copy (1) of README.TXT ... and the last file was Copy (665) of README.TXT

Note: You see a progress bar only if the above takes more than a couple of seconds

Anybody interested :?:

:)



Laszlo
  • Moderators
  • 4713 posts
  • Last active: Mar 31 2012 03:17 AM
  • Joined: 14 Feb 2005
Really nice!

Anybody interested :?:

Always!

Sean
  • Members
  • 2462 posts
  • Last active: Feb 07 2012 04:00 AM
  • Joined: 12 Feb 2007

Anybody interested :?:

Always!

Ditto!

polyethene
  • Members
  • 5519 posts
  • Last active: May 17 2015 06:39 AM
  • Joined: 26 Oct 2012
I move around a lot of large files in my backup scripts and what annoys me is that a Progess looks too tacky and Gui requires more code so I never bothered to do either, I wait for the script to exit or use MsgBox. Your function makes things much easier and meets all my needs - cancel button, force overwright etc. so thank you.

autohotkey.com/net Site Manager

 

Contact me by email (polyethene at autohotkey.net) or message tidbit


SKAN
  • Administrators
  • 9115 posts
  • Last active:
  • Joined: 26 Dec 2005
Thanks to all! :D

Back in FEB 2005 when I had bought my Digital Camera, I was using DOS BAT to move files from the memory stick. When I googled for a tool that could do BATCH in Windows, first I found the WinBatch ( Not free! ) and then further search led me to AutoHotkey ( I heard about AutoIt only from AHK doc and then from our forum! )

Actually I wanted to go after CopyFileEx / MoveFileEx after the much awaited Callback facility was implemented while ShFileOperation() function was always there :!: :O

ShFileOperation() is a good candidate for StdLib as the basic flags should work even with Windows 95. But documenting it with easy-to-understand language is tougher than the wrapper itself! :(

Thanks again :)

Hardeep
  • Members
  • 87 posts
  • Last active: Sep 12 2007 06:40 PM
  • Joined: 02 Jul 2006
Nice work Skan...very useful contributions 8)

SKAN
  • Administrators
  • 9115 posts
  • Last active:
  • Joined: 26 Dec 2005

Nice work Skan...very useful contributions 8)


Thanks! :D
Nice to hear from you.

:)

Yogui
  • Guests
  • Last active:
  • Joined: --
Hi Skan and Forum,

Thanks for the ShellFileOperation funtion is great!!!

Question:
Is there a way to determinate the ahk_id of the window Coping... Moving... etc in Win Explorer that show the progress before (or after but I rather have it before) ShellFileOperation is called.

At the moment I'm using a Hotkey over the coping... Win and I gt the ID store on ""UNDER_HOTKEY_WIN_ID" on My Script

I like to apply the following subroutine to that win whithout clicking on it:

MCDR_WIN_SET_TRANSPARENCY_ON:

WinSet, Transparent, 200, ahk_id %UNDER_HOTKEY_WIN_ID%

ControlGetPos, CONTROL_0_X, CONTROL_0_Y, CONTROL_0_W, CONTROL_0_H, SysAnimate321, ahk_id %UNDER_HOTKEY_WIN_ID%
ControlGetPos, CONTROL_1_X, CONTROL_1_Y, CONTROL_1_W, CONTROL_1_H, Button1, ahk_id %UNDER_HOTKEY_WIN_ID%

WinGetPos, WIN_X, WIN_Y, WIN_W, WIN_H, ahk_id %UNDER_HOTKEY_WIN_ID%

X0 := CONTROL_0_X - 6
Y0 := CONTROL_0_Y + CONTROL_0_H - 1					;MAY NEED ADJUSTMENTS IF SYS FONT IS NOT DEFAULT ETC

X1 := CONTROL_1_X - X0
Y1 := CONTROL_1_Y + CONTROL_1_H - Y0 + 14

WinSet, Region, %X0%-%Y0% W%X1% H%Y1% R20-20, ahk_id %UNDER_HOTKEY_WIN_ID%

MCRD_WIN_ENABLED = 0

WinGet, MCDR_WIN_GROUP_ID_LIST, list, ahk_group MCDR_WIN_GROUP

Loop, %MCDR_WIN_GROUP_ID_LIST%
	{
	ControlGet, MCRD_WIN_ENABLED, Enabled, , Button1, ahk_id MCDR_WIN_GROUP_ID_LIST%A_Index%
	MCRD_WIN_ENABLED += MCRD_WIN_ENABLED			;ZERO ID DISABLE
	}
	
MCRD_WIN_DISABLED := MCDR_WIN_GROUP_ID_LIST - MCRD_WIN_ENABLED
	
WIN_MOVE_X := MonitorWorkArea1Right - X0 - X1
WIN_MOVE_Y := MonitorWorkArea1Bottom - Y0 - (Y1 * MCRD_WIN_DISABLED) 		;MULT BY THE NUMBER OF MCDR WINS

WinMove, ahk_id %UNDER_HOTKEY_WIN_ID%, , WIN_MOVE_X,  WIN_MOVE_Y

Control, Disable, , Button1, ahk_id %UNDER_HOTKEY_WIN_ID%
WinSet, AlwaysOnTop, On, ahk_id %UNDER_HOTKEY_WIN_ID%

;ListVars
	
Return

Then the hotkey again will check the button is disabled and GoSub,
MCDR_WIN_SET_TRANSPARENCY_OFF:
WinSet, TransColor, Off, ahk_id %UNDER_HOTKEY_WIN_ID%
Control, Enable, , Button1, ahk_id %UNDER_HOTKEY_WIN_ID%
WinSet, Region, , ahk_id %UNDER_HOTKEY_WIN_ID%
WinMove, ahk_id %UNDER_HOTKEY_WIN_ID%, , (A_ScreenWidth/2)-(WIN_W/2), (A_ScreenHeight/2)-(WIN_H/2)
Return

Thanks Again!