利用nativewindow监视wndproc消息(好像是一个字典,没搞明白)

朝闻道 朝闻道     2022-08-01     348

关键词:

http://blog.csdn.net/lovefootball/article/details/1784882

在写Windows应用程序的时候,经常会碰到需要修改例如MessageBox或者FileDialog的外观
此时我们需要监视 WndProc的消息
当然也可以直接调用API实现,具体方法请参考
http://www.codeproject.com/csharp/GetSaveFileName.asp?df=100&forumid=96342&exp=0&select=1950454
主要代码如下

 

[c-sharp:nogutter] view plaincopy
 
  1. using System; 
  2. using System.Runtime.InteropServices; 
  3. using System.Windows.Forms; 
  4. using System.Collections.Generic; 
  5.  
  6. namespace testApplication1 
  7.     public delegate void HookWndProcHandler(ref Message m); 
  8.  
  9.     public class HookWndProc 
  10.     { 
  11.         private Dictionary<Control, NativeWindow> nativeWindows = new Dictionary<Control, NativeWindow>(); 
  12.  
  13.         public event HookWndProcHandler WndProcEvent; 
  14.  
  15.         public void BeginHookProc(Control control) 
  16.         { 
  17.             if(nativeWindows.ContainsKey(control))  
  18.             { 
  19.                 return; 
  20.             } 
  21.              
  22.             nativeWindows.Add(control, new HookNativeWindow(this, control)); 
  23.         } 
  24.  
  25.         public void EndHookProc(Control control) 
  26.         { 
  27.             if(!nativeWindows.ContainsKey(control))  
  28.             { 
  29.                 return; 
  30.             } 
  31.  
  32.             NativeWindow window = nativeWindows[control]; 
  33.             nativeWindows.Remove(control); 
  34.             window.ReleaseHandle(); 
  35.             window = null; 
  36.         } 
  37.  
  38.         protected virtual void WndProc(ref Message m)  
  39.         { 
  40.             FireWndProcEvent(ref m); 
  41.         } 
  42.  
  43.         protected void FireWndProcEvent(ref Message m)  
  44.         { 
  45.             if (WndProcEvent != null) 
  46.             { 
  47.                 WndProcEvent(ref m); 
  48.             } 
  49.         } 
  50.         #region NativeWindow 
  51.  
  52.         protected class HookNativeWindow : NativeWindow 
  53.         { 
  54.             private HookWndProc hookWndProc; 
  55.  
  56.             public HookNativeWindow(HookWndProc hookWndProc, Control control) 
  57.             { 
  58.                 this.hookWndProc = hookWndProc; 
  59.  
  60.                 if (control.IsHandleCreated) 
  61.                 { 
  62.                     AssignHandle(control.Handle); 
  63.                 } 
  64.                 else 
  65.                 { 
  66.                     control.HandleCreated += new EventHandler(OnControlHandleCreated); 
  67.                 } 
  68.  
  69.                 control.HandleDestroyed += new EventHandler(OnControlHandleDestroyed); 
  70.             } 
  71.  
  72.             private void OnControlHandleCreated(object sender, EventArgs e) 
  73.             { 
  74.                 AssignHandle(((Control)sender).Handle); 
  75.             } 
  76.  
  77.             private void OnControlHandleDestroyed(object sender, EventArgs e) 
  78.             { 
  79.                 ReleaseHandle(); 
  80.             } 
  81.  
  82.             [System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name ="FullTrust")] 
  83.             protected override void WndProc(ref Message m) 
  84.             { 
  85.                 hookWndProc.WndProc(ref m); 
  86.                 base.WndProc(ref m); 
  87.             } 
  88.         } 
  89.         #endregion 
  90.     } 
