如何将多个控件组合起来开发成新控件? ( 积分: 100 )

  • 主题发起人 主题发起人 mevip
  • 开始时间 开始时间
M

mevip

Unregistered / Unconfirmed
GUEST, unregistred user!
想开发外观如下的控件,如何进行?请说一下步骤
edit1+label1+edit2+label2
 
想开发外观如下的控件,如何进行?请说一下步骤
edit1+label1+edit2+label2
 
看一下TCustomLabeledEdit,它从TCustomEdit继承的

TCustomLabeledEdit = class(TCustomEdit)
 
组件模板:
Componment|Create Componment Template...
 
建议你考虑可以用Panel来做,然后在上面放上你需要的Edits & Labels。。。。
 
TMEdits = class(TCustomControl)
private
FYearEdit: TEdit;
FMonthEdit: TEdit;
FDayEdit: TEdit;

.....................中间省略
constructor TMEdits.Create(Aowner: TComponent);
begin
inherited;
FYearEdit := TEdit.Create(self);
FYearEdit.Parent := self;
FYearEdit.Width := 33;
FYearEdit.Height := 21;
FYearEdit.Top := 0;
FYearEdit.Left := 0;
FYearEdit.ShowHint := true;
FYearEdit.Hint := '年';
FYearEdit.OnKeyPress := CheckNum;
FYearEdit.OnExit := ExitCheckYear;
FYearEdit.OnMouseMove := MoveYear;

FMonthEdit := TEdit.Create(self);
FMonthEdit.Parent := self;
FMonthEdit.Width := 21;
FMonthEdit.Height := 21;
FMonthEdit.Top := 0;
FMonthEdit.ShowHint := true;
FMonthEdit.Hint := '月';
FMonthEdit.OnKeyPress := CheckNum;
FMonthEdit.Left := FYearEdit.Width + 10;
FMonthEdit.OnExit := ExitCheckMonth;
FMonthEdit.OnMouseMove := MoveMonth;
FMonthEdit.OnMouseUp := DayMouseUp;


FDayEdit := TEdit.Create(self);
FDayEdit.Parent := self;
FDayEdit.Width := 21;
FDayEdit.Height := 21;
FDayEdit.Top := 0;
FDayEdit.ShowHint := true;
FDayEdit.Hint := '日';
FDayEdit.OnKeyPress := CheckNum;
FDayEdit.Left := FMonthEdit.Left + FMonthEdit.Width + 10;
FDayEdit.OnExit := ExitCheckDay;
FDayEdit.OnMouseMove := MoveDay;
FDayEdit.OnMouseUp := DayMouseUp;
end;
destructor TMEdits.Destroy;
begin
begin
Buffer.Free;

FYearEdit := nil;
FMonthEdit := nil;
FDayEdit := nil;
inherited;
end;
 
to xuegaoyili:

我在自定义控件中按你的做法动态创建了TEdit,但是在使用该自定义控件的窗体上如果没有TEdit的话会提示错误(找不到类TEdit),在该窗体上已增加了TEdit类所在Unit的引用以后错误依旧,只能在该窗体上放上一个TEdit才行,这是怎么回事?谢谢。
 
我的没这问题呀.是不是你的包含单元的问题.在自定义控件中就少了一些包含单元.那也不对呀.那样的话你连编译也不会过去的...

unit MEdits;

interface

uses
SysUtils, Classes, Controls, Stdctrls, Windows, messages,
Graphics, Forms, Dialogs,
ExtCtrls;
type

TGlassStyle = (
gsBlackness, gsDstInvert, gsMergeCopy, gsMergePaint, gsNotSrcCopy,
gsNotSrcErase, gsPatCopy, gsPatInvert, gsPatPaint, gsSrcAnd,
gsSrcCopy, gsSrcErase, gsSrcInvert, gsSrcPaint, gsWhiteness);

TMEdits = class(TCustomControl)
private
FYearEdit: TEdit;
FMonthEdit: TEdit;
FDayEdit: TEdit;
FCanMove: Boolean; //是否可以移动距离的变量

FColor: TColor;
FStyle: TGlassStyle;
FOnPaint: TNotifyEvent;

procedure SetColor(Value: TColor);
procedure SetStyle(Value: TGlassStyle);
procedure CMCtl3DChanged(var Message: TMessage); message CM_CTL3DCHANGED;
procedure WMEraseBkgnd(var Message: TMessage); message WM_ERASEBKGND;
procedure WMWindowPosChanging(var Message: TWMWindowPosChanging); message WM_WINDOWPOSCHANGING;

///数值检查函数
function CheckYear(Year: integer): Boolean;
function CheckMonth(Month: integer): Boolean;
function CheckDay(Day: integer): Boolean;

/////录入数据的类型检查
procedure CheckNum(Sender: TObject; var Key: Char); //检查键入的字符是否是数字类型

function GetCanMove: Boolean;
procedure SetCanMove(flg: Boolean);

///////使控件能移动的onmousemove过程
procedure MoveYear(Sender: TObject; Shift: TShiftState; X, Y: Integer);
procedure MoveMonth(Sender: TObject; Shift: TShiftState; X, Y: Integer);
procedure MoveDay(Sender: TObject; Shift: TShiftState; X, Y: Integer);

procedure DayMouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);

{ Private declarations }
protected
Buffer: TBitmap;

procedure CreateParams(var Params: TCreateParams); override;
procedure Paint; override;
procedure Resize; override;
procedure ExitCheckYear(Sender: TObject);
procedure ExitCheckMonth(Sender: TObject);
procedure ExitCheckDay(Sender: TObject);

public
{ Public declarations }
constructor Create(Aowner: TComponent); override;
destructor Destroy; override;
property Canvas;

property YearEdit: TEdit read FYearEdit;
property MonthEdit: TEdit read FMonthEdit;
property DayEdit: TEdit read FDayEdit;

published
{ Published declarations }

property CanMove: Boolean read GetCanMove write SetCanMove;
property Align;
property Anchors;
property AutoSize;
property BiDiMode;
property BorderWidth;
property Color: TColor read FColor write SetColor;
property Ctl3D;
property Enabled;
property Style: TGlassStyle read FStyle write SetStyle default gsSrcAnd;
property Visible;

property OnClick;
property OnDblClick;
property OnDragDrop;
property OnDragOver;
property OnEndDrag;
property OnEnter;
property OnExit;
property OnKeyDown;
property OnKeyPress;
property OnKeyUp;
property OnMouseDown;
property OnMouseMove;
property OnMouseUp;
property OnStartDrag;
property OnResize;
property OnPaint: TNotifyEvent read FOnPaint write FOnPaint;
end;

procedure Register;

implementation

这是我的一个控件的声明.就没有你说得问题.你比较一下上面的use部分吧
 
多人接受答案了。
 

Similar threads

D
回复
0
查看
2K
DelphiTeacher的专栏
D
D
回复
0
查看
2K
DelphiTeacher的专栏
D
D
回复
0
查看
1K
DelphiTeacher的专栏
D
D
回复
0
查看
1K
DelphiTeacher的专栏
D
S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
后退
顶部