关于控件事件...在线等待,解决立即放分。 (100分)

  • 主题发起人 desertsmoke
  • 开始时间
D

desertsmoke

Unregistered / Unconfirmed
GUEST, unregistred user!
如果一个组见引用了其它组件,例如:控件TAar由一个Label和两个Shape组成,其中一个Shape
(Shape1)作为Owner,另一个Shape(Shape2) 和 Label(Label1) 的Parent设为 Shape1,那么默
认情况下,只有在Shape1上按下鼠标左键才能触发TAar的OnMouseDown事件。

问题是:如何可以作到在Shape2 和 Label1上按下鼠标左键时触发TAar的OnMouseDown事件?
 
在Shape2 和 Label1的OnMouseDown中
Shape1.OnMouseDown(sender);


 
to:lbd
控件中如何来写?写在什么地方?
 
type
Shape2 = tShape
private
procedure ss(Sender: TObject; Shift: TShiftState; X,
Y: Integer); //自定义
....
constructor TShape2.Create(AOwner: TComponent);
begin
inherited;
self.OnMouseDown:=ss; //转到自定义上
end;

..
procedure TShape2.ss(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
begin
Shape1.OnMouseDown(sender); //向Shape1发送消息
end;
 
to:AIHUA
思路是对的,但是这段代码却实现不了
 
按照这个思路具体代码你自己写

应该不成问题的八
 
type
Shape2 = tShape
private
procedure ss(Sender: TObject; Shift: TShiftState; X,
Y: Integer); //自定义
....
constructor TShape2.Create(AOwner: TComponent);
begin
inherited;
self.OnMouseDown:=ss; //转到自定义上
end;

..
procedure TShape2.ss(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
begin
SendMessage(TShape(Parent).Handle, WM_LButtonDown, 0, x shl 16+y)
end;
 
constructor TShape2.Create(AOwner: TComponent);
begin
inherited;
self.OnMouseDown:=ss; //转到自定义上
end;
安装时提示TShape2是找不到的。
 
constructor TAar.Create;
begin
...
FShape2:=TShape.Create(FShape1);
FShape2.OnMouseDown:=ss;
...
end;
 
FShape2:=TShape.Create(FShape1);
FShape2只是一种控件类型,而右边是一个控件,可以划等号吗?
 
TShape.
constructor Create(AOwner: TComponent); override;
根据声明 TShape.Create 的参数是AOwner
((其中一个Shape(Shape1)作为Owne))

对上号了吧
 
>>FShape2只是一种控件类型
type
TAar = object(Txxx)
private
FShape2: TShape;
...
protected
end;
 
Sorry!是眼误!
 
不如大家来改改看吧!
我把测试的代码贴出来。各位多多帮忙!——谢!
———————————————————————————————————————-
{功能:实现RJ组件}
unit RJ;

interface

uses
Windows, Messages, SysUtils, Classes, Controls, ExtCtrls, Graphics;

type
TMyPortStyle = ( psDown , psUp );
TRJ = class(TPanel)
TimerAlarm:TTimer;
ImageShow:TImage;
private
{ Private declarations }
//FOnImageClick :TMouseEvent;
boolAlarm :Boolean; //Alarm属性的响应变量
boolDirection :Boolean; //Direction属性的响应变量
colorAlarmColor :TColor; //AlarmColor属性的响应变量
boolNowAlarm :Boolean; //控制TimerAlarm组件不被多次创建的变量
colorBackgroundColor:TColor; //BackgroundColor属性的响应变量
FPortStyle :TMyPortStyle;
procedure SetAlarm(AlarmNow:Boolean); //Alarm属性的响应函数
procedure SetDirection(DireNow:Boolean); //Direction属性的响应函数
procedure SetAlarmColor(AlarmColor:TColor);//AlarmColor属性的响应函数
procedure Wmsize(var message:Tmessage);message wm_size;//组件大小变化时的消息响应函数
procedure TimerAnswer(Sender:TObject); //Tiemr1组件的定时相应函数
procedure SetBackgroundColor(BackgroundColor:Tcolor); //BackgroundColor属性的响应函数
Procedure SetPortStyle(PS:TMyPortStyle); //PortStyle属性的响应函数
//Procedure ImageOnMouseDown(Sender: TObject; Shift: TShiftState; X,Y: Integer);
protected
{ Protected declarations }
public
{ Public declarations }
constructor Create(AOwner:TComponent);override; //重载Create函数
//constructor TImage.Create(AOwner: TComponent);
//constructor TShape2.Create(AOwner: TComponent);OverRide;
procedure ChangeImage(ImageName:String); //更换图片的方法
//procedure ImageOnMouseDown(Sender: TObject; Button: TMouseButton;Shift: TShiftState; X, Y: Integer);
published
{ Published declarations }
property Direction : Boolean read boolDirection write SetDirection; //纵横指定
property Alarm : Boolean read boolAlarm write SetAlarm; //告警指定
property AlarmColor : TColor read colorAlarmColor write SetAlarmColor;//告警颜色指定
property BackgroundColor : Tcolor read colorBackgroundColor write SetBackgroundColor;//背景颜色指定
property PortStyle : TMyPortStyle read FPortStyle write SetPortStyle default psDown;
//Property OnImageClick : TMouseEvent read FOnImageClick write FOnImageClick;
end;

procedure Register;
{$R RJ.res}
implementation

procedure Register;
begin
RegisterComponents('PanelVCL', [TRJ]);
end;

{函数功能:重载Create函数}
constructor TRJ.Create(AOwner:TComponent);
begin
inherited Create(AOwner);

//组件原始外观
Height:=103;
Width:=104;
BevelOuter:=bvnone;
BevelInner:=bvnone;
//初始化各属性
boolAlarm:=False;
colorAlarmColor:=clRed;
colorBackgroundColor:=Color;

//创建Image组件
ImageShow:=TImage.Create(self);
ImageShow.Parent:=self;
ImageShow.Top:=2;
ImageShow.Left:=2;
ImageShow.Height:=99;
ImageShow.Width:=100;
ImageShow.Stretch:=true;
ImageShow.Picture.Bitmap.Handle:=LoadBitmap(HInstance,'RJV');
ImageShow.Show;
//ImageShow.OnClick := OnClick;
//ImageShow.OnMouseDown := ImageOnMouseDown;
boolNowAlarm:=False;//设置控制告警的全局变量
boolDirection:=False;

end;

{函数功能:实现缩放功能}
procedure TRJ.Wmsize(var message:TMessage);
begin
inherited;

if (Height > 0)and(Width > 0) then
begin
ImageShow.Height:=Height-4;
ImageShow.Width:=Width-4;
end;
end;

{函数功能:实现告警指定功能,响应Alarm属性
参数说明:对组件属性Alarm的赋值}
procedure TRJ.SetAlarm(AlarmNow:Boolean);
begin
//若第一次进行告警,创建TimerAlarm组件
if boolNowAlarm = False then
begin
TimerAlarm:=TTimer.Create(self);
TimerAlarm.Interval:=1000;
TimerAlarm.OnTimer:=TimerAnswer;
boolNowAlarm:=True;
end;
TimerAlarm.Enabled:=AlarmNow;
BoolAlarm:=AlarmNow;

//停止告警时背景色改为原始背景色
if AlarmNow = False then
Color:=colorBackgroundColor;

end;

{函数功能:TimerAlarm组件的定时响应函数,实现变色}
procedure TRJ.TimerAnswer(Sender:TObject);
begin
if Color = colorBackgroundColor then
Color:=colorAlarmColor
else
Color:=colorBackgroundColor;

end;

{函数功能:设置告警颜色,响应AlarmColor属性
参数说明:对组件属性AlarmColor的赋值}
procedure TRJ.SetAlarmColor(AlarmColor:TColor);
begin
colorAlarmColor:=AlarmColor;
end;

{函数功能:设置背景颜色,响应BackgroundColor属性
参数说明:对组件属性BackgroundColor的赋值}
procedure TRJ.SetBackgroundColor(BackgroundColor:TColor);
begin
colorBackgroundColor:=BackgroundColor;
Color:=colorBackgroundColor;

end;

{函数功能:为用户提供更改图片的方法
参数说明:用户自行应用的res文件中的Bitmap图形名}
procedure TRJ.ChangeImage(ImageName:String);
var
x:pchar;
begin
x:=pchar(ImageName);
ImageShow.Picture.Bitmap.Handle:=LoadBitmap(HInstance,x);
end;

procedure TRJ.SetDirection(DireNow:Boolean);
begin
if DireNow <> boolDirection then
begin
boolDirection:=DireNow;
if DireNow = True then
ImageShow.Picture.Bitmap.Handle:=LoadBitmap(HInstance,'RJH')
else
ImageShow.Picture.Bitmap.Handle:=LoadBitmap(HInstance,'RJV');
end;
end;

procedure TRJ.SetPortStyle(PS: TMyPortStyle);
var
ImgStr:string;
begin
FportStyle := PS;
if PS=psDown then
ImgStr := 'RJV'
else
ImgStr := 'RJV1';
ChangeImage(ImgStr);
end;

{procedure ImageShow.ImageOnMouseDown(Sender: TObject; Button: TMouseButton;Shift: TShiftState; X, Y: Integer);
begin
SendMessage(AOwner.Handle, WM_LButtonDown, 0, x shl 16+y)
end;
}
end.
 
ImageShow.OnMouseDown:=ImageOnMouseDown;
难道不行么?错在哪里?
 
ImageOnMouseDown的定义必须和OnMouseDown一样
参数必须一样
如下
procedure ImageOnMouseDown(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
 
to:LeeChange
是啊!不行,你把代码粘一下试试,是完整的(就要第一个单元就可以)
 
to:AIHUA
是一样的啊!
 
错误嬉戏
 
顶部