📄 main.pas
字号:
private
TargetPt: TPoint;
IsReadonly: Boolean;
function IsGraphSaved: Boolean;
procedure ShowHint(Sender: TObject);
function ForEachCallback(GraphObject: TGraphObject; Action: Integer): Boolean;
end;
var
MainForm: TMainForm;
implementation
{$R *.dfm}
uses
Clipbrd, Printers, DesignProp, ObjectProp, NodeProp, LinkProp, UsageHelp,
AboutDelphiArea, AlignDlg, SizeDlg;
resourcestring
SSaveChanges = 'Graph has been changed, would you like to save changes?';
SViewOnly = 'View Only';
SEditing = 'Editing';
SPan = 'Pan Mode';
SInsertingLink = 'Inserting Link/Line';
SInsertingNode = 'Inserting Node';
SModified = 'Modified';
SUntitled = 'Untitled';
SMultiSelect = '%d objects selected';
SNumOfPoints = '%d point(s)';
SStartPoint = 'startpoint';
SEndPoint = 'endpoint';
SNoName = 'NONAME';
SCanDelete = 'Are you sure to delete ''%s''?';
SCanHook = 'Are you sure to hook %s of ''%s'' link to ''%s'' object?';
SCanLink = 'Are you sure to connect ''%s'' and ''%s'' objects using ''%s'' link?';
SHooked = 'Hooked';
SNodeInfo = '%s Node: %s' + #13#10
+ 'Origin: %d, %d; Size: %d x %d';
SLinkInfo = 'Link: %s' + #13#10
+ 'Startpoint at: (%d, %d) %s' + #13#10
+ 'Endpoint at: (%d, %d) %s' + #13#10
+ 'Breakpoints Count: %d';
const
// ForEachObject Actions
FEO_DELETE = 00;
FEO_SELECT = 01;
FEO_INVERTSELECTION = 02;
FEO_SENDTOBACK = 03;
FEO_BRINGTOFRONT = 04;
FEO_MAKESELECTABLE = 05;
FEO_SETFONTFACE = 06;
FEO_SETFONTSIZE = 07;
FEO_SETFONTBOLD = 08;
FEO_SETFONTITALIC = 09;
FEO_SETFONTUNDERLINE = 10;
FEO_RESETFONTBOLD = 11;
FEO_RESETFONTITALIC = 12;
FEO_RESETFONTUNDERLINE = 13;
FEO_SETALIGNMENTLEFT = 14;
FEO_SETALIGNMENTCENTER = 15;
FEO_SETALIGNMENTRIGHT = 16;
FEO_SETLAYOUTTOP = 17;
FEO_SETLAYOUTCENTER = 18;
FEO_SETLAYOUTBOTTOM = 19;
FEO_REVERSEDIRECTION = 20;
FEO_ROTATE90CW = 21;
FEO_ROTATE90CCW = 22;
FEO_GROW25 = 23;
FEO_SHRINK25 = 24;
function TMainForm.ForEachCallback(GraphObject: TGraphObject; Action: Integer): Boolean;
var
RotateOrg: TPoint;
begin
Result := True;
case Action of
FEO_DELETE:
Result := GraphObject.Delete;
FEO_SELECT:
GraphObject.Selected := True;
FEO_INVERTSELECTION:
GraphObject.Selected := not GraphObject.Selected;
FEO_SENDTOBACK:
GraphObject.SendToBack;
FEO_BRINGTOFRONT:
GraphObject.BringToFront;
FEO_MAKESELECTABLE:
GraphObject.Options := GraphObject.Options + [goSelectable];
FEO_SETFONTFACE:
GraphObject.Font.Name := cbxFontName.Text;
FEO_SETFONTSIZE:
GraphObject.Font.Size := cbxFontSize.Tag;
FEO_SETFONTBOLD:
GraphObject.Font.Style := GraphObject.Font.Style + [fsBold];
FEO_SETFONTITALIC:
GraphObject.Font.Style := GraphObject.Font.Style + [fsItalic];
FEO_SETFONTUNDERLINE:
GraphObject.Font.Style := GraphObject.Font.Style + [fsUnderline];
FEO_RESETFONTBOLD:
GraphObject.Font.Style := GraphObject.Font.Style - [fsBold];
FEO_RESETFONTITALIC:
GraphObject.Font.Style := GraphObject.Font.Style - [fsItalic];
FEO_RESETFONTUNDERLINE:
GraphObject.Font.Style := GraphObject.Font.Style - [fsUnderline];
FEO_SETALIGNMENTLEFT:
if GraphObject is TGraphNode then
TGraphNode(GraphObject).Alignment := taLeftJustify;
FEO_SETALIGNMENTCENTER:
if GraphObject is TGraphNode then
TGraphNode(GraphObject).Alignment := taCenter;
FEO_SETALIGNMENTRIGHT:
if GraphObject is TGraphNode then
TGraphNode(GraphObject).Alignment := taRightJustify;
FEO_SETLAYOUTTOP:
if GraphObject is TGraphNode then
TGraphNode(GraphObject).Layout := tlTop;
FEO_SETLAYOUTCENTER:
if GraphObject is TGraphNode then
TGraphNode(GraphObject).Layout := tlCenter;
FEO_SETLAYOUTBOTTOM:
if GraphObject is TGraphNode then
TGraphNode(GraphObject).Layout := tlBottom;
FEO_REVERSEDIRECTION:
if GraphObject is TGraphLink then
TGraphLink(GraphObject).Reverse;
FEO_ROTATE90CW:
if GraphObject is TGraphLink then
with TGraphLink(GraphObject) do
begin
RotateOrg := CenterOfPoints(Polyline);
Rotate(+Pi / 2, RotateOrg);
end;
FEO_ROTATE90CCW:
if GraphObject is TGraphLink then
with TGraphLink(GraphObject) do
begin
RotateOrg := CenterOfPoints(Polyline);
Rotate(-Pi / 2, RotateOrg);
end;
FEO_GROW25:
if GraphObject is TGraphLink then
TGraphLink(GraphObject).Scale(1.25);
FEO_SHRINK25:
if GraphObject is TGraphLink then
TGraphLink(GraphObject).Scale(0.75);
else
Result := False;
end;
end;
function TMainForm.IsGraphSaved: Boolean;
begin
Result := True;
if SimpleGraph.Modified then
case MessageDlg(SSaveChanges, mtConfirmation, [mbYes, mbNo, mbCancel], 0) of
mrYes:
begin
FileSave.Execute;
Result := not SimpleGraph.Modified;
end;
mrCancel:
Result := False;
end;
end;
procedure TMainForm.ShowHint(Sender: TObject);
begin
StatusBar.Panels[StatusBar.Panels.Count - 1].Text := Application.Hint;
end;
procedure TMainForm.FormCreate(Sender: TObject);
begin
Application.OnHint := ShowHint;
SimpleGraphCommandModeChange(nil);
SimpleGraphZoomChange(nil);
cbxFontName.Items := Screen.Fonts;
if ParamCount > 0 then
begin
SimpleGraph.LoadFromFile(ParamStr(1));
SaveDialog.FileName := ExpandFileName(ParamStr(1));
Caption := SaveDialog.FileName + ' - ' + Application.Title;
end;
end;
procedure TMainForm.FileNewExecute(Sender: TObject);
begin
if IsGraphSaved then
begin
SimpleGraph.Clear;
SimpleGraph.Zoom := 100;
SimpleGraph.CommandMode := cmEdit;
IsReadonly := False;
SaveDialog.FileName := SUntitled;
Caption := SaveDialog.FileName + ' - ' + Application.Title;
end;
end;
procedure TMainForm.FileOpenExecute(Sender: TObject);
begin
OpenDialog.Options := OpenDialog.Options - [ofHideReadOnly];
if IsGraphSaved and OpenDialog.Execute then
begin
SimpleGraph.LoadFromFile(OpenDialog.FileName);
SimpleGraph.Zoom := 100;
IsReadonly := ofReadonly in OpenDialog.Options;
if IsReadonly then
SimpleGraph.CommandMode := cmViewOnly
else
SimpleGraph.CommandMode := cmEdit;
SaveDialog.FileName := OpenDialog.FileName;
Caption := SaveDialog.FileName + ' - ' + Application.Title;
end;
end;
procedure TMainForm.FileMergeUpdate(Sender: TObject);
begin
FileMerge.Enabled := not IsReadonly and (SimpleGraph.Objects.Count > 0);
end;
procedure TMainForm.FileMergeExecute(Sender: TObject);
begin
OpenDialog.Options := OpenDialog.Options + [ofHideReadOnly];
if OpenDialog.Execute then
with SimpleGraph do
MergeFromFile(OpenDialog.FileName, 0, GraphBounds.Bottom + 4 * GridSize);
end;
procedure TMainForm.FileSaveUpdate(Sender: TObject);
begin
FileSave.Enabled := SimpleGraph.Modified;
end;
procedure TMainForm.FileSaveExecute(Sender: TObject);
begin
if SaveDialog.FileName <> SUntitled then
begin
SimpleGraph.SaveToFile(SaveDialog.FileName);
Caption := SaveDialog.FileName + ' - ' + Application.Title;
end
else
begin
if SaveDialog.Execute then
SimpleGraph.SaveToFile(SaveDialog.FileName);
end;
Caption := SaveDialog.FileName + ' - ' + Application.Title;
end;
procedure TMainForm.FileSaveAsUpdate(Sender: TObject);
begin
FileSaveAs.Enabled := (SimpleGraph.Objects.Count > 0);
end;
procedure TMainForm.FileSaveAsExecute(Sender: TObject);
begin
if SaveDialog.Execute then
begin
SimpleGraph.SaveToFile(SaveDialog.FileName);
SimpleGraph.CommandMode := cmEdit;
IsReadonly := False;
Caption := SaveDialog.FileName + ' - ' + Application.Title;
end;
end;
procedure TMainForm.ExportMetafileUpdate(Sender: TObject);
begin
ExportMetafile.Enabled := (SimpleGraph.Objects.Count > 0);
end;
procedure TMainForm.ExportMetafileExecute(Sender: TObject);
begin
SavePictureDialog.Filter := GraphicFilter(TMetafile);
SavePictureDialog.DefaultExt := GraphicExtension(TMetafile);
SavePictureDialog.FileName := ChangeFileExt(SaveDialog.FileName, '.' + SavePictureDialog.DefaultExt);
if SavePictureDialog.Execute then
SimpleGraph.SaveAsMetafile(SavePictureDialog.FileName);
end;
procedure TMainForm.ExportBitmapUpdate(Sender: TObject);
begin
ExportBitmap.Enabled := (SimpleGraph.Objects.Count > 0);
end;
procedure TMainForm.ExportBitmapExecute(Sender: TObject);
begin
SavePictureDialog.Filter := GraphicFilter(TBitmap);
SavePictureDialog.DefaultExt := GraphicExtension(TBitmap);
SavePictureDialog.FileName := ChangeFileExt(SaveDialog.FileName, '.' + SavePictureDialog.DefaultExt);
if SavePictureDialog.Execute then
SimpleGraph.SaveAsBitmap(SavePictureDialog.FileName);
end;
procedure TMainForm.FilePrintUpdate(Sender: TObject);
begin
FilePrint.Enabled :=(Printer.Printers.Count > 0) and (SimpleGraph.Objects.Count > 0);
end;
procedure TMainForm.FilePrintExecute(Sender: TObject);
var
Rect: TRect;
begin
if PrinterSetupDialog.Execute then
begin
SetRect(Rect, 0, 0, Printer.PageWidth, Printer.PageHeight);
InflateRect(Rect, -50, -50);
Printer.Title := Application.Title;
Printer.BeginDoc;
try
SimpleGraph.Print(Printer.Canvas, Rect);
finally
Printer.EndDoc;
end;
end;
end;
procedure TMainForm.FileExitExecute(Sender: TObject);
begin
Close;
end;
procedure TMainForm.EditCutUpdate(Sender: TObject);
begin
EditCut.Enabled := not IsReadonly and (SimpleGraph.SelectedObjects.Count > 0);
end;
procedure TMainForm.EditCutExecute(Sender: TObject);
begin
EditCopy.Execute;
EditDelete.Execute;
end;
procedure TMainForm.EditCopyUpdate(Sender: TObject);
begin
EditCopy.Enabled :=(SimpleGraph.SelectedObjects.Count > 0);
end;
procedure TMainForm.EditCopyExecute(Sender: TObject);
begin
SimpleGraph.CopyToClipboard(True);
end;
procedure TMainForm.EditPasteUpdate(Sender: TObject);
begin
EditPaste.Enabled := not IsReadonly and Clipboard.HasFormat(CF_SIMPLEGRAPH);
end;
procedure TMainForm.EditPasteExecute(Sender: TObject);
begin
SimpleGraph.PasteFromClipboard;
end;
procedure TMainForm.EditDeleteUpdate(Sender: TObject);
begin
EditDelete.Enabled := not IsReadonly and (SimpleGraph.SelectedObjects.Count > 0);
end;
procedure TMainForm.EditDeleteExecute(Sender: TObject);
begin
SimpleGraph.ForEachObject(ForEachCallback, FEO_DELETE, True);
end;
procedure TMainForm.EditSelectAllUpdate(Sender: TObject);
begin
EditSelectAll.Enabled := not IsReadonly and
(SimpleGraph.Objects.Count > SimpleGraph.SelectedObjects.Count);
end;
procedure TMainForm.EditSelectAllExecute(Sender: TObject);
begin
SimpleGraph.ForEachObject(ForEachCallback, FEO_SELECT, False);
end;
procedure TMainForm.EditInvertSelectionUpdate(Sender: TObject);
begin
EditInvertSelection.Enabled := not IsReadonly and (SimpleGraph.Objects.Count > 0);
end;
procedure TMainForm.EditInvertSelectionExecute(Sender: TObject);
begin
SimpleGraph.ForEachObject(ForEachCallback, FEO_INVERTSELECTION, False);
end;
procedure TMainForm.EditMakeAllSelectableUpdate(Sender: TObject);
begin
EditMakeAllSelectable.Enabled := not IsReadonly and (SimpleGraph.Objects.Count > 0);
end;
procedure TMainForm.EditMakeAllSelectableExecute(Sender: TObject);
begin
SimpleGraph.ForEachObject(ForEachCallback, FEO_MAKESELECTABLE, False);
end;
procedure TMainForm.EditSendToBackUpdate(Sender: TObject);
begin
EditSendToBack.Enabled := not IsReadonly and (SimpleGraph.SelectedObjects.Count > 0);
end;
procedure TMainForm.EditSendToBackExecute(Sender: TObject);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -