D
dreamer567
Unregistered / Unconfirmed
GUEST, unregistred user!
现在有这样一个类:
unit MustItem7Class;
interface
uses Windows, SysUtils, Classes, Messages, Controls, ExtCtrls, Graphics;
type
TMustItemPainter = class
private
FCtrl: TControl;
FImage: TImage;
FMustItem: Boolean;
FVisible: Boolean;
procedure SetMustImage(const Value: Boolean);
procedure SetMustItem(const Value: Boolean);
procedure SetVisible(const Value: Boolean);
public
constructor Create(Ctrl: TControl);
destructor Destroy; override;
procedure Repaint;
property MustItem: Boolean read FMustItem write SetMustItem;
property Visible: Boolean read FVisible write SetVisible;
end;
implementation
{$R ImgMustItem.res}
constructor TMustItemPainter.Create(Ctrl: TControl);
begin
FCtrl := Ctrl;
end;
destructor TMustItemPainter.Destroy;
begin
SetMustImage(False);
inherited;
end;
procedure TMustItemPainter.Repaint;
begin
if not Assigned(FImage) then Exit;
FImage.Visible := FVisible;
FImage.Left := FCtrl.Left + FCtrl.Width + 2;
FImage.Top := FCtrl.Top + (FCtrl.Height - FImage.Height) div 2;
end;
procedure TMustItemPainter.SetMustImage(const Value: Boolean);
begin
if Value then
begin
if not Assigned(FImage) then
begin
FImage := TImage.Create(FCtrl);
FImage.Parent := FCtrl.Parent;
FImage.Picture.Bitmap := TBitmap.Create;
FImage.Picture.Bitmap.LoadFromResourceName(HInstance, 'ImgMustItem');
//FImage.Picture.Bitmap.LoadFromFile('../MustItem.bmp');
FImage.AutoSize := True;
end;
Repaint;
end
else begin
FImage.Free;
FImage := nil;
end;
end;
procedure TMustItemPainter.SetMustItem(const Value: Boolean);
begin
FMustItem := Value;
SetMustImage(FMustItem);
end;
procedure TMustItemPainter.SetVisible(const Value: Boolean);
begin
FVisible := Value;
Repaint;
end;
end.
然后我想写一个继承tlabel的控件ttestlabel,加个mustitem属性,如果为真,就画出mustitem.res的图形。关键现在问题出现了,就是在ttestlabel的free的时候,fimagepainter.free出问题,然后发现是TMustItemPainter.fimage.free时出问题。好像是因为tcontrol接收不到焦点的关系。因为以前这个类放在tedit里面是好的,也就是fctrl是twincontrol型的。但是tlabel继承的是tcontrol类,我只好把fctrl改为tcontrol。可是free的问题怎么办?
ttestlabel的代码如下:
TWA7Label = class(TLabel)
private
{ Private 宣言 }
FMustItem: Boolean;
FMustItemImage: TMustItemPainter;
procedure SetMustItem(Value : Boolean);
function GetVisible: Boolean;
procedure SetVisible(value: Boolean);
protected
procedure loaded; override;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure SetBounds(ALeft, ATop, AWidth, AHeight: Integer); override;
published
property MustItem : boolean read FMustItem write SetMustItem;
property Visible : boolean read GetVisible write SetVisible;
end;
procedure Register;
implementation
uses StrUtils, Math;
procedure Register;
begin
RegisterComponents('Company', [TWA7Label]);
end;
{ TWA7Label }
constructor TWA7Label.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FMustItemImage := TMustItemPainter.Create(Self);
FMustItemImage.Visible := Self.Visible;
end;
destructor TWA7Label.Destroy;
begin
FMustItemImage.Free;
inherited;
end;
procedure TWA7Label.SetMustItem(Value: Boolean);
begin
FMustItem := value;
FMustItemImage.MustItem := Value;
end;
procedure TWA7Label.SetVisible(Value: boolean);
begin
inherited Visible := Value;
FMustItemImage.Visible := Visible;
end;
function TWA7Label.GetVisible: Boolean;
begin
Result := inherited Visible;
end;
procedure TWA7Label.SetBounds(ALeft, ATop, AWidth, AHeight: Integer);
begin
inherited;
if Assigned(FMustItemImage) then FMustItemImage.Repaint;
end;
end.
高手啊,帮忙啊。
unit MustItem7Class;
interface
uses Windows, SysUtils, Classes, Messages, Controls, ExtCtrls, Graphics;
type
TMustItemPainter = class
private
FCtrl: TControl;
FImage: TImage;
FMustItem: Boolean;
FVisible: Boolean;
procedure SetMustImage(const Value: Boolean);
procedure SetMustItem(const Value: Boolean);
procedure SetVisible(const Value: Boolean);
public
constructor Create(Ctrl: TControl);
destructor Destroy; override;
procedure Repaint;
property MustItem: Boolean read FMustItem write SetMustItem;
property Visible: Boolean read FVisible write SetVisible;
end;
implementation
{$R ImgMustItem.res}
constructor TMustItemPainter.Create(Ctrl: TControl);
begin
FCtrl := Ctrl;
end;
destructor TMustItemPainter.Destroy;
begin
SetMustImage(False);
inherited;
end;
procedure TMustItemPainter.Repaint;
begin
if not Assigned(FImage) then Exit;
FImage.Visible := FVisible;
FImage.Left := FCtrl.Left + FCtrl.Width + 2;
FImage.Top := FCtrl.Top + (FCtrl.Height - FImage.Height) div 2;
end;
procedure TMustItemPainter.SetMustImage(const Value: Boolean);
begin
if Value then
begin
if not Assigned(FImage) then
begin
FImage := TImage.Create(FCtrl);
FImage.Parent := FCtrl.Parent;
FImage.Picture.Bitmap := TBitmap.Create;
FImage.Picture.Bitmap.LoadFromResourceName(HInstance, 'ImgMustItem');
//FImage.Picture.Bitmap.LoadFromFile('../MustItem.bmp');
FImage.AutoSize := True;
end;
Repaint;
end
else begin
FImage.Free;
FImage := nil;
end;
end;
procedure TMustItemPainter.SetMustItem(const Value: Boolean);
begin
FMustItem := Value;
SetMustImage(FMustItem);
end;
procedure TMustItemPainter.SetVisible(const Value: Boolean);
begin
FVisible := Value;
Repaint;
end;
end.
然后我想写一个继承tlabel的控件ttestlabel,加个mustitem属性,如果为真,就画出mustitem.res的图形。关键现在问题出现了,就是在ttestlabel的free的时候,fimagepainter.free出问题,然后发现是TMustItemPainter.fimage.free时出问题。好像是因为tcontrol接收不到焦点的关系。因为以前这个类放在tedit里面是好的,也就是fctrl是twincontrol型的。但是tlabel继承的是tcontrol类,我只好把fctrl改为tcontrol。可是free的问题怎么办?
ttestlabel的代码如下:
TWA7Label = class(TLabel)
private
{ Private 宣言 }
FMustItem: Boolean;
FMustItemImage: TMustItemPainter;
procedure SetMustItem(Value : Boolean);
function GetVisible: Boolean;
procedure SetVisible(value: Boolean);
protected
procedure loaded; override;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure SetBounds(ALeft, ATop, AWidth, AHeight: Integer); override;
published
property MustItem : boolean read FMustItem write SetMustItem;
property Visible : boolean read GetVisible write SetVisible;
end;
procedure Register;
implementation
uses StrUtils, Math;
procedure Register;
begin
RegisterComponents('Company', [TWA7Label]);
end;
{ TWA7Label }
constructor TWA7Label.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FMustItemImage := TMustItemPainter.Create(Self);
FMustItemImage.Visible := Self.Visible;
end;
destructor TWA7Label.Destroy;
begin
FMustItemImage.Free;
inherited;
end;
procedure TWA7Label.SetMustItem(Value: Boolean);
begin
FMustItem := value;
FMustItemImage.MustItem := Value;
end;
procedure TWA7Label.SetVisible(Value: boolean);
begin
inherited Visible := Value;
FMustItemImage.Visible := Visible;
end;
function TWA7Label.GetVisible: Boolean;
begin
Result := inherited Visible;
end;
procedure TWA7Label.SetBounds(ALeft, ATop, AWidth, AHeight: Integer);
begin
inherited;
if Assigned(FMustItemImage) then FMustItemImage.Repaint;
end;
end.
高手啊,帮忙啊。