Advertisement
believe_me

Untitled

May 27th, 2022
436
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 21.22 KB | None | 0 0
  1. unit GraphUnit;
  2.  
  3. interface
  4.  
  5. uses  Vcl.GRids, System.SysUtils, System.Generics.Collections;
  6.  
  7. type
  8.     TMatrix = array of array of integer;
  9.     TVertex = record
  10.         number, dist: Integer;
  11.     end;
  12.     TVertexArray = array of TVertex;
  13.     TIncidentList = TList<TVertex>;
  14.     TListArray = array of TIncidentList;
  15.     TVertexPointer = ^TVertex;
  16.  
  17.     function IsValidStr(MaxVertexNumber: integer; const Buffer: string; CurrentVertex: integer): boolean;
  18.     procedure ConvertGraphInString(var GraphText: string; var AdjacencyMatrix: TMatrix; var DistMatrix:
  19.                               TMatrix; var PrecMatrix: TMatrix; VertexNumber: integer);
  20.     procedure SaveGraph(const GraphText: String);
  21.     procedure receiveListArray(var ListArray: TListArray; IncidentListStringGrid: TStringGrid);
  22.     procedure receiveArrayOfVertices(var VertexArray: TVertexArray; const Buffer: string);
  23.     procedure receiveMatrices(var AdjacencyMatrix: TMatrix; var DistMatrix:
  24.                               TMatrix; var PrecMatrix: TMatrix);
  25.     procedure receiveAdjacencyMatrix(var ListArray: TListArray; var AdjacencyMatrix: TMatrix);
  26.     procedure receivePrecMatrix(Size: integer; var PrecMatrix: TMatrix);
  27.     procedure receiveDistMatrix(Size: integer; var DistMatrix: TMatrix);
  28. var
  29.     ListArray: TListArray;
  30.  
  31. const
  32.     INT_MAX = 100;
  33.     GRAPH_FILE = 'C:\Users\user\Desktop\71del\7.1\GraphsFiles\text_graph.gv';
  34.     GRAPH_PICTURE_FILE = 'C:\Users\user\Desktop\71del\7.1\GraphsFiles\picture.png';
  35.     CREATE_GRAPH_PICTURE_COMMAND = '/C "C:\graphviz\bin\dot.exe" -Tpng C:\Users\user\Desktop\71del\7.1\GraphsFiles\text_graph.gv -o C:\Users\user\Desktop\71del\7.1\GraphsFiles\picture.png';
  36.  
  37. implementation
  38. Var
  39.     Time: Integer;
  40.  
  41. procedure AddtoList(var ListArray: TListArray; Vertex: TVertex; Index: Integer);
  42. begin
  43.     ListArray[Index].Add(Vertex);
  44. end;
  45.  
  46. function IsValidStr(MaxVertexNumber: integer; const Buffer: string; CurrentVertex: integer): Boolean;
  47. Const
  48.     MIN_V = 1;
  49.     VALID_CHARS = ['0'..'9', ' ', '(', ')'];
  50.     DIGITS = ['0'..'9'];
  51.  
  52. function IsNumberCorrect(StartVertex: integer; MaxVertexNumber: integer; CurrentVertex: integer): Boolean;
  53. const
  54.     MIN_VERTEX_NUMBER = 1;
  55. begin
  56.     Result := (CurrentVertex <= MaxVertexNumber) and (CurrentVertex >= MIN_VERTEX_NUMBER)
  57.                                                 and (CurrentVertex <> StartVertex);
  58. end;
  59. var
  60.     i, j, VertexNumber, Dist: Integer;
  61.     IsCorrectString: Boolean;
  62.     VertexNumberString, DistString: string;
  63. begin
  64.     IsCorrectString := True;
  65.     i := 1;
  66.     while (i <= Length(Buffer)) and IsCorrectString do
  67.     begin
  68.         if not (Buffer[i] in VALID_CHARS) then
  69.             IsCorrectString := false
  70.         else
  71.         begin
  72.             if Buffer[i] <> ' ' then
  73.             begin
  74.                 j := i;
  75.                 while (j <= Length(Buffer)) and IsCorrectString and (Buffer[j] <> '(') do
  76.                     if not (Buffer[j] in DIGITS) then
  77.                         IsCorrectString := false
  78.                     else
  79.                     begin
  80.                         VertexNumberString := VertexNumberString + Buffer[j];
  81.                         inc(j);
  82.                     end;
  83.                 inc(j);
  84.                 while (j <= Length(Buffer)) and IsCorrectString and (Buffer[j] <> ')') do
  85.                     if not (Buffer[j] in DIGITS) then
  86.                         IsCorrectString := false
  87.                     else
  88.                     begin
  89.                         DistString := DistString + Buffer[j];
  90.                         inc(j);
  91.                     end;
  92.                 if DistString = '' then
  93.                     IsCorrectString := false
  94.                 else
  95.                 begin
  96.                     try
  97.                         VertexNumber := strToInt(VertexNumberString);
  98.                         dist := strToInt(DistString);
  99.                     except
  100.                         IsCorrectString := false;
  101.                     end;
  102.                     if IsCorrectString then
  103.                         IsCorrectString := IsNumberCorrect(CurrentVertex,
  104.                           MaxVertexNumber, VertexNumber);
  105.                     VertexNumberString := '';
  106.                     DistString := '';
  107.                     i := j;
  108.                     inc(i);
  109.                 end;
  110.             end;
  111.         end;
  112.         inc(i);
  113.     end;
  114.     Result := IsCorrectString;
  115. end;
  116.  
  117.  
  118. procedure receiveArrayOfVertices(var VertexArray: TVertexArray; const Buffer: string);
  119. var
  120.     i, j, t, VertexNumber, Dist: integer;
  121.     VertexNumberString, DistString: string;
  122.     CurrentVertex: TVertex;
  123. begin
  124.     i := 1;
  125.     t := 0;
  126.     while (i <= Length(Buffer)) do
  127.     begin
  128.         if Buffer[i] <> ' ' then
  129.         begin
  130.             j := i;
  131.             while (j <= Length(Buffer)) and (Buffer[j] <> '(') do
  132.             begin
  133.                 VertexNumberString := VertexNumberString + Buffer[j];
  134.                 inc(j);
  135.             end;
  136.             inc(j);
  137.             while (j <= Length(Buffer)) and (Buffer[j] <> ')') do
  138.             begin
  139.                 DistString := DistString + Buffer[j];
  140.                 inc(j);
  141.             end;
  142.             if VertexNumberString <> '' then
  143.             begin
  144.                 CurrentVertex.number := strToInt(VertexNumberString);
  145.                 CurrentVertex.dist := strToInt(DistString);
  146.                 setlength(VertexArray, length(VertexArray) + 1);
  147.                 VertexArray[t] := CurrentVertex;
  148.                 inc(t);
  149.             end;
  150.             VertexNumberString := '';
  151.             DistString := '';
  152.             i := j;
  153.         end;
  154.         inc(i);
  155.     end;
  156. end;
  157.  
  158. procedure receiveListArray(var ListArray: TListArray; IncidentListStringGrid: TStringGrid);
  159. var
  160.     i, j: integer;
  161.     VertexArray: TVertexArray;
  162.     Buffer: string;
  163.     CurrentList: TIncidentList;
  164. begin
  165.     setlength(ListArray, (IncidentListStringGrid.RowCount - 1));
  166.     for i := 1 to (IncidentListStringGrid.RowCount - 1) do
  167.     begin
  168.         Buffer := IncidentListStringGrid.Cells[1, i];
  169.         receiveArrayOfVertices(VertexArray, Buffer);
  170.         ListArray[i - 1] := TIncidentList.Create;
  171.         for j := 0 to high(VertexArray) do
  172.         begin
  173.             AddtoList(ListArray, VertexArray[j] , i - 1);
  174.         end;
  175.         setlength(VertexArray, 0);
  176.     end;
  177. end;
  178.  
  179. procedure receiveAdjacencyMatrix(var ListArray: TListArray; var AdjacencyMatrix: TMatrix);
  180. var
  181.     i, j: integer;
  182. begin
  183.     setlength(AdjacencyMatrix, length(ListArray), length(ListArray));
  184.     for i := 0 to high(ListArray) do
  185.         for j := 0 to (ListArray[i].Count - 1) do
  186.             AdjacencyMatrix[i][ListArray[i][j].number - 1] := ListArray[i][j].dist;
  187. end;
  188.  
  189. procedure receiveDistMatrix(Size: integer; var DistMatrix: TMatrix);
  190. var
  191.     i, j: integer;
  192. begin
  193.     setlength(DistMatrix, Size, Size);
  194.     for i := 0 to (Size - 1) do
  195.         for j := 0 to (Size - 1) do
  196.             DistMatrix[i][j] := INT_MAX;
  197. end;
  198.  
  199. procedure receivePrecMatrix(Size: integer; var PrecMatrix: TMatrix);
  200. var
  201.     i, j: integer;
  202. begin
  203.     setlength(PrecMatrix, Size, Size);
  204.     for i := 0 to (Size - 1) do
  205.         for j := 0 to (Size - 1) do
  206.             PrecMatrix[i][j] := INT_MAX;
  207. end;
  208.  
  209. procedure receiveMatrices(var AdjacencyMatrix: TMatrix; var DistMatrix:
  210.                               TMatrix; var PrecMatrix: TMatrix);
  211. var
  212.     i, j, t, NewDist: integer;
  213. begin
  214.     for i := 0 to high(AdjacencyMatrix) do
  215.     begin
  216.         DistMatrix[i][i] := 0;
  217.         for j := 0 to high(AdjacencyMatrix) do
  218.             if (AdjacencyMatrix[i][j] <> 0) then
  219.             begin
  220.                 DistMatrix[i][j] := AdjacencyMatrix[i][j];
  221.                 PrecMatrix[i][j] := i;
  222.             end;
  223.     end;
  224.     for t := 0 to  high(AdjacencyMatrix) do
  225.     begin
  226.         for i := 0 to high(AdjacencyMatrix) do
  227.         begin
  228.             for j := 0 to high(AdjacencyMatrix) do
  229.             begin
  230.                 if ((DistMatrix[i][t] < INT_MAX) and (DistMatrix[t][j] < INT_MAX)) then
  231.                 begin
  232.                     NewDist := DistMatrix[i][t] + DistMatrix[t][j];
  233.                     if (NewDist < DistMatrix[i][j]) then
  234.                     begin
  235.                         DistMatrix[i][j] := NewDist;
  236.                         PrecMatrix[i][j] := PrecMatrix[t][j];
  237.                     end;
  238.                 end;
  239.             end;
  240.         end;
  241.     end;
  242. end;
  243.  
  244. procedure ConvertGraphInString(var GraphText: string; var AdjacencyMatrix: TMatrix; var DistMatrix:
  245.                               TMatrix; var PrecMatrix: TMatrix; VertexNumber: integer);
  246. var
  247.     i, j: integer;
  248. begin
  249.     GraphText := 'digraph G {' +  ' graph [dpi = 100];  node [shape=circle]' +
  250.      ' {node [style=filled]' + intToStr(VertexNumber + 1) + '}';
  251.     for i := 0 to high(AdjacencyMatrix) do
  252.         for j := 0 to high(AdjacencyMatrix) do
  253.             if (AdjacencyMatrix[i][j] <> 0) then
  254.             begin
  255.                 GraphText := GraphText + '  ' + intToStr(i + 1) + ' -> ' +
  256.                 intToStr(j + 1) + ' [label="' + intToStr(AdjacencyMatrix[i][j]) + '"';
  257.                 if (i = PrecMatrix[VertexNumber][j]) then
  258.                     GraphText := GraphText +  ',color=red';
  259.                 GraphText := GraphText +  '];';
  260.             end;
  261.     GraphText := GraphText + '}';
  262. end;
  263.  
  264. procedure SaveGraph(const GraphText: String);
  265. var
  266.     GraphFile: TextFile;
  267. begin
  268.     AssignFile(GraphFile, GRAPH_FILE);
  269.     Rewrite(GraphFile);
  270.     Write(GraphFile, GraphText);
  271.     CloseFile(GraphFile);
  272. end;
  273.  
  274. end.
  275.  
  276. unit GraphVisionUnit;
  277.  
  278. interface
  279.  
  280. uses
  281.   Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  282.   Vcl.Controls, Vcl.Forms, Vcl.Dialogs, SimpleFormUnit, Vcl.Menus, Vcl.ExtCtrls, GraphUnit, Vcl.Imaging.PNGImage;
  283.  
  284. type
  285.   TGraphForm = class(TSimpleForm)
  286.     GraphPicture: TImage;
  287.     procedure FormShow(Sender: TObject);
  288.     procedure SetInstructions(); override;
  289.   end;
  290.  
  291. var
  292.   GraphForm: TGraphForm;
  293.  
  294. implementation
  295.  
  296. {$R *.dfm}
  297.  
  298. procedure TGraphForm.SetInstructions;
  299. Const
  300.     NEW_LINE = #13#10;
  301. Begin
  302.     Instructions := 'This is the graph image.'+NEW_LINE
  303.             +'Red lines show the shortest path between current vertex and other.'
  304. End;
  305.  
  306. procedure TGraphForm.FormShow(Sender: TObject);
  307. begin
  308.   inherited;
  309.     GraphPicture.Picture.LoadFromFile(GRAPH_PICTURE_FILE);
  310.     Self.ClientHeight := GraphPicture.Picture.Height;
  311.     Self.ClientWidth := GraphPicture.Picture.Width;
  312. end;
  313.  
  314. end.
  315.  
  316. unit InputUnit;
  317.  
  318. interface
  319.  
  320. uses
  321.   Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  322.   Vcl.Controls, Vcl.Forms, Vcl.Dialogs, SimpleFormUnit, Vcl.Menus, Vcl.Grids,
  323.   Vcl.StdCtrls, Vcl.Samples.Spin, Vcl.Buttons, GraphUnit, GraphVisionUnit, System.UITypes, ShellApi;
  324.  
  325. type
  326.   TInputForm = class(TSimpleForm)
  327.     ShowButton: TBitBtn;
  328.     VertexNumberSpinEdit: TSpinEdit;
  329.     IncidentListStringGrid: TStringGrid;
  330.     OpenDialog: TOpenDialog;
  331.     SaveDialog: TSaveDialog;
  332.     FileMenu: TMenuItem;
  333.     SaveFile: TMenuItem;
  334.     OpenFile: TMenuItem;
  335.     NumberLabel: TLabel;
  336.     VertexLabel: TLabel;
  337.     VertexSpinEdit: TSpinEdit;
  338.     procedure SaveFileClick(Sender: TObject);
  339.     procedure OpenFileClick(Sender: TObject);
  340.     procedure ShowButtonClick(Sender: TObject);
  341.     procedure VertexNumberSpinEditChange(Sender: TObject);
  342.     procedure FormCreate(Sender: TObject);
  343.     procedure CreateGraphPicture();
  344.     procedure FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
  345.     procedure SetInstructions(); override;
  346.     procedure saveInFile(const Name: string; IncidentListStringGrid: TStringGrid);
  347.     function isFileCorrect(const Name: string): boolean;
  348.   end;
  349.  
  350. var
  351.   InputForm: TInputForm;
  352.   IsProcessed: Boolean = False;
  353.  
  354. implementation
  355.  
  356. {$R *.dfm}
  357.  
  358. function TrySetOutputFile(const Name: String): Boolean;
  359. var
  360.     OutputFile: TextFile;
  361. begin
  362.     Result := True;
  363.     AssignFile(OutputFile, Name);
  364.     try
  365.         Rewrite(OutputFile);
  366.     except
  367.         Result := False;
  368.     end;
  369.     close(OutputFile);
  370. end;
  371.  
  372. function TrySetInputFile(const Name: String): Boolean;
  373. var
  374.     InputFile: TextFile;
  375. begin
  376.     Result := True;
  377.     AssignFile(InputFile, Name);
  378.     Try
  379.         Reset(InputFile);
  380.     Except
  381.         Result := False;
  382.     End;
  383.     closeFile(InputFile);
  384. end;
  385.  
  386. function isDataCorrect(var Grid: TStringGrid): boolean;
  387. var
  388.     i, j: integer;
  389. begin
  390.     for i := 1 to (Grid.RowCount - 1) do
  391.     begin
  392.         if not IsValidStr((Grid.RowCount - 1), Grid.Cells[1, i], i) then
  393.         begin
  394.             MessageDlg('Wrong input.', mtError, [mbOk], 0);
  395.             Grid.Cells[1, i] := '';
  396.             Result := false;
  397.         end;
  398.     end;
  399. end;
  400.  
  401. procedure OpenFromFile(const Name: string; var Grid: TStringGrid; var VertexNumberEdit: TSpinEdit);
  402. const
  403.     FIRST_COL = 0;
  404.     SECOND_COL = 1;
  405. var
  406.     i, Width, CurrentVertex: Integer;
  407.     Buffer: String;
  408.     InputFile: TextFile;
  409. begin
  410.     AssignFile(InputFile, Name);
  411.     Reset(InputFile);
  412.     Readln(InputFile, Width);
  413.     VertexNumberEdit.Text := IntToStr(Width);
  414.     i := 1;
  415.     while not EOF(InputFile) do
  416.     begin
  417.         Read(InputFile, CurrentVertex);
  418.         Readln(InputFile, Buffer);
  419.         Grid.Cells[FIRST_COL, i] := intToStr(i);
  420.         Grid.Cells[SECOND_COL, i] := Buffer;
  421.         inc(i);
  422.     end;
  423.     closeFile(InputFile);
  424. end;
  425.  
  426. procedure SetGridSize(const Width, Height: Integer; var Grid: TStringGrid);
  427. Var
  428.     i: Integer;
  429. begin
  430.     Grid.ColCount := Width + 1;
  431.     Grid.RowCount := Height + 1;
  432.     for i := 1 to Height do
  433.     Begin
  434.         Grid.Cells[i, 0] := IntToStr(I);
  435.         Grid.Cells[0, i] := IntToStr(I);
  436.     End;
  437.     Grid.Width := (Width + 1) * Grid.DefaultColWidth + 15;
  438.     Grid.Height := (Height + 1) * Grid.DefaultRowHeight + 15;
  439. end;
  440.  
  441. procedure TInputForm.SetInstructions;
  442. Const
  443.     NEW_LINE = #13#10;
  444. Begin
  445.     inherited;
  446.     Instructions := Instructions + 'This program implements Floud algorithm.'
  447.     + NEW_LINE + 'This program receives incident list and'+ NEW_LINE
  448.         +'outputs the imagw with path.'+NEW_LINE
  449.             +'1. Fullfill grid.'+NEW_LINE+'2. ENTER - to process image.'
  450.                 +NEW_LINE+'3. Show result - see the image.';
  451. End;
  452.  
  453. procedure TInputForm.ShowButtonClick(Sender: TObject);
  454. begin
  455.     if IsProcessed then
  456.     Begin
  457.         if not Assigned(GraphForm) then
  458.             GraphForm := TGraphForm.Create(Self);
  459.         GraphForm.Show;
  460.         IsProcessed := False;
  461.     End
  462.     Else
  463.         MessageDlg('Your data was not processed. Press enter, please', mtError, [mbOk], 0);
  464. end;
  465.  
  466. procedure TInputForm.VertexNumberSpinEditChange(Sender: TObject);
  467. Const
  468.     WINDOW_DEFAULT_HEIGHT = 200;
  469. begin
  470.     SetGridSize(1, StrToint(VertexNumberSpinEdit.Text), IncidentListStringGrid);
  471.     VertexSpinEdit.maxValue := VertexNumberSpinEdit.value;
  472.     VertexSpinEdit.minValue := 1;
  473.     if IncidentListStringGrid.height > ClientHeight-WINDOW_DEFAULT_HEIGHT then
  474.         ClientHeight := IncidentListStringGrid.height + WINDOW_DEFAULT_HEIGHT;
  475. end;
  476.  
  477. procedure TInputForm.FormCreate(Sender: TObject);
  478. begin
  479.   inherited;
  480.     IncidentListStringGrid.Cells[0,0] := 'V';
  481.     IncidentListStringGrid.Cells[1,0] := '1';
  482.     IncidentListStringGrid.Cells[0,1] := '1';
  483.     IncidentListStringGrid.ColWidths[0] := Round(IncidentListStringGrid.Width * 0.1);
  484.     IncidentListStringGrid.ColWidths[1] := Round(IncidentListStringGrid.Width * 0.9);;
  485. end;
  486.  
  487. procedure TInputForm.FormKeyDown(Sender: TObject; var Key: Word;
  488.   Shift: TShiftState);
  489. var
  490.     AdjacencyMatrix, DistMatrix, PrecMatrix: TMatrix;
  491.     MainVertexNumber: integer;
  492.     Size: integer;
  493.     GraphText: string;
  494. Const
  495.     ENTER = 13;
  496.     INFO = 'Your information is processed, thanks.';
  497.     EXCEPTION = 'Input incident list.';
  498. begin
  499.   inherited;
  500.     if (Key = ENTER) and (isDataCorrect(IncidentListStringGrid)) then
  501.     Begin
  502.         receiveListArray(ListArray, IncidentListStringGrid);
  503.         receiveAdjacencyMatrix(ListArray, AdjacencyMatrix);
  504.         Size := length(AdjacencyMatrix);
  505.         receiveDistMatrix(Size, DistMatrix);
  506.         receivePrecMatrix(Size, PrecMatrix);
  507.         receiveMatrices(AdjacencyMatrix, DistMatrix, PrecMatrix);
  508.         MainVertexNumber := VertexSpinEdit.value - 1;
  509.         ConvertGraphInString(GraphText, AdjacencyMatrix, DistMatrix, PrecMatrix, MainVertexNumber);
  510.         SaveGraph(GraphText);
  511.         CreateGraphPicture();
  512.         IsProcessed := True;
  513.         MessageDlg(INFO, mtInformation, [mbOk], 0)
  514.     end;
  515. end;
  516.  
  517. function TInputForm.isFileCorrect(const Name: string): boolean;
  518. var
  519.     CurrentVertex, NumberOfVertice, i: integer;
  520.     Buffer: string;
  521.     IsCorrect: boolean;
  522.     InputFile: TextFile;
  523. begin
  524.     AssignFile(InputFile, Name);
  525.     Reset(InputFile);
  526.     IsCorrect := true;
  527.     i := 0;
  528.     try
  529.         Read(InputFile, NumberOfVertice);
  530.     except
  531.         IsCorrect := false;
  532.     end;
  533.     while (not EOF(InputFile)) and IsCorrect do
  534.     begin
  535.         Read(InputFile, CurrentVertex);
  536.         Readln(InputFile, Buffer);
  537.         IsCorrect := isValidStr(NumberOfVertice, Buffer, (i + 1));
  538.         inc(i);
  539.     end;
  540.     if (i <> NumberOfVertice) then
  541.         IsCorrect := false;
  542.     closeFile(InputFile);
  543.     Result := IsCorrect;
  544. end;
  545.  
  546. procedure TInputForm.OpenFileClick(Sender: TObject);
  547. var
  548.     InputFile: textFile;
  549. begin
  550.     if OpenDialog.Execute then
  551.     begin
  552.         if FileExists(OpenDialog.FileName) then
  553.         Begin
  554.             if TrySetInputFile(OpenDialog.FileName) then
  555.             begin
  556.                 if (IsFileCorrect(OpenDialog.FileName)) then
  557.                 begin
  558.                     OpenFromFile(OpenDialog.FileName, Self.IncidentListStringGrid, VertexNumberSpinEdit);
  559.                 end
  560.                 else
  561.                     MessageDlg('Wrong data in file.', mtError, [mbOk], 0);
  562.             end
  563.             else
  564.                 MessageDlg('This file can not be openned.', mtError, [mbOk], 0);
  565.         end;
  566.     end;
  567. end;
  568.  
  569. procedure TInputForm.saveInFile(const Name: string; IncidentListStringGrid: TStringGrid);
  570. Var
  571.     i, j: Integer;
  572.     OutputFile: TextFile;
  573. Begin
  574.     AssignFile(OutputFile, Name);
  575.     Rewrite(OutputFile);
  576.     Writeln(OutputFile, IncidentListStringGrid.RowCount-1);
  577.     for i := 1 to IncidentListStringGrid.RowCount-1 do
  578.     Begin
  579.         for j := 0 to IncidentListStringGrid.ColCount-1 do
  580.         Begin
  581.             Write(OutputFile, IncidentListStringGrid.Cells[j, i]);
  582.             Write(OutputFile, ' ');
  583.         End;
  584.         Writeln(OutputFile);
  585.     End;
  586.     closeFile(OutputFile);
  587. End;
  588.  
  589. procedure TInputForm.SaveFileClick(Sender: TObject);
  590. Var
  591.     OutputFile: TextFile;
  592. begin
  593.     if SaveDialog.Execute then
  594.     Begin
  595.         If FileExists(SaveDialog.FileName) then
  596.         Begin
  597.             if TrySetOutputFile(SaveDialog.FileName) then
  598.             Begin
  599.                 SaveInFile(SaveDialog.FileName, Self.IncidentListStringGrid);
  600.                 MessageDlg('Graph is saved.', mtInformation, [mbOk], 0);
  601.             End
  602.             Else
  603.                 MessageDlg('Data is not saved', mtError, [mbOk], 0);
  604.         End;
  605.     End;
  606. end;
  607.  
  608. procedure TInputForm.CreateGraphPicture();
  609. Begin
  610.     ShellExecute(Handle, nil, 'cmd.exe', PChar(CREATE_GRAPH_PICTURE_COMMAND), nil, SW_HIDE)
  611. End;
  612.  
  613. end.
  614.  
  615. Unit SimpleFormUnit;
  616.  
  617. interface
  618.  
  619. uses
  620.   Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  621.   Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.Menus, System.UITypes;
  622.  
  623. type
  624.   TSimpleForm = class(TForm)
  625.     Menu: TMainMenu;
  626.     Instruction: TMenuItem;
  627.     Developer: TMenuItem;
  628.     procedure InstructionClick(Sender: TObject);
  629.     procedure DeveloperClick(Sender: TObject);
  630.     Procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
  631.     procedure SetInstructions(); virtual;
  632.     procedure FormCreate(Sender: TObject);
  633.     procedure FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState); virtual;
  634.     Procedure FormCloseQueryNope(Sender: TObject; var CanClose: Boolean);
  635.   private
  636.     { Private declarations }
  637.   protected
  638.     Instructions: String;
  639.   public
  640.     { Public declarations }
  641.   end;
  642.  
  643. var
  644.   SimpleForm: TSimpleForm;
  645.  
  646. implementation
  647.  
  648. {$R *.dfm}
  649.  
  650. procedure TSimpleForm.DeveloperClick(Sender: TObject);
  651. begin
  652.     ShowMessage('Ravodin Alexander 151002');
  653. end;
  654.  
  655. procedure TSimpleForm.InstructionClick(Sender: TObject);
  656. begin
  657.     ShowMessage(Instructions);
  658. end;
  659.  
  660. Procedure TSimpleForm.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
  661. begin
  662.     CanClose := False;
  663.     if MessageDlg('Are you sure if you want to exit?',mtConfirmation, mbOKCancel, 0) = mrOk then
  664.     begin
  665.         CanClose := True;
  666.     end;
  667. end;
  668.  
  669. Procedure TSimpleForm.FormCloseQueryNope(Sender: TObject; var CanClose: Boolean);
  670. Begin
  671.     CanClose := True;
  672. End;
  673.  
  674. procedure TSimpleForm.SetInstructions();
  675. begin
  676.     Instructions := 'This is main window' + #13#10 + #13#10 +
  677.                   'Here you should input incident lists and choose main vertex' +
  678.                   'F1 - see instruction.' + #13#10 + 'F2 - show developer.' +
  679.                   #13#10 + 'ESC - close window.';
  680. end;
  681.  
  682. procedure TSimpleForm.FormCreate(Sender: TObject);
  683. begin
  684.     SetInstructions;
  685. end;
  686.  
  687. procedure TSimpleForm.FormKeyDown(Sender: TObject; var Key: Word;
  688.   Shift: TShiftState);
  689. begin
  690.     if (Key = VK_ESCAPE) then
  691.         Self.Close;
  692. end;
  693.  
  694.  
  695. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement