如何将1个button和1个label合并成一个控件啊????(100分)

  • 主题发起人 主题发起人 ciscopower
  • 开始时间 开始时间
C

ciscopower

Unregistered / Unconfirmed
GUEST, unregistred user!
要求要有labeledit那样类似的效果,也就是button以外的区域都是透明的(可以看到背景)。label
我做出来的控件怎么只能看到button,label不见了?可是设计时,都可以正常显示的。
代码如下:FlatButton 为控件 FlatStyle中的button 控件名。
unit MyButtonLabel;

interface

{$I DFS.inc}

uses Windows, Messages, Classes, Controls, Forms, Graphics, StdCtrls, ExtCtrls,
CommCtrl, Buttons, SysUtils,FlatUtilitys,TFlatButtonUnit;

type
TLabelPosition = (lpAbove, lpBelow, lpLeft, lpRight);
TCustomMyButtonLabel = class(TFlatButton)
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;

TMyButtonLabel = class(TCustomMyButtonLabel)
published
property Color;
property Constraints;
property Ctl3D;
property DragCursor;
property DragKind;
property DragMode;
property EditLabel;
property Enabled;
property Font;
property LabelPosition;
property LabelSpacing;
property PopupMenu;
property ShowHint;
property TabOrder;
property TabStop;
property Text;
property Visible;
property OnClick;
property OnContextPopup;
property OnDblClick;
property OnDragDrop;
property OnDragOver;
property OnEndDock;
property OnEndDrag;
property OnEnter;
property OnExit;
property OnKeyDown;
property OnKeyPress;
property OnKeyUp;
property OnMouseDown;
property OnMouseMove;
property OnMouseUp;
property OnStartDock;
property OnStartDrag;
end;

procedure Register;

implementation

constructor TCustomMyButtonLabel.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FLabelPosition := lpBelow;
FLabelSpacing := 3;
SetupInternalLabel;
end;

procedure TCustomMyButtonLabel.CMBidimodechanged(var Message: TMessage);
begin
inherited;
FEditLabel.BiDiMode := BiDiMode;
end;

procedure TCustomMyButtonLabel.CMEnabledchanged(var Message: TMessage);
begin
inherited;
FEditLabel.Enabled := Enabled;
end;

procedure TCustomMyButtonLabel.CMVisiblechanged(var Message: TMessage);
begin
inherited;
FEditLabel.Visible := Visible;
end;

procedure TCustomMyButtonLabel.Notification(AComponent: TComponent;
Operation: TOperation);
begin
inherited Notification(AComponent, Operation);
if (AComponent = FEditLabel) and (Operation = opRemove) then
FEditLabel := nil;
end;

procedure TCustomMyButtonLabel.SetBounds(ALeft, ATop, AWidth, AHeight: Integer);
begin
inherited SetBounds(ALeft, ATop, AWidth, AHeight);
SetLabelPosition(FLabelPosition);
end;

procedure TCustomMyButtonLabel.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 TCustomMyButtonLabel.SetLabelSpacing(const Value: Integer);
begin
FLabelSpacing := Value;
SetLabelPosition(FLabelPosition);
end;

procedure TCustomMyButtonLabel.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 TCustomMyButtonLabel.SetParent(AParent: TWinControl);
begin
inherited SetParent(AParent);
if FEditLabel = nil then exit;
FEditLabel.Parent := AParent;
FEditLabel.Visible := True;
end;

procedure TCustomMyButtonLabel.SetupInternalLabel;
begin
if Assigned(FEditLabel) then exit;
FEditLabel := TBoundLabel.Create(Self);
FEditLabel.FreeNotification(Self);
// FEditLabel.FocusControl := Self;
end;

procedure Register;
begin
RegisterComponents ('MyVcls', [TMyButtonLabel]);
end;

end.
 
代码太长了,粗粗看了一下。
procedure TCustomMyButtonLabel.SetupInternalLabel;
begin
if Assigned(FEditLabel) then exit;
FEditLabel := TBoundLabel.Create(Self);
FEditLabel.Parent := Self; //你试试,好象少了这一句
 FEditLabel.Top := 0; //好象FEditLabel的位置也没设置
FEditLabel.Left := 0;
FEditLabel.FreeNotification(Self);
// FEditLabel.FocusControl := Self;
end;
 
FEditLabel := TBoundLabel.Create(Self);
FEditLabel.parent := AOwner; //加上这句试试,AOwner是构造器中的AOwner,
传一个AOwner到SetupInternalLabel
//constructor Create(AOwner: TComponent); override;
 
谢谢,大家,其实我的方法是将这个控件删除,然后再次添加,居然就可以了。我的天!!
不过还是谢谢大家。!!!
 
倒~~~~~~~~~~~~~~~
我想这是因为你修改了控件但没有重新编译的缘故。
如果控件修改了,必须重新编译,在设计时的改动才能表现出来。
 
后退
顶部