[c-sharp] view plain copy
 
  1. using System;  
  2. using System.Runtime.InteropServices;  
  3. using System.Windows.Forms;  
  4. using System.Collections.Generic;  
  5.   
  6. namespace testApplication1  
  7. {  
  8.     public delegate void HookWndProcHandler(ref Message m);  
  9.   
  10.     public class HookWndProc  
  11.     {  
  12.         private Dictionary<Control, NativeWindow> nativeWindows = new Dictionary<Control, NativeWindow>();  
  13.   
  14.         public event HookWndProcHandler WndProcEvent;  
  15.   
  16.         public void BeginHookProc(Control control)  
  17.         {  
  18.             if(nativeWindows.ContainsKey(control))   
  19.             {  
  20.                 return;  
  21.             }  
  22.               
  23.             nativeWindows.Add(control, new HookNativeWindow(this, control));  
  24.         }  
  25.   
  26.         public void EndHookProc(Control control)  
  27.         {  
  28.             if(!nativeWindows.ContainsKey(control))   
  29.             {  
  30.                 return;  
  31.             }  
  32.   
  33.             NativeWindow window = nativeWindows[control];  
  34.             nativeWindows.Remove(control);  
  35.             window.ReleaseHandle();  
  36.             window = null;  
  37.         }  
  38.   
  39.         protected virtual void WndProc(ref Message m)   
  40.         {  
  41.             FireWndProcEvent(ref m);  
  42.         }  
  43.   
  44.         protected void FireWndProcEvent(ref Message m)   
  45.         {  
  46.             if (WndProcEvent != null)  
  47.             {  
  48.                 WndProcEvent(ref m);  
  49.             }  
  50.         }  
  51.  
  52.         #region NativeWindow  
  53.   
  54.         protected class HookNativeWindow : NativeWindow  
  55.         {  
  56.             private HookWndProc hookWndProc;  
  57.   
  58.             public HookNativeWindow(HookWndProc hookWndProc, Control control)  
  59.             {  
  60.                 this.hookWndProc = hookWndProc;  
  61.   
  62.                 if (control.IsHandleCreated)  
  63.                 {  
  64.                     AssignHandle(control.Handle);  
  65.                 }  
  66.                 else  
  67.                 {  
  68.                     control.HandleCreated += new EventHandler(OnControlHandleCreated);  
  69.                 }  
  70.   
  71.                 control.HandleDestroyed += new EventHandler(OnControlHandleDestroyed);  
  72.             }  
  73.   
  74.             private void OnControlHandleCreated(object sender, EventArgs e)  
  75.             {  
  76.                 AssignHandle(((Control)sender).Handle);  
  77.             }  
  78.   
  79.             private void OnControlHandleDestroyed(object sender, EventArgs e)  
  80.             {  
  81.                 ReleaseHandle();  
  82.             }  
  83.   
  84.             [System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name = "FullTrust")]  
  85.             protected override void WndProc(ref Message m)  
  86.             {  
  87.                 hookWndProc.WndProc(ref m);  
  88.                 base.WndProc(ref m);  
  89.             }  
  90.         }  
  91.  
  92.         #endregion  
  93.     }  
  94. }  



调用方法,以更改MessageBox的OK按钮文本为例

 

 

 

技术分享
技术分享            HookWndProc hookWndProc = new HookWndProc();
技术分享            hookWndProc.WndProcEvent += new HookWndProcHandler(hookWndProc_WndProcEvent);
技术分享            hookWndProc.BeginHookProc(this);
技术分享            MessageBox.Show("MSG APP", "MessageBoxCaption", MessageBoxButtons.OKCancel);
技术分享            hookWndProc.EndHookProc(this);
技术分享
技术分享private void hookWndProc_WndProcEvent(ref Message m)
技术分享技术分享        ...{
技术分享            IntPtr wnd = FindWindow(null, "MessageBoxCaption");
技术分享
技术分享            if (wnd != IntPtr.Zero)
技术分享技术分享            ...{
技术分享                SetDlgItemText(wnd, 1, "需要修改的文本");
技术分享            }
技术分享        }
技术分享        [DllImport("user32.dll", EntryPoint = "FindWindow", CharSet = CharSet.Auto)]
技术分享        private extern static IntPtr FindWindow(string lpClassName, string lpWindowName);
技术分享
技术分享        [DllImport("user32.dll")]
技术分享        public static extern IntPtr SetDlgItemText(IntPtr hwnd, int id, string caption);

 


也就是说在WndProcEvent事件里面你可以写上你所需要做的事情

如果需要修改FileDialog的外观
则需要在WndProcEvent事件里面写上如下代码

