实例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消息; private
   { Private declarations }
 public
{ Public declarations }
//第二种消息处理方式;可能处理常量或变量消息;
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;
  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;
end;
 
procedure  TForm1.wmme(var message: TMessage);
begin
 ShowMessage(IntToStr(Handle)+ 'wmme'); //第一种消息处理方式
end;
 
procedure  TForm1.WndProc(var message: TMessage);
begin
   if message.Msg=WM_ME then //第二种消息处理方式
     ShowMessage(IntToStr(Handle)+ 'WndProc');
 inherited WndProc(Message); //这里inherited才会触发一次wmme消息;
end;
end.
 
实例2/////////////////////////////////////////////////////////////消息的广播方式/////////////////////////////////////////////////////////////////////////////
///////消息广播只能将消息传递到接收消息的主程序中,MDIChild窗体不能接收到广播消息;/////////
unit Unit1;
interface
uses
 Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls;
type
 TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
 private
    { Private declarations }
 public
    { Public declarations }
    procedure WndProc(var message:TMessage);override;
 end;
var
 Form1: TForm1;
 MyMessage: DWORD;
implementation
{$R *.dfm}
{ TForm1 }
procedure TForm1.WndProc(var message: TMessage);
begin
 if message.Msg=MyMessage then //第3步:重载Windows消息处理过程,处理接收到的MyMessage消息;
 ShowMessage(FloatToStr(Handle)+ 'MyMessage');
 inherited WndProc(Message);
end;
 
procedure TForm1.Button1Click(Sender: TObject);
begin
 SendMessage(HWND_BROADCAST,MyMessage,0,0); //第2步:广播MyMessage消息;
end;
/////////预先注册好Windows消息;第一种方法////////////////////
procedure TForm1.FormCreate(Sender: TObject);
begin
 //同一字符串,如'MyMessageMe'在不同的程序中调用RegisterWindowMessage注册消息时,返回的MyMessage结果是一样的。这样为在不同程序之间广播消息提供了可能。即不同程序注册消息时,必须注册相同的注符串。
    MyMessage:=RegisterWindowMessage('MyMessageMe'); //第1步:注册Windows消息,重点在于返回的值MyMessage
end;
/////////预先注册好Windows消息;第二种方法////////////////////
{ initialization
 MyMessage:=RegisterWindowMessage('MyMessageMe');
}
 
 
//BroadcastSystemMessage,可以在进程之间广播消息;
procedure TForm1.SendSMSMessage;
var
  SMSMessage:Cardinal;
  recipt:Cardinal;
begin
 try
  SMSMessage:=RegisterWindowMessage('SendSMSMessage');
  recipt:=BSM_ALLDESKTOPS; //所以桌面程序都可以接收
  BroadcastSystemMessage(BSF_POSTMESSAGE,@recipt,SMSMessage,0,0);
 except
 end;
end;
 
end.

delphi中Message消息的使用方法的更多相关文章

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

    实例1 unit Unit1; interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Fo ...

  2. delphi中Time消息的使用方法

    unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms ...

  3. Delphi中TStringList类常用属性方法详解

    TStrings是一个抽象类,在实际开发中,是除了基本类型外,应用得最多的. 常规的用法大家都知道,现在来讨论它的一些高级的用法. 先把要讨论的几个属性列出来: 1.CommaText 2.Delim ...

  4. Delphi中的消息截获(六种方法:Hook,SubClass,Override WndProc,Message Handler,RTTI,Form1.WindowProc:=@myfun)good

    Windows是一个基于消息驱动的系统,因此,在很多时候,我们需要截获一些消息然后自己进行处理.而VCL系统又有一些特定的消息.下面对我所了解的delphi环境中截获消息进行一些总结.      就个 ...

  5. Delphi中window消息截获的实现方式(2)

    Delphi是Borland公司提供的一种全新的WINDOWS编程开发工具.由于它采用了具有弹性的和可重用的面向对象Pascal(object-orientedpascal)语言,并有强大的数据库引擎 ...

  6. Delphi中的消息 (转载)

    消息是Windows发出的一个通知,它告诉应用程序某个事件发生了.在Delphi中,大多数情况下Windows的消息被封装在VCL的事件中,我们只需处理相应的VCL事件就可以了,但如果我们需要编写自己 ...

  7. Delphi中文件流的使用方法

    在Delphi中,所有流对象的基类为TStream类, 其中定义了所有流的共同属性和方法.TStream类中定义的属性介绍如下: 1.Size: 此属性以字节返回流中数据大小. 2.Position: ...

  8. delphi中使用自定义资源的方法

    如果要在delphi中使用自定义资源文件*.res文件,比如一个光标,此时可以采用下列步骤: 1,创建包含相应的资源文件,这里是创建一个包含自定义光标的res文件. 2,在主窗体的pas文件中加入编译 ...

  9. Floodlight 中创建消息对象的方法

            在 floodlight 中创建各种openflow message 和 action 等採用的是简单工厂方式.BasicFactory类(实现OFMessageFactory接口.) ...

随机推荐

  1. JS 百度地图导航

    上一篇文章中我们就简单的学习了HTML5 地理定位,那么今天告诉大家我在项目中遇到的一个问题吧,就是怎么实现点击一个按钮就可以调到百度地图,并且获取到你当前的位置,并且导航到指定的地方去. 不想看步骤 ...

  2. Install Java1.6

    声明:一下内容主要来自网络,本来在此基础上根据自己的成功经验做了一些浅陋的(但有价值的)备注说明和修改. (本人也是ubuntu 11.10版本,但是是64位的,因此安装的java也是64位版本.) ...

  3. uboot 各种烧写命令

    norflash 烧写 (7) Nor Flash指令 Nor Flash 的命令经常用于烧写数据到Nor Flash . flinfo  打印Flash存储器的信息,并列出所有Sector. fli ...

  4. android 总结(样式)—跑马灯 button的点击效果 RadioGroup 实现滑动的效果 button 下面有阴影 卡片样式

    <Button android:layout_width="wrap_content" android:layout_height="wrap_content&qu ...

  5. Apache+Tomcat构建Tomcat负载均衡集群

    一.环境介绍 二.安装后端服务器 三.安装前端Apache服务 四.配置Apache使用mod_jk模块实现代理及负载均衡 五.配置Apache基于mod_proxy模块实现代理及负载均衡 六.论坛安 ...

  6. Android 自动生成表格

    Layout.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:a ...

  7. Active Record: 資料庫遷移(Migration) (转)

    Active Record: 資料庫遷移(Migration) Programming today is a race between software engineers striving to b ...

  8. (转)jQuery Mobile 移动开发中的日期插件Mobiscroll 2.3 使用说明

    (原)http://www.cnblogs.com/hxling/archive/2012/12/12/2814207.html jQuery Mobile 移动开发中的日期插件Mobiscroll ...

  9. OD调试2

    通过视频学习,实现了一下简单的traceme爆破.这不是把它的序列号破译出来,只是识别了自己的号码,不算真正的爆破.(与期望有点差异) 先来看一下这款软件的逻辑结构. 先输入用户名  以及序列号 然后 ...

  10. javascript之小积累-.-添加form表单查询的enter键支持

    /*  * 列表查询的enter键支持  * author by 清风  */ function enterEvent() {   document.onkeydown = function(even ...