zoukankan      html  css  js  c++  java
  • 简单深刻:为控件创建MouseEnter和MouseLeave事件(覆盖WndProc,增加对消息的处理,真简单!)——连对CM_MOUSEENTER的消息处理都是颇有深意啊!

    其实很简单:

    unit Unit1; 
    
    interface 
    
    uses 
      Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, 
      StdCtrls; 
    
    type 
    
      TURLLabel = class(TLabel) 
        procedure WndProc(var Message : TMessage); override; 
      end; 
    
    type 
      TForm1 = class(TForm) 
        procedure FormCreate(Sender: TObject); 
      private 
        { Private declarations } 
      public 
        { Public declarations } 
      end; 
    
    var 
      Form1: TForm1; 
    
    implementation 
    
    {$R *.DFM} 
    
    { TURLLabel } 
    
    procedure TURLLabel.WndProc(var Message: TMessage); 
    begin 
      if (Message.Msg = CM_MOUSEENTER) then 
      begin 
        Font.Color := clBlue; 
        Font.Style := Font.Style + [fsUnderline]; 
      end; 
      if (Message.Msg = CM_MOUSELEAVE) then 
      begin 
        Font.Color := clWindowText; 
        Font.Style := Font.Style - [fsUnderline]; 
      end; 
      inherited WndProc(Message); 
    end; 
    
    procedure TForm1.FormCreate(Sender: TObject); 
    begin 
      with TURLLabel.Create(Self) do 
      begin 
        Parent := Self; 
        Left := 10; 
        Top := 10; 
        caption := 'www.delphi3000.com'; 
        Cursor := crHandPoint; 
      end; 
    end; 
    
    end. 

    转自http://www.delphi3000.com/articles/article_1050.asp?SK=

    --------------------------------------------------------------------------------------

    稍微分析一下,inherited WndProc(Message)会导致执行:

    procedure TControl.CMMouseEnter(var Message: TMessage);
    begin
      if FParent <> nil then
        FParent.Perform(CM_MOUSEENTER, 0, Longint(Self)); // 把控件自己的指针当参数传过去,即表明当前消息来自于哪个控件
    end;
    
    procedure TControl.CMMouseLeave(var Message: TMessage);
    begin
      if FParent <> nil then
        FParent.Perform(CM_MOUSELEAVE, 0, Longint(Self));
    end;

    即告诉父控件,有鼠标移入了。但是TWinControl没有CM_MOUSEENTER消息函数,所以就不了了之了。

    这个例子说明,TControl的许多行为都不仅仅属于自己,即使自己处理了,还必须通知一下TWinControl父控件(而且是向上回溯通知所有祖先控件),看它们有什么意见(就好象小孩做了事情,要报告父母一样)。

  • 相关阅读:
    程序基址,X64Dbg软件常用调试技巧查找系统函数调用位置执行到指定位置断点
    #pragma的常用方法讲解,为什么有了条件编译符号“DEBUG”还要来个Debugger.IsAttached
    JDK17Src0.java.base
    nmon的安装和使用
    64位下的相对指令地址X86指令格式(操作码列和指令列解释)
    内存中的程序剖析
    Linux I/O 原理和 Zerocopy 技术全面揭秘
    Ubuntu命令行的垃圾箱,回收站
    SecureCRT密钥链接阿里云
    HTTP API 认证授权术
  • 原文地址:https://www.cnblogs.com/findumars/p/5309515.html
Copyright © 2011-2022 走看看