• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Delphi中Message消息的三种使用方法(覆盖WndProc,覆盖消息函数,改写WMCommand) ...

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

实例1

unit Unit1;

interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls;

const
WM_ME=WM_USER+100; //自定义消息;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
//第一种消息处理方式; 只能处理常量消息;
procedure wmme(var message:TMessage);message WM_ME; //自定义消息处理过程,专门处理WM_ME消息
public
//第二种消息处理方式;可能处理常量或变量消息;
procedure WndProc(var message:TMessage);override; //重载窗口消息过程
//第三种消息处理方式
procedure WMCommand(var Message: TWMCommand); message WM_COMMAND; //命令消息处理过程
procedure WMSysCommand(var Msg:TWMSysCommand);message WM_SYSCOMMAND; //处理系统性消息;
end;
var
Form1: TForm1;
implementation
{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
SendMessage(Handle,WM_ME,0,0); // 发送消息WM_ME ; 消息先由WndProc处理,再交给wmme处理;
end;

procedure TForm1.WMSysCommand(var Msg: TWMSysCommand);
begin
//下面代码的作用是,用户如果点击了标题栏上的最小化和关闭按钮,则隐藏窗体。
if (Msg.CmdType=SC_MINIMIZE) or (Msg.CmdType=SC_CLOSE) then
begin
Self.Hide; // 拦截了系统消息(我猜不能拦截普通消息),而且不再执行DefaultHandler。测试,可以把DefaultHandler放在这句之前和这句之后,先后顺序很明显。
end else
DefaultHandler(Msg); // 这句的作用是继续处理其它消息;
end;

procedure TForm1.WMCommand(var Message: TWMCommand);
begin //第三种消息处理方式
if Message.NotifyCode = BN_CLICKED then // 我怀疑可以拦截所有的消息?
if FindControl(Message.Ctl) = Button1 then showmessage(‘点击了Button1’);
inherited; // 屏蔽这句话的话,Button1Click的内容就不再执行了
end;

procedure TForm1.WndProc(var message: TMessage);
begin
if message.Msg=WM_ME then // 第二种消息处理方式,我怀疑可以拦截所有的消息?
ShowMessage(IntToStr(Handle)+ 'WndProc');
inherited WndProc(Message); // 这里inherited才会触发一次wmme消息;所以先执行WndProc,后执行wmme消息
end;

procedure TForm1.wmme(var message: TMessage);
begin
ShowMessage(IntToStr(Handle)+ 'wmme'); // 第一种消息处理方式
end;

end.

===================================================================

unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs;

type
  TForm1 = class(TForm)
  private
    { Private declarations }
    procedure WndProc(var AMessage:TMessage);override;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.WndProc(var AMessage: TMessage);
begin
  if (AMessage.Msg=WM_SYSCOMMAND) and (AMessage.WParam=SC_CLOSE) then
    Exit
  else
    inherited;

end;

end.

 参考:http://www.cnblogs.com/key-ok/p/3417727.html


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
怎样在delphi中实现控件和窗体的拖拽发布时间:2022-07-18
下一篇:
delphiclientdataset判断某一行值是否存在发布时间:2022-07-18
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap