祝大家中秋节快乐:自定义控件:panel+memo=Tmypanel,请教memo如何继承panel的事件 ( 积分: 100 )

  • 主题发起人 主题发起人 tl_lyq
  • 开始时间 开始时间
T

tl_lyq

Unregistered / Unconfirmed
GUEST, unregistred user!
TMypanel = class(Tpanel)
private
Memo:tMemo;
statusbar:tstatusbar;
.............................
constructor TMypanel.create(AOwner: TComponent);
begin
inherited Create(AOwner);
Memo:=TMemo.Create(Self);

Memo.OnDragDrop :=self.OnDragDrop ; //此处代码在创建对象后使用时不管用
Memo.OnDragOver :=self.OnDragOver ; //此代码在创建对象后使用时不管用
Memo.Font.Size :=self.Font.Size; //此属性memo随panel的变化而变化
Memo.Font.color:=self.Font.color;
Memo.Parent:=self;
memo.Align :=Alclient;
 
TMypanel = class(Tpanel)
private
Memo:tMemo;
statusbar:tstatusbar;
.............................
constructor TMypanel.create(AOwner: TComponent);
begin
inherited Create(AOwner);
Memo:=TMemo.Create(Self);

Memo.OnDragDrop :=self.OnDragDrop ; //此处代码在创建对象后使用时不管用
Memo.OnDragOver :=self.OnDragOver ; //此代码在创建对象后使用时不管用
Memo.Font.Size :=self.Font.Size; //此属性memo随panel的变化而变化
Memo.Font.color:=self.Font.color;
Memo.Parent:=self;
memo.Align :=Alclient;
 
为什么memo继承panel的事件
 
memo 在panel 中,
当其他控件dragover panel中的memo时,memo可以获得其传入的参数。
 
设置一下:Memo.DragMode :=dmAutomatic;
另外,你的 Panel 的self.OnDragOver事件有没有写?可以写个showmessage()测试一下。
 
设置 Memo.DragMode :=dmAutomatic; 不管用
经测试,在新控件Tmypanel边缘处时可以接收拖放。showmessage可以执行,但memo里就是不行。
 
把TMypanel的OnDragDrop 修改成SetOnDragDrop的方式看看,
property OnDragDrop:xxxx read xxx write SetOnDragGrop;

这样在设置它的属性时候
Memo.OnDragDrop :=self.OnDragDrop ;
这里就不会是空的了
 
还不行!!!!!!!!!!!!!!!!!!!!!
代码如下:

unit IDMYpanel;

interface

uses
Graphics,Windows, Messages, SysUtils, StdCtrls, Classes, ComCtrls, CommCtrl,ExtCtrls , Controls ;

type

TMypanel = class(Tpanel)
private
Memo:tMemo;
statusbar:tstatusbar;
procedure ReadOnDragGrop(Sender, Source: TObject; X, Y: Integer);
procedure SetOnDragGrop(Sender, Source: TObject; X, Y: Integer);

Function ReadMemoText():string;
procedure SetMemoText(Const Value:string);
Function ReadMemocolor():Tcolor;
procedure SetMemocolor(Const Value:Tcolor);
Function ReadstatusbarColor():Tcolor;
procedure setstatusbarColor(Const Value:Tcolor);
Function ReadSt1():string;
procedure SetSt1(Const Value:string);
Function ReadSt2():string;
procedure SetSt2(Const Value:string);
Function ReadSt3():string;
procedure SetSt3(Const Value:string);
Function ReadSt4():string;
procedure SetSt4(Const Value:string);

{ Published declarations }

{ Private declarations }

protected
{ Protected declarations }
public
constructor create(AOwner: TComponent); override;
destructor Destroy; override;
{ Public declarations }

published
property MemoText:string read ReadMemoText write SetMemoText;
property St1:string read ReadSt1 write Setst1;
property St2:string read ReadSt2 write Setst2;
property St3:string read ReadSt3 write Setst3;
property St4:string read ReadSt4 write Setst4;
property MemoColor:Tcolor read ReadMemoColor write SetMemoColor;
property statusbarColor:Tcolor read ReadstatusbarColor write SetstatusbarColor;
property OnDragDrop:tdragdropevent read ReadOnDragGrop write SetOnDragGrop;
end;

procedure Register;
implementation

procedure SetOnDragGrop(Sender, Source: TObject; X, Y: Integer);
begin
//
end;
procedure readOnDragGrop(Sender, Source: TObject; X, Y: Integer);
begin
//
end;

Function TMypanel.ReadMemoColor():Tcolor;
begin
Result:=memo.color;
end;
procedure TMypanel.SetMemoColor(Const Value:Tcolor);
begin
Memo.color:=Value;
end;

Function TMypanel.ReadstatusbarColor():Tcolor;
begin
Result:=statusbar.color;
end;
procedure TMypanel.SetstatusbarColor(Const Value:Tcolor);
begin
statusbar.color:=Value;
end;


