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
}