Наши проекты:
Журнал · Discuz!ML · Wiki · DRKB · Помощь проекту |
||
ПРАВИЛА | FAQ | Помощь | Поиск | Участники | Календарь | Избранное | RSS |
[18.119.104.238] |
|
Сообщ.
#1
,
|
|
|
Здаравствуйте знатоки, первый раз пишу компонент и возникла след. проблема:
Решил я создать программу-бд для одной игры и начал писать компонент-панель с изображением и заголовком. Компонент вроде-как написал, все хорошо, НО почиму-то компонент внутри себя отображает 2 image`а причем без имени При компиляции приложения в котором размещен компонент вылезает такая ошибка Если в инспекторе обьектов дать имя 2м image`ам, то компиляция происходит успешно и приложение работает как надо. В идеале хотелось бы чтоб этих изображениё вообще небыло в инспекторе. В спойлере исходник компонента Скрытый текст Компонент использует улучшенный label из пакета alphacontrols, переписать под обычный label не долго unit KM_Item; interface uses SysUtils, Classes, Controls,StdCtrls,ExtCtrls,sLabel,Graphics,acPNG,GIFImg; type Tkm_Item = class(TCustomPanel) private { Private declarations } FLabel: TsLabelFX; FImage: TImage; FBorder: TImage; FItemName: string; function GetCaption:string; procedure SetCaption(const Value:string); function GetImage:TPicture; procedure SetImage(const Image:TPicture); function GetBorder:TPicture; procedure SetBorder(const Image:TPicture); function GetItemName:string; procedure SetItemName(const ItemName:string); protected { Protected declarations } public { Public declarations } constructor Create(AOwner:TComponent); override; published { Published declarations } property Caption:string read GetCaption write SetCaption; property Image:TPicture read GetImage write SetImage; property Border:TPicture read GetBorder write SetBorder; property ItemName:string read GetItemName write SetItemName; end; procedure Register; implementation procedure Register; begin RegisterComponents('Samples', [Tkm_Item]); end; { Tkm_Item } constructor Tkm_Item.Create(AOwner: TComponent); begin inherited Create(AOwner); Width := 128; Height := 168; FLabel := TsLabelFX.Create(Self); with FLabel do begin Parent := Self; Left := 0; Top := 0; Width := 128; Height := 40; Alignment := taCenter; AutoSize := False; Layout := tlCenter; WordWrap := True; Caption := 'Имя предмета'; end; FImage := TImage.Create(AOwner); with FImage do begin Parent := Self; Left := 4; Top := 44; Width := 120; Height := 120; Center := True; Proportional:= True; Transparent := True; end; FBorder := TImage.Create(AOwner); with FBorder do begin Parent := Self; Left := 0; Top := 0; Width := 128; Height := 168; Transparent := True; Cursor := crHandPoint; end; end; function Tkm_Item.GetCaption: string; begin Result := FLabel.Caption; end; function Tkm_Item.GetImage: TPicture; begin Result := FImage.Picture; end; function Tkm_Item.GetItemName: string; begin Result := FItemName; end; function Tkm_Item.GetBorder: TPicture; begin Result := FBorder.Picture; end; procedure Tkm_Item.SetCaption(const Value: string); begin FLabel.Caption := Value; end; procedure Tkm_Item.SetImage(const Image: TPicture); begin FImage.Picture := Image; end; procedure Tkm_Item.SetItemName(const ItemName: string); begin FItemName := ItemName; end; procedure Tkm_Item.SetBorder(const Image: TPicture); begin FBorder.Picture := Image; end; end. |
Сообщ.
#2
,
|
|
|
vmazz, сразу несколько вопросов, а почему компоненты TImage наследуются от AOwner (должно от Self) ... кроме того для всех дочерних элементов нужно определить свойство Parent ... примерно так должно выглядить:
unit KM_Item; interface uses SysUtils, Classes, Controls,StdCtrls,ExtCtrls,sLabel,Graphics,acPNG,GIFImg; type Tkm_Item = class(TCustomPanel) private { Private declarations } FLabel: TsLabelFX; FImage: TImage; FBorder: TImage; FItemName: string; function GetCaption:string; procedure SetCaption(const Value:string); function GetImage:TPicture; procedure SetImage(const Image:TPicture); function GetBorder:TPicture; procedure SetBorder(const Image:TPicture); function GetItemName:string; procedure SetItemName(const ItemName:string); protected { Protected declarations } public { Public declarations } constructor Create(AOwner:TComponent); override; published { Published declarations } property Caption:string read GetCaption write SetCaption; property Image:TPicture read GetImage write SetImage; property Border:TPicture read GetBorder write SetBorder; property ItemName:string read GetItemName write SetItemName; end; procedure Register; implementation procedure Register; begin RegisterComponents('Samples', [Tkm_Item]); end; { Tkm_Item } constructor Tkm_Item.Create(AOwner: TComponent); begin inherited Create(AOwner); Width := 128; Height := 168; FLabel := TsLabelFX.Create(Self); with FLabel do begin Parent := Self; Left := 0; Top := 0; Width := 128; Height := 40; Alignment := taCenter; AutoSize := False; Layout := tlCenter; WordWrap := True; Parent:= Self; Caption := 'Имя предмета'; end; FImage := TImage.Create(Self); with FImage do begin Parent := Self; Left := 4; Top := 44; Width := 120; Height := 120; Center := True; Proportional:= True; Transparent := True; Parent:= Self; end; FBorder := TImage.Create(Self); with FBorder do begin Parent := Self; Left := 0; Top := 0; Width := 128; Height := 168; Transparent := True; Cursor := crHandPoint; Parent:= Self; end; end; function Tkm_Item.GetCaption: string; begin Result := FLabel.Caption; end; function Tkm_Item.GetImage: TPicture; begin Result := FImage.Picture; end; function Tkm_Item.GetItemName: string; begin Result := FItemName; end; function Tkm_Item.GetBorder: TPicture; begin Result := FBorder.Picture; end; procedure Tkm_Item.SetCaption(const Value: string); begin FLabel.Caption := Value; end; procedure Tkm_Item.SetImage(const Image: TPicture); begin FImage.Picture := Image; end; procedure Tkm_Item.SetItemName(const ItemName: string); begin FItemName := ItemName; end; procedure Tkm_Item.SetBorder(const Image: TPicture); begin FBorder.Picture := Image; end; end. з.ы.: также пропиши деструктор для объекта где будут удаляться дочерние элементы ... |
Сообщ.
#3
,
|
|
|
Цитата andrew.virus @ а почему компоненты TImage наследуются от AOwner (должно от Self) Огрромное спасибо, думаю ошибка точно в этом Еще не проверял, но уже уверен А насчет родителя - так определен он, сразу после создания контрола задаю FLabel := TsLabelFX.Create(Self); with FLabel do begin Parent := Self; Left := 0; Top := 0; ... зы: думал не надо деструктор, типа по разрушению родителя дочерные контролы сами разрушаются. |
Сообщ.
#4
,
|
|
|
Цитата vmazz @ А насчет родителя - так определен он, сразу после создания контрола задаю проглядел ... обычно определяю его в последнюю очередь ... |
Сообщ.
#5
,
|
|
|
Цитата НО почиму-то компонент внутри себя отображает 2 image`а Потому что вы их создаёте два. Совет: внутренние контролы (порождённые от TControl) лучше создавать в тот момент когда создано основное окно (например в SetParent или CreateWnd), дабы не нарваться на присловутую ошибку Control has not parent window |