Delphi 自定义窗口过程WinProc

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.
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.

原文地址:https://www.cnblogs.com/YiShen/p/9725165.html

时间: 2024-08-04 20:19:40

Delphi 自定义窗口过程WinProc的相关文章

Delphi自定义窗口过程WinProc

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 = D

windows 窗口过程 线程消息队列

message loop window procedure message loop: a for loop in thread or winmain  GetMessage, TranslateMessage, and DispatchMessage GetMessage: get the message DispatchMessage: Dispatches a message to a window procedure. ----- winproc WindowProc callback

动态创建窗口,并重写窗口过程

在窗口过程中处理滚动条消息 WNDPROC pSubclassOldEditProc; INT iVScrollPos = 0, iVTextPos = 0; LRESULT winProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { //获取新设置的窗口属性,就是获取CNewEdit的指针,方便后面访问m_lOldProc这个成员 if (message == WM_VSCROLL) { //TRACE( _T("%d\n&

cas sso单点登录系列4_cas-server登录页面自定义修改过程(jsp页面修改)

转:http://blog.csdn.net/ae6623/article/details/8861065 SSO单点登录系列4:cas-server登录页面自定义修改过程,全新DIY. 目标: 下面是正文: 打开cas的默认首页,映入眼帘的是满眼的中文and英文混杂体,作为一名合格的用户,我表示很不开心. 于是,打开 Nodepad++,寻找C:\tomcat7\webapps\casServer\WEB-INF\view\jsp\default\ui \casLoginView.jsp这个页

【Android实战】记录自学自定义GifView过程,能同时支持gif和其他图片!【实用篇】

之前写了一篇博客,<[Android实战]记录自学自定义GifView过程,详解属性那些事![学习篇]> 关于自定义GifView的,详细讲解了学习过程及遇到的一些类的解释,然后完成了一个项目,能通过在xml加入自定义 view (MyGifView)中加入自定义属性(my:gif_src = "@drawable/coffee"),达到播放gif图片的效果. 但是,有几个问题 1.gif_src 属性只支持 gif 图,并不支持其他类型的图片 2.只支持默认的引用图片,不

QT笔记之自定义窗口拖拽移动

1.QT自定义标题栏,拖拽标题栏移动窗口(只能拖拽标题,其他位置无法拖拽) 方法一: 转载:http://blog.sina.com.cn/s/blog_4ba5b45e0102e83h.html .h文件中 1 //自己重新实现拖动操作 2 protected: 3 4 void mouseMoveEvent ( QMouseEvent * event ); 5 6 void mousePressEvent ( QMouseEvent * event ); 7 8 void mouseRele

QT自定义窗口(模拟MainWindow)

在这里自定义窗口是通过继承于QFrame,内部分为上下两部分,上半部分就是标题栏,下面是窗口的具体内容.上下两部分通过布局固定位置.最后窗口的各种鼠标拖动,缩放,等操作通过添加鼠标事件来完成一个窗口的常规功能.当然,这个标题栏可以通过布局的改变把它放到任意地方. 下面是实现的代码: #pragma once #ifndef CUSTOMWIDGET_H_ #define CUSTOMWIDGET_H_ #include <QtWidgets/QFrame> class QToolButton;

WPF自定义窗口基类

WPF自定义窗口基类时,窗口基类只定义.cs文件,xaml文件不定义.继承自定义窗口的类xaml文件的根节点就不再是<Window>,而是自定义窗口类名(若自定义窗口与继承者不在同一个命名空间,还得加上命名空间),继承自定义窗口类后台代码也得修改为继承自自定义窗口exp: //继承Window类的自定义窗口类 namespace WPF_Study.Entity { using System.Windows; public class WindowBase:Window { private c

sharepoint 2013基于AD的Form表单登录(四)——开发自定义登录过程需要引用文件路径。

1.Microsoft.IdentityModel.dll 位置 %ProgramFiles%\ReferenceAssemblies\Windows Identity Foundation\v3.5 2.Microsoft.SharePoint.IdentityModel.dll位置 C:\Windows\Microsoft.NET\assembly\GAC_MSIL\Microsoft.SharePoint.IdentityModel\v4.0_15.0.0.0__71e9bce111e94