C# WinForm MessageBox自定义按键文本 COM组件版

发布时间:2024年01月02日

c# 更改弹窗MessageBox按钮文字_c# messagebox.show 字体-CSDN博客

需要用到大佬上传到百度云盘的Hook类,在大佬给的例子的基础上改动了点。

加了ok按键,还原了最基础的messageBox。

为了适配多语言,增加ReadBtnLanguge方法。

应用时自己加GUID和ProgID。

组件实现:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace HookMessageBox
{
    [ComVisible(true)]
    [Guid("")]
    interface IHookMessageBox
    {
        [DispId(1)]
        void ReadBtnLanguage();
        [DispId(1)]
        DialogResult MessageBoxOKBtnShow(string text,string caption, MessageBoxIcon icon);
        [DispId(1)]
        DialogResult MessageBoxYesOrNoBtnShow(string text,string caption, MessageBoxIcon icon);
        [DispId(1)]
        DialogResult MessageBoxYesOrNoOrCancelBtnShow(string text,string caption, MessageBoxIcon icon);
    }

    [ComVisible(true)]
    [Guid("")]
    [ProgId("")]
    public class ClsHookMessageBox: IHookMessageBox
    {
        private string OKText { get; set; }
        private string YesText { get; set; }
        private string NoText { get; set; }
        private string CancelText { get; set; }

        public void ReadBtnLanguage()
        {
            OKText = "OK";
            YesText = "Yes";
            NoText = "No";
            CancelText = "Cancel";
        }

        public DialogResult MessageBoxOKBtnShow(string text,string caption,MessageBoxIcon icon=MessageBoxIcon.None)
        {

          return HookMessageBoxShow<DialogResult>(text:text, caption:caption,okText: OKText, icon:icon);
        }

        public DialogResult MessageBoxYesOrNoBtnShow(string text, string caption, MessageBoxIcon icon = MessageBoxIcon.None)
        {
            string yesText = "Yes";
            string noText = "No";
            return HookMessageBoxShow<DialogResult>(text: text, caption: caption, yesText: YesText, noText: NoText, buttons:MessageBoxButtons.YesNo,icon: icon);
        }

        public DialogResult MessageBoxYesOrNoOrCancelBtnShow(string text, string caption, MessageBoxIcon icon = MessageBoxIcon.None)
        {
            string yesText = "Yes";
            string noText = "No";
            string cancelText = "Cancel";
            return HookMessageBoxShow<DialogResult>(text: text, caption: caption, yesText: YesText, noText: NoText, cancelText: CancelText, buttons: MessageBoxButtons.YesNoCancel, icon: icon);
        }

        /// <summary>
        /// 带返回值
        /// </summary>
        private T HookMessageBoxShow<T>(string text, string caption, MessageBoxButtons buttons= MessageBoxButtons.OK, MessageBoxIcon icon = MessageBoxIcon.None,
            string okText="",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}");
                }
                if (!string.IsNullOrEmpty(okText) && HookINCS.Win32Api_Hook.GetDlgItem(hChildWnd, 1) != 0)//IDCANCEL = 2
                {
                    result = HookINCS.Win32Api_Hook.SetDlgItemTextA(hChildWnd, 1, $"{okText}");
                }
            };
            hook.InstallMessageBoxHook();


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

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

        /// <summary>
        /// 不带返回值
        /// </summary>
        private void HookMessageBoxShow(string text, string caption, MessageBoxButtons buttons= MessageBoxButtons.OK, MessageBoxIcon icon = MessageBoxIcon.None,
            string okText = "", 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}");
                }
                if (!string.IsNullOrEmpty(okText) && HookINCS.Win32Api_Hook.GetDlgItem(hChildWnd, 1) != 0)//IDCANCEL = 2
                {
                    result = HookINCS.Win32Api_Hook.SetDlgItemTextA(hChildWnd, 1, $"{okText}");
                }
            };
            hook.InstallMessageBoxHook();


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

    }
}

调用:

            object obj = Activator.CreateInstance(Type.GetTypeFromProgID("HookMessageBox.ClsHookMessageBox"));
            obj.GetType().GetMethod("ReadBtnLanguage").Invoke(obj, new object[] { });
            obj.GetType().GetMethod("MessageBoxOKBtnShow").Invoke(obj, new object[] { "messageboxText","messageCaption", MessageBoxIcon.None });
            obj.GetType().GetMethod("MessageBoxYesOrNoBtnShow").Invoke(obj, new object[] { "messageboxText","messageCaption", MessageBoxIcon.None });
            obj.GetType().GetMethod("MessageBoxYesOrNoOrCancelBtnShow").Invoke(obj, new object[] { "messageboxText","messageCaption", MessageBoxIcon.None }); });

效果:

遗留问题:

获取弹窗被按下按键。

文章来源:https://blog.csdn.net/Cecile0828/article/details/135344108
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。