📄 editormainformu.pas
字号:
{ ignore exceptions }
end;
StopWait;
end;
end;
procedure TEditorMainForm.HelpAbout(Sender: TObject);
begin
with TJvShellAboutDialog.Create(Application) do
try
Caption := 'RichEdit 2.0 Control Demo';
OtherText := 'RX Library Demo Program';
Execute;
finally
Free;
end;
FocusEditor;
end;
procedure TEditorMainForm.HiddenItemClick(Sender: TObject);
begin
if FUpdating then
Exit;
CurrText.Hidden := not CurrText.Hidden;
HiddenItem.Checked := CurrText.Hidden;
end;
function TEditorMainForm.IndentToRuler(Indent: Integer; IsRight: Boolean): Integer;
var
R: TRect;
P: TPoint;
begin
Indent := Trunc(Indent * RulerAdj);
with Editor do
begin
SendMessage(Handle, EM_GETRECT, 0, Longint(@R));
if IsRight then
begin
P := R.BottomRight;
P.X := P.X - Indent;
end
else
begin
P := R.TopLeft;
P.X := P.X + Indent;
end;
P := ClientToScreen(P);
end;
with Ruler do
P := ScreenToClient(P);
Result := P.X;
end;
procedure TEditorMainForm.InsertBitmap(Sender: TObject);
var
Pict: TPicture;
begin
with FOpenPictureDialog do
begin
if Execute then
begin
Pict := TPicture.Create;
try
Pict.LoadFromFile(FileName);
Clipboard.Assign(Pict);
Editor.PasteFromClipboard;
finally
Pict.Free;
end;
end;
end;
end;
procedure TEditorMainForm.InsertObject(Sender: TObject);
begin
Editor.InsertObjectDialog;
end;
procedure TEditorMainForm.ItalicButtonClick(Sender: TObject);
begin
if FUpdating then
Exit;
if ItalicBtn.Down then
CurrText.Style := CurrText.Style + [fsItalic]
else
CurrText.Style := CurrText.Style - [fsItalic];
end;
procedure TEditorMainForm.LeftIndMouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
FDragging := False;
if FLineVisible then
DrawLine;
DeleteObject(SelectObject(FLineDC, FLinePen));
ReleaseDC(Editor.Handle, FLineDC);
Editor.Paragraph.LeftIndent := Max(-Editor.Paragraph.FirstIndent,
RulerToIndent(LeftInd.Left + FDragOfs, False) -
Editor.Paragraph.FirstIndent);
SelectionChange(Sender);
end;
procedure TEditorMainForm.MainMenuGetImageIndex(Sender: TMenu; Item: TMenuItem;
State: TMenuOwnerDrawState; var ImageIndex: Integer);
begin
if Item.Tag >= 0 then
ImageIndex := Item.Tag;
end;
procedure TEditorMainForm.PerformFileOpen(const AFileName: string);
begin
FProtectChanging := True;
try
Editor.Lines.LoadFromFile(AFileName);
finally
FProtectChanging := False;
end;
SetFileName(AFileName);
Editor.SetFocus;
Editor.Modified := False;
SetModified(False);
end;
procedure TEditorMainForm.ProtectedItemClick(Sender: TObject);
begin
if FUpdating then
Exit;
FProtectChanging := True;
try
CurrText.Protected := not CurrText.Protected;
ProtectedItem.Checked := CurrText.Protected;
finally
FProtectChanging := False;
end;
end;
procedure TEditorMainForm.RichEditChange(Sender: TObject);
begin
SetModified(Editor.Modified);
{ Undo }
UndoBtn.Enabled := Editor.CanUndo;
EditUndoItem.Enabled := UndoBtn.Enabled;
EditUndoItem.Caption := '&Undo ' + UndoNames[Editor.UndoName];
{ Redo }
EditRedoItem.Enabled := Editor.CanRedo;
RedoBtn.Enabled := EditRedoItem.Enabled;
EditRedoItem.Caption := '&Redo ' + UndoNames[Editor.RedoName];
end;
procedure TEditorMainForm.RightIndMouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
FDragging := False;
if FLineVisible then
DrawLine;
DeleteObject(SelectObject(FLineDC, FLinePen));
ReleaseDC(Editor.Handle, FLineDC);
Editor.Paragraph.RightIndent := Max(0, RulerToIndent(RightInd.Left + FDragOfs,
True));
SelectionChange(Sender);
end;
procedure TEditorMainForm.RulerItemMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
FDragOfs := (TControl(Sender).Width div 2);
TControl(Sender).Left := Max(0, TControl(Sender).Left + X - FDragOfs);
FLineDC := GetDCEx(Editor.Handle, 0, DCX_CACHE or DCX_CLIPSIBLINGS
or DCX_LOCKWINDOWUPDATE);
FLinePen := SelectObject(FLineDC, CreatePen(PS_DOT, 1, ColorToRGB(clWindowText)));
SetROP2(FLineDC, R2_XORPEN);
CalcLineOffset(TControl(Sender));
DrawLine;
FDragging := True;
end;
procedure TEditorMainForm.RulerItemMouseMove(Sender: TObject; Shift: TShiftState;
X, Y: Integer);
begin
if FDragging then
begin
DrawLine;
TControl(Sender).Left := Min(Max(0, TControl(Sender).Left + X - FDragOfs),
Ruler.ClientWidth - FDragOfs * 2);
CalcLineOffset(TControl(Sender));
DrawLine;
end;
end;
procedure TEditorMainForm.RulerResize(Sender: TObject);
begin
RulerLine.Width := Ruler.ClientWidth - (RulerLine.Left * 2);
end;
function TEditorMainForm.RulerToIndent(RulerPos: Integer; IsRight: Boolean): Integer;
var
R: TRect;
P: TPoint;
begin
P.Y := 0;
P.X := RulerPos;
with Ruler do
P := ClientToScreen(P);
with Editor do
begin
P := ScreenToClient(P);
SendMessage(Handle, EM_GETRECT, 0, Longint(@R));
if IsRight then
Result := R.BottomRight.X - P.X
else
Result := P.X - R.TopLeft.X;
end;
Result := Trunc(Result / RulerAdj);
end;
procedure TEditorMainForm.SelectFont(Sender: TObject);
begin
FontDialog.Font.Assign(Editor.SelAttributes);
if FontDialog.Execute then
CurrText.Assign(FontDialog.Font);
FocusEditor;
end;
procedure TEditorMainForm.SelectionChange(Sender: TObject);
begin
with Editor.Paragraph do
try
FUpdating := True;
FirstInd.Left := IndentToRuler(FirstIndent, False) - (FirstInd.Width div 2);
LeftInd.Left := IndentToRuler(LeftIndent + FirstIndent, False) - (LeftInd.Width div 2);
RightInd.Left := IndentToRuler(RightIndent, True) - (RightInd.Width div 2);
BoldBtn.Down := fsBold in CurrText.Style;
ItalicBtn.Down := fsItalic in CurrText.Style;
UnderlineBtn.Down := fsUnderline in CurrText.Style;
BulletsBtn.Down := Boolean(Numbering);
SuperscriptBtn.Down := CurrText.SubscriptStyle = ssSuperscript;
SubscriptBtn.Down := CurrText.SubscriptStyle = ssSubscript;
FontSize.AsInteger := CurrText.Size;
FontName.FontName := CurrText.Name;
ProtectedItem.Checked := CurrText.Protected;
DisabledItem.Checked := CurrText.Disabled;
HiddenItem.Checked := CurrText.Hidden;
case Ord(Alignment) of
0: LeftBtn.Down := True;
1: RightBtn.Down := True;
2: CenterBtn.Down := True;
3: JustifyBtn.Down := True;
end;
UpdateCursorPos;
finally
FUpdating := False;
end;
end;
procedure TEditorMainForm.SetEditRect;
var
R: TRect;
Offs: Integer;
begin
with Editor do
begin
if SelectionBar then
Offs := 3
else
Offs := 0;
R := Rect(GutterWid + Offs, 0, ClientWidth - GutterWid, ClientHeight);
SendMessage(Handle, EM_SETRECT, 0, Longint(@R));
end;
end;
procedure TEditorMainForm.SetFileName(const FileName: string);
begin
FFileName := FileName;
Editor.Title := ExtractFileName(FileName);
Caption := Format('%s - %s', [ExtractFileName(FileName), Application.Title]);
end;
procedure TEditorMainForm.SetModified(Value: Boolean);
begin
if Value then
StatusBar.Panels[1].Text := 'Modified'
else
StatusBar.Panels[1].Text := '';
end;
procedure TEditorMainForm.SetupRuler;
var
I: Integer;
S: string;
begin
SetLength(S, 201);
I := 1;
while I < 200 do
begin
S[I] := #9;
S[I + 1] := '|';
Inc(I, 2);
end;
Ruler.Caption := S;
end;
procedure TEditorMainForm.ShowHint(Sender: TObject);
begin
if Length(Application.Hint) > 0 then
begin
StatusBar.SimplePanel := True;
StatusBar.SimpleText := Application.Hint;
end
else
StatusBar.SimplePanel := False;
end;
procedure TEditorMainForm.SubscriptClick(Sender: TObject);
begin
if FUpdating then
Exit;
if SuperscriptBtn.Down then
CurrText.SubscriptStyle := ssSuperscript
else
if SubscriptBtn.Down then
CurrText.SubscriptStyle := ssSubscript
else
CurrText.SubscriptStyle := ssNone;
end;
procedure TEditorMainForm.UnderlineButtonClick(Sender: TObject);
begin
if FUpdating then
Exit;
if UnderlineBtn.Down then
CurrText.Style := CurrText.Style + [fsUnderline]
else
CurrText.Style := CurrText.Style - [fsUnderline];
end;
procedure TEditorMainForm.UpdateCursorPos;
var
CharPos: TPoint;
begin
CharPos := Editor.CaretPos;
StatusBar.Panels[0].Text := Format('Line: %3d Col: %3d',
[CharPos.Y + 1, CharPos.X + 1]);
{ update the status of the cut and copy command }
CopyBtn.Enabled := Editor.SelLength > 0;
EditCopyItem.Enabled := CopyBtn.Enabled;
CopyItm.Enabled := CopyBtn.Enabled;
CutBtn.Enabled := EditCopyItem.Enabled;
CutItm.Enabled := CutBtn.Enabled;
FileSaveSelItem.Enabled := CopyBtn.Enabled;
EditCutItem.Enabled := EditCopyItem.Enabled;
EditObjPropsItem.Enabled := Editor.SelectionType = [stObject];
end;
procedure TEditorMainForm.WMDropFiles(var Msg: TWMDropFiles);
var
cFileName: array[0..MAX_PATH] of Char;
begin
try
if DragQueryFile(Msg.Drop, 0, cFileName, MAX_PATH) > 0 then
begin
Application.BringToFront;
CheckFileSave;
PerformFileOpen(cFileName);
Msg.Result := 0;
end;
finally
DragFinish(Msg.Drop);
end;
end;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -