利用NativeWindow监视WndProc消息(好像是一个字典,没搞明白)
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
主要代码如下
- using System;
- using System.Runtime.InteropServices;
- using System.Windows.Forms;
- using System.Collections.Generic;
- namespace testApplication1
- {
- public delegate void HookWndProcHandler(ref Message m);
- public class HookWndProc
- {
- private Dictionary<Control, NativeWindow> nativeWindows = new Dictionary<Control, NativeWindow>();
- public event HookWndProcHandler WndProcEvent;
- public void BeginHookProc(Control control)
- {
- if(nativeWindows.ContainsKey(control))
- {
- return;
- }
- nativeWindows.Add(control, new HookNativeWindow(this, control));
- }
- public void EndHookProc(Control control)
- {
- if(!nativeWindows.ContainsKey(control))
- {
- return;
- }
- NativeWindow window = nativeWindows[control];
- nativeWindows.Remove(control);
- window.ReleaseHandle();
- window = null;
- }
- protected virtual void WndProc(ref Message m)
- {
- FireWndProcEvent(ref m);
- }
- protected void FireWndProcEvent(ref Message m)
- {
- if (WndProcEvent != null)
- {
- WndProcEvent(ref m);
- }
- }
- #region NativeWindow
- protected class HookNativeWindow : NativeWindow
- {
- private HookWndProc hookWndProc;
- public HookNativeWindow(HookWndProc hookWndProc, Control control)
- {
- this.hookWndProc = hookWndProc;
- if (control.IsHandleCreated)
- {
- AssignHandle(control.Handle);
- }
- else
- {
- control.HandleCreated += new EventHandler(OnControlHandleCreated);
- }
- control.HandleDestroyed += new EventHandler(OnControlHandleDestroyed);
- }
- private void OnControlHandleCreated(object sender, EventArgs e)
- {
- AssignHandle(((Control)sender).Handle);
- }
- private void OnControlHandleDestroyed(object sender, EventArgs e)
- {
- ReleaseHandle();
- }
- [System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name ="FullTrust")]
- protected override void WndProc(ref Message m)
- {
- hookWndProc.WndProc(ref m);
- base.WndProc(ref m);
- }
- }
- #endregion
- }
- }
- using System;
- using System.Runtime.InteropServices;
- using System.Windows.Forms;
- using System.Collections.Generic;
- namespace testApplication1
- {
- public delegate void HookWndProcHandler(ref Message m);
- public class HookWndProc
- {
- private Dictionary<Control, NativeWindow> nativeWindows = new Dictionary<Control, NativeWindow>();
- public event HookWndProcHandler WndProcEvent;
- public void BeginHookProc(Control control)
- {
- if(nativeWindows.ContainsKey(control))
- {
- return;
- }
- nativeWindows.Add(control, new HookNativeWindow(this, control));
- }
- public void EndHookProc(Control control)
- {
- if(!nativeWindows.ContainsKey(control))
- {
- return;
- }
- NativeWindow window = nativeWindows[control];
- nativeWindows.Remove(control);
- window.ReleaseHandle();
- window = null;
- }
- protected virtual void WndProc(ref Message m)
- {
- FireWndProcEvent(ref m);
- }
- protected void FireWndProcEvent(ref Message m)
- {
- if (WndProcEvent != null)
- {
- WndProcEvent(ref m);
- }
- }
- #region NativeWindow
- protected class HookNativeWindow : NativeWindow
- {
- private HookWndProc hookWndProc;
- public HookNativeWindow(HookWndProc hookWndProc, Control control)
- {
- this.hookWndProc = hookWndProc;
- if (control.IsHandleCreated)
- {
- AssignHandle(control.Handle);
- }
- else
- {
- control.HandleCreated += new EventHandler(OnControlHandleCreated);
- }
- control.HandleDestroyed += new EventHandler(OnControlHandleDestroyed);
- }
- private void OnControlHandleCreated(object sender, EventArgs e)
- {
- AssignHandle(((Control)sender).Handle);
- }
- private void OnControlHandleDestroyed(object sender, EventArgs e)
- {
- ReleaseHandle();
- }
- [System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name = "FullTrust")]
- protected override void WndProc(ref Message m)
- {
- hookWndProc.WndProc(ref m);
- base.WndProc(ref m);
- }
- }
- #endregion
- }
- }
调用方法,以更改MessageBox的OK按钮文本为例























也就是说在WndProcEvent事件里面你可以写上你所需要做的事情
如果需要修改FileDialog的外观
则需要在WndProcEvent事件里面写上如下代码
























































