How I can get the width and height from a item in pixel

General TRichView support forum. Please post your questions here
Post Reply
LuckySpacy
Posts: 7
Joined: Fri Nov 22, 2013 7:45 am

How I can get the width and height from a item in pixel

Post by LuckySpacy »

My question is very simple, but I don't find the solution.

1.) How I can get the width and height from a item in pixel ?
2.) Can I get the width and height for each line?

The reason how i ask.
I have a richviewedit and i would the richviewedit height and width fit on the content text.
Sergey Tkachenko
Site Admin
Posts: 17564
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

There are more simple ways to set size of TRichViewEdit to the content, than checking width of each item.
When document is formatted, you can get its height in rv.DocumentHeight property (if document is not formatted, sizes of items are unknown).

As for width, its more difficult.. For example, if you have right-aligned paragraphs, they will be positioned at the right side, so you will not be able to determine document width by its coordinates.
This problem can be solved if you define some default document width and assign it to rve.MaxTextWidth. Text will be wrapped at this width even if rve.Width is greater. After formatting, you can get the real text width as rve.RVData.TextWidth.

Note: rve.DocumentHeight includes rve.TopMargin and rve.BottomMargin.
rve.MaxTextWidth and rve.RVData.TextWidth do not include rve.LeftMargin and rve.RightMargin.
Piotr
Posts: 1
Joined: Mon Sep 29, 2014 8:37 am

Post by Piotr »

I have similar task to solve: I read a template RTF file to TRichViewEdit. Then I have to replace some special fields by values and print it. But without setting MaxTextWidth, all center aligned items move horizontally, while resizing TRichViewEdit and the form loses proper appearance. So I need to set MaxTextWidth to value taken from template file. E.g. the template first line can be a pattern, because it is filled with graphics from left to right margin.
How can I read first line text width?
Sergey Tkachenko
Site Admin
Posts: 17564
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

I do not completely understand your problem, but below you can find the code returning the X coordinate of the rightmost item on the first line (assuming that the direction is LTR):

Code: Select all

var LastLineDItemNo, i: Integer;

LastLineDItemNo := RichViewEdit1.RVData.DrawItems.Count-1;
for i := 1 to RichViewEdit1.RVData.DrawItems.Count-1 do
  if RichViewEdit1.RVData.DrawItems[i].FromNewLine then begin
    LastLineDItemNo := i-1;
    break;
  end;

FIRSTLINEWIDTH := RichViewEdit1.RVData.DrawItems[LastLineDItemNo].Left+RichViewEdit1.RVData.DrawItems[LastLineDItemNo].Width
You may want to add RichViewEdit1.Style.ParaStyles[RichViewEdit1.GetItemPara(0)].RightIndent.
Post Reply