Function TMypanel.ReadMemoText():string;
begin
Result:=memo.Lines.text;
end;
procedure TMypanel.SetMemoText(Const Value:string);
begin
Memo.Lines.text:=Value;
end;

Function TMypanel.ReadSt1():string;
begin
Result:=statusbar.Panels[0].text;
end;
procedure TMypanel.SetSt1(Const Value:string);
begin
statusbar.Panels[0].text:=Value;
end;

Function TMypanel.ReadSt2():string;
begin
Result:=statusbar.Panels[2].text;
end;
procedure TMypanel.SetSt2(Const Value:string);
begin
statusbar.Panels[2].text:=Value;
end;

Function TMypanel.ReadSt3():string;
begin
Result:=statusbar.Panels[4].text;
end;
procedure TMypanel.SetSt3(Const Value:string);
begin
statusbar.Panels[4].text:=Value;
end;

Function TMypanel.ReadSt4():string;
begin
Result:=statusbar.Panels[5].text;
end;
procedure TMypanel.SetSt4(Const Value:string);
begin
statusbar.Panels[5].text:=Value;
end;


constructor TMypanel.create(AOwner: TComponent);
begin
inherited Create(AOwner);
Memo:=TMemo.Create(Self);
Memo.OnDragDrop :=self.OnDragDrop;
Memo.OnDragOver :=self.OnDragOver;
Memo.Alignment :=taCenter;
Memo.Font.Size :=self.Font.Size;
Memo.Font.color:=self.Font.color;
Memo.Parent:=self;
memo.Align :=Alclient;
statusbar:=tstatusbar.create(self);
statusbar.Parent :=self;
statusbar.Align :=alBottom;

with statusbar.Panels.Add do //层
width:=20;

with statusbar.Panels.Add do //X
begin
width:=10;
text:='X';
Bevel:=pbNone;
end;

with statusbar.Panels.Add do //长
width:=20;

with statusbar.Panels.Add do //X
begin
width:=10;
text:='X';
Bevel:=pbNone;
end;

with statusbar.Panels.Add do //宽
width:=20;

with statusbar.Panels.Add do //其他
begin
width:=200;
end;

end;

destructor TMypanel.Destroy;
begin
inherited;
end;

procedure Register;
begin
RegisterComponents('Standard', [TMypanel]);
end;



end.
 
我觉得有一个方法比较简单:Memo.SetSubComponent(True);将Memo设为子控件,如同D6中的TLabeledEdit一样。
 
todreamisx, 没有任何效果啊。
 
还不行啊。请大家帮忙啊!
鼠标挪到memo中时,仍无法接收
 
你不会是想让,memo有容器(如加图片)的功能吧!
如果是那样的话,你可以用别的控件来实现啊!
 
panel 中放 memo 形成组合控件,当从其他对象dropover 至组合控件时,其中的memo的text可以接收拖来的值。如edit1.text.
 
override panel的loaded方法然后把
Memo.OnDragDrop :=self.OnDragDrop ;
Memo.OnDragOver :=self.OnDragOver ;
写入TMyPanel.Loaded方法中
 
写了一段,看是不是这样:
1)依照你的“当从其他对象dropover 至组合控件时,其中的memo的text可以接收拖来
的值。如edit1.text.” 写了两个新方法(以判断TEdit和Label为例):
procedure TMypanel.SetOnDragDrog(Sender, Source: TObject; X, Y: Integer);
begin
if (Source is TEdit)and(Sender is TMemo)then
Memo.Lines.Add(TEdit(Source).Text);
if (Source is TLabel)and(Sender is TMemo)then
Memo.Lines.Add(TLabel(Source).Caption);
end;

procedure TMypanel.SetOnDragOver(Sender, Source: TObject; X, Y: Integer;
State: TDragState; var Accept: Boolean);
begin
if (Source is TEdit)or(Source is TLabel)then
Accept :=true;
end;
然后将这两个方法分别赋给TMyPanel或其成员Memo的对应OnDragDrag和OnDragOver事件
constructor TMypanel.Create(AOwner :TComponent);
begin
inherited Create(AOwner);
Memo :=TMemo.Create(self);
self.OnDragDrop :=self.SetOnDragDrog;
self.OnDragOver :=self.SetOnDragOver;

Memo.OnDragDrop :=self.OnDragDrop ;
Memo.OnDragOver :=self.OnDragOver ;
.......
end;
2)在测试时对主窗体中欲拖动的控件的MouseDown事件设置,如:
procedure TForm1.Edit1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
if Button = mbLeft then
Edit1.BeginDrag(true,10);
end;
这样,拖动窗体中的控件到Memo中时就会显示Text或Caption。
 
问题解决。多谢大家
 
后退
顶部