如何製作一個複合元件 |
答題得分者是:cmf
|
chiehjj
初階會員 發表:21 回覆:63 積分:26 註冊:2002-12-31 發送簡訊給我 |
|
RaynorPao
版主 發表:139 回覆:3622 積分:7025 註冊:2002-08-12 發送簡訊給我 |
引言: 我想要製作一個元件,這個元件包括panel與label兩個基本元件, 這個元件可以有panel的mousedown及label的mousedown事件 及panel的caption屬性及label的caption屬性.我應該如何著手呢chiehjj 你好: 建議你去電腦書店找這一本書 > --
------
-- 若您已經得到滿意的答覆,請適時結案!! -- -- 欲知前世因,今生受者是;欲知來世果,今生做者是 -- -- 一切有為法,如夢幻泡影,如露亦如電,應作如是觀 -- |
cmf
尊榮會員 發表:84 回覆:918 積分:1032 註冊:2002-06-26 發送簡訊給我 |
引言: 我想要製作一個元件,這個元件包括panel與label兩個基本元件, 這個元件可以有panel的mousedown及label的mousedown事件 及panel的caption屬性及label的caption屬性.我應該如何著手呢 謝謝 chiehjjTCustomLabeledPanel=class(TCustomPanel) private FEditLabel: TBoundLabel; FLabelPosition: TLabelPosition; FLabelSpacing: Integer; procedure SetLabelPosition(const Value: TLabelPosition); procedure SetLabelSpacing(const Value: Integer); protected procedure SetParent(AParent: TWinControl); override; procedure Notification(AComponent: TComponent; Operation: TOperation); override; procedure SetName(const Value: TComponentName); override; procedure CMVisiblechanged(var Message: TMessage); message CM_VISIBLECHANGED; procedure CMEnabledchanged(var Message: TMessage); message CM_ENABLEDCHANGED; procedure CMBidimodechanged(var Message: TMessage); message CM_BIDIMODECHANGED; public { Public declarations } constructor Create(AOwner: TComponent); override; procedure SetBounds(ALeft: Integer; ATop: Integer; AWidth: Integer; AHeight: Integer); override; procedure SetupInternalLabel; property EditLabel: TBoundLabel read FEditLabel; property LabelPosition: TLabelPosition read FLabelPosition write SetLabelPosition; property LabelSpacing: Integer read FLabelSpacing write SetLabelSpacing; end; constructor TCustomLabeledPanel.Create(AOwner: TComponent); begin inherited Create(AOwner); FLabelPosition := lpAbove; FLabelSpacing := 3; SetupInternalLabel; end; procedure TCustomLabeledPanel.CMBidimodechanged(var Message: TMessage); begin inherited; FEditLabel.BiDiMode := BiDiMode; end; procedure TCustomLabeledPanel.CMEnabledchanged(var Message: TMessage); begin inherited; FEditLabel.Enabled := Enabled; end; procedure TCustomLabeledPanel.CMVisiblechanged(var Message: TMessage); begin inherited; FEditLabel.Visible := Visible; end; procedure TCustomLabeledPanel.Notification(AComponent: TComponent; Operation: TOperation); begin inherited Notification(AComponent, Operation); if (AComponent = FEditLabel) and (Operation = opRemove) then FEditLabel := nil; end; procedure TCustomLabeledPanel.SetBounds(ALeft, ATop, AWidth, AHeight: Integer); begin inherited SetBounds(ALeft, ATop, AWidth, AHeight); SetLabelPosition(FLabelPosition); end; procedure TCustomLabeledEdit.SetLabelPosition(const Value: TLabelPosition); var P: TPoint; begin if FEditLabel = nil then exit; FLabelPosition := Value; case Value of lpAbove: P := Point(Left, Top - FEditLabel.Height - FLabelSpacing); lpBelow: P := Point(Left, Top + Height + FLabelSpacing); lpLeft : P := Point(Left - FEditLabel.Width - FLabelSpacing, Top + ((Height - FEditLabel.Height) div 2)); lpRight: P := Point(Left + Width + FLabelSpacing, Top + ((Height - FEditLabel.Height) div 2)); end; FEditLabel.SetBounds(P.x, P.y, FEditLabel.Width, FEditLabel.Height); end; procedure TCustomLabeledPanel.SetLabelSpacing(const Value: Integer); begin FLabelSpacing := Value; SetLabelPosition(FLabelPosition); end; procedure TCustomLabeledPanel.SetName(const Value: TComponentName); begin if (csDesigning in ComponentState) and ((FEditlabel.GetTextLen = 0) or (CompareText(FEditLabel.Caption, Name) = 0)) then FEditLabel.Caption := Value; inherited SetName(Value); if csDesigning in ComponentState then Text := ''; end; procedure TCustomLabeledPanel.SetParent(AParent: TWinControl); begin inherited SetParent(AParent); if FEditLabel = nil then exit; FEditLabel.Parent := AParent; FEditLabel.Visible := True; end; procedure TCustomLabeledPanel.SetupInternalLabel; begin if Assigned(FEditLabel) then exit; FEditLabel := TBoundLabel.Create(Self); FEditLabel.FreeNotification(Self); FEditLabel.FocusControl := Self; end; 發表人 - cmf 於 2003/01/14 12:59:00
------
︿︿ |
william
版主 發表:66 回覆:2535 積分:3048 註冊:2002-07-11 發送簡訊給我 |
Incomplete, just some concept:
< class="code">TMyPanel=class(TCustomPanel)
private
FLabel: TLabel;
FPanelMouseDown: TMouseEvent;
FLabelMouseDown: TMouseEvent;
procedure IntPanelMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
procedure IntLabelMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
public
constructor Create(AOwner: TCompoennt);
destructor Destroy; override;
published
property OnPanelMouseDown read FPanelMouseDown write FPanelMouseDown;
property OnLabelMouseDown read FLabelMouseDown write FLabelMouseDOwn;
end;
{....}
constructor TMyPanel.Create(AOwner: TComponent);
begin
inherited;
FLabel := TLabel.Create(self);
FLabel.Parent := self;
FLabel.OnMouseDown := IntLabelMouseDown;
self.OnMouseDoen := IntPanelMouseDown;
end; procedue TMyPanel.IntLabelMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
beign
if Assigned(FLabelMouseDown) then
FLabelMouseDown(Sender,Button,Shift,X,Y);
end;
|
chiehjj
初階會員 發表:21 回覆:63 積分:26 註冊:2002-12-31 發送簡訊給我 |
|
cmf
尊榮會員 發表:84 回覆:918 積分:1032 註冊:2002-06-26 發送簡訊給我 |
引言: cmf你好 謝謝你的解答 試過你的例子執行到 procedure TCustomLabeledPanel1.SetupInternalLabel; begin .. FEditLabel.FocusControl := Self; 就會出現'Undeclared indentifier:'Ffocuscontrol' 是何故? Chiehjj上UESE StdCtrls; TCustomLabel(FEditLabel).FocusControl := Self;
------
︿︿ |
chiehjj
初階會員 發表:21 回覆:63 積分:26 註冊:2002-12-31 發送簡訊給我 |
|
cmf
尊榮會員 發表:84 回覆:918 積分:1032 註冊:2002-06-26 發送簡訊給我 |
引言: cmf前輩 昨天試了幾次你的提示還是出現相同的問題, 我將 TCustomLabel(FEditLabel).FocusControl := Self; 改成 TCustomLabeledPanel1(FEditLabel).FocusControl:= Self; 還是出現相同的'Undeclared indentifier:'Focuscontrol' 未知是否我有什麼步驟弄錯了? Chiehjj上這一行錯了 TCustomLabeledPanel1(FEditLabel).FocusControl:= Self; 請改成 TCustomLabel(FEditLabel).FocusControl := Self; 將你的 source 貼上來, 我 try 看看 發表人 - cmf 於 2003/01/17 11:48:14
------
︿︿ |
chiehjj
初階會員 發表:21 回覆:63 積分:26 註冊:2002-12-31 發送簡訊給我 |
cmf前輩:
程式如下, 請過目. unit CustomLabeledPanel1; interface uses
Windows, Messages, SysUtils, Classes, Controls, ExtCtrls,StdCtrls; type
TCustomLabeledPanel1 = class(TCustomPanel) private
FEditLabel: TBoundLabel;
FLabelPosition: TLabelPosition;
FLabelSpacing: Integer;
procedure SetLabelPosition(const Value: TLabelPosition);
procedure SetLabelSpacing(const Value: Integer);
protected
procedure SetParent(AParent: TWinControl); override;
procedure Notification(AComponent: TComponent; Operation: TOperation); override;
procedure SetName(const Value: TComponentName); override;
procedure CMVisiblechanged(var Message: TMessage);
message CM_VISIBLECHANGED;
procedure CMEnabledchanged(var Message: TMessage);
message CM_ENABLEDCHANGED;
procedure CMBidimodechanged(var Message: TMessage);
message CM_BIDIMODECHANGED;
public
{ Public declarations }
constructor Create(AOwner: TComponent); override;
procedure SetBounds(ALeft: Integer; ATop: Integer; AWidth: Integer; AHeight: Integer); override;
procedure SetupInternalLabel;
property EditLabel: TBoundLabel read FEditLabel;
property LabelPosition: TLabelPosition read FLabelPosition write SetLabelPosition;
property LabelSpacing: Integer read FLabelSpacing write SetLabelSpacing;
end; procedure Register; implementation procedure Register;
begin
RegisterComponents('Samples', [TCustomLabeledPanel1]);
end; constructor TCustomLabeledPanel1.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FLabelPosition := lpAbove;
FLabelSpacing := 3;
SetupInternalLabel;
end; procedure TCustomLabeledPanel1.CMBidimodechanged(var Message: TMessage);
begin
inherited;
FEditLabel.BiDiMode := BiDiMode;
end; procedure TCustomLabeledPanel1.CMEnabledchanged(var Message: TMessage);
begin
inherited;
FEditLabel.Enabled := Enabled;
end; procedure TCustomLabeledPanel1.CMVisiblechanged(var Message: TMessage);
begin
inherited;
FEditLabel.Visible := Visible;
end; procedure TCustomLabeledPanel1.Notification(AComponent: TComponent;
Operation: TOperation);
begin
inherited Notification(AComponent, Operation);
if (AComponent = FEditLabel) and (Operation = opRemove) then
FEditLabel := nil;
end; procedure TCustomLabeledPanel1.SetBounds(ALeft, ATop, AWidth, AHeight: Integer);
begin
inherited SetBounds(ALeft, ATop, AWidth, AHeight);
SetLabelPosition(FLabelPosition);
end; // procedure TCustomLabeledEdit.SetLabelPosition(const Value: TLabelPosition);
// 因為每次執行時都會出現錯誤 我把這一行由改成下面
procedure TCustomLabeledPanel1.SetLabelPosition(const Value: TLabelPosition);
// var
P: TPoint;
begin
if FEditLabel = nil then exit;
FLabelPosition := Value;
case Value of
lpAbove: P := Point(Left, Top - FEditLabel.Height - FLabelSpacing);
lpBelow: P := Point(Left, Top Height FLabelSpacing);
lpLeft : P := Point(Left - FEditLabel.Width - FLabelSpacing,
Top ((Height - FEditLabel.Height) div 2));
lpRight: P := Point(Left Width FLabelSpacing,
Top ((Height - FEditLabel.Height) div 2));
end;
FEditLabel.SetBounds(P.x, P.y, FEditLabel.Width, FEditLabel.Height);
end; procedure TCustomLabeledPanel1.SetLabelSpacing(const Value: Integer);
begin
FLabelSpacing := Value;
SetLabelPosition(FLabelPosition);
end; procedure TCustomLabeledPanel1.SetName(const Value: TComponentName);
begin
if (csDesigning in ComponentState) and ((FEditlabel.GetTextLen = 0) or
(CompareText(FEditLabel.Caption, Name) = 0)) then
FEditLabel.Caption := Value;
inherited SetName(Value);
if csDesigning in ComponentState then
Text := '';
end; procedure TCustomLabeledPanel1.SetParent(AParent: TWinControl);
begin
inherited SetParent(AParent);
if FEditLabel = nil then exit;
FEditLabel.Parent := AParent;
FEditLabel.Visible := True;
end; procedure TCustomLabeledPanel1.SetupInternalLabel;
begin
if Assigned(FEditLabel) then exit;
FEditLabel := TBoundLabel.Create(Self);
FEditLabel.FreeNotification(Self);
//FEditLabel.FocusControl := Self;
//TCustomLabeledPanel1(FEditLabel).fFocusControl:= Self;
TCustomLabel(FEditLabel).FocusControl := Self; end; end. Chiehjj上
|
cmf
尊榮會員 發表:84 回覆:918 積分:1032 註冊:2002-06-26 發送簡訊給我 |
chiehjj:
我做了部分修改
你再試看看 interface uses
Windows, Messages, SysUtils, Classes, Controls, ExtCtrls,StdCtrls; type TMyBoundLabel = class(TCustomLabel)
private
function GetTop: Integer;
function GetLeft: Integer;
function GetWidth: Integer;
function GetHeight: Integer;
procedure SetHeight(const Value: Integer);
procedure SetWidth(const Value: Integer);
protected
procedure AdjustBounds; override;
public
constructor Create(AOwner: TComponent); override;
published
property BiDiMode;
property Caption;
property Color;
property DragCursor;
property DragKind;
property DragMode;
property Font;
property Height: Integer read GetHeight write SetHeight;
property Left: Integer read GetLeft;
property ParentBiDiMode;
property ParentColor;
property ParentFont;
property ParentShowHint;
property PopupMenu;
property ShowAccelChar;
property ShowHint;
property Top: Integer read GetTop;
property Transparent;
property Layout;
property WordWrap;
property Width: Integer read GetWidth write SetWidth;
property OnClick;
property OnContextPopup;
property OnDblClick;
property OnDragDrop;
property OnDragOver;
property OnEndDock;
property OnEndDrag;
property OnMouseDown;
property OnMouseMove;
property OnMouseUp;
property OnStartDock;
property OnStartDrag;
end; TCustomLabeledPanel1 = class(TCustomPanel)
private
{ Private declarations }
FEditLabel: TMyBoundLabel;
FLabelPosition: TLabelPosition;
FLabelSpacing: Integer;
procedure SetLabelPosition(const Value: TLabelPosition);
procedure SetLabelSpacing(const Value: Integer); protected
{ Protected declarations }
procedure SetParent(AParent: TWinControl); override;
procedure Notification(AComponent: TComponent; Operation: TOperation); override;
procedure SetName(const Value: TComponentName); override;
procedure CMVisiblechanged(var Message: TMessage); message CM_VISIBLECHANGED;
procedure CMEnabledchanged(var Message: TMessage); message CM_ENABLEDCHANGED;
procedure CMBidimodechanged(var Message: TMessage); message CM_BIDIMODECHANGED; public
{ Public declarations }
constructor Create(AOwner: TComponent); override;
procedure SetBounds(ALeft: Integer; ATop: Integer; AWidth: Integer; AHeight: Integer); override;
procedure SetupInternalLabel;
property EditLabel: TMyBoundLabel read FEditLabel;
property LabelPosition: TLabelPosition read FLabelPosition write SetLabelPosition;
property LabelSpacing: Integer read FLabelSpacing write SetLabelSpacing; published
{ Published declarations }
end; procedure Register; implementation procedure Register;
begin
RegisterComponents('Samples', [TCustomLabeledPanel1]);
end; constructor TMyBoundLabel.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
Name := 'SubLabel';
SetSubComponent(True);
if Assigned(AOwner) then
Caption := AOwner.Name;
end; procedure TMyBoundLabel.AdjustBounds;
begin
inherited AdjustBounds;
if Owner is TCustomLabeledEdit then
with Owner as TCustomLabeledPanel1 do
SetLabelPosition(LabelPosition);
end; function TMyBoundLabel.GetHeight: Integer;
begin
Result := inherited Height;
end; function TMyBoundLabel.GetLeft: Integer;
begin
Result := inherited Left;
end; function TMyBoundLabel.GetTop: Integer;
begin
Result := inherited Top;
end; function TMyBoundLabel.GetWidth: Integer;
begin
Result := inherited Width;
end; procedure TMyBoundLabel.SetHeight(const Value: Integer);
begin
SetBounds(Left, Top, Width, Value);
end; procedure TMyBoundLabel.SetWidth(const Value: Integer);
begin
SetBounds(Left, Top, Value, Height);
end; constructor TCustomLabeledPanel1.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FLabelPosition := lpAbove;
FLabelSpacing := 3;
SetupInternalLabel;
end; procedure TCustomLabeledPanel1.CMBidimodechanged(var Message: TMessage);
begin
inherited;
FEditLabel.BiDiMode := BiDiMode;
end; procedure TCustomLabeledPanel1.CMEnabledchanged(var Message: TMessage);
begin
inherited;
FEditLabel.Enabled := Enabled;
end; procedure TCustomLabeledPanel1.CMVisiblechanged(var Message: TMessage);
begin
inherited;
FEditLabel.Visible := Visible;
end; procedure TCustomLabeledPanel1.Notification(AComponent: TComponent;
Operation: TOperation);
begin
inherited Notification(AComponent, Operation);
if (AComponent = FEditLabel) and (Operation = opRemove) then
FEditLabel := nil;
end; procedure TCustomLabeledPanel1.SetBounds(ALeft, ATop, AWidth, AHeight: Integer);
begin
inherited SetBounds(ALeft, ATop, AWidth, AHeight);
SetLabelPosition(FLabelPosition);
end; // procedure TCustomLabeledEdit.SetLabelPosition(const Value: TLabelPosition);
// 因為每次執行時都會出現錯誤 我把這一行由改成下面
procedure TCustomLabeledPanel1.SetLabelPosition(const Value: TLabelPosition);
// var
P: TPoint;
begin
if FEditLabel = nil then exit;
FLabelPosition := Value;
case Value of
lpAbove: P := Point(Left, Top - FEditLabel.Height - FLabelSpacing);
lpBelow: P := Point(Left, Top + Height + FLabelSpacing);
lpLeft : P := Point(Left - FEditLabel.Width - FLabelSpacing,
Top + ((Height - FEditLabel.Height) div 2));
lpRight: P := Point(Left + Width + FLabelSpacing,
Top + ((Height - FEditLabel.Height) div 2));
end;
FEditLabel.SetBounds(P.x, P.y, FEditLabel.Width, FEditLabel.Height);
end; procedure TCustomLabeledPanel1.SetLabelSpacing(const Value: Integer);
begin
FLabelSpacing := Value;
SetLabelPosition(FLabelPosition);
end; procedure TCustomLabeledPanel1.SetName(const Value: TComponentName);
begin
if (csDesigning in ComponentState) and ((FEditlabel.GetTextLen = 0) or
(CompareText(FEditLabel.Caption, Name) = 0)) then
FEditLabel.Caption := Value;
inherited SetName(Value);
if csDesigning in ComponentState then
Text := '';
end; procedure TCustomLabeledPanel1.SetParent(AParent: TWinControl);
begin
inherited SetParent(AParent);
if FEditLabel = nil then exit;
FEditLabel.Parent := AParent;
FEditLabel.Visible := True;
end; procedure TCustomLabeledPanel1.SetupInternalLabel;
begin
if Assigned(FEditLabel) then exit;
FEditLabel := TMyBoundLabel.Create(Self);
FEditLabel.FreeNotification(Self);
FEditLabel.FocusControl := Self; end;
------
︿︿ |
chiehjj
初階會員 發表:21 回覆:63 積分:26 註冊:2002-12-31 發送簡訊給我 |
|
cmf
尊榮會員 發表:84 回覆:918 積分:1032 註冊:2002-06-26 發送簡訊給我 |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |