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