"preben nielsen" <prel@post.tele.dk> skrev i en meddelelse
news:40926df8$0$269$edfadb0f@dread12.news.tele.dk...
>
> Du håndterer maxlængde i antal tegn, men hvad hvis du skulle
vise
> din tekst i en label, som var 300 pixel bred og hvor hvert tegn
i
> font'en ikke var lige bred ?
>
> Dét håndterer DrawText()
>
> Ok, jeg skal lige lave et eksempel......
Ok, så er jeg klar.
Funktionen EllipsisText() tegner ikke teksten ! Den returnerer en
string omformet med "..." enten midti (som i en path) eller i
enden.
DrawText() kan dog også bruges til til at tegne direkte med (i
form, på printer, etc), men så skal man ikke bruge flaget
DT_MODIFYSTRING.
Enjoy
--
/\ preben nielsen
\/\ prel@post.tele.dk
----------Smid dette i et modul -----------------------------
Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Private Const DT_CALCRECT = &H400
Private Const DT_PATH_ELLIPSIS = &H4000
Private Const DT_END_ELLIPSIS = &H8000&
Private Const DT_MODIFYSTRING = &H10000
Private Const DT_EXPANDTABS = &H40
Private Const DT_NOPREFIX = &H800
Private Const DT_WORDBREAK = &H10
Private Const DT_TABSTOP = &H80
Private Const DT_WORD_ELLIPSIS = &H40000
Private Declare Function DrawText Lib "user32" Alias "DrawTextA"
(ByVal hDC As Long, ByVal lpString As String, ByVal nCount As
Long, lpRect As RECT, ByVal uFormat As Long) As Long
Private Declare Function GetWindowRect Lib "user32" (ByVal hwnd As
Long, lpRect As RECT) As Long
Function EllipsisText(hDC As Long, ctl As Control, str As String,
Optional blnEndEllipsis As Boolean = True) As String
Dim rc As RECT
Dim lngStyle As Long
With ctl
EllipsisText = str
If TypeOf ctl Is Label Then
rc.Left = 0
rc.Right = ctl.Width \ Screen.TwipsPerPixelX
Else
GetWindowRect ctl.hwnd, rc
End If
rc.Left = rc.Left * 0.96
rc.Right = rc.Right * 0.96
If blnEndEllipsis Then
lngStyle = DT_END_ELLIPSIS
Else
lngStyle = DT_PATH_ELLIPSIS
End If
Call DrawText(hDC, EllipsisText, -1, rc, DT_CALCRECT Or
DT_MODIFYSTRING Or lngStyle)
End With
End Function
----------Brug det evt sådan her i en
form -----------------------------
Private Sub Command1_Click()
Dim strTmp As String
strTmp =
"D:\Dokumenter\Data\Musik\MP3\DetBedsteAfDetBedsteFra80erne"
Label1.Caption = EllipsisText(Me.hDC, Label1, strTmp, False) '
Giver f.eks. "D:\...DetBedsteAfDetBedsteFra80erne"
Label2.Caption = EllipsisText(Me.hDC, Label1, strTmp, True) '
Giver f.eks. "D:\Dokumenter\Data\Musik\MP3\..."
End Sub