Delphi 自定义窗口过程WinProc

1unit ScWndProc; 2 3interface 4uses Forms, Messages; 5 6const 7 DDGM_FOOMSG = WM_USER; //自定义消息 8 9implementation 10 11uses windows,sysutils,Dialogs; 12 13var 14 WProc : Pointer; 15 16function NewWndProc(handle: hWnd; msg,wParam,lParam: LongInt): LongInt ; 17stdcall; 18begin 19 if msg = DDGM_FOOMSG then 20 ShowMessage(Format('收到自定义消息 $%x',[msg])); 21 22 result := CallWindowProc(WProc,handle, msg,wParam,lParam); 23end; 24 25initialization 26 WProc := Pointer(SetWindowLong(application.Handle,GWL_WNDPROC 27 ,integer(@NewWndProc))); 28end. 29 30unit UnitSendVsPost; 31 32interface 33 34uses 35 Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, 36 Dialogs, StdCtrls; 37 38type 39 TFrmSendPostMsg = class(TForm) 40 btnSendMessage: TButton; 41 btnPostMessage: TButton; 42 procedure btnSendMessageClick(Sender: TObject); 43 procedure btnPostMessageClick(Sender: TObject); 44 procedure FormCreate(Sender: TObject); 45 procedure FormDestroy(Sender: TObject); 46 private 47 { Private declarations } 48 OldWndProc : Pointer; 49 WndProcPtr : Pointer; 50 procedure WndMethod(var msg: TMessage); 51 procedure HandleAppMessage(var msg : TMsg; var handled : boolean); 52 public 53 { Public declarations } 54 end; 55 56var 57 FrmSendPostMsg: TFrmSendPostMsg; 58 59implementation 60 61{$R *.dfm} 62uses 63 ScWndProc; 64 65procedure TFrmSendPostMsg.WndMethod(var msg: TMessage); 66begin 67 if msg.Msg = DDGM_FOOMSG then 68 begin 69 ShowMessage(Format('Message seen by WndMethod! value is: $%x',[msg.Msg])); 70 with msg do 71 result := CallWindowProc(OldWndProc,Application.Handle,msg,WParam,LParam); 72 end; 73end; 74 75procedure TFrmSendPostMsg.HandleAppMessage(var msg : TMsg; var handled : boolean); 76begin 77 if msg.message = DDGM_FOOMSG then 78 begin 79 ShowMessage(Format('Message seen by OnMessage! value is: $%x',[msg.message])); 80 //handled := true; 81 end; 82end; 83 84procedure TFrmSendPostMsg.btnSendMessageClick(Sender: TObject); 85begin 86 //发送消息 87 sendmessage(application.Handle,DDGM_FOOMSG,0,0); 88end; 89 90procedure TFrmSendPostMsg.btnPostMessageClick(Sender: TObject); 91begin 92 postmessage(application.Handle,DDGM_FOOMSG,0,0); 93end; 94 95procedure TFrmSendPostMsg.FormCreate(Sender: TObject); 96begin 97 application.OnMessage := HandleAppMessage; // set OnMessage handler 98 WndProcPtr := MakeObjectInstance(WndMethod); 99 OldWndProc := Pointer(SetWindowLong(Application.Handle,GWL_WNDPROC,Integer(WndProcPtr))); 100end; 101 102procedure TFrmSendPostMsg.FormDestroy(Sender: TObject); 103begin 104 SetWindowLong(Application.Handle,GWL_WNDPROC,LongInt(OldWndProc)); 105 FreeObjectInstance(WndProcPtr); 106end; 107 108end. 109 110unit UnitHook; 111 112interface 113 114uses 115 Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, 116 Dialogs, StdCtrls; 117 118type 119 TFrmHookWin = class(TForm) 120 lstMsg: TListBox; 121 btnSendMsg: TButton; 122 procedure btnSendMsgClick(Sender: TObject); 123 procedure FormCreate(Sender: TObject); 124 procedure FormDestroy(Sender: TObject); 125 private 126 { Private declarations } 127 function AppWindowHook(var message: TMessage): boolean; 128 public 129 { Public declarations } 130 end; 131 132var 133 FrmHookWin: TFrmHookWin; 134 135implementation 136 137{$R *.dfm} 138 139function TFrmHookWin.AppWindowHook(var message: TMessage): boolean; 140const 141 strLog = 'MsgID: $%x, WParam: $%x, LParam: $%x'; 142begin 143 Result := true; 144 with message do 145 lstMsg.Items.Add(Format(strLog,[Msg,WParam,LParam])); 146end; 147procedure TFrmHookWin.btnSendMsgClick(Sender: TObject); 148begin 149 SendMessage(application.Handle,WM_NULL,0,0); 150end; 151 152procedure TFrmHookWin.FormCreate(Sender: TObject); 153begin 154 Application.HookMainWindow(self.AppWindowHook); 155end; 156 157procedure TFrmHookWin.FormDestroy(Sender: TObject); 158begin 159 application.UnhookMainWindow(self.AppWindowHook); 160end; 161 162end.
点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

皕杰报表之UUID

​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

2020年前端实用代码段,为你的工作保驾护航

有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )

Delphi 自定义窗口过程WinProc - HelloWorld