Board index » delphi » RichEdit font changes

RichEdit font changes

Hi there

I've been struggling with this all day.

I want to give my users to option to change the font size of a file that I
have loaded in a RichEdit component (a bit like you can increase and
decrease font size in a browser).

The problem is that the paragraphs in the file are already different font
sizes, so I can't do a global change in size.

Is there any way that I can return to font size for each line (or paragraph)
in the file - and then increase it accordingly.

I've been messing around with something like this, but it doesn't work
properly.

procedure TMainForm.ToolButton12Click(Sender: TObject);
var
I : Integer;
L : Integer;
begin
  L := 0;
  RichEdit.WordWrap := False;
    for I := 0 to RichEdit.Lines.Count - 1 do begin
        if L <> 0 then
          RichEdit.SelStart := L + 1
        else
          RichEdit.SelStart := 0;
        RichEdit.SelLength := Length(ThemeEdit.Lines[I]);
        L := L + Length(RichEdit.Lines[I]);
        with RichEdit.SelAttributes do
           size := size + 2;
    end;
  RichEdit.SelStart := 0;
  RichEdit.WordWrap := True;
end;

Any ideas greatly appreciated.

Thanks.

Jeremy

 

Re:RichEdit font changes


Jerry,

I'm not sure if this is the best way, but I think it works.  Trick is to use
Selattributes.ConsistentAttributes on a selected line.  If caSize is there,
you can increase the size of the entire line at once, otherwise do it
character by character for that line only.

 with richedit1 do
  begin
    {make increase size of all characters}
    for i:= lines.Count-1 downto 0 do
    begin
      SelStart:=SendMessage(Handle, EM_LINEINDEX, i, 0);
      SelLength:=length(lines[i]);
     {If whole line is same size, increase whole line at once}
      if caSize in selAttributes.ConsistentAttributes
      then Selattributes.size:=Selattributes.size+2
      else
      begin
       {othjerwise increase size character by character for this line}
        linestart:=selstart;
        for j:= 0 to length(lines[i])-1 do
        begin
          selstart:=linestart+j;
          sellength:=1;
          selattributes.size:=selattributes.size+2;
        end;
      end;
    end;
    SelStart:=0;
    SelLength:=0;
  end;
end;

--
_________________________
Gary
http://www.delphiforfun.org
_________________________

Quote
"Jerry S" <j...@theantcolony.com> wrote in message

news:ZjwX5.183$iM1.2123691@news1.cableinet.net...
Quote
> Hi there

> I've been struggling with this all day.

> I want to give my users to option to change the font size of a file that I
> have loaded in a RichEdit component (a bit like you can increase and
> decrease font size in a browser).

> The problem is that the paragraphs in the file are already different font
> sizes, so I can't do a global change in size.

> Is there any way that I can return to font size for each line (or
paragraph)
> in the file - and then increase it accordingly.

Re:RichEdit font changes


Thanks for the help Gary.

'Fraid it doesn't quite work!

The "selstart := linestart+j" is giving me not enough parameters and
incompatible type errors.

Also when I remove that section (because within any particular line, fonts
are only ever of one size), it produces the same kind of strange result that
my routine produced (i.e., everything seems to be fine, then suddenly it
won't have worked for a line or two). I'm wondering whether there's
something funny about my rich text file!

Thanks again - it was interesting to see your approach.

All the best

Jeremy

Quote
"Gary Darby" <garydn...@delphiforfun.com> wrote in message

news:90mpf1$38uf$1@news3.infoave.net...
Quote
> Jerry,

> I'm not sure if this is the best way, but I think it works.  Trick is to
use
> Selattributes.ConsistentAttributes on a selected line.  If caSize is
there,
> you can increase the size of the entire line at once, otherwise do it
> character by character for that line only.

>  with richedit1 do
>   begin
>     {make increase size of all characters}
>     for i:= lines.Count-1 downto 0 do
>     begin
>       SelStart:=SendMessage(Handle, EM_LINEINDEX, i, 0);
>       SelLength:=length(lines[i]);
>      {If whole line is same size, increase whole line at once}
>       if caSize in selAttributes.ConsistentAttributes
>       then Selattributes.size:=Selattributes.size+2
>       else
>       begin
>        {othjerwise increase size character by character for this line}
>         linestart:=selstart;
>         for j:= 0 to length(lines[i])-1 do
>         begin
>           selstart:=linestart+j;
>           sellength:=1;
>           selattributes.size:=selattributes.size+2;
>         end;
>       end;
>     end;
>     SelStart:=0;
>     SelLength:=0;
>   end;
> end;

> --
> _________________________
> Gary
> http://www.delphiforfun.org
> _________________________

> "Jerry S" <j...@theantcolony.com> wrote in message
> news:ZjwX5.183$iM1.2123691@news1.cableinet.net...
> > Hi there

> > I've been struggling with this all day.

> > I want to give my users to option to change the font size of a file that
I
> > have loaded in a RichEdit component (a bit like you can increase and
> > decrease font size in a browser).

> > The problem is that the paragraphs in the file are already different
font
> > sizes, so I can't do a global change in size.

> > Is there any way that I can return to font size for each line (or
> paragraph)
> > in the file - and then increase it accordingly.

Re:RichEdit font changes


Hmmm.  It seemed to work on my test case.  I only tested with a single file
by initializing a few lines to different size and a few characters within a
line to a different size and they all seemed to increase proportionately.
So maybe it is an issue with attibutes in your file that aren't in my test
file.  Are the results consistent, i.e. are the same lines ignored each
time?

 BTW, "linestart" is just a local variable defined as integer.  Forgot to
include it in my sample code.
_________________________
Gary
http://www.delphiforfun.org
_________________________

Quote
"Jerry S" <j...@theantcolony.com> wrote in message

news:AALX5.1413$E41.60121@news3.cableinet.net...
Quote
> Thanks for the help Gary.

> 'Fraid it doesn't quite work!

> The "selstart := linestart+j" is giving me not enough parameters and
> incompatible type errors.

> Also when I remove that section (because within any particular line, fonts
> are only ever of one size), it produces the same kind of strange result
that
> my routine produced (i.e., everything seems to be fine, then suddenly it
> won't have worked for a line or two). I'm wondering whether there's
> something funny about my rich text file!

> Thanks again - it was interesting to see your approach.

> All the best

> Jeremy
> "Gary Darby" <garydn...@delphiforfun.com> wrote in message
> news:90mpf1$38uf$1@news3.infoave.net...

Re:RichEdit font changes


Gary

Thanks very much!

I've solved the problem.

Just adding "richedit1.setfocus" at the beginning sorted out the difficulty.

By the way, am I right in thinking that "SendMessage(Handle, EM_LINEINDEX,
i, 0);" is returning the position of every start of line?

Thanks again!

All the best

Jerry

Quote
"Gary Darby" <garydn...@delphiforfun.com> wrote in message

news:90o9r5$4a13$1@news3.infoave.net...
Quote
> Hmmm.  It seemed to work on my test case.  I only tested with a single
file
> by initializing a few lines to different size and a few characters within
a
> line to a different size and they all seemed to increase proportionately.
> So maybe it is an issue with attibutes in your file that aren't in my test
> file.  Are the results consistent, i.e. are the same lines ignored each
> time?

Other Threads