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

Delphi自定义窗口过程WinProc

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

interface
uses Forms, Messages;

const
    DDGM_FOOMSG = WM_USER;  //自定义消息

implementation

uses windows,sysutils,Dialogs;

var
    WProc : Pointer;

function NewWndProc(handle: hWnd; msg,wParam,lParam: LongInt): LongInt ;
stdcall;
begin
    if msg = DDGM_FOOMSG then
        ShowMessage(Format('收到自定义消息 $%x',[msg]));

    result := CallWindowProc(WProc,handle, msg,wParam,lParam);
end;

initialization
    WProc := Pointer(SetWindowLong(application.Handle,GWL_WNDPROC
        ,integer(@NewWndProc)));                                             
end.

//发送消息
SendMessage(application.Handle,DDGM_FOOMSG,0,0);


 

unit UnitSendVsPost;

interface

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

type
  TFrmSendPostMsg = class(TForm)
    btnSendMessage: TButton;
    btnPostMessage: TButton;
    procedure btnSendMessageClick(Sender: TObject);
    procedure btnPostMessageClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    { Private declarations }
    OldWndProc : Pointer;
    WndProcPtr : Pointer;
    procedure WndMethod(var msg: TMessage);
    procedure HandleAppMessage(var msg : TMsg; var handled : boolean);
  public
    { Public declarations }
  end;

var
  FrmSendPostMsg: TFrmSendPostMsg;

implementation

{$R *.dfm}
uses
    ScWndProc;

procedure TFrmSendPostMsg.WndMethod(var msg: TMessage);
begin
     if msg.Msg = DDGM_FOOMSG  then
    begin
        ShowMessage(Format('Message seen by WndMethod! value is: $%x',[msg.Msg]));
        with msg do
            result := CallWindowProc(OldWndProc,Application.Handle,msg,WParam,LParam);
    end;
end;

procedure TFrmSendPostMsg.HandleAppMessage(var msg : TMsg; var handled : boolean);
begin
    if msg.message = DDGM_FOOMSG  then
    begin
        ShowMessage(Format('Message seen by OnMessage! value is: $%x',[msg.message]));
        //handled := true;
    end;
end;

procedure TFrmSendPostMsg.btnSendMessageClick(Sender: TObject);
begin
    //发送消息
    sendmessage(application.Handle,DDGM_FOOMSG,0,0);
end;

procedure TFrmSendPostMsg.btnPostMessageClick(Sender: TObject);
begin
     postmessage(application.Handle,DDGM_FOOMSG,0,0);
end;

procedure TFrmSendPostMsg.FormCreate(Sender: TObject);
begin
    application.OnMessage := HandleAppMessage;        // set OnMessage handler
    WndProcPtr := MakeObjectInstance(WndMethod);
    OldWndProc := Pointer(SetWindowLong(Application.Handle,GWL_WNDPROC,Integer(WndProcPtr)));
end;

procedure TFrmSendPostMsg.FormDestroy(Sender: TObject);
begin
    SetWindowLong(Application.Handle,GWL_WNDPROC,LongInt(OldWndProc));
    FreeObjectInstance(WndProcPtr);
end;

end.

 


unit UnitHook;

interface

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

type
  TFrmHookWin = class(TForm)
    lstMsg: TListBox;
    btnSendMsg: TButton;
    procedure btnSendMsgClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    { Private declarations }
    function AppWindowHook(var message: TMessage): boolean;
  public
    { Public declarations }
  end;

var
  FrmHookWin: TFrmHookWin;

implementation

{$R *.dfm}

function TFrmHookWin.AppWindowHook(var message: TMessage): boolean;
const
    strLog = 'MsgID: $%x, WParam: $%x, LParam: $%x';
begin
    Result := true;
    with message do
        lstMsg.Items.Add(Format(strLog,[Msg,WParam,LParam]));
end;
procedure TFrmHookWin.btnSendMsgClick(Sender: TObject);
begin
    SendMessage(application.Handle,WM_NULL,0,0);
end;

procedure TFrmHookWin.FormCreate(Sender: TObject);
begin
     Application.HookMainWindow(self.AppWindowHook);
end;

procedure TFrmHookWin.FormDestroy(Sender: TObject);
begin
     application.UnhookMainWindow(self.AppWindowHook);
end;

end.

 


鲜花

握手

雷人

路过

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

请发表评论

全部评论

专题导读
上一篇:
DELPHI删除指定目录下指定扩展名的文件发布时间:2022-07-18
下一篇:
delphi中报Left side cannot be assigned to错发布时间: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