|
Home Page |
About ToolBook |
ToolBook Tips |
ToolBook Projects |
ToolBook Links |
Guest Book |
How to get size of text in pixels
-- Function GetTextWidth returns size of text in pixels.
-- You can specify font face, size and style and get size of text.
to handle buttonClick
myText = "Text to be justified in this test."
myFace = "Arial"
mySize = 10
myBold = 1
myItalic = 0
myWidth = GetTextWidth(myText, myFace, mySize, myBold, myItalic)
request myWidth
end
to get GetTextWidth myText, myFace, mySize, myBold, myItalic
-- myText - Text
-- myFace - Font Face
-- (If such font doesn't exists function uses device-dependent typeface and returns some unexpectable value).
-- mySize - Font Size
-- myBold - Bold if nonzero
-- myItalic - Italic if nonzero
local hdc
local myFont
linkDLL "GDI"
word createFont(int,int,int,int,int,byte,byte,byte,byte,byte,byte,byte,byte,pointer)
word SelectObject(word, word)
dword GetTextExtent(word, pointer, int)
int SetMapMode(word, int)
int GetMapMode(word)
int GetDeviceCaps(word, int)
end
linkDLL "KERNEL"
WORD GlobalAlloc(WORD,DWORD)
WORD GlobalFree(WORD)
WORD GlobalHandle(WORD)
POINTER GlobalLock(WORD)
WORD GlobalUnlock(WORD)
WORD GetWindowsDirectory(POINTER,INT)
DWORD getWinFlags()
end
linkDLL "USER"
word getDC(word)
end
LOGPIXELSX = 88
MM_TEXT = 1
myFont = getWinPointer(128)
get pointerString(0,myFont,myFace)
hdc = getDC(0)
MapModePrevious = GetMapMode(hdc);
MapModeNew = SetMapMode(hdc, MM_TEXT);
if myBold <> 0
myStyle = 700
else
myStyle = 400
end if
myHeight = round(mySize * GetDeviceCaps(hdc, LOGPIXELSX) div 72)
hfont = CreateFont(-myHeight, 0, 0, 0, myStyle, myItalic, 0, 0, 0, 0, 0, 0, 0, myFont)
hfontOld = SelectObject(hdc, hfont)
myTextPTR = getWinPointer(128)
get pointerString(0, myTextPTR, myText)
dwExtent = GetTextExtent(hdc, myTextPTR, charCount(myText))
wTextWidth = dwExtent bitShiftLeft 16 bitShiftRight 16
MapModeNew = SetMapMode(hdc, MapModePrevious);
get freeWinPointer(myFont)
get freeWinPointer(myTextPTR)
return wTextWidth
end
to get getWinPointer nSize
local word hMem
local retValue
hMem = GlobalAlloc(66,nSize)
return GlobalLock(hMem)
end
to get freeWinPointer pMem
local word hMem, retValue
sysSuspend = false
hMem = GlobalHandle(item 1 of pMem)
retValue = GlobalUnlock(hMem)
sysSuspend = true
return GlobalFree(hMem)
end
|