Multicolor text Topic is solved

Get help with using AutoHotkey and its commands and hotkeys
Pilu
Posts: 49
Joined: 22 Jun 2018, 01:13

Multicolor text

03 Feb 2020, 10:06

Hi,

how can i coloring the words in the parts of sentence?
Is it passible?

I would like this : THIS is a test

Code: Select all

text1 := "This" ;<--Blue
text2 := " is"     ;<--Black
text3 := " a"     ;<--red
text4 := " test" ;<--green

All := text1 text2 text3 text4

Gui, add, Text, x2 y10 w100 h20, % all
Gui show, AutoSize
return
cefay
Posts: 56
Joined: 26 Jan 2020, 12:53

Re: Multicolor text

03 Feb 2020, 10:12

Code: Select all

Gui, Add, Text, Section cBlue, Blue
Gui, Add, Text, Section ys cGreen, Green
Gui, Add, Text, Section ys cRed, Red
Gui, Show,, Colours
User avatar
boiler
Posts: 4668
Joined: 21 Dec 2014, 02:44

Re: Multicolor text

03 Feb 2020, 10:13

text1 := "This" ;<--Blue
text2 := " is" ;<--Black
text3 := " a" ;<--red
text4 := " test" ;<--green
colors := ["Blue", "Black", "Red", "Green"]

loop, 4
{
Gui, Font, % "c" colors[A_Index]
Gui, Add, Text, % (A_Index = 1 ? "x2" : "x+1") . " y10 h20", % text%A_Index%
}
Gui show, AutoSize
return
scriptor2016
Posts: 634
Joined: 21 Dec 2015, 02:34

Re: Multicolor text

03 Feb 2020, 10:23

Code: Select all

gui, font, s20
gui, add, text, cRed x15 y0, T
gui, add, text, cBlue x30 y0, E
gui, add, text, cLime x45 y0, X
gui, add, text, cGreen x62 y0, T
gui, show, x200 y200 w200 h100, MyGUI
Return
cefay
Posts: 56
Joined: 26 Jan 2020, 12:53

Re: Multicolor text

03 Feb 2020, 10:26

@scriptor2016 I thing your idea is really bad even if we use monospaced font
User avatar
SpeedMaster
Posts: 379
Joined: 12 Nov 2016, 16:09

Re: Multicolor text  Topic is solved

05 Feb 2020, 05:58

Use monospaced font + colored layers + function sliceword() 8-)

Code: Select all

gui, font, s12, consolas

var:="This is a test.`nHow can i coloring the words in the parts of sentence ?"

gui, add, text, w500 h60  , % var
gui, add, text, xp yp wp hp  cred backgroundtrans ,  % SliceWord(var, " a | can |words |of ")
gui, add, text, xp yp wp hp cblue backgroundtrans ,  % SliceWord(var, "This | i | in |the")
gui, add, text, xp yp wp hp  cgreen backgroundtrans ,  % SliceWord(var, " test.|How |coloring |parts")
gui, add, text, xp yp wp hp cpurple backgroundtrans ,  % SliceWord(var, "sentence")

gui, show,  h200
return

guiclose:
~esc::
exitapp
return

SliceWord(text, words) {
X:=1, output := ""
if (text) && (!words)
return regexreplace(text, "(?!\v).", " ")

while (X := RegExMatch(text, "(.*?)(" words "|\v)", M, X+StrLen(M)))
	output .= Spaces(StrLen(M1)) M2
return output
}

Spaces(x)
{
	Loop %x%
		Result .= " "
	return Result
}
cheers,
Pilu
Posts: 49
Joined: 22 Jun 2018, 01:13

Re: Multicolor text

05 Feb 2020, 06:15

Thanks the answer SpeedMaster, boiler those solutions is good.

I would like to add the moving text script. So the looping variant is not work.

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.
#SingleInstance, force

	Gui +hwndGUIHWND
	MovingText:= "This is a test"
	MovingText_Width:=Fnt_GetStringWidth(0,MovingText)
	MovingText_X2:=""
	Gui, Add, Text, x10 y50 vMovingText_V, % MovingText
	Gui, Show, Center w300
	
	WinGetPos, , , Gui_Width, , ahk_id %GUIHWND%
	MovingText_X1:=Gui_Width*96/A_ScreenDPI
	GuiControl, Move, MovingText_V, % "x" MovingText_X1
	SetTimer, StartMoving, 10
