gpt4 book ai didi

delphi - 新的自定义组件导致 IDE 崩溃

转载 作者:行者123 更新时间:2023-12-02 04:57:55 24 4
gpt4 key购买 nike

这是我第一次尝试创建组件,我想我会从一个非常基本的 LED(灯泡而不是文本)开始,在阅读了几篇文章后,我想出了以下代码(正在运行),我关闭了关闭 IDE (XE10.1 update2),当尝试在新的空白应用程序中使用该组件时,添加控件时 IDE 崩溃,任何人都可以提供帮助:

unit ZaxLED;

interface

uses
Windows, Messages, Controls, Forms, Graphics, ExtCtrls, Classes, math;

type
TZaxLED = class(TGraphicControl)
private
{ Private declarations }
FColorOn: Tcolor;
FColorOff: Tcolor;
Color: Tcolor;
FStatus: Boolean;
FOnChange: TNotifyEvent;

procedure SetColorOn(Value: Tcolor);
procedure SetColorOff(Value: Tcolor);

function GetStatus: Boolean;
procedure SetStatus(Value: Boolean);

protected
{ Protected declarations }
procedure Paint; override;

public
{ Public declarations }
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure SetBounds(ALeft, ATop, AWidth, AHeight: Integer); override;
published
{ Published declarations }
property width default 17;
property height default 17;
property Align;
property Anchors;
property Constraints;
property ColorOn: Tcolor read FColorOn write SetColorOn default clLime;
property ColorOff: Tcolor read FColorOff write SetColorOff default clGray;

property Status: Boolean read GetStatus write SetStatus default True;
property OnChange: TNotifyEvent read FOnChange write FOnChange;

end;

procedure Register;

implementation

procedure Register;
begin
RegisterComponents('Samples', [TZaxLED]);
end;

{ TZaxLED }

constructor TZaxLED.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
width := 17;
height := 17;
ColorOn := clLime;
ColorOff := clGray;
Status := False;
Color := ColorOff;
end;

destructor TZaxLED.Destroy;
begin
inherited Destroy;
end;

function TZaxLED.GetStatus: Boolean;
begin
Result := FStatus;
end;

procedure TZaxLED.Paint;
var
Radius, xCenter, YCenter: Integer;
begin
if csDesigning in ComponentState then
begin
Canvas.Pen.Style := psDot;
Canvas.Brush.Style := bsClear;
Canvas.Rectangle(ClientRect);
end;


Canvas.Brush.Color := Color;
Radius := Floor(width / 2) - 2;
xCenter := Floor(width / 2);
YCenter := Floor(height / 2);
Canvas.Ellipse(xCenter - Radius, YCenter - Radius, xCenter + Radius,
YCenter + Radius);

end;

procedure TZaxLED.SetBounds(ALeft, ATop, AWidth, AHeight: Integer);
begin
if Autosize and (Align in [alNone, alCustom]) then
inherited SetBounds(ALeft, ATop, width, height)
else
inherited SetBounds(ALeft, ATop, AWidth, AHeight);
end;

procedure TZaxLED.SetColorOff(Value: Tcolor);
begin
FColorOff := Value;
if not Status then
ColorOff := Value;
end;

procedure TZaxLED.SetColorOn(Value: Tcolor);
begin
FColorOn := Value;
if Status then
ColorOn := Value;
end;

procedure TZaxLED.SetStatus(Value: Boolean);
begin
if Value <> FStatus then
begin
FStatus := Value;
if FStatus then
Color := ColorOn
else
Color := ColorOff;
if Assigned(FOnChange) then
FOnChange(Self);
end;
end;

end.
<小时/>

我已经更新了代码以接受来自 @Ari0nhh 的评论,我认为这是有效的,但 led 现在在设计或运行时不会改变颜色

procedure TZaxLED.SetColorOff(Value: Tcolor);
begin
FColorOff := Value;
end;

procedure TZaxLED.SetColorOn(Value: Tcolor);
begin
FColorOn := Value;
end;

