
delphi7中 如何实现用一个控件显示两个变量信息
2013-08-19
展开全部
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ComCtrls, StdCtrls;type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end; TPLC = class(TWinControl)
private
FStatuText: String;
FStatuBit: Byte;
procedure SetStatuBit(const Value: Byte);
procedure WMPaint(var Message:TWMPaint);message WM_PAINT;
public
constructor Create(AOwner: TComponent);override;
published
property StatuBit: Byte read FStatuBit write SetStatuBit default 0;
end;var
Form1: TForm1;
plc: TPLC;
b: Boolean=False;implementation{$R *.dfm}{ TPLC }constructor TPLC.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
Visible:=True;
FStatuBit:=0;
FStatuText:='停止工作';
Font.Color:=clRed;
Color:=clBlack;
Width:=220;
Height:=110;
end;procedure TPLC.SetStatuBit(const Value: Byte);
begin
if Value<>FStatuBit then
FStatuBit := Value;
if FStatubit=0 then
begin
Font.Color:=clRed;
FStatuText:='停止工作';
end
else begin
Font.Color:=clGreen;
FStatuText:='正常工作';
end;
UpdateWindow(Handle);
end;procedure TPLC.WMPaint(var Message: TWMPaint);
var
ca: TCanvas;
begin
inherited;
ca:=TCanvas.Create;
ca.Handle:=GetDC(Handle);
try
ca.Font.Color:=Font.Color;
ca.Font.Name:='宋体';
ca.Font.Style:=[fsBold];
ca.Font.Size:=15;
ca.Brush.Style:=bsClear;
ca.TextOut((Width-ca.TextWidth(FStatuText)) div 2,(Height-ca.TextHeight('W')) div 2, FStatuText);
finally
ReleaseDC(Handle, ca.Handle);
ca.Free;
end;
end;procedure TForm1.Button1Click(Sender: TObject);
begin
if b then
PLC.StatuBit:=0
else
PLC.StatuBit:=1;
b:=not b;
end;procedure TForm1.FormCreate(Sender: TObject);
begin
plc:=TPLC.Create(Self);
InsertControl(plc);
end;procedure TForm1.FormDestroy(Sender: TObject);
begin
if Assigned(plc) then plc.Free;
end;end.
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ComCtrls, StdCtrls;type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end; TPLC = class(TWinControl)
private
FStatuText: String;
FStatuBit: Byte;
procedure SetStatuBit(const Value: Byte);
procedure WMPaint(var Message:TWMPaint);message WM_PAINT;
public
constructor Create(AOwner: TComponent);override;
published
property StatuBit: Byte read FStatuBit write SetStatuBit default 0;
end;var
Form1: TForm1;
plc: TPLC;
b: Boolean=False;implementation{$R *.dfm}{ TPLC }constructor TPLC.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
Visible:=True;
FStatuBit:=0;
FStatuText:='停止工作';
Font.Color:=clRed;
Color:=clBlack;
Width:=220;
Height:=110;
end;procedure TPLC.SetStatuBit(const Value: Byte);
begin
if Value<>FStatuBit then
FStatuBit := Value;
if FStatubit=0 then
begin
Font.Color:=clRed;
FStatuText:='停止工作';
end
else begin
Font.Color:=clGreen;
FStatuText:='正常工作';
end;
UpdateWindow(Handle);
end;procedure TPLC.WMPaint(var Message: TWMPaint);
var
ca: TCanvas;
begin
inherited;
ca:=TCanvas.Create;
ca.Handle:=GetDC(Handle);
try
ca.Font.Color:=Font.Color;
ca.Font.Name:='宋体';
ca.Font.Style:=[fsBold];
ca.Font.Size:=15;
ca.Brush.Style:=bsClear;
ca.TextOut((Width-ca.TextWidth(FStatuText)) div 2,(Height-ca.TextHeight('W')) div 2, FStatuText);
finally
ReleaseDC(Handle, ca.Handle);
ca.Free;
end;
end;procedure TForm1.Button1Click(Sender: TObject);
begin
if b then
PLC.StatuBit:=0
else
PLC.StatuBit:=1;
b:=not b;
end;procedure TForm1.FormCreate(Sender: TObject);
begin
plc:=TPLC.Create(Self);
InsertControl(plc);
end;procedure TForm1.FormDestroy(Sender: TObject);
begin
if Assigned(plc) then plc.Free;
end;end.
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询