概述
思路:
通过SetWindowsHookEx函数,获取所有的Form对象,修改其Icon属性。
实现:
class WindowHooker
{
public class HookControlEventArgs:EventArgs
{
public Control Control;
public HookControlEventArgs(Control c)
{
this.Control = c;
}
}
#region win32 api
internal delegate IntPtr HOOKPROC(int nCode, IntPtr wParam, IntPtr lParam);
[DllImport("User32.dll", CharSet = CharSet.Auto)]
internal static extern IntPtr SetWindowsHookEx(int idHook, HOOKPROC lpfn, int hMod, int dwThreadId);
[DllImport("User32.dll", CharSet = CharSet.Auto)]
internal static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, IntPtr lParam);
#endregion
internal IntPtr PHook = IntPtr.Zero;
internal HOOKPROC PHookProc = null;
public event EventHandler<HookControlEventArgs> OnHookControl;
public void Hook()
{
PHookProc = new HOOKPROC(FnHookProc);
// 如果代码改为:SetWindowsHookEx(5, FnHookProc, 0, AppDomain.GetCurrentThreadId());
// 则会报如下错误:
// ”类型的已垃圾回收委托进行了回调。这可能会导致应用程序崩溃、损坏和数据丢失。向非托管代码传递委托时,托管应用程序必须让这些委托保持活动状态,直到确信不会再次调用它们。”
PHook = SetWindowsHookEx(5, PHookProc, 0, AppDomain.GetCurrentThreadId());
}
private IntPtr FnHookProc(int nCode, IntPtr wParam, IntPtr lParam)
{
switch (nCode)
{
case 5:
{
var control = Control.FromHandle(wParam);
if (control != null)
{
var frm = control as Form;
if (frm != null)
{
if(OnHookControl!=null)
{
OnHookControl(this, new HookControlEventArgs(frm));
}
}
}
break;
}
}
return CallNextHookEx(PHook, nCode, wParam, lParam);
}
}
调用:
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
WindowHooker hooker = new WindowHooker();
hooker.OnHookControl += (o, e) => {
Console.WriteLine(e.Control.GetType().ToString());
var frm = e.Control as Form;
frm.Icon = GetResAsIcon("app.ico");
};
hooker.Hook();
Application.Run(new FrmMain());
}
PS:
1、事件顺序:Form_Load、Hook、Form_Shown
————————————————
版权声明:本文为CSDN博主「frozleaf」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/frozleaf/article/details/89429629
最后
以上就是淡然战斗机为你收集整理的C#启动前为所有的Form设置统一的Icon的全部内容,希望文章能够帮你解决C#启动前为所有的Form设置统一的Icon所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复