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+'")"
随机推荐
- BZOJ 1858: [Scoi2010]序列操作( 线段树 )
略恶心的线段树...不过只要弄清楚了AC应该不难.... ---------------------------------------------------------------- #inclu ...
- 今天才知道mysql
insert MySQL中的INSERT语句和标准的INSERT不太一样,在标准的SQL语句中,一次插入一条记录的INSERT语句只有一种形式.INSERT INTO tablename(列名…) V ...
- oschina开源硬件其它开源,开源硬件
硬件驱动/工具 25Linux内核 101桌面环境 40开源字体 58嵌入式操作系统 33输入法 110开源硬件 57开源图书 5开源家居 17物联网 87开源货币/比特币 32NASA 开源项目 1 ...
- CCIE路由实验(2) -- BGP选路原则
BGP路径属性分为4类: 公认必遵(Well-Known Mandatory):BGP更新报文中必须包含的,且必须被所有BGP厂商实现所能识别的,包括ORIGIN,AS-PATH和Next_Hop 1 ...
- HDU 1573 X问题 中国剩余定理
链接:pid=1573">http://acm.hdu.edu.cn/showproblem.php? pid=1573 题意:求在小于等于N的正整数中有多少个X满足:X mod a[ ...
- weblogic公布的项目用途myeclipse正常启动,点击startWeblogic.cmd报错解决方案
今天在做项目中遇到的问题.使用weblogic公布的项目,使用myeclipse正常启动,但点击startWeblogic.cmd会报错.我提出了一个class not found.楚是什么问题.后来 ...
- 使用storyboard实现页面跳转,简单的数据传递
由于最近才接触到IOS,苹果已经建议storyboard来搭建所有界面了,于是我 也追随时尚,直接开始使用storyboard.(不料在涉及到页面跳转的时候,遇到的问题是:点击后没有任何反应)众所周知 ...
- C++ 中dynamic_cast<>的用法
/*这是从网上摘下的样例,主要讲述了 dynamic_cast<> 的用法.*/ /* 作用:将一个基类对象指针(或引用)cast到继承类指针,dynamic_cast会依据基类指针是否真 ...
- SRM 577 Div II Level Two: EllysRoomAssignmentsDiv2
题目来源: http://community.topcoder.com/tc?module=ProblemDetail&rd=15497&pm=12521 这个问题要注意的就是只需要直 ...
- C++:互斥量C++实现,内存调试,自动锁
/*互斥量C++实现+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/ class CMutex { public: C ...