#region DllImports








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
利用NativeWindow监视WndProc消息(好像是一个字典,没搞明白)的更多相关文章
- 利用Delphi监视注册表的变化
转帖:利用Delphi监视注册表的变化 2009-12-23 11:53:51 分类: 利用Delphi监视注册表的变化 我们在编写软件的时候,常常需要把一些信息保存到系统的注册表中.如果 ...
- WPF 利用HwndSource拦截Windows消息
WPF提供了一个HwndSource可以使你更快的实现处理Windows消息. 通过HwndSource.FromHwnd得到的HwndSource可以添加(AddHook)移除(Remove)Hoo ...
- php 利用activeMq+stomp实现消息队列
php 利用activeMq+stomp实现消息队列 一.activeMq概述 ActiveMQ 是Apache出品,最流行的,能力强劲的开源消息总线.ActiveMQ 是一个完全支持JMS1.1和J ...
- 如何利用wireshark对TCP消息进行分析
原文:https://www.cnblogs.com/studyofadeerlet/p/7485298.html 如何利用wireshark对TCP消息进行分析 (1) 几个概念介绍 1 seq ...
- 利用cron监视后台进程状态
利用cron监视后台进程状态 http://blog.csdn.net/dyx810601/article/details/72967758 1. 利用cron监视后台进程状态,如果进程死掉或服务器重 ...
- Java开发笔记(一百一十四)利用Socket传输文本消息
前面介绍了HTTP协议的网络通信,包括接口调用.文件下载和文件上传,这些功能固然已经覆盖了常见的联网操作,可是HTTP协议拥有专门的通信规则,这些规则一方面有利于维持正常的数据交互,另一方面不可避免地 ...
- 利用SignalR创建即时消息
1. 什么是SignalR? SignalR 是一个及时消息推送,它与.NET 的 WCF ,WebAPI类似 是客户端和服务器进行消息交换的一种工具 2.SignalR 的作用? 它可以实时同步在线 ...
- 一个input标签搞定含内外描边及阴影的按钮~
自从怀孕以来,我就变得很是轻松,偶尔写一两个页面,或者偶尔调试一个两个bug,或者偶尔给做JS的同事打打下手,修改个bug什么......一个习惯于忙碌的工作的人,这一闲下来,感觉还真TM很不舒服-怎 ...
- 通过一个模拟程序让你明白WCF大致的执行流程
原文http://www.cnblogs.com/artech/archive/2011/12/07/wcf-how-to-work.html 在<通过一个模拟程序让你明白ASP.NET MVC ...
随机推荐
- Visual studio 2008 的语法高亮插件 NShader
前段时间一直在使用matlab,今天需要使用vs2008,而用惯了matlab,习惯了其中一项选中变量高亮的设置,突然回来使用VS,感到各种不适应,顿时想到了一个词:矫情 呵呵,于是在网上找各种插件, ...
- 在安装软件CAJViewer时出现,“错误1327。无效驱动器:F:
解决的方法:DOS中输入例如以下命令: [plain] view plaincopy subst F: %TEMP% 回车退出就可以,必要时重新启动电脑. 软件成功安装之后能够执行下面命令,将该虚拟分 ...
- MySQL 通配符学习小结
MySQL 通配符 SQL的模式匹配同意你使用"_"匹配不论什么单个字符,而"%"匹配随意数目字符(包含零个字符).在 MySQL中,SQL的模式缺省是忽略大写 ...
- Linux如何关闭防火墙和查看防火墙的具体情况
1.Linux下关闭和开启防火墙 1) 重启后生效 开启: chkconfig iptables on 关闭: chkconfig iptables off 2) 即时生效,重启后失效 开启: ser ...
- Quartz contention when running in load balanced environment--reference
1.8.3 appears to have addressed this issue with a single application server. However, we're seeing t ...
- google(转帖)
本帖最后由 qiushui_007 于 2014-6-10 16:14 编辑 IP Addresses of Google Global Cachewww.kookle.co.nr Bulgaria ...
- HDU 1756 Cupid's Arrow 判断点在多边形的内部
Cupid's Arrow Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tot ...
- angularjs项目中关于服务的应用
/** *普通ajax请求公共服务 */ mainModule.factory('myService',function($http,$q){ var service = {}; var baseUr ...
- 关于——GCD
GCD全称是Grand Central Dispatch,可译为“牛逼的中枢调度器”,纯C语言提供了强大的函数. GCD中2个核心概念 任务:执行什么操作. 队列:用来存放任务.(说白点,任务只有 ...
- java_log_01
logback&slf4j(本文中的版本为logback1.1.7.slf4j1.7.21),参照 原作者:Ceki Gülcü.Sébastien Pennec中文版译者:陈华联系方式:cl ...