c#更改弹窗MessageBox按钮文字

更新时间:2023-06-21 22:26:40 阅读: 评论:0

c#更改弹窗MessageBox按钮⽂字
(附件下载所需积分是系统⾃动调整的,没法改为低分。。。。够贱)
需要⽤到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)
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
{
// t 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);// otherwi, continue with any possible chained hooks; //返回,让后⾯的程            }
具体调⽤⽅式
private void checkBox_pau_CheckedChanged(object nder, EventArgs e)
{
BeginInvoke(new Action(() =>
{
if (!checkBox_pau.Checked)
{
Properties.Settings.Default.pau = fal;
Properties.Settings.Default.pauWorker = fal;
Logger.Log("已取消暂停,恢复运⾏");
return;
}
Action<DialogResult> handle = (dialogResult) => {
if (dialogResult == DialogResult.Yes)
{
Properties.Settings.Default.pau = true;
Logger.Warn("已暂停,将暂停开启新线程和已开启的⼯作线程");
}
el if (dialogResult == DialogResult.No)
{
Properties.Settings.Default.pauWorker = true;
Logger.Warn("已暂停开启新线程,已开启线程将继续运⾏直到完成");
}
};
HookMessageBoxShow($"(Y)暂停整个程序\r\n(N)暂停开启新线程,已开启线程会继续执⾏", "暂停", MessageBoxButtons.YesNoCancel, MessageBoxIcon                    "暂停(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)//ID
YES = 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();
}

本文发布于:2023-06-21 22:26:40,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/89/1048965.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:定义   事件   函数   弹窗
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图