关键词:
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按钮文本为例
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事件里面写上如下代码
...{
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中使用死信队列。我希望每当队列收到新消息时,它... 查看详情