AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

AHK Functions - RandStr()
Goto page Previous  1, 2, 3, 4, 5, 6, 7  Next
 
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions
View previous topic :: View next topic  
Author Message
PhiLho



Joined: 27 Dec 2005
Posts: 6721
Location: France (near Paris)

PostPosted: Fri Apr 28, 2006 11:15 am    Post subject: Reply with quote

Edited my first post so the first function follows more my coding rules (these functions will go in my function library) and to post the two others.
Of course, these are only one way to do this, there may be other tricks and al. Laszlo and toralf would remove some braces and pull out some obscure tricks. Smile But they seems to be a good compromise between readability (what is the logic behind the code) and optimization (strings are small, so over-optimizing may be fun, but quite useless).
_________________
vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2")
Back to top
View user's profile Send private message Visit poster's website
SKAN



Joined: 26 Dec 2005
Posts: 5880

PostPosted: Fri Apr 28, 2006 11:38 am    Post subject: Reply with quote

Dear PhiLho, Smile

You wrote:
Edited my first post so the first function follows more my coding rules (these functions will go in my function library) and to post the two others.


Thanks again! Very Happy

My version of CheckHexC() was dependent on the other two functions. Your version is just great!

This following function was posted by Rubberduck
@ http://www.autohotkey.com/forum/viewtopic.php?t=3401

I tried using it and had some errors (which I do not remember / not able to reproduce now).

Code:
;**** Function RGBtoHex(R,G,B)
;**** R,G and B are decimals. Returned is a hex e.g. 0xFFACBC
RGBtoHex(R,G,B)
{
   local Ret_Val
   SetFormat, Integer, Hex
   Ret_Val := (R << 16) | (G << 8) | B
   SetFormat, Integer, D
   return, Ret_Val
}


Do you see any error in it?

Regards, Smile
_________________
SKAN - Suresh Kumar A N


Last edited by SKAN on Fri Apr 28, 2006 10:08 pm; edited 2 times in total
Back to top
View user's profile Send private message
PhiLho



Joined: 27 Dec 2005
Posts: 6721
Location: France (near Paris)

PostPosted: Fri Apr 28, 2006 1:21 pm    Post subject: Reply with quote

No, it is very close of my algorithm, without parsing and without removing the 0x prefix.
Of course, if any value is above 255, you get problems.
And it has a flaw, like my code (which I corrected and updated): for 0, 5, 0, it gives 0x500, no 0x000500.
Please, can you remove your quotes of my code, as I update my original post but the old code is still in your messages? Thank you.
Of course, you are free to post any variation of my code as you feel it.
_________________
vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2")
Back to top
View user's profile Send private message Visit poster's website
SKAN



Joined: 26 Dec 2005
Posts: 5880

PostPosted: Fri Apr 28, 2006 1:42 pm    Post subject: Reply with quote

Dear PhiLho, Smile

You wrote:
it has a flaw, like my code (which I corrected and updated): for 0, 5, 0, it gives 0x500, no 0x000500.


Yes!.. I remember now.. This was it.. I am glad it has been rectified!

Thanks! Very Happy

You wrote:
can you remove your quotes of my code, as I update my original post but the old code is still in your messages?


Sure & have done it ..

Regards, Smile
_________________
SKAN - Suresh Kumar A N
Back to top
View user's profile Send private message
Titan



Joined: 11 Aug 2004
Posts: 5068
Location: imaginationland

PostPosted: Fri Apr 28, 2006 9:53 pm    Post subject: Reply with quote

ownage (req. 1.0.46.x+) :
Code:
MsgBox, % "Example:`n`n" . rgbToHex("255,255,255") . "`n" . HexToRgb("#FFFFFF") . "`n" . CheckHexC("000000")

rgbToHex(s, d = "") {
   StringSplit, s, s, % d = "" ? "," : d
   SetFormat, Integer, % (f := A_FormatInteger) = "D" ? "H" : f
   h := s1 + 0 . s2 + 0 . s3 + 0
   SetFormat, Integer, %f%
   Return, "#" . RegExReplace(RegExReplace(h, "0x(.)(?=$|0x)", "0$1"), "0x")
}

