МІНІСТЕРСТВО ОСВІТИ І НАУКИ УКРАЇНИ
Національний університет Львівська політехніка
Кафедра ПЗ
До лабораторної роботи №3
З курсу “Методи та засоби КІТ”
На тему:
Текстовий редактор з перевіркою орфографії.
Тема:
Текстовий редактор з перевіркою орфографії.
Мета:
Ознайомитись з RichEdit, вивчити властивості компонента, та його методи.
function delsp(swp:string):string;
var
i:integer;
begin
for i:=0 to length(swp) do
begin
if swp[i]=' ' then
delete(swp,i,1);
if swp[i]=chr(13) then
delete(swp,i,1);
end;
delsp:=swp;
end;
procedure TForm1.SpeedButton1Click(Sender: TObject);
begin
RichEdit1.Clear;
new:=1;
fname:='NewDoc.txt';
form1.Caption:='EasyPad-'+fname;
end;
procedure TForm1.SpeedButton12Click(Sender: TObject);
begin
if FontDialog1.Execute then
begin
RichEdit1.SelAttributes.Charset:=FontDialog1.Font.Charset;
RichEdit1.SelAttributes.Color:=FontDialog1.Font.Color;
RichEdit1.SelAttributes.Name:=FontDialog1.Font.Name;
RichEdit1.SelAttributes.Pitch:=FontDialog1.Font.Pitch;
RichEdit1.SelAttributes.Size:=FontDialog1.Font.Size;
RichEdit1.SelAttributes.Style:=FontDialog1.Font.Style;
RichEdit1.SelAttributes.Height:=FontDialog1.Font.Height;
end;
end;
procedure TForm1.FormShow(Sender: TObject);
begin
lb.Items.LoadFromFile('dict.txt');
n19.Checked:=True;
new:=1;
RichEdit1.Height:=Form1.Height-StatusBar1.Height-Toolbar1.Height;;
RichEdit1.Width:=Form1.Width-5;
SpeedButton14.Font.Color:=ClBlack;
SpeedButton13.Font.Color:=ClBlack;
SpeedButton15.Font.Color:=ClBlack;
SpeedButton19.Font.Color:=ClBlack;
RichEdit1.SetFocus;
fname:='NewDoc.txt';
form1.Caption:='EasyPad-'+fname;
end;
procedure TForm1.FormConstrainedResize(Sender: TObject; var MinWidth,
MinHeight, MaxWidth, MaxHeight: Integer);
begin
RichEdit1.Height:=Form1.Height-StatusBar1.Height-Toolbar1.Height;;
RichEdit1.Width:=Form1.Width-5;
end;
procedure TForm1.FormActivate(Sender: TObject);
begin
RichEdit1.Height:=Form1.Height-StatusBar1.Height-Toolbar1.Height;
RichEdit1.Width:=Form1.Width-5;
end;
procedure TForm1.N5Click(Sender: TObject);
begin
form2.Show;
end;
procedure TForm1.SpeedButton2Click(Sender: TObject);
begin
new:=0;
if OpenDialog1.Execute then
RichEdit1.Lines.LoadFromFile(OpenDialog1.FileName);
form1.Caption:='EasyPad-'+OpenDialog1.FileName
end;
procedure TForm1.SpeedButton3Click(Sender: TObject);
begin
if new=1 then
begin
if SaveDialog1.Execute then
RichEdit1.Lines.SaveToFile(SaveDialog1.FileName);
new:=0;
end
else
RichEdit1.Lines.SaveToFile(OpenDialog1.FileName);
change:=0;
end;
procedure TForm1.SpeedButton14Click(Sender: TObject);
begin
if SpeedButton14.Font.Color=ClBlack then
begin
SpeedButton14.Font.Color:=ClRed;
SpeedButton14.Repaint;
RichEdit1.SelAttributes.Style := [fsItalic];
end
else
begin
SpeedButton14.Font.Color:=ClBlack;
RichEdit1.SelAttributes.Style := [];
end;
end;
procedure TForm1.SpeedButton13Click(Sender: TObject);
begin
if SpeedButton13.Font.Color=ClBlack then
begin
SpeedButton13.Font.Color:=ClRed;
RichEdit1.SelAttributes.Style := [fsBold];
end
else
begin
SpeedButton13.Font.Color:=ClBlack;
RichEdit1.SelAttributes.Style := [];
end;
end;
//(fsBold, fsItalic, fsUnderline, fsStrikeOut);
procedure TForm1.SpeedButton15Click(Sender: TObject);
begin
if SpeedButton15.Font.Color=ClBlack then
begin
SpeedButton15.Font.Color:=ClRed;
RichEdit1.SelAttributes.Style := [fsUnderline];
end
else
begin
SpeedButton15.Font.Color:=ClBlack;
RichEdit1.SelAttributes.Style := [];
end;
end;
procedure TForm1.SpeedButton19Click(Sender: TObject);
begin
if SpeedButton19.Font.Color=ClBlack then
begin
SpeedButton19.Font.Color:=ClRed;
RichEdit1.SelAttributes.Style := [fsBold, fsItalic];
end
else
begin
SpeedButton19.Font.Color:=ClBlack;
RichEdit1.SelAttributes.Style := [];
end;
end;
procedure TForm1.SpeedButton16Click(Sender: TObject);
begin
RichEdit1.Paragraph.Alignment:=taLeftJustify;
end;
procedure TForm1.SpeedButton17Click(Sender: TObject);
begin
RichEdit1.Paragraph.Alignment:=TAcenter;
end;
procedure TForm1.SpeedButton18Click(Sender: TObject);
begin
RichEdit1.Paragraph.Alignment:=taRightJustify;
end;
procedure TForm1.RichEdit1Enter(Sender: TObject);
begin
ComboBox1.Text:=inttostr(RichEdit1.SelAttributes.Size);
end;
procedure TForm1.RichEdit1KeyUp(Sender: TObject; var Key: Word;
Shift: TShiftState);
var
s:string;
i:integer;
zm:bool;
begin
if key =32 then
begin
lb.Items.LoadFromFile('dict.txt');
s:=RichEdit1.Lines.Strings[RichEdit1.Lines.Count-1];
if (s[length(s)-1]=' ') or (s=' ') then exit;
//if s[RichEdit1.CaretPos.X]=' ' then exit; //ÄÎÐÎÁÈÒÈ!!!!!!!!!!
spp:=length(s)-1;
for i:=spp downto 0 do
begin
if s[i]=' ' then
begin
sspp:=i;
break;
end
else
sspp:=0;
end;
sw:=copy(s,sspp,spp);
if s[sspp]<>' ' then
delete(sw,length(sw)+1,1)
else
begin
sw:=copy(s,sspp+1,spp);
delete(sw,length(sw),1);
end;
for i:=0 to lb.Items.Count-1 do
if sw<>lb.Items.Strings[i] then
zm:=True
else
begin
zm:=false;
break;
end;
if zm=true then
begin
RichEdit1.SelStart:=sspp;
RichEdit1.SelLength:=spp;
RichEdit1.selattributes.color:=clBlue;
lb.Visible := False;
RichEdit1.SetFocus;
s:=RichEdit1.Lines.Strings[RichEdit1.Lines.Count-1];
delete(s,length(s)+1,1);
for i:=0 to lb.Items.Count-1 do
begin
s:=lb.Items.Strings[i];
if (sw[1])<>(s[1]) then
lb.Selected[i]:=true;
end;
lb.DeleteSelected;
lb.Left := RichEdit1.CaretPos.x;
lb.Top := RichEdit1.CaretPos.y + lb.height;
lb.Visible := True;
lb.Items.Add('Çàïèñàòè â ñëîâíèê');
lb.Items.Add('Ïðîïóñòèòè');
lb.SetFocus;
end
end;
ComboBox1.Text:=inttostr(RichEdit1.SelAttributes.Size);
end;
procedure TForm1.RichEdit1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
ComboBox1.Text:=inttostr(RichEdit1.SelAttributes.Size);
end;
procedure TForm1.ComboBox1Change(Sender: TObject);
begin
RichEdit1.SelAttributes.Size:=strtoint(ComboBox1.Text);
end;
procedure TForm1.SpeedButton4Click(Sender: TObject);
begin
FindDialog1.Position := Point(RichEdit1.Left + RichEdit1.Width, RichEdit1.Top);
FindDialog1.Execute;
end;
procedure TForm1.FindDialog1Find(Sender: TObject);
var
FoundAt: LongInt;
StartPos, ToEnd: Integer;
begin
with RichEdit1 do
begin
{ begin the search after the current selection if there is one }
{ otherwise, begin at the start of the text }
if SelLength <> 0 then
StartPos := SelStart + SelLength
else
StartPos := 0;
{ ToEnd is the length from StartPos to the end of the text in the rich edit control }
ToEnd := Length(Text) - StartPos;
FoundAt := FindText(FindDialog1.FindText, StartPos, ToEnd, [stMatchCase]);
if FoundAt <> -1 then
begin
SetFocus;
SelStart := FoundAt;
SelLength := Length(FindDialog1.FindText);
end;
end;
end;
procedure TForm1.SpeedButton5Click(Sender: TObject);
begin
RichEdit1.CutToClipboard;
end;
procedure TForm1.SpeedButton6Click(Sender: TObject);
begin
RichEdit1.CopyToClipboard;
end;
procedure TForm1.SpeedButton7Click(Sender: TObject);
begin
RichEdit1.PasteFromClipboard;
end;
procedure TForm1.SpeedButton8Click(Sender: TObject);
begin
RichEdit1.Lines.Insert(RichEdit1.Lines.Count,DateToStr(date));
end;
procedure TForm1.RichEdit1Change(Sender: TObject);
begin
change:=1;
end;
procedure TForm1.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
if change=1 then
form3.ShowModal;
if cs=1 then
canclose:=False
else
canclose:=True;
end;
procedure TForm1.N6Click(Sender: TObject);
begin
SpeedButton1.Click;
end;
procedure TForm1.N7Click(Sender: TObject);
begin
SpeedButton2.Click;
end;
procedure TForm1.N8Click(Sender: TObject);
begin
SpeedButton3.Click;
end;
procedure TForm1.N9Click(Sender: TObject);
begin
if SaveDialog1.Execute then
RichEdit1.Lines.SaveToFile(SaveDialog1.FileName);
end;
procedure TForm1.N10Click(Sender: TObject);
begin
close;
end;
procedure TForm1.N12Click(Sender: TObject);
begin
SpeedButton6.Click;
end;
procedure TForm1.N13Click(Sender: TObject);
begin
SpeedButton7.Click;
end;
procedure TForm1.N14Click(Sender: TObject);
begin
if new=1 then
begin
if SaveDialog1.Execute then
RichEdit1.Lines.SaveToFile(SaveDialog1.FileName);
new:=0;
end
else
RichEdit1.Lines.SaveToFile(OpenDialog1.FileName);
change:=0;
RichEdit1.Lines.Clear;
end;
procedure TForm1.N15Click(Sender: TObject);
begin
RichEdit1.SelectAll;
end;
procedure TForm1.N16Click(Sender: TObject);
begin
SpeedButton4.Click;
end;
procedure TForm1.N18Click(Sender: TObject);
begin
RichEdit1.Lines.Insert(RichEdit1.Lines.Count,DateToStr(date));
end;
procedure TForm1.N17Click(Sender: TObject);
begin
RichEdit1.Lines.Insert(RichEdit1.Lines.Count,TimeToStr(Time));
end;
procedure TForm1.N19Click(Sender: TObject);
begin
if n19.Checked then
begin
Toolbar1.Visible:=False;
n19.Checked:=False;
end
else
begin
Toolbar1.Visible:=True;
n19.Checked:=True;
end;
end;
procedure TForm1.N20Click(Sender: TObject);
begin
RichEdit1.SelectAll;
end;
procedure TForm1.N22Click(Sender: TObject);
begin
RichEdit1.CutToClipboard;
end;
procedure TForm1.N21Click(Sender: TObject);
begin
RichEdit1.CopyToClipboard;
end;
procedure TForm1.N23Click(Sender: TObject);
begin
RichEdit1.PasteFromClipboard;
end;
procedure TForm1.N11Click(Sender: TObject);
begin
RichEdit1.Undo;
end;
procedure TForm1.RichEdit1KeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
//showmessage(inttostr(key));
if (key = Ord('J')) and (ssCtrl in Shift) then
begin
lb.Left := RichEdit1.CaretPos.x;
lb.Top := RichEdit1.CaretPos.y + lb.height;
lb.Visible := True;
lb.SetFocus;
end;
end;
procedure TForm1.lbKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
var
s:string;
f:Tstringlist;
begin
if key=13 then
begin
s:=RichEdit1.Lines.Strings[RichEdit1.Lines.Count-1];
if (lb.Items.Strings[Lb.ItemIndex]='Ïðîïóñòèòè') or (lb.Items.Strings[Lb.ItemIndex]='Çàïèñàòè â ñëîâíèê') then
begin
lb.Visible:=false;
lb.Items.LoadFromFile('dict.txt');
insert(' ',s,length(s));
RichEdit1.Lines.Strings[RichEdit1.Lines.Count-1]:=s;
RichEdit1.SelAttributes.Color:=clBlack;
RichEdit1.SetFocus;
RichEdit1.CutToClipboard;
//RichEdit1.PasteFromClipboard;
if lb.Items.Strings[Lb.ItemIndex]='Çàïèñàòè â ñëîâíèê' then
begin
f:=Tstringlist.Create;
f.LoadFromFile('dict.txt');
s:=delsp(sw);
f.Add(sw);
f.SaveToFile('dict.txt');
f.Free;
end;
exit;
end
else
delete(s,sspp+1,spp);
insert(' '+lb.Items.Strings[Lb.ItemIndex],s,length(s));
RichEdit1.Lines.Strings[RichEdit1.Lines.Count-1]:=s;
lb.Visible:=false;
lb.Items.LoadFromFile('dict.txt');
RichEdit1.SetFocus;
end;
end;
Вікно_1:
Перевірка орфографії
Висновок:
На цій лабораторній роботі я ознайомився із компонентом RichEdit, вивчив методи та властивості.