GUISetState("@SW_HIDE") should be GUISetState(@SW_HIDE). Macros shouldn't be encased in quotation marks.
I can't get this to work correctly on Windows 7. I was half way through re-tweaking the code (style wise) but soon realised that a re-code would be better, because looking at it most of the Global variables can be done away with and using the Return command for the functions would be cleaner and easier to manage.
What I had. Do you use SciTE4AutoIt3?
Code: Select all
#AutoIt3Wrapper_Au3Check_Parameters=-d -w 1 -w 2 -w 3 -w- 4 -w 5 -w 6 -w 7
;
; AutoIt Version: 3.0
; Language: English
; Platform: Win9x/NT
; Author: Roland Toth (contact@rolandtoth.hu)
;
; Script Function:
; Converts color string on the clipboard to RGB color format and pastes to Windows' color picker
;
#NoTrayIcon
#include <Array.au3>
#include <WinAPI.au3>
#include <WindowsConstants.au3>
; variables
Global $sProgramName = 'PasteColor', $vColor = ClipGet(), $r, $g, $b, $aArray = 0, $sUserInput = '', $iConfirm = 0
; I use the prefix $ for Global as it makes for easier reading.
Global $hActiveWindow = WinGetHandle('[ACTIVE]', ''), _
$hActiveHandle = ControlGetHandle($hActiveWindow, '', ControlGetFocus($hActiveWindow)), _
$iControlID = _WinAPI_GetDlgCtrlID($hActiveHandle)
; functions
Func _ShowError()
Return MsgBox(17, $sProgramName & ' Error', 'Invalid color!')
EndFunc ;==>_ShowError
Func _ActivateFocus()
If ControlFocus($hActiveWindow, '', $hActiveHandle) = 0 Then
Exit MsgBox(17, $sProgramName & ' Error', 'Cannot find active window!' & @CRLF & @CRLF & 'Exiting...')
EndIf
EndFunc ;==>_ActivateFocus
Func _NextControl()
$iControlID += 1
ControlFocus($hActiveWindow, '', $iControlID)
EndFunc ;==>_NextControl
Func _GetText($iControlID)
Return ControlGetText($hActiveWindow, '', $iControlID)
EndFunc ;==>_GetText
Func _CopyX($iRepeat)
If $iRepeat > 4 And $iConfirm = MsgBox(33, $sProgramName, 'You are about to copy ' & $iRepeat & ' items.' & @CRLF & @CRLF & 'Continue?') = 2 Then
Exit
EndIf
_ActivateFocus()
$aArray = 0
Local $aArray[$iRepeat]
$aArray[0] = _GetText($hActiveHandle)
For $i = 1 To $iRepeat - 1
_NextControl()
$aArray[$i] = _GetText('')
Next
ClipPut(_ArrayToString($aArray, ','))
_ActivateFocus()
Exit
EndFunc ;==>_CopyX
Func _PasteX($sInput)
If Not $sInput = '' Then
$aArray = StringSplit($sInput, ',')
Else
$aArray = StringSplit(ClipGet(), ',')
EndIf
If $aArray[0] > 4 And $iConfirm = MsgBox(33, $sProgramName, 'You are about to paste ' & $aArray[0] & ' items.' & @CRLF & @CRLF & 'Continue?') = 2 Then
Exit
EndIf
_ActivateFocus()
ControlSetText($hActiveWindow, '', $hActiveHandle, $aArray[1])
For $i = 2 To $aArray[0]
_NextControl()
ControlSetText($hActiveWindow, '', '', $aArray[$i])
Next
_ActivateFocus()
Exit
EndFunc ;==>_PasteX
Func _CopyColor()
_ActivateFocus()
$r = _GetText($hActiveHandle)
_NextControl()
$g = _GetText('')
_NextControl()
$b = _GetText('')
If $vColor = 'copy' Or $vColor = 'rgb' Then
$vColor = $r & ', ' & $g & ', ' & $b
ElseIf $vColor = 'hex' Then
_RGBtoHex('', $r, $g, $b)
ElseIf $vColor = 'hexx' Then
_RGBtoHex('#', $r, $g, $b)
EndIf
ClipPut($vColor)
_ActivateFocus()
Exit
EndFunc ;==>_CopyColor
Func _RGBtoHex($prefix, $r, $g, $b)
$vColor = $prefix & Hex($r, 2) & Hex($g, 2) & Hex($b, 2);
EndFunc ;==>_RGBtoHex
Func _SixDigitColor($vVar)
$aArray = StringSplit($vVar, '');
$vColor = ''
For $count = 1 To StringLen($vVar)
$vColor &= $aArray[$count] & $aArray[$count]
Next
EndFunc ;==>_SixDigitColor
Func _SplitRGB($vVar)
$aArray = StringSplit($vVar, ',');
$r = $aArray[1] + 0 ; removing leading zeros
$g = $aArray[2] + 0
$b = $aArray[3] + 0
EndFunc ;==>_SplitRGB
Func _HexToRGB($vVar)
$vVar = '0x' & $vVar
$r = BitAND(BitShift($vVar, 16), 0xFF)
$g = BitAND(BitShift($vVar, 8), 0xFF)
$b = BitAND($vVar, 0xFF)
EndFunc ;==>_HexToRGB
Func _PasteColor($r, $g, $b)
_ActivateFocus()
ControlSetText($hActiveWindow, '', $hActiveHandle, $r)
_NextControl()
ControlSetText($hActiveWindow, '', '', $g)
_NextControl()
ControlSetText($hActiveWindow, '', '', $b)
_ActivateFocus()
EndFunc ;==>_PasteColor
Func _ValidateInput($vVar)
If StringRegExp($vVar, '\b((0x0|0x00){1})?([0-9a-fA-F]){6}\b|^(0*([01]?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])[, \s]\s*){2}(0*([01]?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])([,]{1})?)\s*$', 0) = 0 Then
If ($CmdLine[0] > 0 And $CmdLine[1] = '/silent') Then
_ShowError()
Exit
Else
_ShowError()
_GetInput($sUserInput)
_CleanInput($vColor)
_ValidateInput($vColor)
Return
EndIf
EndIf
; convert color to Hex or RGB
If StringInStr($vVar, ',') > 0 Then
_SplitRGB($vVar)
Else
_HexToRGB($vVar)
EndIf
EndFunc ;==>_ValidateInput
Func _CleanInput($vVar)
$vVar = StringStripWS($vVar, 1)
$vVar = StringStripWS($vVar, 2)
If StringLeft($vVar, 1) = '#' Then
$vVar = StringTrimLeft($vVar, 1)
EndIf
$vVar = StringReplace($vVar, ' ', ',')
$vVar = StringReplace($vVar, ',,', ',')
$vColor = StringReplace($vVar, ',,', ',')
If StringLen($vColor) = 3 And StringInStr($vColor, ',') = 0 And StringIsXDigit($vColor) Then
_SixDigitColor($vColor)
EndIf
EndFunc ;==>_CleanInput
Func _GetInput($vVar)
Local $hWnd = WinGetHandle(AutoItWinGetTitle()) ; Internal AutoIt Window >> http://www.autoitscript.com/forum/topic/133648-autoitwingettitleautoitwinsettitle-an-example-of-usage/
WinSetState($hWnd, '', @SW_SHOW)
WinSetOnTop($hWnd, '', 1)
WinSetState($hWnd, '', @SW_HIDE)
If $CmdLine[0] = 0 Then
$sUserInput = InputBox($sProgramName, 'Enter color:', StringLeft($vVar, 100), ' M100', Default, 132, Default, Default, Default, $hWnd)
; Msgbox(0, 'Color', 'Input is: ' & $vColor)
; Msgbox(0, 'Color', 'Window is: ' & $hActiveWindow)
; Msgbox(0, 'Color', 'Handle is: ' & $hActiveHandle)
; Msgbox(0, 'Color', 'ControlID is: ' & $iControlID)
; Exit
; If an error occurred Exit the application.
If @error Then
Exit
EndIf
$vColor = $sUserInput
EndIf
If $vColor = 'copy' Or $vColor = 'rgb' Or $vColor = 'hex' Or $vColor = 'hexx' Then
_CopyColor()
Return
EndIf
If StringLeft($vColor, 1) = 'c' And StringIsDigit(StringTrimLeft($vColor, 1)) And StringTrimLeft($vColor, 1) > 0 Then
_CopyX(StringTrimLeft($vColor, 1))
Return
EndIf
If StringLeft($vColor, 1) = 'p' Then
_PasteX(StringTrimLeft($vColor, 1))
Return
EndIf
EndFunc ;==>_GetInput
_GetInput($vColor)
_CleanInput($vColor)
_ValidateInput($vColor)
_PasteColor($r, $g, $b)
Exit