hexToRgb(s, d = "") {
   SetFormat, Integer, % (f := A_FormatInteger) = "H" ? "D" : f
   Loop, % StrLen(s := RegExReplace(s, "^(?:0x|#)")) // 2
      c%A_Index% := 0 + (h := "0x" . SubStr(s, A_Index * 2 - 1, 2))
   SetFormat, Integer, %f%
   Return, c1 . (d := d = "" ? "," : d) . c2 . d . c3
}

CheckHexC(s, d = "") {
   If InStr(s, (d := d = "" ? "," : d))
      e := hexToRgb(rgbToHex(s, d), d) = s
   Else e := rgbToHex(hexToRgb(s)) = (InStr(s, "#") ? "" : "#"
      . RegExReplace(s, "^0x"))
   Return, e
}

_________________

RegExReplace("irc.freenode.net/ahk", "^(?=(.(?=[\0-r\[]*((?<=\.).))))(?:[c-\x73]{2,8}(\S))+((2)|\b[^\2-]){2}\D++$", "$u3$1$3$4$2")


Last edited by Titan on Wed Nov 29, 2006 8:33 pm; edited 1 time in total
Back to top
View user's profile Send private message Visit poster's website
robiandi
Guest





PostPosted: Tue May 02, 2006 12:51 pm    Post subject: Reply with quote

@Goyyah:
Today your string manipulation functions have been very useful for me. Many thanks.
Back to top
SKAN



Joined: 26 Dec 2005
Posts: 5880

PostPosted: Fri Jun 30, 2006 8:07 pm    Post subject: Reply with quote

Quote:
GetBinaryType() - requires Windows NT/2000/XP or later

Usage : GetBinaryType(Filename)

GetBinaryType() determines whether a file is executable, and if so, what type of
executable file it is, and returns one of the following:

    32-Bit
    DOS
    16-Bit
    PIF
    POSIX
    OS2-16-Bit
    64-Bit


    ... and also

    File not found
    Not Executable


MSDN Reference : GetBinaryType function.

I have been using console based ExeType.exe bundled with the Windows 98 ResKit,
and now I have written GUI-ExeType - A Drag & Drop GUI Utility for similar functionality.

GetBinaryType() does not consider the file extension. You might have downloaded a .ZIP file
that does not Unzip. It could be an executable which you can find out by dropping it on GUI-ExeType!.

    Gui-ExeType.ahk - calls GetBinaryType()
Code:
/* ; * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * ;
*                                                                            *
*  Filename: Gui-ExeType.ahk                                                 *
*  Desc    : Drag and Drop GUI for finding the type of an Executable file.   *
*  Author  : A.N.Suresh Kumar aka "Goyyah"                                   *
*  Dated   : 30-Jun-2006 / Last Modifed: 30-Jun-2006                         *
*                                                                            *
*/ ; * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * ;

If A_OSType <> WIN32_NT
   ExitApp

Gui, +AlwaysOnTop +ToolWindow
Gui +LastFound
ID:= WinExist()
Gui, Add, GroupBox, w285 h66, Drag `&& Drop File here %A_Space%
Gui, Font, S10, Verdana
Gui, Add, Text, x15 y27 w75 Right, File name:
Gui, Add, Text, x15 y+1 w75 Right, Exe. type:
Gui, Font, Bold
Gui, Add, Text, x93 y27 vFile w200
Gui, Add, Text, x93 y+1 vType w200
Gui, Show, AutoSize, GUI-ExeType
Return

GuiClose:
GuiEscape:
 ExitApp
Return

GuiDropFiles:
  FileType:=GetBinaryType(A_GuiControlEvent)
  SplitPath, A_GuiControlEvent, FileName
  GuiControl,,File, %FileName%
  GuiControl,,Type, %FileType%
Return

GetBinaryType(FileName) {
  IfNotExist, %FileName%, Return "File not found"
  Binary:=DllCall("GetBinaryType","Str", FileName, "UInt *", RetVal)
  IfEqual, Binary, 0, Return "Not Executable"
  BinaryTypes := "32-Bit|DOS|16-Bit|PIF|POSIX|OS2-16-Bit|64-Bit"
  StringSplit, BinaryType , BinaryTypes, |
  BinaryType:="BinaryType" . RetVal + 1
Return (%BinaryType%)
}




_________________
SKAN - Suresh Kumar A N
Back to top
View user's profile Send private message
SKAN



Joined: 26 Dec 2005
Posts: 5880

PostPosted: Sun Jul 02, 2006 10:11 am    Post subject: GetSysColor() Reply with quote

Quote:
GetSysColor()

Usage : GetSysColor(DisplayElement)

GetSysColor() Retrieves the current color of the specified display element and returns it as RGB Hexcode.
Display elements are the parts of a window and the display that appear on the system display screen.

Edit: Function has been updated - Many thanks to Mr.Laszlo

Code:
GetSysColor(d_element) {
A_FI:=A_FormatInteger
SetFormat, Integer, Hex
BGR:=DllCall("GetSysColor"
 ,Int,d_element)+0x1000000
SetFormat,Integer,%A_FI%
StringMid,R,BGR,8,2
StringMid,G,BGR,6,2
StringMid,B,BGR,4,2
RGB := R G B
StringUpper,RGB,RGB
Return RGB
}


MSDN Reference : GetSysColor function.

Copy / Paste / Try example:

Code:
; Example to simulate a Tooltip Window

InfoText := GetSysColor(23) ; COLOR_INFOTEXT
InfoWind := GetSysColor(24) ; COLOR_INFOBK

Gui, 99:-Caption +ToolWindow +AlwaysOnTop +Border
Gui, 99:Margin, 2, 2
Gui, 99:Color, %InfoWind%
Gui, 99:Add, Text, c%InfoText%, This is a Simulated`nTooltip - Goyyah!

CoordMode, Mouse, Screen
MouseGetPos, X, Y
Gui, 99:Show, x%X% y%Y%, AHK.Simulated.Tooltip.Window

Sleep 4000
ExitApp
Return

GetSysColor(d_element) {
A_FI:=A_FormatInteger
SetFormat, Integer, Hex
BGR:=DllCall("GetSysColor"
 ,Int,d_element)+0x1000000
SetFormat,Integer,%A_FI%
StringMid,R,BGR,8,2
StringMid,G,BGR,6,2
StringMid,B,BGR,4,2
RGB := R G B
StringUpper,RGB,RGB
Return RGB
}




The following are the Parameters to be used with GetSysColor().

Code:


COLOR_3DDKSHADOW = 21       ; Dark shadow for three-dimensional display elements.

COLOR_3DFACE = 15           ; Face color for three-dimensional display elements
                            ; and for dialog box backgrounds.

COLOR_3DHIGHLIGHT = 20      ; Highlight color for three-dimensional display elements
                            ; (for edges facing the light source.)

COLOR_3DHILIGHT = 20        ; Highlight color for three-dimensional display elements
                            ; (for edges facing the light source.)

COLOR_3DLIGHT = 22          ; Light color for three-dimensional display elements
                            ; (for edges facing the light source.)

COLOR_3DSHADOW = 16         ; Shadow color for three-dimensional display elements
                            ; (for edges facing away from the light source).

COLOR_ACTIVEBORDER = 10     ; Active window border.

COLOR_ACTIVECAPTION = 2     ; Active window title bar. Specifies the left side color
                            ; in the color gradient of an active window's title bar
                            ; if the gradient effect is enabled.
                            ; Windows NT and Windows 95: This remark does not apply.

COLOR_APPWORKSPACE = 12     ; Background color of multiple document interface
                            ; (MDI) applications.

COLOR_BACKGROUND = 1        ; Desktop.

COLOR_BTNFACE = 15          ; Face color for three-dimensional display elements and
                            ; for dialog box backgrounds.

COLOR_BTNHIGHLIGHT = 20     ; Highlight color for three-dimensional display elements
                            ; (for edges facing the light source.)

COLOR_BTNHILIGHT = 20       ; Highlight color for three-dimensional display elements
                            ; (for edges facing the light source.)

COLOR_BTNSHADOW = 16        ; Shadow color for three-dimensional display elements
                            ; (for edges facing away from the light source).
COLOR_BTNTEXT = 18          ; Text on push buttons.

COLOR_CAPTIONTEXT = 9       ; Text in caption, size box, and scroll bar arrow box.

COLOR_DESKTOP =  1          ; Desktop.

COLOR_GRADIENTACTIVECAPTION = 27 ; See comment below.

/*
Right side color in the color gradient of an active window's title bar.
COLOR_ACTIVECAPTION specifies the left side color. Use SPI_GETGRADIENTCAPTIONS
with the SystemParametersInfo function to determine whether the gradient effect
is enabled.
Windows NT and Windows 95:  This value is not supported.
*/

COLOR_GRADIENTINACTIVECAPTION = 28 ; See comment below.

