OnClick事件的Sender参数的前世今生——TWinControl.WinProc优先捕捉到鼠标消息,然后使用IsControlMouseMsg函数进行消息转发给图形子控件(意外发现OnClick是由WM_LBUTTONUP触发的)
这是一个再普通不过的Button1Click执行体:
procedure TForm1.Button1Click(Sender: TObject);
begin
ShowMessage('I am Button1');
end;
点击Button1以后,具体过程是:Form收到Button1发来的WM_COMMAND,然后发一个CN_COMMAND给Button1,这个过程就不描述了。这里研究的是VCL在接下去是如何执行的:
procedure TButton.CNCommand(var Message: TWMCommand);
begin
if Message.NotifyCode = BN_CLICKED then Click;
end; procedure TButton.Click;
var
Form: TCustomForm;
begin
Form := GetParentForm(Self);
if Form <> nil then Form.ModalResult := ModalResult;
inherited Click;
end; procedure TControl.Click;
begin
{ Call OnClick if assigned and not equal to associated action's OnExecute.
If associated action's OnExecute assigned then call it, otherwise, call
OnClick. }
if Assigned(FOnClick) and (Action <> nil) and (@FOnClick <> @Action.OnExecute) then
FOnClick(Self)
else if not (csDesigning in ComponentState) and (ActionLink <> nil) then
ActionLink.Execute(Self)
else if Assigned(FOnClick) then
FOnClick(Self); // 这里的Self代表当前TControl,也就是Button1对象
end;
说白了就是这么简单啊。
注意,每一个具有Sender参数的函数,都是由VCL框架提供的。但每一个不同的事件,分别由VCL不同函数来提供Sender参数的具体内容。以上分析仅仅分析了OnClick事件这一种情况。
-------------------------------------------------------------------------------------------------------
那么TImage1的OnClick的Sender是谁提供的呢?在Form1上只放置一个Image1,载入图片后双击增加以下代码,经过测试:
procedure TForm1.Image1Click(Sender: TObject);
begin
ShowMessage(Sender.ClassName); // 显示结果TImage
if Sender is TImage then
ShowMessage(TImage(Sender).Name); // 显示结果是Image1
end;
而且鼠标点击图片后,不松手,就不会执行以上代码,这充分说明不是WM_LBUTTONDOWN触发此事件。于是忽略所有有关WM_LBUTTONDOWN的过程。
仔细查看TCustomForm的代码,并没有发现WM_LBUTTONUP的覆盖消息函数,这说明Image1的触发过程,仍是VCL框架提供的功能(要么是TControl,要么是TWinControl)。继续仔细查看,发现只有TControl对WM_LBUTTONUP有直接的响应函数:
procedure WMLButtonUp(var Message: TWMLButtonUp); message WM_LBUTTONUP; procedure TControl.WMLButtonUp(var Message: TWMLButtonUp);
begin
inherited;
if csCaptureMouse in ControlStyle then MouseCapture := False;
if csClicked in ControlState then
begin
Exclude(FControlState, csClicked);
if PtInRect(ClientRect, SmallPointToPoint(Message.Pos)) then
Click;
end;
DoMouseUp(Message, mbLeft);
end;
同时TWinControl.WndProc里也有对WM_LBUTTONUP的处理,那么谁先谁后呢?当然还是WndProc优先运行,因为它是Form1这个窗口所直接对应的窗口函数。只有在这里找不到处理,才会调用Dispatch去寻找消息处理函数。
procedure TWinControl.WndProc(var Message: TMessage);
var
Form: TCustomForm;
begin
case Message.Msg of
WM_SETFOCUS:
begin
Form := GetParentForm(Self);
if (Form <> nil) and not Form.SetFocusedControl(Self) then Exit;
end;
WM_KILLFOCUS:
if csFocusing in ControlState then Exit;
WM_NCHITTEST: // 注意这里,鼠标移动时,也会不停的执行
begin
inherited WndProc(Message);
if (Message.Result = HTTRANSPARENT) and (ControlAtPos(ScreenToClient(
SmallPointToPoint(TWMNCHitTest(Message).Pos)), False) <> nil) then
Message.Result := HTCLIENT;
Exit;
end;
WM_MOUSEFIRST..WM_MOUSELAST:
begin
if Message.Msg = WM_LBUTTONUP then // 不这样改造,WM_MOVE消息会不停的来干扰
begin
tag := ; // 下断点,可以准确捕捉鼠标点击图片后弹起时的消息
end;
if IsControlMouseMsg(TWMMouse(Message)) then // 检测并转发鼠标消息。如果是直接点击WinControl,那么此处捕捉无效,会继续通过TControl.WndProc和Dispatch继续传递消息
begin
{ Check HandleAllocated because IsControlMouseMsg might have freed the
window if user code executed something like Parent := nil. }
if (Message.Result = ) and HandleAllocated then
DefWindowProc(Handle, Message.Msg, Message.wParam, Message.lParam);
Exit; // 图形子控件对消息处理完毕,直接退出了。不给父控件处理的机会
end;
end;
WM_KEYFIRST..WM_KEYLAST:
if Dragging then Exit;
WM_CANCELMODE:
if (GetCapture = Handle) and (CaptureControl <> nil) and
(CaptureControl.Parent = Self) then
CaptureControl.Perform(WM_CANCELMODE, , );
end;
inherited WndProc(Message);
end; function TWinControl.IsControlMouseMsg(var Message: TWMMouse): Boolean;
var
Control: TControl;
P: TPoint;
begin
if GetCapture = Handle then
begin
if (CaptureControl <> nil) and (CaptureControl.Parent = Self) then
Control := CaptureControl
else
Control := nil;
end
else
Control := ControlAtPos(SmallPointToPoint(Message.Pos), False); // 检测鼠标正在点击自己的哪个图形子控件
Result := False;
if Control <> nil then
begin
P.X := Message.XPos - Control.Left;
P.Y := Message.YPos - Control.Top;
Message.Result := Control.Perform(Message.Msg, Message.Keys, Longint(PointToSmallPoint(P))); // 此时调试器显示msg的值是514,经过查询WM_LBUTTONUP = $0202;正是514,说明WM_LBUTTONUP消息被Image1的父控件正确转发给Image1
Result := True;
end;
end; procedure TControl.WMLButtonUp(var Message: TWMLButtonUp);
begin
inherited; // 注意,如果是直接点击Form1,会执行TCustomForm.DefaultHandler(var Message);相当于给子类控件提供了新的处理消息的机会
if csCaptureMouse in ControlStyle then MouseCapture := False;
if csClicked in ControlState then
begin
Exclude(FControlState, csClicked);
if PtInRect(ClientRect, SmallPointToPoint(Message.Pos)) then
Click; // 先执行OnClick,后执行MouseUp
end;
DoMouseUp(Message, mbLeft);
end; procedure TControl.Click;
begin
{ Call OnClick if assigned and not equal to associated action's OnExecute.
If associated action's OnExecute assigned then call it, otherwise, call
OnClick. }
if Assigned(FOnClick) and (Action <> nil) and (@FOnClick <> @Action.OnExecute) then
FOnClick(Self)
else if not (csDesigning in ComponentState) and (ActionLink <> nil) then
ActionLink.Execute(Self)
else if Assigned(FOnClick) then
FOnClick(Self); // 执行Image1Click,注意它的Sender参数
end;
OnClick事件的Sender参数的前世今生——TWinControl.WinProc优先捕捉到鼠标消息,然后使用IsControlMouseMsg函数进行消息转发给图形子控件(意外发现OnClick是由WM_LBUTTONUP触发的)的更多相关文章
- TCustomControl绘制自己和图形子控件共四步,TWinControl关键属性方法速记
TCustomControl = class(TWinControl) private FCanvas: TCanvas; procedure WMPaint(var Message: TWMPain ...
- 关于Asp.net事件,如何在触发子控件的事件时,同步触发父页面的事件
对页面引用自定义控件后,通过绑定自定义事件,页面绑定子控件的事件,在子控件做了某些修改动作后,如何同步操作父页面的方法:下面我煮了个栗子,同学们可以来尝一尝试一试 a.aspx 引用 UserCont ...
- 记录下帮助一位网友解决的关于android子控件的onTouch或onClick和父OnTouch 冲突的问题。
前三天收到位网友的私信求助,问题大概如标题所示.具体是下面的情况,个人感觉,这个问题挺有趣,也会在实际项目开发中很常见.不想看前奏的请直接跳至解决方法. 问题原型: 父控件是自定义的 LinearLa ...
- 解决ListView中Item的子控件与Item点击事件冲突
常常会碰到在ListView中点击当中一个Item.会一并触发其子控件的点击事件.比如Item中的Button.ImageButton等.导致了点击Item中Button以外区域也会触发Button点 ...
- 截取scrollview的滑动事件,传递给子控件
重写一个ScrollView public class MyScrollView extends ScrollView{ public MyScrollView(Context context, At ...
- JQuery 点击子控件事件,不会触发父控件的事件
$('.order-delete').on('tap', function (e) { console.log('删除1'); c ...
- HTML 控件和web控件 OnClientClick和OnClick OnServerClick区别
^_^ 本来对html控件,服务器控件的知识模模糊糊的.今天特地查了相关的知识. 下面是我写代码总结的. 这些事件 主要用于在客户端执行验证,然后决定是否执行服务端事件 (没接触之前就为此 ...
- Android开发之解决父控件拦截子控件事件问题
以ViewPager为例: public class TopNewsViewPager extends ViewPager { public TopNewsViewPager(Context cont ...
- onclick事件对动态参数类型为字符串的处理
onclick="solveRow("'+row.isbesolve+'")"
随机推荐
- Maven--几个需要补充的问题(三)
<Maven--搭建开发环境(一)> <Maven--构建企业级仓库(二)> <Maven—几个需要补充的问题(三)> 前两篇由于篇幅太长,为了给读者理解方便,这篇 ...
- ZigBee研究之旅(二)
在学习ZigBee设备CC2530模块时,编程后程序无法运行,但又十分确定程序的真确性的情况下,看看是不是project栏下的option选项配置的有问题,我是经常在这里出问题,一开始找不到原因,特此 ...
- AFNetworking 3.0x版本最新特性
AFNetworking是一款在OS X和iOS下都令人喜爱的网络库.为了迎合iOS新版本的升级, AFNetworking在3.0版本中删除了基于 NSURLConnection API的所有支持. ...
- ARM流水线(pipeline)
- portal---easyui
一,效果图. 二,源代码. <!doctype html><html><head> <meta http-equiv="Content-Type&q ...
- 将树莓派变成一个web服务器(2):Nginx+Flask+uWSGI部署全过程
1)安装Flask,uwsgi,nginx sudo apt-get update sudo apt-get install python-flask #Flask sudo apt-get inst ...
- Week8(10月31日):并发
Part I:提问 =========================== 1. 更新关联.删除关联数据,需要注意哪些问题?以Instructor类为例说明. 2. 已知某请假系统,请实现以下界面的 ...
- Windows Phone 8初学者开发—第16部分:使用应用程序栏
原文 Windows Phone 8初学者开发—第16部分:使用应用程序栏 原文地址: http://channel9.msdn.com/Series/Windows-Phone-8-Develop ...
- SolrCloud简介
原文地址:http://www.chepoo.com/solrcloud-introduction.html 一.简介 SolrCloud是Solr4.0版本以后基于Solr和Zookeeper的分布 ...
- mojo 默认use utf8;
my $endtime=strftime("%Y%m%d%H%M%S",localtime()); my $d=encode_utf8('验证'); if ($a3 =~/$d/) ...