winform设置所有窗体统一图标

class WindowHookerManager
    {
        static WindowHooker hooker = new WindowHooker();
        public static void SetAllFormIcon()
        {

            hooker.OnHookControl += (o, e) =>
            {
                Console.WriteLine(e.Control.GetType().ToString());
                var frm = e.Control as Form;
                if (frm != null)
                {
                    //var iconPath = Application.StartupPath + "\\Coconut3.ico";
                    //if (System.IO.File.Exists(iconPath))
                    //{
                    //    frm.Icon = new System.Drawing.Icon(iconPath);
                    //}
                    frm.Icon = Resources.Coconut3;
                }
            };
            hooker.Hook();
        }
    }

    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);
        }
    }


++++++++调用+++++++
        static void Main()
        {
            //设置所有窗体统一图标
             WindowHookerManager.SetAllFormIcon();

            Application.Run(new frmLogin());
        }

 

posted @ 2021-11-12 16:05  212的s  阅读(477)  评论(0编辑  收藏  举报