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

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

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

应用时自己加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(text:text, caption:caption,okText: OKText, icon:icon);
        }

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

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

        private DialogResult HookMessageBoxShow(string text, string caption, MessageBoxButtons buttons= MessageBoxButtons.OK, MessageBoxIcon icon = MessageBoxIcon.None,
            string okText="",string yesText = "", string noText = "", string cancelText = "")
        {
            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}");
                }
                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)//IDOK =  1
                {
                    result = HookINCS.Win32Api_Hook.SetDlgItemTextA(hChildWnd, 1, $"{okText}");
                }
            };
            hook.InstallMessageBoxHook();
            DialogResult dialogResult = MessageBox.Show(text, caption, buttons, icon);
            hook.UninstallMessageBoxHook();//卸载钩子
            return dialogResult;
        }
    }
}

调用:

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

效果:

C# WinForm MessageBox自定义按键文本 COM组件版_第1张图片C# WinForm MessageBox自定义按键文本 COM组件版_第2张图片C# WinForm MessageBox自定义按键文本 COM组件版_第3张图片

你可能感兴趣的:(c#,开发语言)