(附件下载所需积分是系统自动调整的,没法改为低分。。。。够贱)
链接: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:
///
/// 弹窗钩子
///
private int msboxHook = 0;
///
/// 弹窗钩子回调函数
///
private HookProc mboxHook;
///
/// 定义一个自定义的事件
///
public event EventHandler OnMessageBoxShow;
public class MessageBoxEventArgs
{
public MessageBoxEventArgs() { }
public MessageBoxEventArgs(IntPtr _hChildWnd)
{
hChildWnd = _hChildWnd;
}
public IntPtr hChildWnd;
}
///
/// 安装弹窗钩子
///
///
public void InstallMessageBoxHook()
{
InstallMessageBoxHook(HookType.WH_CBT);
}
///
/// 安装弹窗钩子
///
///
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();
}
}
///
/// 卸载弹窗钩子
///
public void UninstallMessageBoxHook()
{
UninstallHook(ref msboxHook);
}
///
/// 这是一个钩子回调函数,参数都是统一的
///
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 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);
}));
}
///
/// 带返回值
///
///
///
///
///
///
///
///
///
///
///
public T HookMessageBoxShow(string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon = MessageBoxIcon.Information,
string yesText = "",string noText = "",string cancelText = "",Func 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);
}
///
/// 不带返回值
///
///
///
///
///
///
///
///
///
public void HookMessageBoxShow(string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon = MessageBoxIcon.Information,
string yesText = "", string noText = "", string cancelText = "", Action 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();
}