Delphi中的Windows消息处理

本文介绍了使用Delphi实现自定义消息处理的方法,包括在同一应用程序内的不同表单间发送消息的具体示例,以及如何在不同应用程序之间通过注册窗口消息来传递自定义消息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这里采用自定义消息的方法。

注意:delphi中自定义消息值范围为:WM_USER到$7FFF之间。

 

1.应用程序中的消息(在同一个应用程序的不同Form间进行消息处理)示例:

Form1:

unit Unit1;

interface

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

const
  SX_MY_MESSAGE = WM_USER + 100;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

uses Unit2;

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
  Form2.Show
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  //Form2.Perform(SX_MY_MESSAGE, 0, 0);
  //SendMessage(Form2.Handle, SX_MY_MESSAGE, 0, 0);
  PostMessage(Form2.Handle, SX_MY_MESSAGE, 0, 0);
end;

end.

Form2:

unit Unit2;

interface

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

const
  SX_MY_MESSAGE = WM_USER + 100;

type
  TForm2 = class(TForm)
  private
    { Private declarations }
    procedure SXMyMessage(var Msg : TMessage); message SX_MY_MESSAGE;
  public
    { Public declarations }
  end;

var
  Form2: TForm2;


implementation

{$R *.dfm}

procedure TForm2.SXMyMessage(var Msg : TMessage);
begin
  MessageDlg('Receive My Message!', mtInformation, [mbOK], 0);
end;

end.

 

2.应用程序间的消息

利用RegisterWindowMessage在不同程序窗体间传递消息

转自:http://blog.youkuaiyun.com/xiaowenjie/archive/2007/03/10/1525870.aspx

 

3.广播消息

TWinControl的派生类可以广播一条消息记录给他拥有的控件,方法名为Broadcast()。

例如:

var

  M : TMessage;

begin

  with M do

  begin

    Message := 0;

    wParam := 0;

    lParam := 0;

    Result := 0;

  end;

  Panel1.Breadcast(M);

end;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值