掲示板システム
ホーム
アクセス解析
カテゴリ
ログアウト
文字列省略時の...の位置 (ID:32741)
名前
ホームページ(ブログ、Twitterなど)のURL (省略可)
本文
中を省略する場合は、'...'が文字列の中央あたりにあった方がイイね。 function MinimizeString(const aStr: string; Canvas: TCanvas; MaxWidth: Integer; isMiddle: Boolean=True): string; const MINIMIZESTR = '...'; var i, cWidth, fWidth: Integer; pT, pL, pB: PChar; cc: Char; sLeft: string; begin result := aStr; if (Canvas.TextWidth(aStr) < MaxWidth) then exit; pT := PChar(aStr); cWidth := Canvas.TextWidth(MINIMIZESTR); if not isMiddle then begin while (Canvas.TextWidth(pT) + cWidth > MaxWidth) do begin if IsDBCSLeadByte(Byte(pT^)) then inc(pT, 2) else inc(pT); end; result := MINIMIZESTR + pT; end else begin pL := pT + Length(aStr); pB := pT; repeat if IsDBCSLeadByte(Byte(pB^)) then inc(pB, 2) else inc(pB); cc := pB^; pB^ := #0; sLeft := pT; fWidth := Canvas.TextWidth(sLeft) + cWidth; pB^ := cc; until fWidth > (MaxWidth div 2); while (Canvas.TextWidth(pB) > MaxWidth - fWidth) do begin if pB > pL then break; if IsDBCSLeadByte(Byte(pB^)) then inc(pB, 2) else inc(pB); end; result := sLeft + MINIMIZESTR + pB; end; end;
←解決時は質問者本人がここをチェックしてください。
更新する
戻る
掲示板システム
Copyright 2021 Takeshi Okamoto All Rights Reserved.