Board index » delphi » RichEdit SuperScript and Subscript ...

RichEdit SuperScript and Subscript ...

Hi there,

how can I implement SuperScript and SubScript in a RichEdit control ?

Paul

 

Re:RichEdit SuperScript and Subscript ...


Don't know where I got this from...

Example:

uses
  RichEdit;

procedure TForm1.SuperscriptButtonClick(Sender: TObject);
var
  Format: TCharFormat;
begin
  FillChar(Format, SizeOf(TCharFormat), 0);
  Format.cbSize := SizeOf(TCharFormat);
  Format.dwMask := CFM_OFFSET;
  Format.yOffset := 40;
  RichEdit1.Perform(EM_SETCHARFORMAT, SCF_SELECTION, LPARAM(@Format));
end;

procedure TForm1.SubscriptButtonClick(Sender: TObject);
var
  Format: TCharFormat;
begin
  FillChar(Format, SizeOf(TCharFormat), 0);
  Format.cbSize := SizeOf(TCharFormat);
  Format.dwMask := CFM_OFFSET;
  Format.yOffset := -40;
  RichEdit1.Perform(EM_SETCHARFORMAT, SCF_SELECTION, LPARAM(@Format));
end;

Quote
Paul Oud wrote:

> Hi there,

> how can I implement SuperScript and SubScript in a RichEdit control ?

> Paul

Re:RichEdit SuperScript and Subscript ...


In article <3BA68DF8.D6CF4...@5thelephant.com>, Chris Willig

Quote
<ch...@5thelephant.com> writes:
>procedure TForm1.SuperscriptButtonClick(Sender: TObject);
>var
>  Format: TCharFormat;
>begin
>  FillChar(Format, SizeOf(TCharFormat), 0);
>  Format.cbSize := SizeOf(TCharFormat);
>  Format.dwMask := CFM_OFFSET;
>  Format.yOffset := 40;
>  RichEdit1.Perform(EM_SETCHARFORMAT, SCF_SELECTION, LPARAM(@Format));
>end;

You may also want to change the font size of the super/sub-script. MS Word
appears to use SS size = 2/3 normal, super offset = 1/3 normal size, sub offset
= 1/8 normal size. The following (untested) should do this for you ...

uses
  RichEdit;

procedure SetCharScript(RE : TRichEdit; Super : boolean);
{super- or sub-script selected characters in a TRichEdit}
{offset size and amount iaw MS Word}
var
  FontSize : integer;
  Format: TCharFormat;
begin
  FontSize := RE.SelAttributes.Size; // in points of 72/inch
  FillChar(Format, SizeOf(TCharFormat), 0);
  with Format do begin
    cbSize := SizeOf(TCharFormat);
    dwMask := CFM_OFFSET or CFM_SIZE;
    if Super then
      {superscript}
      yOffset := (FontSize * 20) div 3   // 1/3 normal in twips of 1440/inch
    else
      {subscript}
      yOffset := (FontSize * -20) div 8; // 1/8 normal in twips
    yHeight := (FontSize * 40) div 3;    // 2/3 normal in twips
  end; {with Format}
  RE.Perform(EM_SETCHARFORMAT, SCF_SELECTION, integer(@Format));
end;

Alan Lloyd
alangll...@aol.com

Other Threads