CRazY ABouT/programming

[Delphi] Frame as a component

띵스 2024. 11. 29. 10:45

Component로 Form위에 반복적으로 사용할 프레임 생성

 

type
  TFrameLpr = class(TFrame)
    pnlTitle: TPanel;
    pnlInfo: TPanel;
    pnlImage: TPanel;
    swEachLprState: TToggleSwitch;
    btnBarControl: TButton;
    imgCar: TImage;
    popGate: TPopupMenu;
    popOpen: TMenuItem;
    popClose: TMenuItem;
    popOpenLock: TMenuItem;
    popOpenUnLock: TMenuItem;
    lblUnitName: TLabel;
    lblCarType: TLabel;
    lblCarNo: TLabel;
    imgUnitNameBack: TImage;
    lblIndicator: TLabel;
    imgIndicatorList: TcxImageList;
    imgIndicator: TImage;
    lblDeptInfo: TLabel;
    imgVisitorPopup: TImage;
    procedure btnBarControlClick(Sender: TObject);
    procedure popOpenClick(Sender: TObject);
    procedure imgVisitorPopupClick(Sender: TObject);
  private
    { Private declarations }
    FLprStatus: TLprStatus;
    FUnitName: string;
    FCarType: string;
    FCarNo: string;
    FDeptInfo: string;
    FImagePath: string;
    FActive: Boolean;
    FDspActive: Boolean;
    FFrameIndex: Integer;
    FUseBarControl: Boolean;
    FUseEachLprState: Boolean;
    FInputVisitorInfo: Boolean;
    FUnitInfo: TUnitInfoClass;
    LprClientSocket: TClientSocket;
    DspClientSocket: TClientSocket;
    LprLiveThd: TThread;
    FCurrentCarInfo: TR_CAR_INFO;
    procedure SocketConnected(Sender: TObject; Socket: TCustomWinSocket);
    procedure SocketDisconnected(Sender: TObject; Socket: TCustomWinSocket);
    procedure SocketError(Sender: TObject; Socket: TCustomWinSocket; ErrorEvent: TErrorEvent; var ErrorCode: Integer);
    procedure SocketRead(Sender: TObject; Socket: TCustomWinSocket);
    procedure DspSocketConnected(Sender: TObject; Socket: TCustomWinSocket);
    procedure DspSocketDisconnected(Sender: TObject; Socket: TCustomWinSocket);
    procedure DspSocketError(Sender: TObject; Socket: TCustomWinSocket; ErrorEvent: TErrorEvent; var ErrorCode: Integer);
    procedure DspSocketRead(Sender: TObject; Socket: TCustomWinSocket);
    procedure SetActive(const Value: Boolean);
    procedure SetCarNo(const Value: string);
    procedure SetCarType(const Value: string);
    procedure SetUnitName(const Value: string);
    function GetUnitInfo: TUnitInfoClass;
    procedure SetUnitInfo(const Value: TUnitInfoClass);
    procedure SetImagePath(const Value: string);
    procedure SetLprStatus(const Value: TLprStatus);
    procedure SetDeptInfo(const Value: string);
    procedure tmrHeartbeatTimer(Sender: TObject);
    procedure tmrDspHeartbeatTimer(Sender: TObject);
    procedure tmrWaitVisitorPopupTimer(Sender: TObject);
    procedure SetDspActive(const Value: Boolean);
    procedure InLprProcess(LprBuf: TR_LPR_CLIENT);
    procedure OutLprProcess(LprBuf: TR_LPR_CLIENT);
    procedure DspProc(const buf: TR_LPR_CLIENT);
    procedure SetUseBarControl(const Value: Boolean);
    procedure SetUseEachLprState(const Value: Boolean);
    procedure SetInputVisitorInfo(const Value: Boolean);
  public
    { Public declarations }
    tmrHeartbeat: TTimer;
    tmrDspHeartbeat: TTimer;
    tmrWaitVisitorPopup: TTimer;
    procedure SendDspText(ACarType: TCarType; ACarNo: string); overload;
    procedure SendDspText(AText: AnsiString); overload;
    procedure ClearFrame;
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
  published
    property LprStatus  : TLprStatus         read FLprStatus           write SetLprStatus;
    property UnitName   : string             read FUnitName            write SetUnitName;
    property CarType    : string             read FCarType             write SetCarType;
    property CarNo      : string             read FCarNo               write SetCarNo;
    property DeptInfo   : string             read FDeptInfo            write SetDeptInfo;
    property ImagePath  : string             read FImagePath           write SetImagePath;
    property Active     : Boolean            read FActive              write SetActive;
    property DspActive  : Boolean            read FDspActive           write SetDspActive;
    property FrameIndex : Integer            read FFrameIndex          write FFrameIndex;
    property UseBarControl: Boolean          read FUseBarControl       write SetUseBarControl;
    property UseEachLprState: Boolean        read FUseEachLprState     write SetUseEachLprState;
    property UnitInfo   : TUnitInfoClass     read GetUnitInfo          write SetUnitInfo;
    property InputVisitorInfo: Boolean       read FInputVisitorInfo    write SetInputVisitorInfo;
    property CurrentCarInfo: TR_CAR_INFO     read FCurrentCarInfo;
  end;

