控件中包含控件(100分)

  • 主题发起人 主题发起人 ll710915
  • 开始时间 开始时间
L

ll710915

Unregistered / Unconfirmed
GUEST, unregistred user!
我想作一控件,它上面又包含edit和button两个控件,该如何做?
 
从TPanle开始继承,然后分别实现edit和button(都是画出来的哟)。

看看LMD类似控件的源码即可。
 
使用“聚合”技术,就是继承TCustomControl,然后声明一个TEdit和TButton,
type TMyComponent=class(TCustomControl)
private
AEdit: TEdit;
AButton: TButton
.
public
constructor create(AOwner: TComponent);override;
destructor detroy;override;
end;

Create的实现
AEdit:=TEdit.Create(Self);
Abutton:=Tbutton.Create(Self);
Abutton.Parent:=Self;
AEdit.Parent:=Self
//这里是关键
.
.
.
没有Delphi,随手写的,可能有拼写错误。^_^
找一下D4 Unleashed 书上有一个经典聚合控件 TLabelEdit,就明白了。
 
unit unit1;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ExtCtrls;

type
test = class(TPanel)

private


protected

public

constructor Create(AOwner: TComponent)
override;
// destructor Destory;//override;

published
end;

implementation


constructor test.Create(AOwner: TComponent);
begin
inherited Create(AOwner);

button1:=TButton.Create(self);
with button1 do
begin
Parent:=self;
Caption;='aaa';
end;

Height:=300;
Width:=300;
Visible:=True;

LoGo:=TLabel.Create(self);
LoGo.Width:=100;
LoGo.Height:=100;
LoGo.Parent :=self;
LoGo.Color:=self.Color;
LoGo.Visible:=true;

end;


end.

兄弟自己改改把,应该可以了!
 
给个以前写的给你当例子吧:
应该直接拷贝下去就能用的了。
unit ButtonEdit;

interface

uses
SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
Forms, Dialogs, StdCtrls, Buttons, Menus;

type
TButtonEdit = class(TCustomMemo)
private
FButtonVisible: Boolean;
FButtonFlat: Boolean;
FOnButtonClick: TNotifyEvent;
procedure SetButtonVisible(const Value: Boolean);
procedure SetButtonBounds;
procedure SetButtonFlat(const Value: Boolean);
function GetButtonHint: string;
procedure SetButtonHint(const Value: string);
protected
FButton: TSpeedButton;
procedure BtnClickHandler(Sender: TObject)
virtual;
procedure UpdateFormatRect;
procedure WMSize(var Msg: TWMSize)
message WM_SIZE;
procedure WMSetCursor(var Msg: TWMSetCursor)
message WM_SETCURSOR;
procedure CMEnabledChanged(var Msg: TWMNoParams)
message CM_ENABLEDCHANGED;
procedure CreateHandle
override;
public
constructor Create(AOwner: TComponent)
override;
published
property ButtonVisible: Boolean read FButtonVisible write SetButtonVisible default True;
property ButtonFlat: Boolean read FButtonFlat write SetButtonFlat;
property ButtonHint: string read GetButtonHint write SetButtonHint;
property Align;
property Alignment;
property Anchors;
property AutoSelect;
property AutoSize;
property BorderStyle;
property CharCase;
property Color;
property Ctl3D;
property Enabled;
property Font;
property HideSelection;
property MaxLength;
property ParentColor;
property ParentCtl3D;
property ParentFont;
property ParentShowHint;
property PasswordChar;
property PopupMenu;
property ReadOnly;
property ShowHint;
property TabOrder;
property TabStop;
property Text;
property Visible;
property OnChange;
property OnClick;
property OnEnter;
property OnExit;
property OnKeyDown;
property OnKeyPress;
property OnKeyUp;
property OnMouseDown;
property OnMouseMove;
property OnMouseUp;
property OnButtonClick: TNotifyEvent read FOnButtonClick write FOnButtonClick;
end;

implementation

constructor TButtonEdit.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
Height := 21;
Width := 150;
WordWrap := False;
WantReturns := False;
FButtonVisible := True;

FButton := TSpeedButton.Create(Self);
with FButton do
begin
Parent := Self;
Caption := '...';
Align := alRight;
Spacing := -1;
ShowHint := True;
Margin := -1;
OnClick := BtnClickHandler;
end;
end;

procedure TButtonEdit.CreateHandle;
begin
inherited CreateHandle;
UpdateFormatRect;
end;

procedure TButtonEdit.UpdateFormatRect;
var
Rect: TRect;
begin
Rect := ClientRect;
if FButtonVisible then
Dec(Rect.Right, FButton.Height)
else
Inc(Rect.Right, FButton.Height);
SendMessage(Handle, EM_SETRECTNP, 0, Longint(@Rect));
end;

procedure TButtonEdit.WMSize(var Msg: TWMSize);
begin
inherited;
FButton.Width := FButton.Height;
UpdateFormatRect;
end;

procedure TButtonEdit.WMSetCursor(var Msg: TWMSetCursor);
var
P: TPoint;
PosWidth: Integer;
begin
GetCursorPos(P);
P := ScreenToClient(P);
PosWidth := ClientWidth;
if FButtonVisible then
PosWidth := PosWidth - FButton.Width;
if (P.X >= PosWidth) then
SetCursor(Screen.Cursors[crDefault])
else
inherited;
end;

procedure TButtonEdit.CMEnabledChanged(var Msg: TWMNoParams);
begin
inherited;
FButton.Enabled := Enabled;
end;

procedure TButtonEdit.SetButtonBounds;
begin
if not FButtonVisible then
FButton.Width := 0
else
FButton.Width := Height - 1;
UpdateFormatRect;
if not (csLoading in ComponentState) then
begin
SendMessage(Handle, EM_SETMARGINS, EC_LEFTMARGIN, 0);
SendMessage(Handle, EM_SETMARGINS, EC_RIGHTMARGIN, MakeLong(0, 2));
end;
end;

procedure TButtonEdit.SetButtonVisible(const Value: Boolean);
begin
if FButtonVisible <> Value then
begin
FButtonVisible := Value;
FButton.Visible := Value;
SetButtonBounds;
Invalidate;
end;
end;

procedure TButtonEdit.SetButtonFlat(const Value: Boolean);
begin
if FButtonFlat <> Value then
begin
FButtonFlat := Value;
FButton.Flat := Value;
Invalidate;
end;
end;

function TButtonEdit.GetButtonHint: string;
begin
Result := FButton.Hint;
end;

procedure TButtonEdit.SetButtonHint(const Value: string);
begin
FButton.Hint := Value;
end;

procedure TButtonEdit.BtnClickHandler(Sender: TObject);
begin
if Assigned(FOnButtonClick) then
FOnButtonClick(Self);
end;

end.

 
楼上的兄弟都是大好人
 
没有特殊要求应该写成ActiveX控件,这样才符合"多快好省"的原则。如果出于继承的目
的而写VCL组件,这种情况还不如重写ActiveX控件更方便。
 
多人接受答案了。
 
后退
顶部