各位大侠,想问个问题,DrawText中如果设置了Dt_WordBreak标志后,显示的时候会按英文单词进行换行显示,现在的问题是我能不能得到换行后每行的字符个数,或者是有没有一个这样的系统API,给它一个区域和字体大小,它能按WordBreak那样返回能容下的字符数。
  试过DrawTextEx(Handle, PChar(AText), -1, cRect, defGetTextLengrthFlag, @dtParams);通过dtParams.uiLengthDrawn来取得字符数,但是只对英文有效,中文不行。
  各位,有没有方法可以解决一下~谢谢

解决方案 »

  1.   

    用这个方法试下:http://stackoverflow.com/questions/7007280/delphi-draw-text-multiline-in-the-centre-of-a-rectfunction DrawTextCentered(Canvas: TCanvas; const R: TRect; S: String): Integer;
    var
      DrawRect: TRect;
      DrawFlags: Cardinal;
      DrawParams: TDrawTextParams;
    begin
      DrawRect := R;
      DrawFlags := DT_END_ELLIPSIS or DT_NOPREFIX or DT_WORDBREAK or
        DT_EDITCONTROL or DT_CENTER;
      DrawText(Canvas.Handle, PChar(S), -1, DrawRect, DrawFlags or DT_CALCRECT);
      DrawRect.Right := R.Right;
      if DrawRect.Bottom < R.Bottom then
        OffsetRect(DrawRect, 0, (R.Bottom - DrawRect.Bottom) div 2)
      else
        DrawRect.Bottom := R.Bottom;
      ZeroMemory(@DrawParams, SizeOf(DrawParams));
      DrawParams.cbSize := SizeOf(DrawParams);
      DrawTextEx(Canvas.Handle, PChar(S), -1, DrawRect, DrawFlags, @DrawParams);
      Result := DrawParams.uiLengthDrawn;
    end;