/*
Right side color in the color gradient of an inactive window's title bar.
COLOR_INACTIVECAPTION specifies the left side color.
Windows NT and Windows 95:  This value is not supported.
*/

COLOR_GRAYTEXT = 17         ; Grayed (disabled) text. This color is set to 0 if the
                            ; current display driver does not support a solid gray
                            ; color.

COLOR_HIGHLIGHT = 13        ; Item(s) selected in a control.

COLOR_HIGHLIGHTTEXT = 14    ; Text of item(s) selected in a control.

COLOR_HOTLIGHT = 26         ; Color for a hyperlink or hot-tracked item.
                            ; Windows NT & Windows 95: This value is not supported.

COLOR_INACTIVEBORDER = 11   ; Inactive window border.

COLOR_INACTIVECAPTION = 3   ; See comment below.

/*
Inactive window caption. Specifies the left side color in the color gradient
of an inactive window's title bar if the gradient effect is enabled.
Windows NT and Windows 95:  This remark does not apply.
*/
 
COLOR_INACTIVECAPTIONTEXT = 19 ; Color of text in an inactive caption.

COLOR_INFOBK = 24           ; Background color for tooltip controls.

COLOR_INFOTEXT = 23         ; Text color for tooltip controls.

COLOR_MENU = 4              ; Menu background.

COLOR_MENUHILIGHT = 29      ; See comment below.

/*
The color used to highlight menu items when the menu appears as a flat menu.
See SystemParametersInfo.The highlighted menu item is outlined with COLOR_HIGHLIGHT.
Windows 2000/NT & Windows Me/98/95:  This value is not supported.
*/

COLOR_MENUBAR = 30          ; See comment below.

/*
The background color for the menu bar when menus appear as flat menus.
See SystemParametersInfo. However, COLOR_MENU continues to specify the background
color of the menu popup.
Windows 2000/NT and Windows Me/98/95:  This value is not supported.
*/

COLOR_MENUTEXT = 7          ; Text in menus.

COLOR_SCROLLBAR = 0         ; Scroll bar gray area.

COLOR_WINDOW = 5            ; Window background.

COLOR_WINDOWFRAME = 6       ; Window frame.

COLOR_WINDOWTEXT = 8        ; Text in windows.



Edit: 2006-Jul-03 : Chris has already posted













Quote:

Short Version: 2007-Nov-20

Code:
GetSysColor( Dis=1, RGB=1, Pre="" ) {
  Static Hex := "123456789ABCDEF0"
  VarSetCapacity( TMV,4,0 ), NumPut(DllCall( "GetSysColor",Int,Dis), TMV ), Ptr:=&TMV-1
  Loop 3
    V%A_Index% := SubStr(Hex, (*++Ptr >> 4), 1) . SubStr(Hex, (*Ptr & 15), 1)
Return Pre . ( RGB ? V1 V2 V3 : V3 V2 V1 )
}


If RGB is false the returned hex will be BGR
Pre is prefix which maybe "0x" or "#"

_________________
SKAN - Suresh Kumar A N


Last edited by SKAN on Thu Feb 21, 2008 3:30 pm; edited 6 times in total
Back to top
View user's profile Send private message
Laszlo



Joined: 14 Feb 2005
Posts: 4012
Location: Pittsburgh

PostPosted: Sun Jul 02, 2006 3:07 pm    Post subject: Reply with quote

Nice find, as always!
To handle leading 0's, this looks better
Code:
BGR:=DllCall("GetSysColor",Int,d_element)+0x1000000
Back to top
View user's profile Send private message
SKAN



Joined: 26 Dec 2005
Posts: 5880

PostPosted: Sun Jul 02, 2006 5:15 pm    Post subject: Reply with quote

Dear Laszlo, Smile

You wrote:
Nice find, as always!


Thanks ! Very Happy

You wrote:
To handle leading 0's, this looks better
Code:
BGR:=DllCall("GetSysColor",Int,d_element)+0x1000000


Oops! Sad . I did not test the function enough ... Sad
Many thanks for noticing & pointing it out.

I request you to look into the updated code & let me know whether its logically correct.

Regards, Smile
_________________
SKAN - Suresh Kumar A N
Back to top
View user's profile Send private message
Laszlo



Joined: 14 Feb 2005
Posts: 4012
Location: Pittsburgh