return

StartMoving:
	if (MovingText_X2="")
		MovingText_X2:=MovingText_X1
	MovingText_X2-=1
		if (MovingText_X2<-MovingText_Width)
		MovingText_X2:=MovingText_X1
	GuiControl, Move, MovingText_V, % "x" MovingText_X2
return

GuiClose:
Exitapp
return

------------------------------
;
; Function: Fnt_GetStringSize
;
; Description:
;
;   Calculates the width and height (in pixels) of a string of text.
;
; Parameters:
;
;   hFont - Handle to a logical font.  Set to 0 to use the default GUI font.
;
;   p_String - The string to be measured.
;
;   r_Width, r_Height - Output variables. [Optional] These variables are loaded
;       with the width and height of the string.
;
; Returns:
;
;   Address to a SIZE structure if successful, otherwise FALSE.
;
; Remarks:
;
;   LF (Line Feed) and/or CR+LF (Carriage Return and Line Feed) characters are
;   not considered when calculating the height of the string.
;
; Observations:
;
;   The width of the tab character is usually determined by the control, not by
;   the font, so including tab characters in the string may not produce the
;   desired results.
;
;-------------------------------------------------------------------------------
Fnt_GetStringSize(hFont,p_String,ByRef r_Width="",ByRef r_Height="")
{
    Static Dummy6596
          ,DEFAULT_GUI_FONT:=17
          ,HWND_DESKTOP    :=0
          ,SIZE

    ;-- Workaround for AutoHotkey Basic
    PtrType:=(A_PtrSize=8) ? "Ptr":"UInt"

    ;-- If needed, get the handle to the default GUI font
    if not hFont
        hFont:=DllCall("GetStockObject","Int",DEFAULT_GUI_FONT)

    ;-- Select the font into the device context for the desktop
    hDC      :=DllCall("GetDC",PtrType,HWND_DESKTOP)
    hFont_Old:=DllCall("SelectObject",PtrType,hDC,PtrType,hFont)

    ;-- Get string size
    VarSetCapacity(SIZE,8,0)
    RC:=DllCall("GetTextExtentPoint32"
        ,PtrType,hDC                                    ;-- hDC
        ,"Str",p_String                                 ;-- lpString
        ,"Int",StrLen(p_String)                         ;-- c (string length)
        ,PtrType,&SIZE)                                 ;-- lpSize

    ;-- Housekeeping
    DllCall("SelectObject",PtrType,hDC,PtrType,hFont_Old)
        ;-- Necessary to avoid memory leak

    DllCall("ReleaseDC",PtrType,HWND_DESKTOP,PtrType,hDC)

    ;-- Return to sender
    if RC
        {
        r_Width :=NumGet(SIZE,0,"Int")
        r_Height:=NumGet(SIZE,4,"Int")
        Return &SIZE
        }
     else
        Return False
}


;------------------------------
;
; Function: Fnt_GetStringWidth
;
; Description:
;
;   Calculates the width of a string of text.
;
; Returns:
;
;   The width of the string of text if successful, otherwise -1.
;
; Calls To Other Functions:
;
; * <Fnt_GetStringSize>
;
; Remarks:
;
;   This function is just a call to <Fnt_GetStringSize> to get the width of a
;   string.  Note that there is no associated "GetStringHeight"  function
;   because the height of a string is the same as the font height.  In addition
;   to <Fnt_GetStringSize>, the height can collected from <Fnt_GetFontHeight>.
;
;-------------------------------------------------------------------------------
Fnt_GetStringWidth(hFont,p_String)
{
    Return (pSIZE:=Fnt_GetStringSize(hFont,p_String)) ? NumGet(pSIZE+0,0,"Int"):-1
}

Return to “Ask For Help”

Who is online

Users browsing this forum: barbudo2005, Chunjee, vlakmire, yuyu and 157 guests