利用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 ...
随机推荐
- C语言判断系统数据大/小端存储方式
小端存储:数据的低位部分,存储于存储器的低地址空间里. 大端存储:数据的低位部分,存储于存储器的高地址空间里. 首先,一般PC数据存储方式是小端存储. 基本实现思想是:将存储器中所存的数据按字节以地址 ...
- uva12118
一开始以为直接算联通块个数就行了 后来发现还得分联通块里的奇点... 还要注意m = 0的情况... #include<iostream> #include<algorithm> ...
- 如何评估oracle AWR 的在oracle 中的存储情况
如何评估oracle AWR 的在oracle 中的存储情况 如果把awr保存时间从默认的7天修改为30天,采样时间从1小时修改到更短,修改之后如何预估空间占用? ------------------ ...
- geektool--一款很geek的工具
2016/12/18 今天尝试一款很geek的工具 geektool 听名字就超级geek有木有 get it geektool website 从官网直接下载app,一键傻瓜式安装. use it ...
- 【微信公众号】将微信公众号消息里的FromUserName即OpenID转成UnionID
最近在调试微信公众号开发者模式,处理公众号消息,收到如下回调消息内容 <xml><ToUserName><![CDATA[gh_29********21]]>< ...
- 基于RMAN的异机数据库克隆(rman duplicate)
对于基于生产环境下的数据库的版本升级或者测试新的应用程序的性能及其影响,备份恢复等等,我们可以采取从生产环境以克隆的方式将其克隆到本地而不影响生产数据库的正常使用.实现这个功能我们可以借助rman d ...
- JQ插件之imgAreaSelect实现对图片的在线截图功能(java版)
前言:在做网站的时候经常用的功能就是,用户上传图片对自己上传的图片进行截图,DIV自己的头像.或者上传幻灯片大图进行DIV设置小图. 解决方案:目前我知道的解决方案有两个如下: 一.fla ...
- 第一篇:数据库需求与ER建模
前言 在数据库建设过程中,哪一步最重要?绝大多数资料会告诉你,是需求分析阶段.这一步的好坏甚至直接决定数据库项目的成败. 需求分析阶段,也被称为ER建模(entity-relationship mod ...
- 模板-->单变元模线性方程
如果有相应的OJ题目,欢迎同学们提供相应的链接 相关链接 所有模板的快速链接 extend_gcd模板 poj_2115_C Looooops,my_ac_code 简单的测试 None 代码模板 / ...
- 教学项目之-通过Python实现简单的计算器
教学项目之-通过Python实现简单的计算器 计算器开发需求 实现加减乘除及拓号优先级解析 用户输入 1 - 2 * ( (60-30 +(-40/5) * (9-2*5/3 + 7 /3*99/ ...