1
0
mirror of synced 2024-09-19 09:46:09 +00:00
x2utils/X2UtService.GUIContext.Form.pas

133 lines
2.7 KiB
ObjectPascal
Raw Normal View History

2016-10-21 11:58:28 +00:00
unit X2UtService.GUIContext.Form;
interface
uses
System.Classes,
Vcl.Controls,
Vcl.ExtCtrls,
Vcl.Forms,
Vcl.StdCtrls,
Winapi.Messages,
X2UtService.Intf;
const
CM_AFTERSHOW = WM_USER + 1;
type
TX2ServiceContextGUIForm = class(TForm)
btnClose: TButton;
gbStatus: TGroupBox;
lblStatus: TLabel;
2016-10-21 12:35:35 +00:00
shpStatus: TShape;
2016-10-21 11:58:28 +00:00
gbCustomControl: TGroupBox;
lblControlCode: TLabel;
edtControlCode: TEdit;
btnSend: TButton;
procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
2016-10-21 12:35:35 +00:00
procedure edtControlCodeChange(Sender: TObject);
procedure btnSendClick(Sender: TObject);
procedure btnCloseClick(Sender: TObject);
2016-10-21 11:58:28 +00:00
private
FContext: IX2ServiceContext;
FService: IX2Service;
protected
procedure DoShow; override;
procedure CMAfterShow(var Msg: TMessage); message CM_AFTERSHOW;
2016-10-21 12:35:35 +00:00
function GetControlCode: Byte;
2016-10-21 11:58:28 +00:00
public
property Context: IX2ServiceContext read FContext write FContext;
property Service: IX2Service read FService write FService;
end;
implementation
uses
System.Math,
System.SysUtils,
2016-10-21 12:35:35 +00:00
Vcl.Graphics,
2016-10-21 11:58:28 +00:00
Winapi.Windows;
{$R *.dfm}
2016-10-21 12:35:35 +00:00
const
StatusColorStarting = $00B0FFB0;
StatusColorStarted = clGreen;
StatusColorStopping = $008080FF;
StatusColorStopped = clRed;
2016-10-21 11:58:28 +00:00
// #ToDo1 -oMvR: 21-10-2016: separate service handling out to thread to prevent blocking of the UI
{ TX2ServiceContextGUIForm }
procedure TX2ServiceContextGUIForm.DoShow;
begin
inherited DoShow;
PostMessage(Self.Handle, CM_AFTERSHOW, 0, 0);
end;
procedure TX2ServiceContextGUIForm.CMAfterShow(var Msg: TMessage);
begin
2016-10-21 12:35:35 +00:00
shpStatus.Brush.Color := StatusColorStarting;
2016-10-21 11:58:28 +00:00
lblStatus.Caption := 'Starting...';
2016-10-21 12:35:35 +00:00
Application.ProcessMessages;
2016-10-21 11:58:28 +00:00
if Service.Start(Context) then
2016-10-21 12:35:35 +00:00
begin
shpStatus.Brush.Color := StatusColorStarted;
lblStatus.Caption := 'Started';
end else
begin
shpStatus.Brush.Color := StatusColorStopped;
2016-10-21 11:58:28 +00:00
lblStatus.Caption := 'Failed to start';
2016-10-21 12:35:35 +00:00
end;
2016-10-21 11:58:28 +00:00
end;
procedure TX2ServiceContextGUIForm.edtControlCodeChange(Sender: TObject);
begin
2016-10-21 12:35:35 +00:00
edtControlCode.Text := IntToStr(GetControlCode);
end;
procedure TX2ServiceContextGUIForm.btnSendClick(Sender: TObject);
begin
Service.DoCustomControl(GetControlCode);
end;
procedure TX2ServiceContextGUIForm.btnCloseClick(Sender: TObject);
begin
Close;
2016-10-21 11:58:28 +00:00
end;
procedure TX2ServiceContextGUIForm.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
2016-10-21 12:35:35 +00:00
shpStatus.Brush.Color := StatusColorStopping;
2016-10-21 11:58:28 +00:00
lblStatus.Caption := 'Stopping...';
2016-10-21 12:35:35 +00:00
Application.ProcessMessages;
2016-10-21 11:58:28 +00:00
CanClose := Service.Stop;
if not CanClose then
lblStatus.Caption := 'Failed to stop';
end;
2016-10-21 12:35:35 +00:00
function TX2ServiceContextGUIForm.GetControlCode: Byte;
begin
Result := Byte(Min(Max(StrToIntDef(edtControlCode.Text, 0), 128), 255));
end;
2016-10-21 11:58:28 +00:00
end.