📄 editor.pas
字号:
unit Editor;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ComCtrls, StdCtrls, Menus,utils;
type
TWrAction= (wrInsert, wrDelete, wrRename);
TEditorForm = class(TForm)
StatusBar: TStatusBar;
PageControl1: TPageControl;
TabSheet1: TTabSheet;
RichEdit1: TRichEdit;
PopupMenu1: TPopupMenu;
NClosePage: TMenuItem;
NInsert: TMenuItem;
OpenDialog1: TOpenDialog;
N1: TMenuItem;
N2: TMenuItem;
NReadOnly: TMenuItem;
procedure RichEdit1KeyUp(Sender: TObject; var Key: Word;
Shift: TShiftState);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure RichEdit1Change(Sender: TObject);
procedure PageControl1Change(Sender: TObject);
procedure NInsertClick(Sender: TObject);
procedure NClosePageClick(Sender: TObject);
procedure NReadOnlyClick(Sender: TObject);
private
{ Private declarations }
function GetStringPos(const S:string):Integer;
procedure HandleMessage(var Msg: TMessage);
protected
procedure WndProc(var Msg: TMessage); override;
public
{ Public declarations }
procedure WriteComponentCode(Component:string;ComponentClass:
TComponentClass;Action:TWrAction);
procedure WriteEventCode(Event:string);
procedure GotoLine(RichEdit:TRichEdit;Line:Integer);
function GetLinkEditor:TRichEdit;
function MoveToString(const FindStr:string):Boolean;
procedure RenameUnit(UnitName:string);
end;
var
EditorForm: TEditorForm;
implementation
uses MainForm,Uconst, utype;
var
Insert:boolean;
{$R *.DFM}
procedure TEditorForm.HandleMessage(var Msg: TMessage);
begin
if Msg.Msg = WM_SYSCOMMAND then begin
if Msg.WParam =SC_MAXIMIZE then begin
// 弥措拳 滚瓢阑 穿弗 版快, 俊叼磐狼 农扁甫 炼例茄促.
Top:=FMainForm.Top+ FMainForm.Height;
Left:=0;
Width:= Screen.Width;
Height:= Screen.Height - Top;
Msg.Msg:= WM_NULL;
end;
end;
end;
procedure TEditorForm.WndProc(var Msg: TMessage);
begin
// HandleMessage(Msg);
inherited WndProc(Msg);
end;
procedure TEditorForm.RichEdit1KeyUp(Sender: TObject; var Key: Word;
Shift: TShiftState);
var
LinePos,LineWidth,CaretPos,RowCount:integer;
begin
{泅犁 某返狼 困摹甫 炼荤茄促.}
LineWidth:=SendMessage((ActiveControl as TRichEdit).Handle, EM_LINEINDEX, -1,0);
CaretPos:=(ActiveControl as TRichEdit).SelStart-LineWidth+1;
StatusBar.Panels[0].Text:=IntToStr(CaretPos)+'Col';
{傈眉 青 吝俊辑 泅犁 青狼 困摹甫 炼荤茄促.}
RowCount:=SendMessage((ActiveControl as TRichEdit).Handle, EM_LINEFROMCHAR, -1,0);
StatusBar.Panels[1].Text:=inttostr(RowCount)+'/'+inttostr((ActiveControl as TRichEdit).Lines.Count);
if (Key=VK_INSERT) then begin
Insert:=not Insert;
if Insert then StatusBar.Panels[2].Text:='Insert'
else StatusBar.Panels[2].Text:='Modified';
end
else if (ssCtrl in Shift) and (Key=Word('Y')) then begin {泅犁 青 昏力}
LinePos:=SendMessage((ActiveControl as TRichEdit).Handle, EM_LINEFROMCHAR, -1,0);
RichEdit1.Lines.Delete(LinePos);
end
else if (ssCtrl in Shift) and (Key=VK_NEXT) then {付瘤阜 青栏肺 捞悼}
SendMessage((ActiveControl as TRichEdit).Handle, EM_LINESCROLL,0,(ActiveControl as TRichEdit).Lines.Count)
else if (ssCtrl in Shift) and (Key=VK_PRIOR) then {盖贸澜 青栏肺 捞悼}
SendMessage((ActiveControl as TRichEdit).Handle, EM_LINESCROLL,0,-(ActiveControl as TRichEdit).Lines.Count);
end;
procedure TEditorForm.FormClose(Sender: TObject; var Action: TCloseAction);
var
I,SaveResp:integer;
FileName:string;
begin
for I := PageControl1.PageCount-1 downto 0 do begin
FileName:=(PageControl1.Pages[I]).Caption;
if (PageControl1.Pages[I].Controls[0] as TRichEdit).Modified then begin
SaveResp := MessageDlg(Format('Save Changes to %s?',
[ExtractFilename(FileName)]), mtWarning, mbYesNoCancel, 0);
case SaveResp of
idYes: FMainForm.SaveFile(ProjectPath+Caption);
idNo: ;
idCancel: Abort;
end;
end;
end;
end;
// 俊叼磐俊 荐沥捞 惯积茄 版快 橇肺璃飘狼 惑怕客 滚瓢狼 荤侩 咯何甫 搬沥茄促.
procedure TEditorForm.RichEdit1Change(Sender: TObject);
begin
// 蜡聪飘甫 贸澜俊 佬绰 版快俊绰 函拳甫 公矫茄促.
if not TRichEdit(Sender).Modified then Exit;
StatusBar.Panels[3].Text:='Modified';
ProjectInfo.ProjectState:=psChange;
FMainForm.sbSaveAll.Enabled:=True;
end;
procedure TEditorForm.PageControl1Change(Sender: TObject);
begin
// 汽狼 鸥捞撇阑 泅犁 祈笼窍绊 乐绰 颇老疙栏肺 焊咯霖促.
if ExtractFileExt(PageControl1.ActivePage.Caption)='' then
// 祈笼窍绊 乐绰 颇老捞 蜡聪飘 颇老捞搁 pas 犬厘磊甫 眠啊茄促.
Caption:=PageControl1.ActivePage.Caption+'.pas'
else Caption:=PageControl1.ActivePage.Caption;
FMainForm.SelectUnit:= Caption;
ActiveRichEdit:=TRichEdit(PageControl1.ActivePage.Controls[0]);
end;
procedure TEditorForm.NInsertClick(Sender: TObject);
var
List:TStringList;
I:Integer;
begin
if OpenDialog1.Execute then
begin
List:=TStringList.Create;
List.LoadFromFile(OpenDialog1.FileName);
for I:=0 to List.Count-1 do
(ActiveControl as TRichEdit).Lines.Insert(1,List[I]);
end;
end;
procedure TEditorForm.NClosePageClick(Sender: TObject);
begin
PageControl1.ActivePage.Destroy;
PageControl1.SetFocus;
end;
procedure TEditorForm.NReadOnlyClick(Sender: TObject);
begin
NReadOnly.Checked:=not NReadOnly.Checked;
(ActiveControl as TRichEdit).ReadOnly:=NReadOnly.Checked;
if NReadOnly.Checked then StatusBar.Panels[2].Text:='ReadOnly'
else StatusBar.Panels[2].Text:='';
end;
procedure TEditorForm.WriteComponentCode(Component:string;ComponentClass:
TComponentClass;Action:TWrAction);
const
ClassName= 'TForm';
var
InsertPos: Integer;
RichEdit: TRichEdit;
// 哪欺惩飘甫 眠啊 , 昏力 ,函版窍绰 秦寸 内靛甫 累己 肚绰 昏力,函版茄促.
begin
RichEdit:=GetLinkEditor;
if Action=wrInsert then
InsertPos:=GetStringPos(ClassName) // 内靛甫 火涝秦具 且 青阑 炼荤茄促.
else
InsertPos:=GetStringPos(Component);
if InsertPos=0 then Exit;
case Action of
wrInsert: // 内靛甫 累己窍绰 版快
begin
RichEdit.Lines.Insert(InsertPos+1,' '+Component+':'+ComponentClass.ClassName+';');
end;
wrDelete: // 内靛甫 昏力窍绰 版快
RichEdit.Lines.Delete(InsertPos);
end;
end;
function TEditorForm.GetStringPos(const S:string):Integer;
// 家胶 内靛俊辑 漂沥茄 巩磊凯 S啊 乐绰 青狼 困摹甫 舅妨霖促.
var
I: Integer;
RichEdit: TRichEdit;
begin
Result:=0;
RichEdit:=GetLinkEditor;
with RichEdit do begin
for I:=0 to Lines.Count-1 do
if Pos(S,Lines[I])>0 then begin
Result:=I;
Exit;
end;
end;
end;
// 捞亥飘 捞抚 Event俊 措茄 扁夯 内靛甫 累己茄促.
procedure TEditorForm.WriteEventCode(Event:string);
var
RichEdit: TRichEdit;
begin
RichEdit:=GetLinkEditor;
with RichEdit.Lines do begin
Insert(Count-2,'');
Insert(Count-2,'procedure '+'T'+Name+'.'+Event);
Insert(Count-2,'begin');
Insert(Count-2,'');
Insert(Count-2,'end;');
end;
RichEdit.SetFocus;
GotoLine(RichEdit,RichEdit.Lines.Count-3);
end;
procedure TEditorForm.GotoLine(RichEdit:TRichEdit;Line:Integer);
var
NewLine,LinePos,CurLine:integer;
begin
// 漂沥茄 青 锅龋肺 捞悼茄促.
RichEdit.SelStart :=SendMessage(RichEdit.handle, EM_LINEINDEX, Line, 0) - 1;
end;
function TEditorForm.GetLinkEditor:TRichEdit;
var
I:Integer;
begin
// 急琶茄 汽苞 蜡聪飘俊 楷搬等 RichEdit甫 茫绰促.
Result:=TRichEdit(PageControl1.ActivePage.Controls[0]);
for I:=0 to PageControl1.PageCount-1 do
if TTabSheet(PageControl1.Pages[I]).Caption=FMainForm.SelectUnit then
Result:=TRichEdit(PageControl1.Pages[I].Controls[0]);
end;
// 家胶 内靛俊辑 巩磊凯 FindStr阑 茫篮 促澜 秦寸 青栏肺 捞悼茄促.
function TEditorForm.MoveToString(const FindStr:string):Boolean;
var
RichEdit: TRichEdit;
FindPos:Integer;
begin
Result:=True;
RichEdit:=GetLinkEditor;
FindPos:=GetStringPos(FindStr);
RichEdit.SetFocus;
GotoLine(RichEdit,FindPos+3);
end;
// unit狼 捞抚阑 UnitName栏肺 官槽促.
procedure TEditorForm.RenameUnit(UnitName:string);
var
RichEdit:TRichEdit;
Name:string;
begin
RichEdit:= GetLinkEditor;
Name:= RichEdit.Lines[0];
if Pos('unit', Name) > 0 then
RichEdit.Lines[0]:= Format('%s %s;',['unit',GetNetFileName(UnitName)]);
end;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -