Article ID: 152475
Article Last Modified on 1/19/2007
Option Explicit
Const LOGPIXELSX = 88
Const LOGPIXELSY = 90
Const TWIPSPERINCH = 1440
Type Size
cx As Long
cy As Long
End Type
Declare Function GetDC Lib "user32" (ByVal hWnd As Long) As Long
Declare Function GetDeviceCaps Lib "gdi32" (ByVal hDC As Long, _
ByVal nIndex As Long) As Long
Declare Function GetFocus Lib "user32" () As Long
Declare Function GetTextExtentPoint Lib "gdi32" Alias _
"GetTextExtentPointA" (ByVal hDC As Long, ByVal lpsz As String, _
ByVal cbString As Long, lpSIZE As Size) As Long
Declare Function ReleaseDC Lib "user32" (ByVal hWnd As Long, _
ByVal hDC As Long) As Long
Sub ConvertPixelsToTwips(X As Long, Y As Long)
Dim hDC As Long, hWnd As Long, RetVal As Long
Dim XPIXELSPERINCH As Long, YPIXELSPERINCH As Long
'' Retrieve the current number of pixels per inch, which is
'' resolution-dependent.
hDC = GetDC(0)
XPIXELSPERINCH = GetDeviceCaps(hDC, LOGPIXELSX)
YPIXELSPERINCH = GetDeviceCaps(hDC, LOGPIXELSY)
RetVal = ReleaseDC(0, hDC)
'' Compute and return the measurements in twips.
X = (X / XPIXELSPERINCH) * TWIPSPERINCH
Y = (Y / YPIXELSPERINCH) * TWIPSPERINCH
End Sub
Sub LimitTextToControlWidth(KeyAscii As Integer)
Dim AC As Control
Dim Txt As String
Dim TxtWidth As Long, SpaceWidth As Long
Dim hWnd As Long, hDC As Long
Dim lpSIZE As Size
Dim RetVal As Long
'' Exit if a non-printable character is typed.
If KeyAscii < 32 Then Exit Sub
'' Record active control.
Set AC = Screen.ActiveControl
'' Get the control text.
Txt = AC.Text & ""
'' Append typed character into the text.
If KeyAscii > 32 Then
Txt = Left(Txt, AC.SelStart)
Txt = Txt & Chr$(KeyAscii)
Txt = Txt & Mid(Txt, AC.SelStart + 1 + AC.SelLength)
End If
hWnd = GetFocus()
hDC = GetDC(hWnd)
'' Compute the width of the text.
RetVal = GetTextExtentPoint(hDC, Txt, Len(Txt), lpSIZE)
ConvertPixelsToTwips lpSIZE.cx, lpSIZE.cy
TxtWidth = lpSIZE.cx
'' Compute width of a space.
RetVal = GetTextExtentPoint(hDC, " ", 1, lpSIZE)
ConvertPixelsToTwips lpSIZE.cx, lpSIZE.cy
SpaceWidth = lpSIZE.cx
'' Are there trailing spaces to contend with?
If AC.SelStart + 1 > Len(Txt) Then
'' Add number of spaces * SpaceWidth to width of string.
TxtWidth = TxtWidth + ((AC.SelStart + 1 - Len(Txt)) * _
SpaceWidth)
End If
If TxtWidth + (SpaceWidth / 2) > AC.Width Then
Beep
KeyAscii = 0
End If
End Sub
Option Explicit
Const LOGPIXELSX = 88
Const LOGPIXELSY = 90
Const TWIPSPERINCH = 1440
Type SIZE
cx As Integer
cy As Integer
End Type
Declare Function GetDC Lib "User" (ByVal hWnd As Integer) As Integer
Declare Function GetDeviceCaps Lib "GDI" (ByVal hDC As Integer, _
ByVal nIndex As Integer) As Integer
Declare Function GetTextExtentPoint Lib "GDI" (ByVal hDC As _
Integer, ByVal lpszString As String, ByVal cbString As _
Integer, lpSIZE As SIZE) As Integer
Declare Function GetFocus Lib "User" () As Integer
Declare Function ReleaseDC Lib "User" (ByVal hWnd As Integer, _
ByVal hDC As Integer) As Integer
S
Sub LimitTextToControlWidth (KeyAscii)
Dim AC As Control
Dim Txt As String
Dim TxtWidth As Integer, SpaceWidth As Integer
Dim hWnd As Integer, hDC As Integer
Dim lpSIZE As SIZE
Dim RetVal As Integer
'' Exit if a non-printable character is typed.
If KeyAscii < 32 Then Exit Sub
'' Record active control.
Set AC = Screen.ActiveControl
'' Get the control text.
Txt = AC.Text & ""
'' Append typed character into the text.
If KeyAscii > 32 Then
Txt = Left(Txt, AC.SelStart)
Txt = Txt & Chr$(KeyAscii)
Txt = Txt & Mid(Txt, AC.SelStart + 1 + AC.SelLength)
End If
hWnd = GetFocus()
hDC = GetDC(hWnd)
'' Compute the width of the text.
RetVal = GetTextExtentPoint(hDC, Txt, Len(Txt), lpSIZE)
ConvertPixelsToTwips lpSIZE.cx, lpSIZE.cy
TxtWidth = lpSIZE.cx
'' Compute width of a space.
RetVal = GetTextExtentPoint(hDC, " ", 1, lpSIZE)
ConvertPixelsToTwips lpSIZE.cx, lpSIZE.cy
SpaceWidth = lpSIZE.cx
'' Are there trailing spaces to contend with?
If AC.SelStart + 1 > Len(Txt) Then
'' Add number of spaces * SpaceWidth to width of string.
TxtWidth = TxtWidth + ((AC.SelStart + 1 - Len(Txt)) * _
SpaceWidth)
End If
If TxtWidth + (SpaceWidth / 2) > AC.Width Then
Beep
KeyAscii = 0
End If
End Sub
Sub ConvertPixelsToTwips (X As Integer, Y As Integer)
Dim hDC As Integer, hWnd As Integer, RetVal As Integer
Dim XPIXELSPERINCH As Integer, YPIXELSPERINCH As Integer
'' Retrieve the current number of pixels per inch, which is
'' resolution-dependent.
hDC = GetDC(0)
XPIXELSPERINCH = GetDeviceCaps(hDC, LOGPIXELSX)
YPIXELSPERINCH = GetDeviceCaps(hDC, LOGPIXELSY)
RetVal = ReleaseDC(0, hDC)
'' Compute and return the measurements in twips.
X = (X / XPIXELSPERINCH) * TWIPSPERINCH
Y = (Y / YPIXELSPERINCH) * TWIPSPERINCH
End Sub
Name: One
Left: 1"
Top: .5"
Width: 1"
Height: 0.1667"
FontName: Arial
FontSize: 12
Name: Two
Left: 1"
Top: 1"
Width: 1"
Height: 0.1667"
FontName: Arial
FontSize: 8
Name: Three
Left: 1"
Top: 1.5"
Width: 1"
Height: 0.3743"
FontName: Arial
FontSize: 24
Sub One_KeyPress(KeyAscii As Integer)
LimitTextToControlWidth KeyAscii
End Sub
This is a test of the emergency broadcast system
Text box Text allowed to type
---------------------------------
One This is a test
Two This is a test of the
Three This isKeywords: kbhowto kbprogramming kbusage KB152475