技术分享if (m.Msg == WM_ENTERIDLE)
技术分享技术分享...{
技术分享    uint dialogHandle = (uint)m.LParam;
技术分享    uint listviewHandle = FindWindowEx(dialogHandle, 0, "SHELLDLL_DefView", "");
技术分享    if (listviewHandle != 0 && listviewHandle != lastListViewHandle)
技术分享技术分享    ...{
技术分享        SendMessage(listviewHandle, WM_COMMAND, (uint)View, 0);
技术分享}
技术分享lastListViewHandle = listviewHandle;
技术分享
技术分享
技术分享
技术分享技术分享    /**//// <summary>
技术分享    /// FileListViewType
技术分享    /// </summary>
技术分享    public enum FileListView
技术分享技术分享    ...{
技术分享        Icons = 0x7029,
技术分享        SmallIcons = 0x702a,
技术分享        List = 0x702b,
技术分享        Details = 0x702c,
技术分享        Thumbnails = 0x7031,
技术分享        XpThumbnails = 0x702d
技术分享    }
技术分享
技术分享
技术分享技术分享        /**//// <summary>
技术分享        /// win message : command
技术分享        /// </summary>
技术分享        private const uint WM_COMMAND = 0x0111;
技术分享
技术分享技术分享        /**//// <summary>
技术分享        /// win message : enter idle
技术分享        /// </summary>
技术分享        private const uint WM_ENTERIDLE = 0x0121;
技术分享
技术分享技术分享        /**//// <summary>
技术分享        /// listview type
技术分享        /// </summary>
技术分享        private FileListView view = FileListView.Thumbnails;
技术分享
技术分享技术分享        /**//// <summary>
技术分享        /// dialog handle
技术分享        /// </summary>
技术分享        private uint lastListViewHandle = 0;
技术分享
技术分享技术分享DllImports
#region DllImports
技术分享
技术分享        [DllImport("user32.dll", EntryPoint="SendMessageA", CallingConvention=CallingConvention.StdCall,CharSet=CharSet.Ansi)] 
技术分享        private static extern uint SendMessage(uint Hdc, uint Msg_Const, uint wParam, uint lParam);
技术分享
技术分享        [DllImport("user32.dll", EntryPoint="FindWindowExA", CallingConvention=CallingConvention.StdCall,CharSet=CharSet.Ansi)] 
技术分享        private static extern uint FindWindowEx(uint hwndParent, uint hwndChildAfter, string lpszClass,string lpszWindow);
技术分享
技术分享        #endregion

 

 

 SetDlgItemText(wnd, 1, "需要修改的文本");

private const int IDOK = 1;
private const int IDCANCEL = 2;
private const int IDABORT = 3;
private const int IDRETRY = 4;
private const int IDIGNORE = 5;
private const int IDYES = 6;
private const int IDNO = 7;

 

 

 

欢迎转载,请注明出处~~

http://blog.csdn.net/jiangxinyu/article/details/8080409

c#重写wndproc拦截发送系统消息+windows消息常量值

C#重写WndProc拦截发送系统消息+windows消息常量值(1)#region截获消息///截获消息处理XP不能关机问题protectedoverridevoidWndProc(refMessagemessage){switch(message.Msg){caseWM_QUERYENDSESSION:isClosed=true;break;}base.WndProc(refmessage); 查看详情

wndproc和hook区别

 1)WndProc函数作用:主要在程序中拦截并处理系统消息和自定义消息       比如:windows程序会产生很多消息,比如你单击鼠标,移动窗口都会产生消息。这个函数就是默认的消息处理函数。你可以重... 查看详情

在 wndproc 和 MSG 中处理窗口消息,有啥区别?

】在wndproc和MSG中处理窗口消息,有啥区别?【英文标题】:HandlingwindowmessagesinwndprocvsMSG,what\'sthedifference?在wndproc和MSG中处理窗口消息,有什么区别?【发布时间】:2019-09-2217:21:49【问题描述】:我注意到来自winuser.h的MSG(它是typ... 查看详情

如何在 WPF 中处理 WndProc 消息?

】如何在WPF中处理WndProc消息?【英文标题】:HowtohandleWndProcmessagesinWPF?【发布时间】:2010-10-1201:58:00【问题描述】:在Windows窗体中,我只需覆盖WndProc,并在消息进入时开始处理。谁能告诉我一个如何在WPF中实现相同目标的示例... 查看详情

尝试读取或写入受保护的内存。这通常指示其他内存已损坏

...Proc,IntPtrhWnd,Int32msg,IntPtrwParam,IntPtrlParam)在System.Windows.Forms.NativeWindow.DefWndProc(Message&m)在System.Windows.Forms.Control.DefWndProc(Message&m)在System.Windows.Forms.Control.WndProc(Message&m)在System.Windows.Forms.ComboBox.WndProc(Message&m)在System.Windows.Forms.Control.Contr... 查看详情

tcontrol的消息覆盖函数大全(15个wm_函数和17个cm_函数,它的wndproc就处理鼠标与键盘消息)

注意,这些函数只有Private一种形式(也就是不允许覆盖,但仍在动态表格中):TControl=class(TComponent)private//15个私有消息处理,大多是鼠标消息。注意,消息函数大多只是一个中介,且TWinControl并不重写。procedureWMNCLButtonDown(varMess... 查看详情

defwndproc/wndproc/imessagefilter的区别(代码片段)

...程序进行的,但当没有对应的事件时通常的做法是声明DefWndProc或者WndProc或者IMessageFilter,经常在网上看见有文章将三者并列,那么它们有什么区别呢?DefWndProc和WndProc都是继承自Control类中的虚方法,原型如下:1:protectedoverridevoid... 查看详情

twincontrol的消息覆盖函数大全(41个wm_函数和31个cm_函数,它的wndproc就处理鼠标(转发)键盘(取消拖动)焦点和wm_nchittest一共4类消息)

注意,这些函数只有Private一种形式(也就是不允许覆盖,但仍在动态表格中):TWinControl=class(TControl)private//41个windows消息,几乎全部消息都是私有函数(因为不需要别人来调用)。很多都是覆盖消息,也有少部分是首次出现。//... 查看详情

Win32 ListBox WNDPROC 从未调用过

】Win32ListBoxWNDPROC从未调用过【英文标题】:Win32ListBoxWNDPROCnevercalled【发布时间】:2012-11-1119:13:01【问题描述】:我正在围绕Win32控件/对话框/窗口编写一个相当简单的包装器。我遇到的问题是ListBox和ComboBox的行为似乎完全不同。... 查看详情

nativewindow_01

1、  “{$O-}”关闭优化  “{$O-}”打开优化2、unitNativeWindow;interfaceusesWindows,Messages,SysUtils;procedureCreateWindow;implementationfunctionProcWindow(_hWnd:HWND;_uMsg:UINT;_wParam:WPARAM;_lParam:LPARAM):longin 查看详情

利用cron监视后台进程状态

    利用cron监视后台进程状态http://blog.csdn.net/dyx810601/article/details/729677581.利用cron监视后台进程状态,如果进程死掉或服务器重启后自动拉起进程。目的:Linux 下服务器程序会因为各种原因dump掉,就会影响用户... 查看详情

钩子注入的原理机制

...子,系统会将其加载入使用user32的进程中,因而它也可被利用为无进程木马的进程注入手段。木马编写者首先把一个实际为木马主体的dll文件载入内存,然后通过“线程注射”技术将其注入其他进程的内存空间,最后这个dll里的... 查看详情

你能得到 NativeWindow 最小化的事件吗?

】你能得到NativeWindow最小化的事件吗?【英文标题】:CanyougettheNativeWindowminimizedevent?【发布时间】:2012-01-3100:35:58【问题描述】:在AIR中使用NativeWindow时,每次最小化/未最小化窗口时,您能否获得一个事件?我尝试与DisplayStateCh... 查看详情

WndProc 的替代方案,因为在从组件派生的类中需要?

】WndProc的替代方案,因为在从组件派生的类中需要?【英文标题】:AlternativetoWndProcbecauseneededinClassderivedfromComponent?【发布时间】:2013-03-1311:36:21【问题描述】:我正在编写一个从Component派生的自定义控件。在此控件中,我需要... 查看详情

duilib消息处理过程

...t;Create()调用CWindowWnd->RegisterWindowClass()注册CWindowWnd->__WndProc()为本界面窗口消息处理函数 __WndProc()调用虚函数CWindowWnd->HandleMessage()处理消息WinImplBase继承CWindowWnd并重写了HandleMessage()处理消息该函数拦截WM_CREATE、WM_MOUSEMOVE... 查看详情

AIR - 设置 NativeWindow 的大小以包括系统镶边

】AIR-设置NativeWindow的大小以包括系统镶边【英文标题】:AIR-setsizeofNativeWindowtoincludesystemchrome【发布时间】:2012-02-1407:44:09【问题描述】:如何找出系统chrome的大小,以便我可以指定窗口大小以达到我想要的舞台大小?如果我的... 查看详情

openfire 监视器节 xml 消息

】openfire监视器节xml消息【英文标题】:openfiremonitorstanzaxmlmessage【发布时间】:2009-10-1812:22:59【问题描述】:在openfire管理控制面板页面中,有没有我可以用来监控xml节的插件。我知道可以从smack客户端监控它,但我想直接从服... 查看详情

配置 SQS 死信队列以在收到消息时引发云监视警报

】配置SQS死信队列以在收到消息时引发云监视警报【英文标题】:ConfigureSQSDeadletterQueuetoraiseacloudwatchalarmonreceivingamessage【发布时间】:2020-05-2908:42:27【问题描述】:我在AmazonSQS中使用死信队列。我希望每当队列收到新消息时,它... 查看详情