最佳答案

我发现您的代码存在一些问题。

  • 您的 uses 子句需要清理。不要对您实际不使用的单元创建依赖关系。仅由组件内部代码使用的单元应移至 implementation 部分的 uses 子句。 interface 部分的 uses 子句应仅引用满足公共(public)接口(interface)直接使用的类型/引用所需的单元。

  • 当已经存在继承的 Color 属性时,正在声明 Color 数据成员。该数据成员是多余且不必要的,因为它的唯一目的是将选定的 Status 颜色从 SetStatus() 传送到 Paint(),其中不是必需的,因为 Paint() 可以(并且应该)直接确定该颜色值。

  • Status 属性声明时的默认值为 True,但该属性在构造函数中被初始化为 False。

  • ColorOnColorOff 属性 setter 以递归方式调用自身,而不是触发重绘以便显示新的状态图像。

  • Status 属性 setter 也不会触发重绘。

话虽如此,尝试更像这样的东西:

unit ZaxLED;

interface

uses
Classes, Controls, Graphics;

type
TZaxLED = class(TGraphicControl)
private
{ Private declarations }
FColorOn: TColor;
FColorOff: TColor;
FStatus: Boolean;
FOnChange: TNotifyEvent;

procedure SetColorOn(Value: TColor);
procedure SetColorOff(Value: TColor);
procedure SetStatus(Value: Boolean);

protected
{ Protected declarations }
procedure Paint; override;

public
{ Public declarations }
constructor Create(AOwner: TComponent); override;
procedure SetBounds(ALeft, ATop, AWidth, AHeight: Integer); override;

published
{ Published declarations }
property Width default 17;
property Height default 17;
property Align;
property Anchors;
property Constraints;
property ColorOn: TColor read FColorOn write SetColorOn default clLime;
property ColorOff: TColor read FColorOff write SetColorOff default clGray;
property Status: Boolean read FStatus write SetStatus default False;
property OnChange: TNotifyEvent read FOnChange write FOnChange;
end;

procedure Register;

implementation

uses
Math;

procedure Register;
begin
RegisterComponents('Samples', [TZaxLED]);
end;

{ TZaxLED }

constructor TZaxLED.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FColorOn := clLime;
FColorOff := clGray;
FStatus := False;
Width := 17;
Height := 17;
end;

procedure TZaxLED.Paint;
var
Radius, xCenter, YCenter: Integer;
begin
if csDesigning in ComponentState then
begin
Canvas.Pen.Style := psDot;
Canvas.Brush.Style := bsClear;
Canvas.Rectangle(ClientRect);
end;

if FStatus then
Canvas.Brush.Color := FColorOn
else
Canvas.Brush.Color := FColorOff;

Radius := Floor(Width / 2) - 2;
xCenter := Floor(Width / 2);
YCenter := Floor(Height / 2);
Canvas.Ellipse(xCenter - Radius, YCenter - Radius, xCenter + Radius, YCenter + Radius);
end;

procedure TZaxLED.SetBounds(ALeft, ATop, AWidth, AHeight: Integer);
begin
if AutoSize and (Align in [alNone, alCustom]) then
begin
AWidth := Width;
AHeight:= Height;
end;
inherited SetBounds(ALeft, ATop, AWidth, AHeight);
end;

procedure TZaxLED.SetColorOff(Value: TColor);
begin
if FColorOff <> Value then
begin
FColorOff := Value;
if not FStatus then Invalidate;
end;
end;

procedure TZaxLED.SetColorOn(Value: TColor);
begin
if FColorOn <> Value then
begin
FColorOn := Value;
if FStatus then Invalidate;
end;
end;

procedure TZaxLED.SetStatus(Value: Boolean);
begin
if Value <> FStatus then
begin
FStatus := Value;
Invalidate;
if Assigned(FOnChange) then
FOnChange(Self);
end;
end;

end.

关于delphi - 新的自定义组件导致 IDE 崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41627488/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com