子类化

bafhv04 2010-02-08 11:38:32

unit Main;

interface

uses
SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
Forms, Dialogs, StdCtrls;

type
TMainForm = class(TForm)
SendBtn: TButton;
PostBtn: TButton;
procedure SendBtnClick(Sender: TObject);
procedure PostBtnClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
OldWndProc: Pointer;
WndProcPtr: Pointer;
procedure WndMethod(var Msg: TMessage);
procedure HandleAppMessage(var Msg: TMsg; var Handled: Boolean);
end;

var
MainForm: TMainForm;

implementation

{$R *.DFM}

uses ScWndPrc;


procedure TMainForm.HandleAppMessage(var Msg: TMsg; var Handled: Boolean);
{ OnMessage handler for Application object. }
begin
if Msg.Message = DDGM_FOOMSG then
{ if it's the user-defined message, then alert the user. }
ShowMessage(Format('Message seen by OnMessage! Value is: $%x',
[Msg.Message]));
end;

procedure TMainForm.WndMethod(var Msg: TMessage);
begin
if Msg.Msg = DDGM_FOOMSG then
{ if it's the user-defined message, then alert the user. }
ShowMessage(Format('Message seen by WndMethod! Value is: $%x', [Msg.Msg]));
with Msg do
{ Pass message on to old window procedure. }
Result := CallWindowProc(OldWndProc, Application.Handle, Msg, wParam,
lParam);
end;

procedure TMainForm.SendBtnClick(Sender: TObject);
begin
SendMessage(Application.Handle, DDGM_FOOMSG, 0, 0);
end;

procedure TMainForm.PostBtnClick(Sender: TObject);
begin
PostMessage(Application.Handle, DDGM_FOOMSG, 0, 0);
end;

procedure TMainForm.FormCreate(Sender: TObject);
begin
Application.OnMessage := HandleAppMessage; // set OnMessage handler
WndProcPtr := MakeObjectInstance(WndMethod); // make window proc
{ Set window procedure of application window. }
OldWndProc := Pointer(SetWindowLong(Application.Handle, GWL_WNDPROC,
Integer(WndProcPtr)));
end;

procedure TMainForm.FormDestroy(Sender: TObject);
begin
{ Restore old window procedure for Application window }
SetWindowLong(Application.Handle, GWL_WNDPROC, Longint(OldWndProc));
{ Free our user-created window procedure }
FreeObjectInstance(WndProcPtr);
end;

end.


为什么 Application.OnMessage 只能看到由POSTMESSAGE 发来的消息.而SENDMESSAGE 不行.
...全文
70 2 打赏 收藏 举报
写回复
2 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
kfcoffe 2010-02-08
  • 打赏
  • 举报
回复
SendMessage 发送的消息是没送到消息队列中去地! 而是直接调用的目标窗口的wndproc窗口处理函数
等处理完了才返回!
而poseMessage则发送到消息队列中去。立即返回,不管处不处理。。。。。。。。。。
regersubadm 2010-02-08
  • 打赏
  • 举报
回复
SendMessage在消息循环的GetMessage的时候就已经被处理了,直接调到相应的消息处理过程里面去了,不会再经过Application.OnMessage了。
相关推荐
发帖
Windows SDK/API

1177

社区成员

Delphi Windows SDK/API
社区管理员
  • Windows SDK/API社区
加入社区
帖子事件
创建了帖子
2010-02-08 11:38
社区公告
暂无公告