PostPosted: Sun Jul 02, 2006 5:42 pm    Post subject: Reply with quote

"BGR=000000%BGR%" inserts 0's in the front. It is OK, but needs an extra instruction to remove 0x first. The easiest is to add 0x1000000 to the dll result (instead of +0). That makes the result always 7 hex digits, and you just cut the digits needed.
Code:
BGR:=DllCall("GetSysColor",Int,d_element)+0x1000000
SetFormat,Integer,%A_FI%
StringMid,R,BGR,8,2
StringMid,G,BGR,6,2
StringMid,B,BGR,4,2


Last edited by Laszlo on Sun Jul 02, 2006 5:49 pm; edited 1 time in total
Back to top
View user's profile Send private message
SKAN



Joined: 26 Dec 2005
Posts: 5880

PostPosted: Sun Jul 02, 2006 5:47 pm    Post subject: Reply with quote

Laszlo wrote:
The easiest is to add 0x1000000 to the dll result (instead of +0). That makes the result always 7 hex digits, and you just cut the digits needed.


But the output color is slightly different ... or Am I wrong?

Please enlighten!

Regards, Smile
_________________
SKAN - Suresh Kumar A N


Last edited by SKAN on Sun Jul 02, 2006 6:11 pm; edited 1 time in total
Back to top
View user's profile Send private message
Laszlo



Joined: 14 Feb 2005
Posts: 4012
Location: Pittsburgh

PostPosted: Sun Jul 02, 2006 5:53 pm    Post subject: Reply with quote

(I sent my post too fast. See edit above. Both versions should be the same, because adding 0x1000000 does not cause a carry.)
Back to top
View user's profile Send private message
SKAN



Joined: 26 Dec 2005
Posts: 5880

PostPosted: Sun Jul 02, 2006 5:53 pm    Post subject: Reply with quote

Laszlo wrote:
(Both versions should be the same, because adding 0x1000000 does not cause a carry.)


I did not change the string position for 3 StringMids and was getting bizarre results Embarassed

Many thanks!

Regards, Smile
_________________
SKAN - Suresh Kumar A N
Back to top
View user's profile Send private message
SKAN



Joined: 26 Dec 2005
Posts: 5880

PostPosted: Tue Jul 25, 2006 10:38 am    Post subject: GetShortPathName() Reply with quote

Quote:
GetShortPathName()

Usage : GetShortPathName(LongPath)

GetShortPathName() is a wrapper for GetShortPathName function available in Kernel32.dll.
This function converts Folder & Filenames to DOS-Styled 8.3 naming convention.

Refer Topic : Reduce paths to dos terminology (like C:\docume~1\) started by Kerry

Edit: Function has been updated - Many thanks to Mr.PhiLho

The Wrapper:


Code:
GetShortPathName(LongPath) {
; http://www.autohotkey.com/forum/viewtopic.php?p=69366#69366
StringLen:=(StrLen(Longpath)+1)
VarSetCapacity(Shortpath,StringLen)
DllCall("GetShortPathName", Str,LongPath
       , Str,ShortPath, Int,StringLen)
Return Shortpath
}


Copy / Paste / Try Example :

Code:
MsgBox, 64, %A_ScriptName%     , % GetShortPathName(A_ScriptFullPath), 3
MsgBox, 64, Windows Directory  , % GetShortPathName(A_Windir)        , 3
MsgBox, 64, Temp Directory     , % GetShortPathName(A_Temp)          , 3
MsgBox, 64, Desktop            , % GetShortPathName(A_Desktop)       , 3
MsgBox, 64, Desktop (All Users), % GetShortPathName(A_DesktopCommon) , 3
MsgBox, 64, AutoHotkey.Exe     , % GetShortPathName(A_AhkPath)       , 3
Return

GetShortPathName(LongPath) {
; http://www.autohotkey.com/forum/viewtopic.php?p=69366#69366
StringLen:=(StrLen(Longpath)+1)
VarSetCapacity(Shortpath,StringLen)
DllCall("GetShortPathName", Str,LongPath
       , Str,ShortPath, Int,StringLen)
Return Shortpath
}



Regards, Smile
_________________
SKAN - Suresh Kumar A N


Last edited by SKAN on Tue Jul 25, 2006 11:41 am; edited 1 time in total
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions All times are GMT
Goto page Previous  1, 2, 3, 4, 5, 6, 7  Next
Page 2 of 7

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group