Label 位置总是不对(50分)

  • 主题发起人 主题发起人 nkiller
  • 开始时间 开始时间
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.
 
TBoundLabel 没有FocusControl 属性的!
 
// FPanelLabel.FocusControl := Self; 这句总是出错,原控件有
Edit有焦点,Panel没有

看SetLabelPosition过程,好像没什么问题啊,怎么位置不对了
 
运行的时候不对,和期望的不一样,显示一半,另一半被Panel覆盖了,必须在 FormShow 的时候写上 MyPanel.LabelPosition := lpLeft; 才能显示正常,不然难看死了!

// FPanelLabel.FocusControl := Self;
这个不是关键问题,有没有无所谓的,重要的 Label 位置问题
 
忘记说了,环境是 BDS2006+Update1
 
帮顶!

http://www.source520.com

站长开发推广同盟 站长朋友的终极驿站
同时拥有海量源码电子经典书籍下载

http://www.source520.com/search/search.asp

"编程.站长"论坛搜索引擎-----为中国站长注入动力!
 
我的控件http://www.2ccc.com/article.asp?articleid=2933
也遇到过你这样的问题,我给解决了。
 
To: zyt_1978
你能不能直接说明问题的关键所在呢?那个地方还需要注册(我最烦的事情,如果不是经常去,我绝对不注册)
谢谢!
 
帮顶!

╭=========================================╮

80G海量源代码,控件,书籍全免费狂下不停!

http://www.source520.com

╰=========================================╯
 
放了这么多天,居然没人能帮上忙,失望透顶!
我已经将全部代码都贴了出来,只是想把这段程序
完美些而已。而且既然写了,就是想知道自己的问
题出在哪里,以后再写类似的程序时不会再犯相同
的错误,同时也能让别人少走些弯路。
知其然并知其所以然!!
 
不知道你有没有跟踪过
在调用SetLabelPosition过程中,FPanelLabel.Width的值为3,但是在设计期他的值远远大于这个数,原因是此时FPanelLabel.Caption=""。
我只是简单的把setname中的
// if (csDesigning in ComponentState) and (FPanelLabel <> nil) and
// ((FPanelLabel.GetTextLen = 0) or
// (CompareText(FPanelLabel.Caption, Name) = 0)) then
这三行给屏蔽了一下,就可以了

你再仔细看看,把这个判断条件修改一下,简单的屏蔽是有缺陷的
 
看到蛋炒饭的帖子后调试了一个晚上,发现问题好像是出现在 TBoundLabel 上
于是又检查了 TBoundLabel 的代码
{ TBoundLabel }

TBoundLabel = 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 OnMouseActivate;
property OnMouseDown;
property OnMouseMove;
property OnMouseUp;
property OnStartDock;
property OnStartDrag;
end;

发现了下面这个过程
protected
procedure AdjustBounds; override;

procedure TBoundLabel.AdjustBounds;
begin
inherited AdjustBounds;
if Owner is TCustomLabeledEdit then
with Owner as TCustomLabeledEdit do
SetLabelPosition(LabelPosition);
end;

晕倒!原来 TBoundLabel 是专门为 TCustomLabeledEdit 写的!!

于是写了下面的代码:

{ TBoundLabelEx }

TBoundLabelEx = class(TBoundLabel)
protected
procedure AdjustBounds; override;
published
property BiDiMode;
property Caption;
property Color;
property DragCursor;
property DragKind;
property DragMode;
property Font;
property Height;
property Left;
property ParentBiDiMode;
property ParentColor;
property ParentFont;
property ParentShowHint;
property PopupMenu;
property ShowAccelChar;
property ShowHint;
property Top;
property Transparent;
property Layout;
property WordWrap;
property Width;
property OnClick;
property OnContextPopup;
property OnDblClick;
property OnDragDrop;
property OnDragOver;
property OnEndDock;
property OnEndDrag;
property OnMouseActivate;
property OnMouseDown;
property OnMouseMove;
property OnMouseUp;
property OnStartDock;
property OnStartDrag;
end;

从 TBoundLabel 继承下来,重写 AdjustBounds 过程
支持 TCustomLabeledPanel
procedure TBoundLabelEx.AdjustBounds;
begin
inherited AdjustBounds;
if Owner is TCustomLabeledPanel then
with Owner as TCustomLabeledPanel do
SetLabelPosition(LabelPosition);
end;

然后把 TBoundLabel 全替换成 TBoundLabelEx

搞定!!!
 
多人接受答案了。
 

Similar threads

I
回复
0
查看
582
import
I
I
回复
0
查看
625
import
I
I
回复
0
查看
527
import
I
I
回复
0
查看
759
import
I
后退
顶部