unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ComCtrls, IniFiles, ExtCtrls, Spin, Mask, Grids,
  ValEdit,jpeg;

type
  TPerem = record
    Name:string;
    Offset:byte;
  end;

  TParm = record
    Port:string;
    FullScrean:boolean;
    Timeout:word;
    Color:tcolor;
    FontColor:tcolor;
    FontSize:byte;
    Preambul:boolean;
  end;

  tBuffer = record
    Offset:byte;
    BufferLength:byte;
  end;

  TLog = record
    Max,Min:integer;
    Name:string;
    Formula:string;
    Height:word;
    PenColor:tcolor;
    PeremsCout:byte;
    Perems:array of TPerem;
    PeremName:string;
  end;

  TPribor = class(TImage)
  procedure Draw;
  private
    BackMap:tbitmap;
    Name:string;
    Vid:byte;
    Formula:string;
    Size:word;
    X,Y:word;
    Max,Min:integer;
    PinNum:word;
    NumLen:word;
    StartCone:smallint;
    EndCone:smallint;
    Horiz:boolean;
    Revers:boolean;
    ShkalaColor:tcolor;
    StrelkaColor:tcolor;
    TextColor:tcolor;
    Trevoga:boolean;
    TrevogaLow:boolean;
    TrevogaPoz:real;
    TrevSwich:boolean;
    TrevSwichedCol:tcolor;
  end;

  TAllData = record
    Parm:TParm;
    BuffersCout:byte;
    Buffers:array of TBuffer;
    LogCout:byte;
    Logs:array of TLog;
    PriborCout:byte;
    Pribors:array of TPribor
  end;

  TForm1 = class(TForm)
    PageControl1: TPageControl;
    TabSheet1: TTabSheet;
    TabSheet4: TTabSheet;
    Button1: TButton;
    Button2: TButton;
    OpenDialog1: TOpenDialog;
    SaveDialog1: TSaveDialog;
    ColorDialog1: TColorDialog;
    CheckBox1: TCheckBox;
    CheckBox2: TCheckBox;
    ComboBox1: TComboBox;
    Label1: TLabel;
    SpinEdit1: TSpinEdit;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    SpinEdit2: TSpinEdit;
    Bevel1: TBevel;
    Bevel2: TBevel;
    ListBox3: TListBox;
    Button3: TButton;
    Button4: TButton;
    MaskEdit1: TMaskEdit;
    Label11: TLabel;
    Edit2: TEdit;
    Label12: TLabel;
    MaskEdit6: TMaskEdit;
    Label13: TLabel;
    SpinEdit4: TSpinEdit;
    Bevel3: TBevel;
    Edit3: TEdit;
    Label14: TLabel;
    ComboBox2: TComboBox;
    Button9: TButton;
    SpinEdit5: TSpinEdit;
    Label15: TLabel;
    SpinEdit6: TSpinEdit;
    Label16: TLabel;
    SpinButton2: TSpinButton;
    Button10: TButton;
    Label17: TLabel;
    ListBox2: TListBox;
    Button5: TButton;
    SpinButton1: TSpinButton;
    Button6: TButton;
    MaskEdit5: TMaskEdit;
    Edit1: TEdit;
    Bevel7: TBevel;
    Label8: TLabel;
    Label9: TLabel;
    ListBox1: TListBox;
    Button7: TButton;
    Button8: TButton;
    MaskEdit4: TMaskEdit;
    Label7: TLabel;
    Bevel6: TBevel;
    MaskEdit3: TMaskEdit;
    Label6: TLabel;
    Label10: TLabel;
    Bevel5: TBevel;
    Label19: TLabel;
    TabSheet2: TTabSheet;
    ListBox4: TListBox;
    Button11: TButton;
    Button12: TButton;
    Bevel8: TBevel;
    SpinEdit3: TSpinEdit;
    Label18: TLabel;
    Label20: TLabel;
    Label21: TLabel;
    SpinEdit7: TSpinEdit;
    SpinEdit8: TSpinEdit;
    SpinEdit11: TSpinEdit;
    CheckBox3: TCheckBox;
    Bevel9: TBevel;
    CheckBox4: TCheckBox;
    MaskEdit7: TMaskEdit;
    Label25: TLabel;
    RadioGroup1: TRadioGroup;
    MaskEdit8: TMaskEdit;
    Label22: TLabel;
    Label23: TLabel;
    Label27: TLabel;
    MaskEdit9: TMaskEdit;
    MaskEdit10: TMaskEdit;
    Label28: TLabel;
    RadioGroup2: TRadioGroup;
    Label29: TLabel;
    SpinEdit9: TSpinEdit;
    Label30: TLabel;
    Label31: TLabel;
    SpinEdit10: TSpinEdit;
    Label32: TLabel;
    TabSheet3: TTabSheet;
    SpinEdit12: TSpinEdit;
    SpinEdit13: TSpinEdit;
    Label34: TLabel;
    Label35: TLabel;
    Panel1: TPanel;
    Label36: TLabel;
    CheckBox5: TCheckBox;
    Label26: TLabel;
    Edit4: TEdit;
    ComboBox3: TComboBox;
    Button13: TButton;
    RadioGroup3: TRadioGroup;
    Label24: TLabel;
    Label33: TLabel;
    SpinEdit14: TSpinEdit;
    Label37: TLabel;
    SpinEdit15: TSpinEdit;
    Edit5: TEdit;
    Label38: TLabel;
    SpinEdit16: TSpinEdit;
    Label39: TLabel;
    SpinEdit17: TSpinEdit;
    Label40: TLabel;
    SpinEdit18: TSpinEdit;
    Bevel4: TBevel;
    CheckBox6: TCheckBox;
    Edit6: TEdit;
    Image1: TImage;
    Button14: TButton;
    Label41: TLabel;
    Label42: TLabel;
    MaskEdit11: TMaskEdit;
    SpinButton3: TSpinButton;
    MaskEdit2: TMaskEdit;
    procedure Button1Click(Sender: TObject);
    procedure Load;
    procedure Save;
    procedure Button2Click(Sender: TObject);
    procedure MaskEdit1Change(Sender: TObject);
    procedure Edit1DblClick(Sender: TObject);
    procedure Edit1KeyPress(Sender: TObject; var Key: Char);
    procedure ListBox1Click(Sender: TObject);
    procedure Button7Click(Sender: TObject);
    procedure Button8Click(Sender: TObject);
    procedure MaskEdit3Change(Sender: TObject);
    procedure MaskEdit4Change(Sender: TObject);
    procedure Edit1Change(Sender: TObject);
    procedure Button5Click(Sender: TObject);
    procedure Button6Click(Sender: TObject);
    procedure ListBox2Click(Sender: TObject);
    procedure MaskEdit5Change(Sender: TObject);
    procedure SpinButton1UpClick(Sender: TObject);
    procedure SpinButton1DownClick(Sender: TObject);
    procedure SpinButton2UpClick(Sender: TObject);
    procedure SpinButton2DownClick(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure ListBox3Click(Sender: TObject);
    procedure Button9Click(Sender: TObject);
    procedure Edit2Change(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure Button10Click(Sender: TObject);
    procedure MaskEdit2Change(Sender: TObject);
    procedure SpinEdit2Change(Sender: TObject);
    procedure CheckBox2Click(Sender: TObject);
    procedure ComboBox1Change(Sender: TObject);
    procedure CheckBox1Click(Sender: TObject);
    procedure SpinEdit1Change(Sender: TObject);
    procedure MaskEdit6Change(Sender: TObject);
    procedure Panel1MouseMove(Sender: TObject; Shift: TShiftState; X,
      Y: Integer);
    procedure PageControl1Change(Sender: TObject);
    procedure Button13Click(Sender: TObject);
    procedure Button11Click(Sender: TObject);
    procedure Button12Click(Sender: TObject);
    procedure ListBox4Click(Sender: TObject);
    procedure Edit6Change(Sender: TObject);
    procedure Button1MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure Button1MouseMove(Sender: TObject; Shift: TShiftState; X,
      Y: Integer);
    procedure Button1MouseUp(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure Button14Click(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure SpinEdit16Change(Sender: TObject);
    procedure SpinEdit17Change(Sender: TObject);
    procedure SpinEdit18Change(Sender: TObject);
    procedure SpinButton3DownClick(Sender: TObject);
    procedure SpinButton3UpClick(Sender: TObject);
  private
    { Private declarations }
    Data: TAllData;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  Form2: TForm;
  obbutton: tbutton;
  xo,yo:integer;
  LastItem:integer=-1;
  LastItemPr:integer=-1;

implementation

{$R *.dfm}


procedure TForm1.Button1MouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
 TControl(sender).Tag:=1;
 xo:=x;
 yo:=y;
 if TControl(sender).Cursor = crSizeNWSE then TControl(sender).Tag:=2;
end;

procedure TForm1.Button1MouseMove(Sender: TObject; Shift: TShiftState; X,
  Y: Integer);
begin
  if TControl(sender).Tag = 1 then begin
   TControl(sender).Top:=TControl(sender).Top+y-yo;
   TControl(sender).Left:=TControl(sender).Left+x-xo;
  end ;
  if TControl(sender).Tag = 2 then begin
    TPribor(sender).Size:=(x+y) div 4;
    TPribor(sender).Draw;
   end;
  if (x > TControl(sender).Width-10) and (y > TControl(sender).Height-10) and (sender.ClassType<>tlabel) then
    TControl(sender).Cursor:= crSizeNWSE else TControl(sender).Cursor:= crSizeAll;
end;

procedure TForm1.Button1MouseUp(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
 TControl(sender).Tag:=0;
 if sender.ClassType=TPribor then begin
    TPribor(sender).X:=TControl(sender).Left;
    TPribor(sender).Y:=TControl(sender).Top;
  end else begin
   SpinEdit17.Value:=Label41.Left;
   SpinEdit18.Value:=Label41.Top;
   end;
   if ListBox4.ItemIndex <> -1 then
      begin
        SpinEdit12.Value:=Data.Pribors[ListBox4.ItemIndex].X;
        SpinEdit13.Value:=Data.Pribors[ListBox4.ItemIndex].Y;
        SpinEdit3.Value:=Data.Pribors[ListBox4.ItemIndex].Size;
      end;
end;



procedure TPribor.Draw;
var i,tx,ty,kol:integer; val,val1:real; s:string;
  nach,kon,rl:real;
begin
  BackMap.Free;
  BackMap:=tbitmap.Create;
  BackMap.Canvas.Brush.Color:=0;
  BackMap.TransparentColor:=BackMap.Canvas.Brush.Color;
  BackMap.Transparent:=true;
  BackMap.Canvas.Font.Size:=round(size*0.13);
  BackMap.Canvas.Font.Name:='Arial';
  BackMap.Canvas.Font.Color:=TextColor;
 case Vid of
 0: begin
  Width:=size*2+1;
  Height:=size*2+1;
  nach:=-pi / 180 * StartCone;
  kon:=-pi / 180 * EndCone;
  rl:=(kon-nach)/(max-min)*(TrevogaPoz-min)+nach;
  kol:=PinNum;
  BackMap.Width:=Width;
  BackMap.Height:=Height;
  BackMap.Canvas.Pen.Width:=round(size*0.013);
  BackMap.Canvas.Pen.Color:=ShkalaColor;
  if nach>kon then
    BackMap.Canvas.Arc(0,0,size*2+1,size*2+1,size+round(cos(nach)*100),size+round(sin(nach)*100),size+round(cos(kon)*100),size+round(sin(kon)*100))
  else
    BackMap.Canvas.Arc(0,0,size*2+1,size*2+1,size+round(cos(kon)*100),size+round(sin(kon)*100),size+round(cos(nach)*100),size+round(sin(nach)*100));
  for i:=0 to kol do begin
    val:=(kon-nach)/kol*i+nach;
    s:=copy(floattostr(i*((max-min) / kol)+min),0,NumLen);
    tx:=BackMap.Canvas.TextWidth(s) div 2;
    ty:=BackMap.Canvas.TextHeight(s) div 2;
    BackMap.Canvas.TextOut(size+round(cos(val)*size*0.75)-tx,size+round(sin(val)*size*0.75)-ty,s);
   end;
 for i:=1 to kol do begin
    val1:=(kon-nach)/kol*(i-0.5)+nach;
    val:=(kon-nach)/kol*i+nach;
    BackMap.Canvas.MoveTo(size+round(cos(val1)*size),size+round(sin(val1)*size));
    BackMap.Canvas.LineTo(size+round(cos(val1)*size*0.95),size+round(sin(val1)*size*0.95));
    BackMap.Canvas.MoveTo(size+round(cos(val)*size),size+round(sin(val)*size));
    BackMap.Canvas.LineTo(size+round(cos(val)*size*0.9),size+round(sin(val)*size*0.9));
  end;
 BackMap.Canvas.Ellipse(size-size div 10,size-size div 10,size+size div 10,size+size div 10);
 BackMap.Canvas.Pen.Width:=round(size * 0.04);
 BackMap.Canvas.Pen.Color:=$0000FF;
  s:=Name;
  tx:=BackMap.Canvas.TextWidth(s) div 2;
  ty:=BackMap.Canvas.TextHeight(s);
  BackMap.Canvas.TextOut(size-tx,size-ty-size div 10,s);

 if Trevoga then
  if not TrevogaLow then
    if nach<kon then
        BackMap.Canvas.Arc(0,0,size*2+1,size*2+1,size+round(cos(kon)*100),size+round(sin(kon)*100),size+round(cos(rl)*100),size+round(sin(rl)*100))
      else
        BackMap.Canvas.Arc(0,0,size*2+1,size*2+1,size+round(cos(rl)*100),size+round(sin(rl)*100),size+round(cos(kon)*100),size+round(sin(kon)*100))
   else
    if nach<kon then
        BackMap.Canvas.Arc(0,0,size*2+1,size*2+1,size+round(cos(rl)*100),size+round(sin(rl)*100),size+round(cos(nach)*100),size+round(sin(nach)*100))
      else
        BackMap.Canvas.Arc(0,0,size*2+1,size*2+1,size+round(cos(nach)*100),size+round(sin(nach)*100),size+round(cos(rl)*100),size+round(sin(rl)*100));

  val:=nach;
  s:=inttostr(min);
  tx:=BackMap.Canvas.TextWidth(s) div 2;
  BackMap.Canvas.Pen.Color:=StrelkaColor;
  BackMap.Canvas.TextOut(size-tx,size+size div 10,s);
  BackMap.Canvas.MoveTo(size,size);
  BackMap.Canvas.LineTo(size+round(cos(val+pi)*size*0.2),size+round(sin(val+pi)*size*0.2));
  BackMap.Canvas.MoveTo(size,size);
  BackMap.Canvas.LineTo(size+round(cos(val)*size*0.9),size+round(sin(val)*size*0.9));


 BackMap.Canvas.Font.Size:=9;
 BackMap.Canvas.TextOut(Width-10,Height-10,'->');
  end;

 1: begin
  Width:=size;
  Height:=size*2;
  s:=Name;
  tx:=BackMap.Canvas.TextWidth(s);
  ty:=BackMap.Canvas.TextHeight(s);
  if Width<tx then Width:=tx;
  BackMap.Width:=Width;
  BackMap.Height:=Height;
  BackMap.Canvas.TextOut(0,0,s);
  BackMap.Canvas.Pen.Width:=round(size*0.013);
  BackMap.Canvas.Pen.Color:=ShkalaColor;
  if not revers then begin
    nach:= BackMap.Height-ty*0.5;
    kon:= ty*2.5;
   end else begin
    kon:= BackMap.Height-ty*0.5;
    nach:= ty*2.5;
   end;
  rl:=(kon-nach)/(max-min)*(TrevogaPoz-min)+nach;
  kol:=PinNum;
  BackMap.Canvas.MoveTo(round(size*0.1),round(nach));
  BackMap.Canvas.LineTo(round(size*0.1),round(kon));
  for i:=0 to kol do begin
    val:=(kon-nach)/kol*i+nach;
    val1:=(kon-nach)/kol*(i+0.5)+nach;
    BackMap.Canvas.MoveTo(round(size*0.1),round(val));
    BackMap.Canvas.LineTo(round(size*0.17),round(val));
    if i<>kol then begin
      BackMap.Canvas.MoveTo(round(size*0.1),round(val1));
      BackMap.Canvas.LineTo(round(size*0.13),round(val1));
     end;
    s:=copy(floattostr(i*((max-min) / kol)+min),0,NumLen);
    BackMap.Canvas.TextOut(round(size*0.2),round(val)-ty div 2,s);
   end;
  BackMap.Canvas.Pen.Width:=round(size * 0.04);
  BackMap.Canvas.Pen.Color:=$0000FF;
 if Trevoga then
  if not TrevogaLow then
    begin
      BackMap.Canvas.MoveTo(round(size*0.1),round(rl));
      BackMap.Canvas.LineTo(round(size*0.1),round(kon));
    end else
    begin
      BackMap.Canvas.MoveTo(round(size*0.1),round(rl));
      BackMap.Canvas.LineTo(round(size*0.1),round(nach));
    end;

  s:=inttostr(min);
  BackMap.Canvas.TextOut(tx-BackMap.Canvas.TextWidth(s),ty,s);
  BackMap.Canvas.Pen.Color:=StrelkaColor;
  BackMap.Canvas.MoveTo(0,round(nach));
  BackMap.Canvas.LineTo(round(size*0.15),round(nach));

 BackMap.Canvas.Font.Size:=9;
 BackMap.Canvas.TextOut(Width-10,Height-10,'->');
  end;

 2: begin
  BackMap.Canvas.Font.Name:='Arial';
  BackMap.Canvas.Font.Color:=TextColor;
  BackMap.Canvas.Font.Size:=round(size*0.13);
  s:=Name;
  ty:=BackMap.Canvas.TextHeight(s);
  Width:=BackMap.Canvas.TextWidth(s);
  Height:=ty*2;
  BackMap.Width:=Width;
  BackMap.Height:=Height;
  BackMap.Canvas.TextOut(0,0,s);
  s:=inttostr(min);
  tx:=BackMap.Canvas.TextHeight(s);
  BackMap.Canvas.TextOut(Width-tx,ty,inttostr(min));
   end;
 3: begin
  s:=Name;
  Width:=BackMap.Canvas.TextWidth(s);
  Height:=BackMap.Canvas.TextHeight(s);
  BackMap.Width:=Width;
  BackMap.Height:=Height;
  BackMap.Canvas.TextOut(0,0,s);
  end;
 4: begin
    if FileExists('.\icons\'+name+'.bmp') then begin
      BackMap.LoadFromFile('.\icons\'+name+'.bmp');
      Width:=size div 3;
      Height:=size div 2;
      Proportional:=true;
      Stretch:=true;
     end;
  end;
 end;
 Left:=x;
 Top:=y;
 Picture.Bitmap:=BackMap;
end;


procedure TForm1.Load;
var
  IniFile: TIniFile;
  i,j: byte;
begin

  ListBox1.Clear;
  ListBox2.Clear;
  ListBox3.Clear;

  IniFile:= TIniFile.Create(OpenDialog1.FileName);

  Data.Parm.FullScrean:= IniFile.ReadBool('настройки','на_весь_экран',false);
  Data.Parm.Timeout:=IniFile.ReadInteger('настройки','таймаут_отв',100);
  Data.Parm.Color:= IniFile.ReadInteger('настройки','цвет_фона',clBtnFace);
  Data.Parm.FontColor:= IniFile.ReadInteger('настройки','цвет_шрифта',clred);
  Data.Parm.FontSize:= IniFile.ReadInteger('настройки','размер_шрифта',10);
  Data.Parm.Preambul:=IniFile.ReadBool('настройки','преамбула',true);
  Data.Parm.Port:=IniFile.ReadString('настройки','порт','com1');

  Data.BuffersCout:= IniFile.ReadInteger('настройки','кол-во_областей',1);
  SetLength(Data.Buffers, Data.BuffersCout);
  for j:=0 to Data.BuffersCout-1 do
    begin
      Data.Buffers[j].Offset:= IniFile.ReadInteger('области','смещение_'+inttostr(j),$00);
      Data.Buffers[j].BufferLength:= IniFile.ReadInteger('области','размер_'+inttostr(j),$01);
      ListBox1.Items.Add('Область'+inttostr(j));
    end;

  Data.LogCout:= IniFile.ReadInteger('настройки','кол-во_логов',1);
  SetLength(Data.Logs, Data.LogCout);
  for i:=0 to Data.LogCout-1 do
    begin
     Data.Logs[i].Formula:=IniFile.ReadString('логер_'+inttostr(i),'формула','0');
     Data.Logs[i].Height:= IniFile.ReadInteger('логер_'+inttostr(i),'высота',150);
     Data.Logs[i].Name:=IniFile.ReadString('логер_'+inttostr(i),'название','датчик'+inttostr(i));
     Data.Logs[i].Max:= IniFile.ReadInteger('логер_'+inttostr(i),'макс_знач',100);
     Data.Logs[i].Min:= IniFile.ReadInteger('логер_'+inttostr(i),'мин_знач',0);
     Data.Logs[i].PenColor:= IniFile.ReadInteger('логер_'+inttostr(i),'цвет',$ff0000);
     Data.Logs[i].PeremName:=IniFile.ReadString('логер_'+inttostr(i),'Имя_перем','');
     ListBox3.Items.Add(Data.Logs[i].Name);
     Data.Logs[i].PeremsCout:= IniFile.ReadInteger('логер_'+inttostr(i),'кол-во_перем',1);
     SetLength(Data.Logs[i].Perems, Data.Logs[i].PeremsCout);
     if Data.Logs[i].PeremsCout>0 then
     for j:=0 to Data.Logs[i].PeremsCout-1 do
       begin
         Data.Logs[i].Perems[j].Name:= IniFile.ReadString('логер_'+inttostr(i),'переменная_'+inttostr(j),'p'+inttostr(j));
         Data.Logs[i].Perems[j].Offset:= IniFile.Readinteger('логер_'+inttostr(i),'отступ_пер_'+inttostr(j),$ff);
       //  ListBox2.Items.Add(Data.Perems[j].Name);
       end;
    end;

  Data.PriborCout:= IniFile.ReadInteger('настройки','кол-во_приборов',1);
  SetLength(Data.Pribors, Data.PriborCout);
  for i:=0 to Data.PriborCout-1 do
    begin
      Data.Pribors[i]:=TPribor.Create(self);
      Data.Pribors[i].Parent:=Panel1;
      Data.Pribors[i].Transparent:=true;
      Data.Pribors[i].OnMouseDown:=Button1MouseDown;
      Data.Pribors[i].OnMouseMove:=Button1MouseMove;
      Data.Pribors[i].OnMouseUp:=Button1MouseUp;
      Data.Pribors[i].Name:=IniFile.ReadString('Прибор_'+inttostr(i),'Название','NoName');
      Data.Pribors[i].Vid:=IniFile.ReadInteger('Прибор_'+inttostr(i),'Вид',0);
      Data.Pribors[i].Formula:=IniFile.ReadString('Прибор_'+inttostr(i),'Формула','');
      Data.Pribors[i].Size:=IniFile.ReadInteger('Прибор_'+inttostr(i),'Размер',50);
      Data.Pribors[i].X:=IniFile.ReadInteger('Прибор_'+inttostr(i),'Х',50);
      Data.Pribors[i].Y:=IniFile.ReadInteger('Прибор_'+inttostr(i),'У',50);
      Data.Pribors[i].Max:=IniFile.ReadInteger('Прибор_'+inttostr(i),'Максимум',50);
      Data.Pribors[i].Min:=IniFile.ReadInteger('Прибор_'+inttostr(i),'Минимум',0);
      Data.Pribors[i].PinNum:=IniFile.ReadInteger('Прибор_'+inttostr(i),'ГрадацияШк',50);
      Data.Pribors[i].NumLen:=IniFile.ReadInteger('Прибор_'+inttostr(i),'Кол-воЦиврШк',50);
      Data.Pribors[i].StartCone:=IniFile.ReadInteger('Прибор_'+inttostr(i),'НачУгол',50);
      Data.Pribors[i].EndCone:=IniFile.ReadInteger('Прибор_'+inttostr(i),'КонУгол',50);
      Data.Pribors[i].Horiz:=IniFile.ReadBool('Прибор_'+inttostr(i),'Горизонтальный',false);
      Data.Pribors[i].Revers:=IniFile.ReadBool('Прибор_'+inttostr(i),'Реверс',false);
      Data.Pribors[i].ShkalaColor:=IniFile.ReadInteger('Прибор_'+inttostr(i),'ЦветШк',50);
      Data.Pribors[i].StrelkaColor:=IniFile.ReadInteger('Прибор_'+inttostr(i),'ЦветСтр',50);
      Data.Pribors[i].TextColor:=IniFile.ReadInteger('Прибор_'+inttostr(i),'ЦветТекст',50);
      Data.Pribors[i].Trevoga:=IniFile.ReadBool('Прибор_'+inttostr(i),'Тревога',false);
      Data.Pribors[i].TrevogaLow:=IniFile.ReadBool('Прибор_'+inttostr(i),'ТревогаНиже',false);
      Data.Pribors[i].TrevogaPoz:=IniFile.ReadFloat('Прибор_'+inttostr(i),'ТревогаУровень',10);
      Data.Pribors[i].TrevSwich:=IniFile.ReadBool('Прибор_'+inttostr(i),'ТревогаМенятьЦвет',false);
      Data.Pribors[i].TrevSwichedCol:=IniFile.ReadInteger('Прибор_'+inttostr(i),'ТревогаЦвет',50);
      ListBox4.Items.Add(Data.Pribors[i].Name);
   end;


  CheckBox6.Checked:=IniFile.ReadBool('Часы','Отображать',false);
  SpinEdit16.Value:=IniFile.ReadInteger('Часы','Размер',0);
  SpinEdit17.Value:=IniFile.ReadInteger('Часы','Х',0);
  SpinEdit18.Value:=IniFile.ReadInteger('Часы','У',0);
  MaskEdit11.Text:='$'+inttohex(IniFile.ReadInteger('Часы','Цвет',0),6);

  IniFile.Free;

  SpinEdit16Change(self);
  SpinEdit17Change(self);
  SpinEdit18Change(self);
  Label41.Caption:=timetostr(time);

  panel1.Color:=Data.Parm.Color;
  ComboBox1.Text:=Data.Parm.Port;
  CheckBox1.Checked:=Data.Parm.Preambul;
  SpinEdit1.Value:=Data.Parm.Timeout;
  CheckBox2.Checked:=Data.Parm.FullScrean;
  MaskEdit1.Text:='$'+inttohex(Data.Parm.Color,6);
  MaskEdit2.Text:='$'+inttohex(Data.Parm.FontColor,6);
  SpinEdit2.Value:=Data.Parm.FontSize;
end;

procedure TForm1.Save;
var
  IniFile: TIniFile; f:file;
  i,j,k: byte;
begin
  if SaveDialog1.FileName = '' then exit;

  AssignFile(f,SaveDialog1.FileName);
  rewrite(f);
  CloseFile(f);

  IniFile:= TIniFile.Create(SaveDialog1.FileName);
  ListBox3Click(ListBox3);

  IniFile.WriteBool('настройки','на_весь_экран',Data.Parm.FullScrean);
  IniFile.WriteInteger('настройки','таймаут_отв',Data.Parm.Timeout);
  IniFile.WriteString('настройки','цвет_фона','$'+inttohex(Data.Parm.Color,6));
  IniFile.WriteString('настройки','цвет_шрифта','$'+inttohex(Data.Parm.FontColor,6));
  IniFile.WriteInteger('настройки','размер_шрифта',Data.Parm.FontSize);
  IniFile.WriteBool('настройки','преамбула',Data.Parm.Preambul);
  IniFile.WriteString('настройки','порт',Data.Parm.Port);

  IniFile.WriteInteger('настройки','кол-во_областей',Data.BuffersCout);
  for j:=0 to Data.BuffersCout-1 do
    begin
      IniFile.WriteString('области','смещение_'+inttostr(j),'$'+inttohex(Data.Buffers[j].Offset,2));
      IniFile.WriteString('области','размер_'+inttostr(j),'$'+inttohex(Data.Buffers[j].BufferLength,2));
    end;

  k:=0;
  for i:=0 to Data.LogCout-1 do
   if copy(ListBox3.Items[i],0,5) <> '(del)' then
    begin
     IniFile.WriteString('логер_'+inttostr(i-k),'формула',Data.Logs[i].Formula);
     IniFile.WriteInteger('логер_'+inttostr(i-k),'высота',Data.Logs[i].Height);
     IniFile.WriteString('логер_'+inttostr(i-k),'название',Data.Logs[i].Name);
     IniFile.WriteInteger('логер_'+inttostr(i-k),'макс_знач',Data.Logs[i].Max);
     IniFile.WriteInteger('логер_'+inttostr(i-k),'мин_знач',Data.Logs[i].Min);
     IniFile.WriteString('логер_'+inttostr(i-k),'цвет','$'+inttohex(Data.Logs[i].PenColor,6));
     IniFile.WriteString('логер_'+inttostr(i-k),'Имя_перем',Data.Logs[i].PeremName);
     if Data.Logs[i].PeremsCout>0 then
     for j:=0 to Data.Logs[i].PeremsCout-1 do
        begin
          IniFile.WriteString('логер_'+inttostr(i-k),'переменная_'+inttostr(j),Data.Logs[i].Perems[j].Name);
          IniFile.WriteString('логер_'+inttostr(i-k),'отступ_пер_'+inttostr(j),'$'+inttohex(Data.Logs[i].Perems[j].Offset,2));
        end;
     IniFile.WriteInteger('логер_'+inttostr(i-k),'кол-во_перем',Data.Logs[i].PeremsCout);
    end else k:=k+1;
  IniFile.WriteInteger('настройки','кол-во_логов',Data.LogCout-k);

  ListBox4Click(ListBox4);
  k:=0;
  for i:=0 to Data.PriborCout-1 do
   if copy(ListBox4.Items[i],0,5) <> '(del)' then
    begin
      IniFile.WriteString('Прибор_'+inttostr(i),'Название',Data.Pribors[i].Name);
      IniFile.WriteInteger('Прибор_'+inttostr(i),'Вид',Data.Pribors[i].Vid);
      IniFile.WriteString('Прибор_'+inttostr(i),'Формула',Data.Pribors[i].Formula);
      IniFile.WriteInteger('Прибор_'+inttostr(i),'Размер',Data.Pribors[i].Size);
      IniFile.WriteInteger('Прибор_'+inttostr(i),'Х',Data.Pribors[i].X);
      IniFile.WriteInteger('Прибор_'+inttostr(i),'У',Data.Pribors[i].Y);
      IniFile.WriteInteger('Прибор_'+inttostr(i),'Максимум',Data.Pribors[i].Max);
      IniFile.WriteInteger('Прибор_'+inttostr(i),'Минимум',Data.Pribors[i].Min);
      IniFile.WriteInteger('Прибор_'+inttostr(i),'ГрадацияШк',Data.Pribors[i].PinNum);
      IniFile.WriteInteger('Прибор_'+inttostr(i),'Кол-воЦиврШк',Data.Pribors[i].NumLen);
      IniFile.WriteInteger('Прибор_'+inttostr(i),'НачУгол',Data.Pribors[i].StartCone);
      IniFile.WriteInteger('Прибор_'+inttostr(i),'КонУгол',Data.Pribors[i].EndCone);
      IniFile.WriteBool('Прибор_'+inttostr(i),'Горизонтальный',Data.Pribors[i].Horiz);
      IniFile.WriteBool('Прибор_'+inttostr(i),'Реверс',Data.Pribors[i].Revers);
      IniFile.WriteInteger('Прибор_'+inttostr(i),'ЦветШк',Data.Pribors[i].ShkalaColor);
      IniFile.WriteInteger('Прибор_'+inttostr(i),'ЦветСтр',Data.Pribors[i].StrelkaColor);
      IniFile.WriteInteger('Прибор_'+inttostr(i),'ЦветТекст',Data.Pribors[i].TextColor);
      IniFile.WriteBool('Прибор_'+inttostr(i),'Тревога',Data.Pribors[i].Trevoga);
      IniFile.WriteBool('Прибор_'+inttostr(i),'ТревогаНиже',Data.Pribors[i].TrevogaLow);
      IniFile.WriteFloat('Прибор_'+inttostr(i),'ТревогаУровень',Data.Pribors[i].TrevogaPoz);
      IniFile.WriteBool('Прибор_'+inttostr(i),'ТревогаМенятьЦвет',Data.Pribors[i].TrevSwich);
      IniFile.WriteInteger('Прибор_'+inttostr(i),'ТревогаЦвет',Data.Pribors[i].TrevSwichedCol);
    end else k:=k+1;
  IniFile.WriteInteger('настройки','кол-во_приборов',Data.PriborCout-k);

  IniFile.WriteBool('Часы','Отображать',CheckBox6.Checked);
  IniFile.WriteInteger('Часы','Размер',SpinEdit16.Value);
  IniFile.WriteInteger('Часы','Х',SpinEdit17.Value);
  IniFile.WriteInteger('Часы','У',SpinEdit18.Value);
  IniFile.WriteInteger('Часы','Цвет',strtoint(tedit(MaskEdit11).Text));

  IniFile.Free;

end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  OpenDialog1.Execute;
  Load;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  if OpenDialog1.FileName <> '' then
  begin
    SaveDialog1.FileName:=OpenDialog1.FileName;
    save;
  end else Button10Click(Button10);
end;

procedure TForm1.MaskEdit1Change(Sender: TObject);
begin
  TEdit(Sender).Color:=strtoint(TEdit(Sender).Text);
  Data.Parm.Color:=TEdit(Sender).Color;
end;

procedure TForm1.Edit1DblClick(Sender: TObject);
begin
  ColorDialog1.Color:=strtoint(TEdit(Sender).Text);
  ColorDialog1.Execute;
  TEdit(Sender).Text:='$'+inttohex(ColorDialog1.Color,6);
end;

procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
    if not (key in (['0'..'9']+['a'..'f']+['A'..'F'])) and (ord(key) <> VK_BACK) then
        key := #0;
end;

procedure TForm1.ListBox1Click(Sender: TObject);
begin
  if ListBox1.ItemIndex = -1 then exit;
  MaskEdit3.Text:='$'+inttohex(Data.Buffers[ListBox1.ItemIndex].Offset,2);
  MaskEdit4.Text:='$'+inttohex(Data.Buffers[ListBox1.ItemIndex].BufferLength,2);
end;

procedure TForm1.Button7Click(Sender: TObject);
begin
  ListBox1.Items.Add('Область'+inttostr(ListBox1.Count));
  Data.BuffersCout:=ListBox1.Count;
  SetLength(Data.Buffers, Data.BuffersCout);
end;

procedure TForm1.Button8Click(Sender: TObject);
begin
  if ListBox1.Count < 1 then exit;
  ListBox1.Items.Delete(ListBox1.Count-1);
  Data.BuffersCout:=ListBox1.Count;
  SetLength(Data.Buffers, Data.BuffersCout);
end;

procedure TForm1.MaskEdit3Change(Sender: TObject);
begin
  if ListBox1.ItemIndex = -1 then exit;
  Data.Buffers[ListBox1.ItemIndex].Offset:=strtoint(TEdit(MaskEdit3).Text);
end;

procedure TForm1.MaskEdit4Change(Sender: TObject);
begin
  if ListBox1.ItemIndex = -1 then exit;
  Data.Buffers[ListBox1.ItemIndex].BufferLength:=strtoint(TEdit(MaskEdit4).Text);
end;

procedure TForm1.Edit1Change(Sender: TObject);
 var i:integer;
begin
  if ListBox3.ItemIndex = -1 then exit;
  if ListBox2.ItemIndex = -1 then exit;
  Data.Logs[ListBox3.ItemIndex].Perems[ListBox2.ItemIndex].Name:=Edit1.Text;
  ListBox2.Items[ListBox2.ItemIndex]:=Edit1.Text;
  ComboBox2.Text:='';
  ComboBox2.Clear;
  for i:=0 to Data.Logs[ListBox3.ItemIndex].PeremsCout-1 do begin
    ComboBox2.Items.Add(Data.Logs[ListBox3.ItemIndex].Perems[i].Name);
   end;
end;

procedure TForm1.Button5Click(Sender: TObject);
 var i:integer;
begin
  if ListBox3.ItemIndex = -1 then exit;
  Data.Logs[ListBox3.ItemIndex].PeremsCout:=Data.Logs[ListBox3.ItemIndex].PeremsCout+1;
  SetLength(Data.Logs[ListBox3.ItemIndex].Perems, Data.Logs[ListBox3.ItemIndex].PeremsCout);
  Data.Logs[ListBox3.ItemIndex].Perems[Data.Logs[ListBox3.ItemIndex].PeremsCout-1].Name:='NoName'+inttostr(Data.Logs[ListBox3.ItemIndex].PeremsCout);
  ListBox2.Items.Add(Data.Logs[ListBox3.ItemIndex].Perems[Data.Logs[ListBox3.ItemIndex].PeremsCout-1].Name);
  ComboBox2.Text:='';
  ComboBox2.Clear;
  for i:=0 to Data.Logs[ListBox3.ItemIndex].PeremsCout-1 do begin
    ComboBox2.Items.Add(Data.Logs[ListBox3.ItemIndex].Perems[i].Name);
   end;
end;

procedure TForm1.Button6Click(Sender: TObject);
 var i:integer;
begin
  if ListBox3.ItemIndex = -1 then exit;
  if ListBox2.ItemIndex = -1 then exit;
  for i:=ListBox2.ItemIndex to ListBox2.Count-2 do
   Data.Logs[ListBox3.ItemIndex].Perems[i]:=Data.Logs[ListBox3.ItemIndex].Perems[i+1];

  Data.Logs[ListBox3.ItemIndex].PeremsCout:=Data.Logs[ListBox3.ItemIndex].PeremsCout-1;
  SetLength(Data.Logs[ListBox3.ItemIndex].Perems, Data.Logs[ListBox3.ItemIndex].PeremsCout);
  ComboBox2.Text:='';
  ComboBox2.Clear;
  for i:=0 to Data.Logs[ListBox3.ItemIndex].PeremsCout-1 do begin
    ComboBox2.Items.Add(Data.Logs[ListBox3.ItemIndex].Perems[i].Name);
   end;
    ListBox2.Items.Delete(ListBox2.ItemIndex);
end;

procedure TForm1.ListBox2Click(Sender: TObject);
begin
  if ListBox3.ItemIndex = -1 then exit;
  if ListBox2.ItemIndex = -1 then exit;
  Edit1.Text:=Data.Logs[ListBox3.ItemIndex].Perems[ListBox2.ItemIndex].Name;
  MaskEdit5.Text:='$'+inttohex(Data.Logs[ListBox3.ItemIndex].Perems[ListBox2.ItemIndex].Offset,2);
  Label19.Caption:= 'Переменная №'+ inttostr(ListBox2.ItemIndex);
end;

procedure TForm1.MaskEdit5Change(Sender: TObject);
begin
  if ListBox3.ItemIndex = -1 then exit;
  if ListBox2.ItemIndex = -1 then exit;
  Data.Logs[ListBox3.ItemIndex].Perems[ListBox2.ItemIndex].Offset:=strtoint(TEdit(MaskEdit5).Text);
end;

procedure TForm1.SpinButton1UpClick(Sender: TObject);
var Temp: TPerem; i:integer;
begin
  if ListBox3.ItemIndex = -1 then exit;
  if ListBox2.ItemIndex < 1 then exit;
  i:=ListBox2.ItemIndex;
  Temp:=Data.Logs[ListBox3.ItemIndex].Perems[ListBox2.ItemIndex-1];
  Data.Logs[ListBox3.ItemIndex].Perems[ListBox2.ItemIndex-1]:=Data.Logs[ListBox3.ItemIndex].Perems[ListBox2.ItemIndex];
  Data.Logs[ListBox3.ItemIndex].Perems[ListBox2.ItemIndex]:=Temp;
  ListBox2.Items.Move(ListBox2.ItemIndex,ListBox2.ItemIndex-1);
  ListBox2.ItemIndex:=i-1;
    ComboBox2.Text:='';
  ComboBox2.Clear;
  for i:=0 to Data.Logs[ListBox3.ItemIndex].PeremsCout-1 do begin
    ComboBox2.Items.Add(Data.Logs[ListBox3.ItemIndex].Perems[i].Name);
   end;
end;

procedure TForm1.SpinButton1DownClick(Sender: TObject);
var Temp: TPerem; i:integer;
begin
  if ListBox3.ItemIndex = -1 then exit;
  if ListBox2.ItemIndex = -1 then exit;
  if ListBox2.ItemIndex >= ListBox2.Count-1 then exit;
  i:=ListBox2.ItemIndex;
  Temp:=Data.Logs[ListBox3.ItemIndex].Perems[ListBox2.ItemIndex+1];
  Data.Logs[ListBox3.ItemIndex].Perems[ListBox2.ItemIndex+1]:=Data.Logs[ListBox3.ItemIndex].Perems[ListBox2.ItemIndex];
  Data.Logs[ListBox3.ItemIndex].Perems[ListBox2.ItemIndex]:=Temp;
  ListBox2.Items.Move(ListBox2.ItemIndex,ListBox2.ItemIndex+1);
  ListBox2.ItemIndex:=i+1;
    ComboBox2.Text:='';
  ComboBox2.Clear;
  for i:=0 to Data.Logs[ListBox3.ItemIndex].PeremsCout-1 do begin
    ComboBox2.Items.Add(Data.Logs[ListBox3.ItemIndex].Perems[i].Name);
   end;
end;

procedure TForm1.SpinButton2UpClick(Sender: TObject);
var Temp: TLog;
begin
  if ListBox3.ItemIndex < 1 then exit;
  LastItem:=ListBox3.ItemIndex-1;
  Temp:=Data.Logs[ListBox3.ItemIndex-1];
  Data.Logs[ListBox3.ItemIndex-1]:=Data.Logs[ListBox3.ItemIndex];
  Data.Logs[ListBox3.ItemIndex]:=Temp;
  ListBox3.Items.Move(ListBox3.ItemIndex,ListBox3.ItemIndex-1);
  ListBox3.ItemIndex:=LastItem;
end;

procedure TForm1.SpinButton2DownClick(Sender: TObject);
var Temp: TLog;
begin
  if ListBox3.ItemIndex = -1 then exit;
  if ListBox3.ItemIndex >= ListBox3.Count-1 then exit;
  LastItem:=ListBox3.ItemIndex+1;
  Temp:=Data.Logs[ListBox3.ItemIndex+1];
  Data.Logs[ListBox3.ItemIndex+1]:=Data.Logs[ListBox3.ItemIndex];
  Data.Logs[ListBox3.ItemIndex]:=Temp;
  ListBox3.Items.Move(ListBox3.ItemIndex,ListBox3.ItemIndex+1);
  ListBox3.ItemIndex:=LastItem;
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
  ListBox3.Items.Add('БезИмени'+inttostr(ListBox3.Count));
  Data.LogCout:=ListBox3.Count;
  SetLength(Data.Logs, Data.LogCout);
  Data.Logs[Data.LogCout-1].Name:=ListBox3.Items[ListBox3.Count-1];
  Data.Logs[Data.LogCout-1].Max:=1;
end;

procedure TForm1.Button4Click(Sender: TObject);
begin
  if ListBox3.ItemIndex = -1 then exit;
  if copy(ListBox3.Items[ListBox3.ItemIndex],0,5) <> '(del)' then
  ListBox3.Items[ListBox3.ItemIndex]:='(del)'+ ListBox3.Items[ListBox3.ItemIndex]
  else ListBox3.Items[ListBox3.ItemIndex]:= Data.Logs[ListBox3.ItemIndex].Name;
end;

procedure TForm1.ListBox3Click(Sender: TObject);
var i:integer;
begin

 if LastItem <> -1 then begin
  Data.Logs[LastItem].PenColor:=strtoint(TEdit(MaskEdit6).Text);
  Data.Logs[LastItem].Height:=SpinEdit4.Value;
  Data.Logs[LastItem].Formula:=Edit3.Text;
  Data.Logs[LastItem].Min:=SpinEdit5.Value;
  Data.Logs[LastItem].Max:=SpinEdit6.Value;
  Data.Logs[LastItem].PeremName:=Edit5.Text;
 end;

  if ListBox3.ItemIndex = -1 then exit;
  Edit2.Text:=Data.Logs[ListBox3.ItemIndex].Name;
  Edit5.Text:=Data.Logs[ListBox3.ItemIndex].PeremName;
  MaskEdit6.Text:='$'+inttohex(Data.Logs[ListBox3.ItemIndex].PenColor,6);
  SpinEdit4.Value:=Data.Logs[ListBox3.ItemIndex].Height;

  Edit3.Text:=Data.Logs[ListBox3.ItemIndex].Formula;
  SpinEdit5.Value:=Data.Logs[ListBox3.ItemIndex].Min;
  SpinEdit6.Value:=Data.Logs[ListBox3.ItemIndex].Max;

  ComboBox2.Clear;
  ComboBox2.Text:='';
  ListBox2.Clear;
  for i:=0 to Data.Logs[ListBox3.ItemIndex].PeremsCout-1 do begin
    ListBox2.Items.Add(Data.Logs[ListBox3.ItemIndex].Perems[i].Name);
    ComboBox2.Items.Add(Data.Logs[ListBox3.ItemIndex].Perems[i].Name);
   end;
  edit1.Text:='';
  LastItem:=ListBox3.ItemIndex;
end;

procedure TForm1.Button9Click(Sender: TObject);
begin
  Edit3.Text:=Edit3.Text+ComboBox2.Text;
end;

procedure TForm1.Edit2Change(Sender: TObject);
begin
  if ListBox3.ItemIndex = -1 then exit;
  Data.Logs[ListBox3.ItemIndex].Name:=Edit2.Text;
  if copy(ListBox3.Items[ListBox3.ItemIndex],0,5) <> '(del)' then
  ListBox3.Items[ListBox3.ItemIndex]:=Edit2.Text;
end;

procedure TForm1.FormCreate(Sender: TObject);
var jpg: TJpegImage;
begin
if FileExists('.\set.ini') then
begin
  OpenDialog1.FileName:='.\set.ini';
  load;
end else Button1Click(self);
if FileExists('.\back.jpg') then begin
  jpg:= TJpegImage.create;
  jpg.LoadFromFile('.\back.jpg');
  Image1.Picture.Bitmap.Width:=400;
  Image1.Picture.Bitmap.Height:=300;
  Image1.Picture.Bitmap.Canvas.StretchDraw(rect(0,0,400,300),jpg);
  jpg.Free;
 end;
end;

procedure TForm1.Button10Click(Sender: TObject);
begin
  SaveDialog1.Execute;
  save;
end;

procedure TForm1.MaskEdit2Change(Sender: TObject);
begin
  TEdit(Sender).Color:=strtoint(TEdit(Sender).Text);
  Data.Parm.FontColor:=TEdit(Sender).Color;
end;

procedure TForm1.SpinEdit2Change(Sender: TObject);
begin
  Data.Parm.FontSize:=SpinEdit2.Value;
end;

procedure TForm1.CheckBox2Click(Sender: TObject);
begin
  Data.Parm.FullScrean:=CheckBox2.Checked;
end;

procedure TForm1.ComboBox1Change(Sender: TObject);
begin
 Data.Parm.Port:=ComboBox1.Text;
end;

procedure TForm1.CheckBox1Click(Sender: TObject);
begin
  Data.Parm.Preambul:=CheckBox1.Checked;
end;

procedure TForm1.SpinEdit1Change(Sender: TObject);
begin
  Data.Parm.Timeout:=SpinEdit1.Value;
end;

procedure TForm1.MaskEdit6Change(Sender: TObject);
begin
  TEdit(Sender).Color:=strtoint(TEdit(Sender).Text);
end;

procedure TForm1.Panel1MouseMove(Sender: TObject; Shift: TShiftState; X,
  Y: Integer);
begin
  Label36.Caption:='X:'+inttostr(x)+' Y:'+inttostr(y);
end;

procedure TForm1.PageControl1Change(Sender: TObject);
var i:integer;
begin
if ListBox3.Count > 0 then begin
  ListBox3Click(self);
  ComboBox3.Clear;
  for i:=0 to Data.LogCout-1 do begin
    ComboBox3.Items.Add(Data.Logs[i].PeremName);
   end;
 end;
if ListBox4.ItemIndex <> -1 then begin
  ListBox4Click(self);
 end;
for i:=0 to Data.PriborCout-1 do
  Data.Pribors[i].Draw;

  panel1.Color:=Data.Parm.Color;
end;

procedure TForm1.Button13Click(Sender: TObject);
begin
  Edit4.Text:=Edit4.Text+ComboBox3.Text;
end;

procedure TForm1.Button11Click(Sender: TObject);
begin
  ListBox4.Items.Add('БезИмени'+inttostr(ListBox4.Count));
  Data.PriborCout:=ListBox4.Count;
  SetLength(Data.Pribors, Data.PriborCout);
  Data.Pribors[Data.PriborCout-1]:=TPribor.Create(self);
  Data.Pribors[Data.PriborCout-1].Parent:=Panel1;
  Data.Pribors[Data.PriborCout-1].Name:=ListBox4.Items[ListBox4.Count-1];
  Data.Pribors[Data.PriborCout-1].Transparent:=true;
  Data.Pribors[Data.PriborCout-1].OnMouseDown:=Button1MouseDown;
  Data.Pribors[Data.PriborCout-1].OnMouseMove:=Button1MouseMove;
  Data.Pribors[Data.PriborCout-1].OnMouseUp:=Button1MouseUp;
  Data.Pribors[Data.PriborCout-1].Size:=50;
  Data.Pribors[Data.PriborCout-1].Max:=50;
  Data.Pribors[Data.PriborCout-1].PinNum:=5;
  Data.Pribors[Data.PriborCout-1].NumLen:=5;
  Data.Pribors[Data.PriborCout-1].StartCone:=90;
  Data.Pribors[Data.PriborCout-1].EndCone:=-90;
  Data.Pribors[Data.PriborCout-1].ShkalaColor:=$f0f0f0;
  Data.Pribors[Data.PriborCout-1].StrelkaColor:=$00FFFF;
  Data.Pribors[Data.PriborCout-1].TextColor:=$f0f0f0;
end;

procedure TForm1.Button12Click(Sender: TObject);
begin
  if ListBox4.ItemIndex = -1 then exit;
  if copy(ListBox4.Items[ListBox4.ItemIndex],0,5) <> '(del)' then
  ListBox4.Items[ListBox4.ItemIndex]:='(del)'+ ListBox4.Items[ListBox4.ItemIndex]
  else ListBox4.Items[ListBox4.ItemIndex]:= Data.Pribors[ListBox4.ItemIndex].Name;
end;

procedure TForm1.ListBox4Click(Sender: TObject);
begin

 if LastItemPr <> -1 then begin
  Data.Pribors[LastItemPr].Vid:=RadioGroup1.ItemIndex;
  Data.Pribors[LastItemPr].Formula:=Edit4.Text;
  Data.Pribors[LastItemPr].Size:=SpinEdit3.Value;
  Data.Pribors[LastItemPr].X:=SpinEdit12.Value;
  Data.Pribors[LastItemPr].Y:=SpinEdit13.Value;
  Data.Pribors[LastItemPr].Max:=SpinEdit14.Value;
  Data.Pribors[LastItemPr].Min:=SpinEdit15.Value;
  Data.Pribors[LastItemPr].PinNum:=SpinEdit9.Value;
  Data.Pribors[LastItemPr].NumLen:=SpinEdit10.Value;
  Data.Pribors[LastItemPr].StartCone:=SpinEdit7.Value;
  Data.Pribors[LastItemPr].EndCone:=SpinEdit8.Value;
  Data.Pribors[LastItemPr].Horiz:=boolean(RadioGroup2.ItemIndex);
  Data.Pribors[LastItemPr].Revers:=CheckBox5.Checked;
  Data.Pribors[LastItemPr].ShkalaColor:=strtoint(TEdit(MaskEdit8).Text);
  Data.Pribors[LastItemPr].StrelkaColor:=strtoint(TEdit(MaskEdit9).Text);
  Data.Pribors[LastItemPr].TextColor:=strtoint(TEdit(MaskEdit10).Text);
  Data.Pribors[LastItemPr].Trevoga:=CheckBox3.Checked;
  Data.Pribors[LastItemPr].TrevogaLow:=boolean(RadioGroup3.ItemIndex);
  Data.Pribors[LastItemPr].TrevogaPoz:=SpinEdit11.Value;
  Data.Pribors[LastItemPr].TrevSwich:=CheckBox4.Checked;
  Data.Pribors[LastItemPr].TrevSwichedCol:=strtoint(TEdit(MaskEdit7).Text);
 end;

  if ListBox4.ItemIndex = -1 then exit;
  Edit6.Text:=Data.Pribors[ListBox4.ItemIndex].Name;
  RadioGroup1.ItemIndex:=Data.Pribors[ListBox4.ItemIndex].Vid;
  Edit4.Text:=Data.Pribors[ListBox4.ItemIndex].Formula;
  SpinEdit3.Value:=Data.Pribors[ListBox4.ItemIndex].Size;
  SpinEdit12.Value:=Data.Pribors[ListBox4.ItemIndex].X;
  SpinEdit13.Value:=Data.Pribors[ListBox4.ItemIndex].Y;
  SpinEdit14.Value:=Data.Pribors[ListBox4.ItemIndex].Max;
  SpinEdit15.Value:=Data.Pribors[ListBox4.ItemIndex].Min;
  SpinEdit9.Value:=Data.Pribors[ListBox4.ItemIndex].PinNum;
  SpinEdit10.Value:=Data.Pribors[ListBox4.ItemIndex].NumLen;
  SpinEdit7.Value:=Data.Pribors[ListBox4.ItemIndex].StartCone;
  SpinEdit8.Value:=Data.Pribors[ListBox4.ItemIndex].EndCone;
  RadioGroup2.ItemIndex:=integer(Data.Pribors[ListBox4.ItemIndex].Horiz);
  CheckBox5.Checked:=Data.Pribors[ListBox4.ItemIndex].Revers;
  MaskEdit8.Text:='$'+inttohex(Data.Pribors[ListBox4.ItemIndex].ShkalaColor,6);
  MaskEdit9.Text:='$'+inttohex(Data.Pribors[ListBox4.ItemIndex].StrelkaColor,6);
  MaskEdit10.Text:='$'+inttohex(Data.Pribors[ListBox4.ItemIndex].TextColor,6);
  CheckBox3.Checked:=Data.Pribors[ListBox4.ItemIndex].Trevoga;
  RadioGroup3.ItemIndex:=integer(Data.Pribors[ListBox4.ItemIndex].TrevogaLow);
  SpinEdit11.Value:=round(Data.Pribors[ListBox4.ItemIndex].TrevogaPoz);
  CheckBox4.Checked:=Data.Pribors[ListBox4.ItemIndex].TrevSwich;
  MaskEdit7.Text:='$'+inttohex(Data.Pribors[ListBox4.ItemIndex].TrevSwichedCol,6);

  LastItemPr:=ListBox4.ItemIndex;
end;

procedure TForm1.Edit6Change(Sender: TObject);
begin
  if ListBox4.ItemIndex = -1 then exit;
  Data.Pribors[ListBox4.ItemIndex].Name:=Edit6.Text;
  if copy(ListBox4.Items[ListBox4.ItemIndex],0,5) <> '(del)' then
  ListBox4.Items[ListBox4.ItemIndex]:=Edit6.Text;
end;

procedure TForm1.Button14Click(Sender: TObject);
begin
Form2:=tform.Create(self);
Form2.OnClose:=FormClose;
Form2.Height:=380;
Form2.Width:=450;
panel1.Parent:=Form2;
obbutton:=tbutton.Create(self);
obbutton.Parent:=Form2;
obbutton.Top:=313;
obbutton.Left:=50;
obbutton.Caption:='Обновить';
obbutton.OnClick:=PageControl1Change;
Form2.Show;
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
panel1.Parent:=TabSheet3;
end;

procedure TForm1.SpinEdit16Change(Sender: TObject);
begin
Label41.Visible:=CheckBox6.Checked;
Label41.Font.Size:=round(SpinEdit16.Value*0.13);
Label41.Font.Color:=strtoint(tedit(MaskEdit11).Text);
end;

procedure TForm1.SpinEdit17Change(Sender: TObject);
begin
Label41.Left:=SpinEdit17.Value;
end;

procedure TForm1.SpinEdit18Change(Sender: TObject);
begin
Label41.Top:=SpinEdit18.Value;
end;

procedure TForm1.SpinButton3DownClick(Sender: TObject);
var temp:TPribor; i:integer;
begin
  if ListBox4.ItemIndex = -1 then exit;
  if ListBox4.ItemIndex >= ListBox4.Count-1 then exit;
  Temp:=Data.Pribors[ListBox4.ItemIndex+1];
  Data.Pribors[ListBox4.ItemIndex+1]:=Data.Pribors[ListBox4.ItemIndex];
  Data.Pribors[ListBox4.ItemIndex]:=Temp;
  LastItemPr:=ListBox4.ItemIndex+1;
  ListBox4.Items.Move(ListBox4.ItemIndex,ListBox4.ItemIndex+1);
  ListBox4.ItemIndex:=LastItemPr;
  for i:=0 to Data.PriborCout-1 do
    Data.Pribors[i].BringToFront;
end;

procedure TForm1.SpinButton3UpClick(Sender: TObject);
var temp:TPribor; i:integer;
begin
  if ListBox4.ItemIndex < 1 then exit;
  Temp:=Data.Pribors[ListBox4.ItemIndex-1];
  Data.Pribors[ListBox4.ItemIndex-1]:=Data.Pribors[ListBox4.ItemIndex];
  Data.Pribors[ListBox4.ItemIndex]:=Temp;
  LastItemPr:=ListBox4.ItemIndex-1;
  ListBox4.Items.Move(ListBox4.ItemIndex,ListBox4.ItemIndex-1);
  ListBox4.ItemIndex:=LastItemPr;
  for i:=0 to Data.PriborCout-1 do
    Data.Pribors[i].BringToFront;
end;

end.
