N
nkiller
Unregistered / Unconfirmed
GUEST, unregistred user!
按照 TLabeledEdit 写了个 TLabeledPanel,可运行时 Label 的位置总是不对,请高手指教!
以下是完整的代码:
unit nkLabeledPanel;
interface
uses
Messages, Windows, SysUtils, Classes, Controls, StdCtrls,
ExtCtrls;
type
{ TCustomLabeledPanel }
TCustomLabeledPanel = class(TCustomPanel)
private
FPanelLabel: 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 PanelLabel: TBoundLabel read FPanelLabel;
property LabelPosition: TLabelPosition read FLabelPosition write SetLabelPosition default lpAbove;
property LabelSpacing: Integer read FLabelSpacing write SetLabelSpacing default 3;
end;
{ TLabeledPanel }
TLabeledPanel = class(TCustomLabeledPanel)
public
{ public declarations }
property DockManager;
published
{ Published declarations }
property Align;
property Alignment;
property Anchors;
property AutoSize;
property BevelEdges;
property BevelInner;
property BevelKind;
property BevelOuter;
property BevelWidth;
property BiDiMode;
property BorderWidth;
property BorderStyle;
property Caption;
property Color;
property Constraints;
property Ctl3D;
property UseDockManager default True;
property DockSite;
property DragCursor;
property DragKind;
property DragMode;
property Enabled;
property FullRepaint;
property Font;
property LabelPosition;
property LabelSpacing;
property Locked;
property Padding;
property PanelLabel;
property ParentBiDiMode;
property ParentBackground;
property ParentColor;
property ParentCtl3D;
property ParentFont;
property ParentShowHint;
property PopupMenu;
property ShowHint;
property TabOrder;
property TabStop;
property VerticalAlignment;
property Visible;
property OnAlignInsertBefore;
property OnAlignPosition;
property OnCanResize;
property OnClick;
property OnConstrainedResize;
property OnContextPopup;
property OnDockDrop;
property OnDockOver;
property OnDblClick;
property OnDragDrop;
property OnDragOver;
property OnEndDock;
property OnEndDrag;
property OnEnter;
property OnExit;
property OnGetSiteInfo;
property OnMouseActivate;
property OnMouseDown;
property OnMouseEnter;
property OnMouseLeave;
property OnMouseMove;
property OnMouseUp;
property OnResize;
property OnStartDock;
property OnStartDrag;
property OnUnDock;
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('nkVclLib', [TLabeledPanel]);
end;
{ TCustomLabeledPanel }
constructor TCustomLabeledPanel.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FLabelPosition := lpAbove;
FLabelSpacing := 3;
Width := 121;
Height := 25;
SetupInternalLabel;
end;
procedure TCustomLabeledPanel.CMBidimodechanged(var Message: TMessage);
begin
inherited;
if FPanelLabel <> nil then
FPanelLabel.BiDiMode := BiDiMode;
end;
procedure TCustomLabeledPanel.CMEnabledchanged(var Message: TMessage);
begin
inherited;
if FPanelLabel <> nil then
FPanelLabel.Enabled := Enabled;
end;
procedure TCustomLabeledPanel.CMVisiblechanged(var Message: TMessage);
begin
inherited;
if FPanelLabel <> nil then
FPanelLabel.Visible := Visible;
end;
procedure TCustomLabeledPanel.Notification(AComponent: TComponent;
Operation: TOperation);
begin
inherited Notification(AComponent, Operation);
if (AComponent = FPanelLabel) and (Operation = opRemove) then
FPanelLabel := nil;
end;
procedure TCustomLabeledPanel.SetBounds(ALeft, ATop, AWidth, AHeight: Integer);
begin
inherited SetBounds(ALeft, ATop, AWidth, AHeight);
SetLabelPosition(FLabelPosition);
end;
procedure TCustomLabeledPanel.SetLabelPosition(const Value: TLabelPosition);
var
P: TPoint;
begin
if FPanelLabel = nil then exit;
FLabelPosition := Value;
case Value of
lpAbove: P := Point(Left, Top - FPanelLabel.Height - FLabelSpacing);
lpBelow: P := Point(Left, Top + Height + FLabelSpacing);
lpLeft : P := Point(Left - FPanelLabel.Width - FLabelSpacing,
Top + ((Height - FPanelLabel.Height) div 2));
lpRight: P := Point(Left + Width + FLabelSpacing,
Top + ((Height - FPanelLabel.Height) div 2));
end;
FPanelLabel.SetBounds(P.x, P.y, FPanelLabel.Width, FPanelLabel.Height);
end;
procedure TCustomLabeledPanel.SetLabelSpacing(const Value: Integer);
begin
FLabelSpacing := Value;
SetLabelPosition(FLabelPosition);
end;
procedure TCustomLabeledPanel.SetName(const Value: TComponentName);
var
LClearText: Boolean;
begin
if (csDesigning in ComponentState) and (FPanelLabel <> nil) and
((FPanelLabel.GetTextLen = 0) or
(CompareText(FPanelLabel.Caption, Name) = 0)) then
FPanelLabel.Caption := Value;
LClearText := (csDesigning in ComponentState) and (Text = '');
inherited SetName(Value);
if LClearText then
Text := '';
end;
procedure TCustomLabeledPanel.SetParent(AParent: TWinControl);
begin
inherited SetParent(AParent);
if FPanelLabel = nil then exit;
FPanelLabel.Parent := AParent;
FPanelLabel.Visible := True;
end;
procedure TCustomLabeledPanel.SetupInternalLabel;
begin
if Assigned(FPanelLabel) then exit;
FPanelLabel := TBoundLabel.Create(Self);
FPanelLabel.FreeNotification(Self);
// FPanelLabel.FocusControl := Self; 这句总是出错,原控件有
end;
end.
以下是完整的代码:
unit nkLabeledPanel;
interface
uses
Messages, Windows, SysUtils, Classes, Controls, StdCtrls,
ExtCtrls;
type
{ TCustomLabeledPanel }
TCustomLabeledPanel = class(TCustomPanel)
private
FPanelLabel: 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 PanelLabel: TBoundLabel read FPanelLabel;
property LabelPosition: TLabelPosition read FLabelPosition write SetLabelPosition default lpAbove;
property LabelSpacing: Integer read FLabelSpacing write SetLabelSpacing default 3;
end;
{ TLabeledPanel }
TLabeledPanel = class(TCustomLabeledPanel)
public
{ public declarations }
property DockManager;
published
{ Published declarations }
property Align;
property Alignment;
property Anchors;
property AutoSize;
property BevelEdges;
property BevelInner;
property BevelKind;
property BevelOuter;
property BevelWidth;
property BiDiMode;
property BorderWidth;
property BorderStyle;
property Caption;
property Color;
property Constraints;
property Ctl3D;
property UseDockManager default True;
property DockSite;
property DragCursor;
property DragKind;
property DragMode;
property Enabled;
property FullRepaint;
property Font;
property LabelPosition;
property LabelSpacing;
property Locked;
property Padding;
property PanelLabel;
property ParentBiDiMode;
property ParentBackground;
property ParentColor;
property ParentCtl3D;
property ParentFont;
property ParentShowHint;
property PopupMenu;
property ShowHint;
property TabOrder;
property TabStop;
property VerticalAlignment;
property Visible;
property OnAlignInsertBefore;
property OnAlignPosition;
property OnCanResize;
property OnClick;
property OnConstrainedResize;
property OnContextPopup;
property OnDockDrop;
property OnDockOver;
property OnDblClick;
property OnDragDrop;
property OnDragOver;
property OnEndDock;
property OnEndDrag;
property OnEnter;
property OnExit;
property OnGetSiteInfo;
property OnMouseActivate;
property OnMouseDown;
property OnMouseEnter;
property OnMouseLeave;
property OnMouseMove;
property OnMouseUp;
property OnResize;
property OnStartDock;
property OnStartDrag;
property OnUnDock;
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('nkVclLib', [TLabeledPanel]);
end;
{ TCustomLabeledPanel }
constructor TCustomLabeledPanel.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FLabelPosition := lpAbove;
FLabelSpacing := 3;
Width := 121;
Height := 25;
SetupInternalLabel;
end;
procedure TCustomLabeledPanel.CMBidimodechanged(var Message: TMessage);
begin
inherited;
if FPanelLabel <> nil then
FPanelLabel.BiDiMode := BiDiMode;
end;
procedure TCustomLabeledPanel.CMEnabledchanged(var Message: TMessage);
begin
inherited;
if FPanelLabel <> nil then
FPanelLabel.Enabled := Enabled;
end;
procedure TCustomLabeledPanel.CMVisiblechanged(var Message: TMessage);
begin
inherited;
if FPanelLabel <> nil then
FPanelLabel.Visible := Visible;
end;
procedure TCustomLabeledPanel.Notification(AComponent: TComponent;
Operation: TOperation);
begin
inherited Notification(AComponent, Operation);
if (AComponent = FPanelLabel) and (Operation = opRemove) then
FPanelLabel := nil;
end;
procedure TCustomLabeledPanel.SetBounds(ALeft, ATop, AWidth, AHeight: Integer);
begin
inherited SetBounds(ALeft, ATop, AWidth, AHeight);
SetLabelPosition(FLabelPosition);
end;
procedure TCustomLabeledPanel.SetLabelPosition(const Value: TLabelPosition);
var
P: TPoint;
begin
if FPanelLabel = nil then exit;
FLabelPosition := Value;
case Value of
lpAbove: P := Point(Left, Top - FPanelLabel.Height - FLabelSpacing);
lpBelow: P := Point(Left, Top + Height + FLabelSpacing);
lpLeft : P := Point(Left - FPanelLabel.Width - FLabelSpacing,
Top + ((Height - FPanelLabel.Height) div 2));
lpRight: P := Point(Left + Width + FLabelSpacing,
Top + ((Height - FPanelLabel.Height) div 2));
end;
FPanelLabel.SetBounds(P.x, P.y, FPanelLabel.Width, FPanelLabel.Height);
end;
procedure TCustomLabeledPanel.SetLabelSpacing(const Value: Integer);
begin
FLabelSpacing := Value;
SetLabelPosition(FLabelPosition);
end;
procedure TCustomLabeledPanel.SetName(const Value: TComponentName);
var
LClearText: Boolean;
begin
if (csDesigning in ComponentState) and (FPanelLabel <> nil) and
((FPanelLabel.GetTextLen = 0) or
(CompareText(FPanelLabel.Caption, Name) = 0)) then
FPanelLabel.Caption := Value;
LClearText := (csDesigning in ComponentState) and (Text = '');
inherited SetName(Value);
if LClearText then
Text := '';
end;
procedure TCustomLabeledPanel.SetParent(AParent: TWinControl);
begin
inherited SetParent(AParent);
if FPanelLabel = nil then exit;
FPanelLabel.Parent := AParent;
FPanelLabel.Visible := True;
end;
procedure TCustomLabeledPanel.SetupInternalLabel;
begin
if Assigned(FPanelLabel) then exit;
FPanelLabel := TBoundLabel.Create(Self);
FPanelLabel.FreeNotification(Self);
// FPanelLabel.FocusControl := Self; 这句总是出错,原控件有
end;
end.