Main폼 내 Frame위치에 위에서 생성한 Frame을 위치하고 동적으로 생성하는 코드 추가

 

procedure TfrmMain.pnlLprFramesResize(Sender: TObject);
begin
  DrawFrames(_preLprCount, _LprFrameGroup, 1);
end;

procedure TfrmMain.DrawFrames(AFrameCount: Integer; const AFrameGroup: TArray<TFrame>; const AFlag: Integer =0 );
begin
  for var i := Low(AFrameGroup) to High(AFrameGroup) do
  begin
    AFrameGroup[i].Height := pnlLprFrames.Height div (((AFrameCount-AFlag) div _FramesInARow)+1);
    AFrameGroup[i].Top := AFrameGroup[i].Height * (i div _FramesInARow);
    AFrameGroup[i].Width := pnlLprFrames.Width div _FramesInARow;
    AFrameGroup[i].Left := AFrameGroup[i].Width * (i mod _FramesInARow);
  end;
end;

 

procedure TfrmMain.AddFrame(var AFrameGroup: TArray<TFrame>);
var
  LastIndex: Word;
begin
  SetLength(AFrameGroup, Length(AFrameGroup)+1);
  LastIndex := Length(AFrameGroup)-1;

  AFrameGroup[LastIndex] := TFrameLpr.Create(pnlLprFrames);
  with TFrameLpr(AFrameGroup[LastIndex]) do
  begin
    Name := 'FrameLpr'+IntToStr(LastIndex);
    Tag := LastIndex;
    Width := pnlLprFrames.Width div _FramesInARow;
    Height := pnlLprFrames.Height div ((LastIndex div _FramesInARow)+1);
    Left := Width * (LastIndex mod _FramesInARow);
    Top := Height * (LastIndex div _FramesInARow);
    Parent := pnlLprFrames;
    UseBarControl := ConfigInfo.UseBarControl;
    UseEachLprState := ConfigInfo.UseEachLprState;
  end;

  TFrameLpr(pnlLprFrames.FindChildControl(AFrameGroup[LastIndex].Name)).UnitName := AFrameGroup[LastIndex].Name;
  TFrameLpr(pnlLprFrames.FindChildControl(AFrameGroup[LastIndex].Name)).FrameIndex := LastIndex;
//  TFrameLpr(pnlLprFrames.FindChildControl(AFrameGroup[LastIndex].Name)).Caption

  DrawFrames(LastIndex, AFrameGroup, 0);
end;

procedure TfrmMain.RemoveFrame(var AFrameGroup: TArray<TFrame>);
var
  LastIndex: Word;
begin
  LastIndex := Length(AFrameGroup)-1;
  AFrameGroup[LastIndex].Free;
  AFrameGroup[LastIndex] := nil;
  SetLength(AFrameGroup, LastIndex);
  DrawFrames(LastIndex, AFrameGroup, 1);
end;