c# 更改弹窗MessageBox按钮文字

(附件下载所需积分是系统自动调整的,没法改为低分。。。。够贱)

链接:https://pan.baidu.com/s/1zQ0AryH1e_F2sDhQkNU_tg 
提取码:5aru 

https://download.csdn.net/download/jwb7832007/12521690

需要用到hook,具体hook类在附件里,此hook类也是网上扒来的,忘了地址了。。。。否则作为尊重应该贴上原文地址的。。。

原hook类写了有键鼠hook,MessageBox 是我后添加的,如果有其他hook也可以参照我的方式自己添加。

详解自定义hook:(以下都是hook类里有的,不需要再复制添加)

具体调用方式在末尾的第二个代码段内有,复制过去就可以用

首先定义一个事件,在写对应的钩子回调函数,在函数内触发事件即可,至于事件的具体执行什么动作则在外部定义hook时设置,和注册控件的事件一样的。

下面就是一个自定义hook所需要添加的完整代码
Hook.cs:

 
        /// <summary>
        /// 弹窗钩子
        /// </summary>
        private int msboxHook = 0;
        /// <summary>
        /// 弹窗钩子回调函数
        /// </summary>
        private HookProc mboxHook;
/// <summary>
        /// 定义一个自定义的事件 
        /// </summary>
        public event EventHandler<MessageBoxEventArgs> OnMessageBoxShow;

        public class MessageBoxEventArgs
            {
            public MessageBoxEventArgs() { }
            public MessageBoxEventArgs(IntPtr _hChildWnd)
                {
                hChildWnd = _hChildWnd;
                }
            public IntPtr hChildWnd;
            }

        /// <summary>
        /// 安装弹窗钩子
        /// </summary>
        /// <param name="type"></param>
        public void InstallMessageBoxHook()
            {
            InstallMessageBoxHook(HookType.WH_CBT);
            }
        /// <summary>
        /// 安装弹窗钩子
        /// </summary>
        /// <param name="type"></param>
        public void InstallMessageBoxHook(HookType type)
            {
            if (msboxHook == 0)
                {
                mboxHook = new HookProc(DefaultMessageBoxHookProc);
                msboxHook = Win32Api_Hook.SetWindowsHookEx(
                    type,
                    mboxHook,
                    Win32Api_Hook.GetModuleHandle(
                        Process.GetCurrentProcess().MainModule.ModuleName
                        ),
                    Win32Api_Hook.GetCurrentThreadId()//自身线程,如果是0则表示全局
                    );
                if (msboxHook == 0)
                    UninstallMessageBoxHook();
                }
            }

        /// <summary>
        /// 卸载弹窗钩子
        /// </summary>
        public void UninstallMessageBoxHook()
            {
            UninstallHook(ref msboxHook);
            }


        /// <summary>
        /// 这是一个钩子回调函数,参数都是统一的
        /// </summary>

        private int DefaultMessageBoxHookProc(int nCode, int wParam, IntPtr lParam)
            {
            IntPtr hChildWnd;// msgbox is "child"
            // notification that a window is about to be activated
            // window handle is wParam
            if (nCode == 5)//HCBT_ACTIVATE = 5   
                {
                // set window handles of messagebox
                hChildWnd = (IntPtr)wParam;
                //to get the text of yes button
                //自定义事件,在外部调用时通过注册事件执行对应逻辑即可
                OnMessageBoxShow.Invoke(this, new MessageBoxEventArgs(hChildWnd));


            //return (IntPtr)1; //直接返回了,该消息就处理结束了
            return Win32Api_Hook.CallNextHookEx(msboxHook, nCode, wParam, lParam);// otherwise, continue with any possible chained hooks; //返回,让后面的程序处理该消息
            }

具体调用方式

        private void checkBox_pause_CheckedChanged(object sender, EventArgs e)
            {
            BeginInvoke(new Action(() =>
            {
                if (!checkBox_pause.Checked)
                    {
                    Properties.Settings.Default.pause = false;
                    Properties.Settings.Default.pauseWorker = false;
                    Logger.Log("已取消暂停,恢复运行");
                    return;
                    }
                Action<DialogResult> handle = (dialogResult) => {
                    if (dialogResult == DialogResult.Yes)
                        {
                        Properties.Settings.Default.pause = true;
                        Logger.Warn("已暂停,将暂停开启新线程和已开启的工作线程");
                        }
                    else if (dialogResult == DialogResult.No)
                        {
                        Properties.Settings.Default.pauseWorker = true;
                        Logger.Warn("已暂停开启新线程,已开启线程将继续运行直到完成");
                        }
                };
                HookMessageBoxShow($"(Y)暂停整个程序\r\n(N)暂停开启新线程,已开启线程会继续执行", "暂停", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Information,
                    "暂停(Y)", "暂停开新(N)", "", handle);
            }));
            }



        /// <summary>
        /// 带返回值
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="text"></param>
        /// <param name="caption"></param>
        /// <param name="buttons"></param>
        /// <param name="icon"></param>
        /// <param name="yesText"></param>
        /// <param name="noText"></param>
        /// <param name="cancelText"></param>
        /// <param name="handleResult"></param>
        /// <returns></returns>
        public T HookMessageBoxShow<T>(string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon = MessageBoxIcon.Information,
            string yesText = "",string noText = "",string cancelText = "",Func<DialogResult,T> handleResult = null)
            {
            //hook 修改弹出窗按钮 文本
            var hook = new HookINCS.Hook();
            hook.OnMessageBoxShow += (s, mbe) =>
            {
                IntPtr hChildWnd = mbe.hChildWnd;
                int result;
                if (!string.IsNullOrEmpty(yesText) && HookINCS.Win32Api_Hook.GetDlgItem(hChildWnd, 6) != 0)//IDYES = 6
                    {
                    result = HookINCS.Win32Api_Hook.SetDlgItemTextA(hChildWnd, 6, $"{yesText}");//在Project.Resources里自定义文本
                    }
                if (!string.IsNullOrEmpty(noText) && HookINCS.Win32Api_Hook.GetDlgItem(hChildWnd, 7) != 0)//IDNO = 7
                    {
                    result = HookINCS.Win32Api_Hook.SetDlgItemTextA(hChildWnd, 7, $"{noText}");
                    }
                if (!string.IsNullOrEmpty(cancelText) && HookINCS.Win32Api_Hook.GetDlgItem(hChildWnd, 2) != 0)//IDCANCEL = 2
                    {
                    result = HookINCS.Win32Api_Hook.SetDlgItemTextA(hChildWnd, 2, $"{cancelText}");
                    }
            };
            hook.InstallMessageBoxHook();


            DialogResult dialogResult = MessageBox.Show(text, caption, buttons, icon);
            if (handleResult == null)
                {
                //卸载钩子
                hook.UninstallMessageBoxHook();
                return default;
                }

            //卸载钩子
            hook.UninstallMessageBoxHook();
            return handleResult(dialogResult);
            }

        /// <summary>
        /// 不带返回值
        /// </summary>
        /// <param name="text"></param>
        /// <param name="caption"></param>
        /// <param name="buttons"></param>
        /// <param name="icon"></param>
        /// <param name="yesText"></param>
        /// <param name="noText"></param>
        /// <param name="cancelText"></param>
        /// <param name="handleResult"></param>
        public void HookMessageBoxShow(string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon = MessageBoxIcon.Information,
            string yesText = "", string noText = "", string cancelText = "", Action<DialogResult> handleResult = null)
            {
            //hook 修改弹出窗按钮 文本
            var hook = new HookINCS.Hook();
            hook.OnMessageBoxShow += (s, mbe) =>
            {
                IntPtr hChildWnd = mbe.hChildWnd;
                int result;
                if (!string.IsNullOrEmpty(yesText) && HookINCS.Win32Api_Hook.GetDlgItem(hChildWnd, 6) != 0)//IDYES = 6
                    {
                    result = HookINCS.Win32Api_Hook.SetDlgItemTextA(hChildWnd, 6, $"{yesText}");//在Project.Resources里自定义文本
                    }
                if (!string.IsNullOrEmpty(noText) && HookINCS.Win32Api_Hook.GetDlgItem(hChildWnd, 7) != 0)//IDNO = 7
                    {
                    result = HookINCS.Win32Api_Hook.SetDlgItemTextA(hChildWnd, 7, $"{noText}");
                    }
                if (!string.IsNullOrEmpty(cancelText) && HookINCS.Win32Api_Hook.GetDlgItem(hChildWnd, 2) != 0)//IDCANCEL = 2
                    {
                    result = HookINCS.Win32Api_Hook.SetDlgItemTextA(hChildWnd, 2, $"{cancelText}");
                    }
            };
            hook.InstallMessageBoxHook();


            DialogResult dialogResult = MessageBox.Show(text, caption, buttons, icon);
            if (handleResult != null)
                handleResult(dialogResult);
            //卸载钩子
            hook.UninstallMessageBoxHook();
            }

 

  • 1
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值