如何在delphi程序中为某一控件添加自定义事件?(50分)

  • 主题发起人 主题发起人 wzsgislab
  • 开始时间 开始时间
你想加在哪个单元,引用窗体/控件源码?
 
1.从该控件类继承一新类。假如自定义事件<br>2.修改该控件的WindowProc,在自定义的WindowProc中响应自定义事件<br>3.修改Delphi Source :(
 
继承,创建。
 
继承该控件,并添加事件,记得注册控件喔。
 
&nbsp; &nbsp; &nbsp;本例中,FtooBig为定义的事件处理过程指针,OnTooBig为事件属性名。<br>事件处理过程指针FtooBig通过程序的初始化使之指向过程TooBig1。<br>在Delphi的表单(Form1)上放置三个编辑框,分别为Edit1、Edit2和Edit3,<br>放一按钮Button1。程序中设私有整型变量val1、val2和res,变量res用来记录val1和val2的乘积,<br>并用Edit3显示出来。当通过Edit1和Edit2输入的数据有一个大于100时,<br>会触发一个事件,并调用事件处理过程TooBig1显示一个对话框,说明此事件已经发生并已进行处理。<br><br>源程序代码如下, 该程序在Delphi 4.0中调试通过。 <br><br>unit Unit1;<br>interface<br>&nbsp; uses Windows, Messages, SysUtils, Classes,Graphics, Controls, Forms, Dialogs,StdCtrls;<br>type<br>&nbsp; TForm1 = class(TForm)<br>&nbsp; &nbsp; Edit1: TEdit; &nbsp; &nbsp;{输入第一个整数}<br>&nbsp; &nbsp; Edit2: TEdit; &nbsp; &nbsp;{输入第二个整数}<br>&nbsp; &nbsp; Edit3: TEdit; &nbsp; &nbsp;{输出前二个整数的积}<br>&nbsp; &nbsp; Button1: TButton;<br>&nbsp; &nbsp; procedure Button1Click(Sender: TObject);<br>&nbsp; &nbsp; procedure TooBig1(Sender: TObject); &nbsp; &nbsp; {当事件触发后调用此过程}<br>&nbsp; &nbsp; procedure FormCreate(Sender: TObject);<br>&nbsp; private<br>&nbsp; &nbsp; val1,val2,res:integer; {val1和val2存放输入的两个整数,res存放两数的积}<br>&nbsp; &nbsp; FTooBig : TNotifyEvent; &nbsp; {定义一个指向事件处理器的指针FTooBig}<br>&nbsp; &nbsp; { Private declarations }<br>&nbsp; public<br>&nbsp; &nbsp; { Public declarations }<br>&nbsp; published<br>&nbsp; &nbsp; property &nbsp;OnTooBig:TNotifyevent read FTooBig write FTooBig;{定义事件}<br>&nbsp; end;<br><br>var<br>&nbsp; Form1: TForm1;<br><br>implementation<br>{$R *.DFM}<br>procedure TForm1.Button1Click(Sender: TObject);<br>begin<br>&nbsp; val1 := StrToInt(Edit1.Text);<br>&nbsp; val2 := StrToInt(Edit2.Text);<br>&nbsp; if(val1&lt; 100)and(val2&lt; 100) then<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; res := val1*val2;<br>&nbsp; &nbsp; &nbsp; Edit3.Text := IntToStr(res);<br>&nbsp; &nbsp; end<br>&nbsp; else<br>&nbsp; &nbsp; if assigned(FTooBig) then &nbsp; OnTooBig(Self);<br>end;<br><br>procedure TForm1.TooBig1(Sender: TObject);<br>begin<br>&nbsp; Application.MessageBox('Too Big',' Test Event! ',MB_OK);<br>end;<br><br>procedure TForm1.FormCreate(Sender: TObject);<br>begin<br>&nbsp; val1:=1;<br>&nbsp; val2:=1;<br>&nbsp; FTooBig := TooBig1;{使事件处理指针指向事件处理器}<br>end;<br><br>end.<br><br>--------------------------------------------------------------------------------<br>
 
举个简单例子。<br>众所周知,TScrollBox本身没有滚动事件。我们可以定义如下:<br><br>type<br>&nbsp; TJScrollBox = class(TScrollBox)<br>&nbsp; private<br>&nbsp; &nbsp; { Private declarations }<br>&nbsp; &nbsp; FOnHScroll, FOnVScroll: TNotifyEvent;<br>&nbsp; &nbsp; procedure WMVScroll(var Msg: TWMVScroll); message WM_VSCROLL;<br>&nbsp; &nbsp; procedure WMHScroll(var Msg: TWMHScroll); message WM_HSCROLL;<br>&nbsp; protected<br>&nbsp; &nbsp; { Protected declarations }<br>&nbsp; public<br>&nbsp; &nbsp; { Public declarations }<br>&nbsp; published<br>&nbsp; &nbsp; { Published declarations }<br>&nbsp; &nbsp; property OnHScroll: TNotifyEvent read FOnHScroll write FOnHScroll;<br>&nbsp; &nbsp; property OnVScroll: TNotifyEvent read FOnVScroll write FOnVScroll;<br>&nbsp; end;<br><br>procedure Register;<br><br>implementation<br><br>procedure Register;<br>begin<br>&nbsp; RegisterComponents('JComponents', [TJScrollBox]);<br>end;<br><br>procedure TJScrollBox.WMVScroll(var Msg: TWMVScroll);<br>begin<br>&nbsp; inherited;<br>&nbsp; if Assigned(FOnVScroll) then FOnVScroll(Self);<br>end;<br><br>procedure TJScrollBox.WMHScroll(var Msg: TWMHScroll);<br>begin<br>&nbsp; inherited;<br>&nbsp; if Assigned(FOnHScroll) then FOnHScroll(Self);<br>end;<br><br>//////////////////////////////////Example///////////////////////////<br><br>procedure TForm1.JScrollBox1HScroll(Sender: TObject);<br>begin<br>&nbsp; ShowMessage(IntToStr(JScrollBox1.HorzScrollBar.Position));<br>end;<br><br>procedure TForm1.JScrollBox1VScroll(Sender: TObject);<br>begin<br>&nbsp; ShowMessage(IntToStr(JScrollBox1.VertScrollBar.Position));<br>end;<br><br>
 
多人接受答案了。
 
后退
顶部