RichEdit
RichEdit is a multi-line editing tool like Memo, but it has additional features
more than Memo such as changing font attributes for each paragraph, each line, each
word, and each letter in the same Rich edit control.
Here is an important procedures used with Rich edit. By the way current text you
are reading exists in RichEdit component so that we can change font name, style,
and color any where.
Befor starting with RichEdit examples you must drop:
- RichEdit from Win32 page
- ToolBar from Win32 page
- StatusBar from Win32 page
- FontDialog from Dialogs page
- ColorDialog from Dialogs page
Changing selected text font
Rich editor has two font attributes objects: DefAttributes in which default rich
edit font stored; and SelAttributes by which we can read and set current selected
text font attributes such as font name, color, size, and style.
Add new button on ToolBar and place this code on it's OnClick event:
(If you does not use ToolBar befor don't worry it is very simple, just drop a ToolBar
then right click on it and select New button).
with FontDialog1 do
begin
(*** Get selected text font attributes ***)
Font.Name:= RichEdit1.SelAttributes.Name;
Font.Size:= RichEdit1.SelAttributes.Size;
Font.Color:= RichEdit1.SelAttributes.Color;
Font.Style:= RichEdit1.SelAttributes.Style;
(*** Display Font dialog ***)
if Execute then
begin
RichEdit1.SelAttributes.Name:= Font.Name;
RichEdit1.SelAttributes.Size:= Font.Size;
RichEdit1.SelAttributes.Color:= Font.Color;
RichEdit1.SelAttributes.Style:= Font.Style;
end; // if Execute
end; // with FontDialog1
Font combo box
In most rich text editors we found that font name can be selected from a combo box
which exists in tool bar. Follow these steps to make font combo box:
Drop a ComboBox in ToolBar. To fill font combo box with installed fonts write this
line at form's OnCreate event:
ComboBox1.Items:= Screen.Fonts;
Screen is a global object which holds all installed windows fonts at Fonts StringList
object. Screen object has many other properties and methods. One of it's properties
as an example is Cursor wich used to change mouse pointer such as:
Screen.Cursor:= crHourGlass;
Screen.Cursor:= crDefault;
At RichEdit OnSelectionChange event write:
ComboBox1.Text:=
RichEdit1.SelAttributes.Name
This statement changes font name wich exists in combo box according to current selected
font name. When user moves the cursor or double clicked at any text on rich edit,
font combo box will displays current font name of text pointed to by cursor.
At font ComboBox's OnClick event write:
RichEdit1.SelAttributes.Name:=
ComboBox1.Text;
I think above statement is very clear.
Changing color
You can change text color by using font dialog box but font dialog box has a few
set of colors, some times you need to mix your own colors. To change text color with
any color you want follow these steps:
Add new button on ToolBar and write this code at OnClick event:
with ColorDialog1 do
begin
(*** Get current text color ***)
Color:= RichEdit1.SelAttributes.Color;
if Execute then
RichEdit1.SelAttributes.Color:= Color;
end;
Changing font style
Font styles such as: Bold, Italic, and underline always can be toggeled by a button,
it selected font not bold it will be bold and if it is bold it will be normal.
Example:
- Add three new buttons on ToolBar
- At first button (Bold) write:
with RichEdit1.SelAttributes do
if fsBold in Style then
Style:= Style - [fsBold]
else
Style:= Style + [fsBold];
- At second button (Italic) write:
with RichEdit1.SelAttributes do
if fsItalic in Style then
Style:= Style - [fsItalic]
else
Style:= Style + [fsItalic];
- At third button (Underline) write:
with RichEdit1.SelAttributes do
if fsUnderline in Style then
Style:= Style - [fsUnderline]
else
Style:= Style + [fsUnderline];
Paragraph Alignment
There are three alignment options in rich editor: left justify, center, and right
justify.
Example:
- Add three new buttons on ToolBar
- At first one (Left) write:
RichEdit1.Paragraph.Alignment:=
taLeftJustify;
- At second one (Center) write:
RichEdit1.Paragraph.Alignment:=
taCenter;
- At third one (Right) write:
RichEdit1.Paragraph.Alignment:=
taRightJustify;
See also
Ex006 Memo and Main Menu