123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using System.Runtime.InteropServices;
- using System.Drawing;
- using System.Management;
- using System.Windows.Forms.VisualStyles;
- namespace PDF_Office.CustomControl
- {
- /// <summary>
- /// 自定义可以更改按钮文字的Messagebox
- /// </summary>
- public class MessageBoxEx
- {
- private static string[] okstring = new string[] { "Ok" };
- private static string[] ok = okstring;
- private static string[] okcancelstring = new string[] { "OK", "Cancel" };
- private static string[] okcancel = okcancelstring;
- private static string[] abortretryignorestring = new string[] { "About", "Retry", "Ignore" };
- private static string[] abortretryignore = abortretryignorestring;
- private static string[] yesnocancelstring = new string[] { "Yes", "No", "Cancel" };
- private static string[] yesnocancel = yesnocancelstring;
- private static string[] yesnostring = new string[] { "Yes", "No" };
- private static string[] yesno = yesnostring;
- private static string[] retrycancelstring = new string[] { "Retry", "Cancel" };
- private static string[] retrycancel = retrycancelstring;
- public static DialogResult Show(string text,string[] buttonTitles = null)
- {
- if(buttonTitles!=null&&buttonTitles.Length==1)
- {
- ok = buttonTitles;
- }
- else
- {
- ok = okstring;
- }
- myProc = new HookProc(OK);
- SetHook();
- //默认标题为产品名
- DialogResult result = MessageBox.Show(text, Application.ProductName);
-
- UnHook();
- return result;
- }
- public static DialogResult Show(string text,string title,string[] buttonTitles = null)
- {
- if (buttonTitles != null && buttonTitles.Length == 1)
- {
- ok = buttonTitles;
- }
- else
- {
- ok = okstring;
- }
- myProc = new HookProc(OK);
- SetHook();
- DialogResult result = MessageBox.Show(text,title);
- UnHook();
- return result;
- }
- public static DialogResult Show(string text, string caption, MessageBoxButtons buttons, string[] buttonTitles=null)
- {
- switch (buttons)
- {
- case MessageBoxButtons.OK:
- if (buttonTitles != null && buttonTitles.Length == 1)
- {
- ok = buttonTitles;
- }
- else
- {
- ok = okstring;
- }
- myProc = new HookProc(OK);
- break;
- case MessageBoxButtons.OKCancel:
- if (buttonTitles != null && buttonTitles.Length == 2)
- {
- okcancel = buttonTitles;
- }
- else
- {
- okcancel = okcancelstring;
- }
- myProc = new HookProc(OKCancel);
- break;
- case MessageBoxButtons.AbortRetryIgnore:
- if (buttonTitles != null && buttonTitles.Length == 3)
- {
- abortretryignore = buttonTitles;
- }
- else
- {
- abortretryignore = abortretryignorestring;
- }
- myProc = new HookProc(AbortRetryIgnore);
- break;
- case MessageBoxButtons.YesNoCancel:
- if (buttonTitles != null && buttonTitles.Length == 3)
- {
- yesnocancel = buttonTitles;
- }
- else
- {
- yesnocancel = yesnocancelstring;
- }
- myProc = new HookProc(YesNoCancel);
- break;
- case MessageBoxButtons.YesNo:
- if (buttonTitles != null && buttonTitles.Length == 2)
- {
- yesno = buttonTitles;
- }
- else
- {
- yesno = yesnostring;
- }
- myProc = new HookProc(YesNo);
- break;
- case MessageBoxButtons.RetryCancel:
- if (buttonTitles != null && buttonTitles.Length == 2)
- {
- retrycancel = buttonTitles;
- }
- else
- {
- retrycancel = retrycancelstring;
- }
- myProc = new HookProc(RetryCancel);
- break;
- default:
- break;
- }
- SetHook();
- DialogResult result = MessageBox.Show(text, string.IsNullOrEmpty(caption)?Application.ProductName:caption, buttons);
- UnHook();
- return result;
- }
- public static DialogResult Show(string text, string caption, MessageBoxButtons buttons,MessageBoxIcon icons,string[] buttonTitles = null)
- {
- switch (buttons)
- {
- case MessageBoxButtons.OK:
- if (buttonTitles != null && buttonTitles.Length == 1)
- {
- ok = buttonTitles;
- }
- else
- {
- ok = okstring;
- }
- myProc = new HookProc(OK);
- break;
- case MessageBoxButtons.OKCancel:
- if (buttonTitles != null && buttonTitles.Length == 2)
- {
- okcancel = buttonTitles;
- }
- else
- {
- okcancel = okcancelstring;
- }
- myProc = new HookProc(OKCancel);
- break;
- case MessageBoxButtons.AbortRetryIgnore:
- if (buttonTitles != null && buttonTitles.Length == 3)
- {
- abortretryignore = buttonTitles;
- }
- else
- {
- abortretryignore = abortretryignorestring;
- }
- myProc = new HookProc(AbortRetryIgnore);
- break;
- case MessageBoxButtons.YesNoCancel:
- if (buttonTitles != null && buttonTitles.Length == 3)
- {
- yesnocancel = buttonTitles;
- }
- else
- {
- yesnocancel = yesnocancelstring;
- }
- myProc = new HookProc(YesNoCancel);
- break;
- case MessageBoxButtons.YesNo:
- if (buttonTitles != null && buttonTitles.Length == 2)
- {
- yesno = buttonTitles;
- }
- else
- {
- yesno = yesnostring;
- }
- myProc = new HookProc(YesNo);
- break;
- case MessageBoxButtons.RetryCancel:
- if (buttonTitles != null && buttonTitles.Length == 2)
- {
- retrycancel = buttonTitles;
- }
- else
- {
- retrycancel = retrycancelstring;
- }
- myProc = new HookProc(RetryCancel);
- break;
- default:
- break;
- }
- SetHook();
- DialogResult result = MessageBox.Show(text, string.IsNullOrEmpty(caption) ? Application.ProductName : caption, buttons, icons);
- UnHook();
- return result;
- }
-
- public static DialogResult Show(string text, string caption, MessageBoxButtons buttons,
- MessageBoxIcon icon, MessageBoxDefaultButton defaultButton, string[] buttonTitles=null)
- {
- switch (buttons)
- {
- case MessageBoxButtons.OK:
- if (buttonTitles != null && buttonTitles.Length == 1)
- {
- ok = buttonTitles;
- }
- else
- {
- ok = okstring;
- }
- myProc = new HookProc(OK);
- break;
- case MessageBoxButtons.OKCancel:
- if (buttonTitles != null && buttonTitles.Length == 2)
- {
- okcancel = buttonTitles;
- }
- else
- {
- okcancel = okcancelstring;
- }
- myProc = new HookProc(OKCancel);
- break;
- case MessageBoxButtons.AbortRetryIgnore:
- if (buttonTitles != null && buttonTitles.Length == 3)
- {
- abortretryignore = buttonTitles;
- }
- else
- {
- abortretryignore = abortretryignorestring;
- }
- myProc = new HookProc(AbortRetryIgnore);
- break;
- case MessageBoxButtons.YesNoCancel:
- if (buttonTitles != null && buttonTitles.Length == 3)
- {
- yesnocancel = buttonTitles;
- }
- else
- {
- yesnocancel = yesnocancelstring;
- }
- myProc = new HookProc(YesNoCancel);
- break;
- case MessageBoxButtons.YesNo:
- if (buttonTitles != null && buttonTitles.Length == 2)
- {
- yesno = buttonTitles;
- }
- else
- {
- yesno = yesnostring;
- }
- myProc = new HookProc(YesNo);
- break;
- case MessageBoxButtons.RetryCancel:
- if (buttonTitles != null && buttonTitles.Length == 2)
- {
- retrycancel = buttonTitles;
- }
- else
- {
- retrycancel = retrycancelstring;
- }
- myProc = new HookProc(RetryCancel);
- break;
- default:
- break;
- }
- DialogResult result = MessageBox.Show(text, string.IsNullOrEmpty(caption) ? Application.ProductName : caption, buttons, icon, defaultButton);
- return result;
- }
- //用钩子的方式 更改文本
- public enum HookType
- {
- Keyboard = 2,//键盘操作
- CBT = 5,//窗口操作
- Mouse = 7, //鼠标操作
- };
- [DllImport("kernel32.dll")]
- static extern int GetCurrentThreadId();//得到当前的线程ID
- [DllImport("user32.dll")]
- static extern int GetDlgItem(IntPtr hDlg, int nIDDlgItem);//得到Dialog窗口的子项
- [DllImport("user32", EntryPoint = "SetDlgItemText")]
- static extern int SetDlgItemTextA(IntPtr hDlg, int nIDDlgItem, string lpString);//设置Dialog窗口子项的文本
- [DllImport("user32.dll", EntryPoint = "SetWindowTextW", CharSet = CharSet.Unicode)]
- private static extern bool SetWindowText(IntPtr hWnd, string lpString);
- [DllImport("user32.dll")]
- static extern void UnhookWindowsHookEx(IntPtr handle);//解掉挂钩
- [DllImport("user32.dll")]
- static extern IntPtr SetWindowsHookEx(int idHook, [MarshalAs(UnmanagedType.FunctionPtr)] HookProc lpfn, IntPtr
- hInstance, int threadID);//设置挂钩
- [DllImport("user32.dll")]
- static extern IntPtr CallNextHookEx(IntPtr handle, int code, IntPtr wparam, IntPtr lparam);//进行下一个挂钩,如果有的话
- static IntPtr _nextHookPtr;
- ////must be global, or it will be Collected by GC, then no callback func can be used for the Hook
- static HookProc myProc = new HookProc(MyHookProc);
- private delegate IntPtr HookProc(int code, IntPtr wparam, IntPtr lparam);
- private static IntPtr OK(int code, IntPtr wparam, IntPtr lparam)
- {
- IntPtr hChildWnd;
- if (code == 5)//HCBT_ACTIVATE = 5
- {
- hChildWnd = wparam;
- var index = (IntPtr)GetDlgItem(hChildWnd,1);
- if (index != IntPtr.Zero)
- {
- SetWindowText(index, ok[0]);
- }
- }
- else
- CallNextHookEx(_nextHookPtr, code, wparam, lparam);
- return IntPtr.Zero;
- }
- private static IntPtr OKCancel(int code, IntPtr wparam, IntPtr lparam)
- {
- IntPtr hChildWnd;
- bool result = false;
- if (code == 5)//HCBT_ACTIVATE = 5
- {
- hChildWnd = wparam;
- IntPtr index = (IntPtr)GetDlgItem(hChildWnd, 1);
- if (index != IntPtr.Zero)
- {
- result = SetWindowText(index, okcancel[0]);
- }
- index = (IntPtr)GetDlgItem(hChildWnd, 2);
- if (index != IntPtr.Zero)
- {
- result = SetWindowText(index, okcancel[1]);
- }
- }
- else
- CallNextHookEx(_nextHookPtr, code, wparam, lparam);
- return IntPtr.Zero;
- }
- private static IntPtr RetryCancel(int code, IntPtr wparam, IntPtr lparam)
- {
- IntPtr hChildWnd;
- if (code == 5)//HCBT_ACTIVATE = 5
- {
- hChildWnd = wparam;
- var index = (IntPtr)GetDlgItem(hChildWnd,4);
- if (index!=IntPtr.Zero)
- {
- SetWindowText(index, retrycancel[0]);
- }
- index = (IntPtr)GetDlgItem(hChildWnd, 2);
- if (GetDlgItem(hChildWnd, 2) != 0)
- {
- SetWindowText(index, retrycancel[1]);
- }
- }
- else
- CallNextHookEx(_nextHookPtr, code, wparam, lparam);
- return IntPtr.Zero;
- }
- private static IntPtr YesNo(int code, IntPtr wparam, IntPtr lparam)
- {
- IntPtr hChildWnd;
- if (code == 5)//HCBT_ACTIVATE = 5
- {
- hChildWnd = wparam;
- var index = (IntPtr)GetDlgItem(hChildWnd, 6);
- if (index!=IntPtr.Zero)
- {
- SetWindowText(index, yesno[0]);
- }
- index = (IntPtr)GetDlgItem(hChildWnd, 7);
- if (index != IntPtr.Zero)
- {
- SetWindowText(index, yesno[1]);
- }
- }
- else
- CallNextHookEx(_nextHookPtr, code, wparam, lparam);
- return IntPtr.Zero;
- }
- private static IntPtr YesNoCancel(int code, IntPtr wparam, IntPtr lparam)
- {
- IntPtr hChildWnd;
- if (code == 5)//HCBT_ACTIVATE = 5
- {
- hChildWnd = wparam;
- var index = (IntPtr)GetDlgItem(hChildWnd, 6);
- if (index != IntPtr.Zero)
- {
- SetWindowText(index,yesnocancel[0]);
- }
- index = (IntPtr)GetDlgItem(hChildWnd, 7);
- if (index != IntPtr.Zero)
- {
- SetWindowText(index, yesnocancel[1]);
- }
- index = (IntPtr)GetDlgItem(hChildWnd, 2);
- if (index != IntPtr.Zero)
- {
- SetWindowText(index, yesnocancel[2]);
- }
- }
- else
- CallNextHookEx(_nextHookPtr, code, wparam, lparam);
- return IntPtr.Zero;
- }
- private static IntPtr AbortRetryIgnore(int code, IntPtr wparam, IntPtr lparam)
- {
- IntPtr hChildWnd;
- if (code == 5)//HCBT_ACTIVATE = 5
- {
- hChildWnd = wparam;
- var index = (IntPtr)GetDlgItem(hChildWnd, 3);
- if (index != IntPtr.Zero)
- {
- SetWindowText(index, abortretryignore[0]);
- }
- index = (IntPtr)GetDlgItem(hChildWnd, 4);
- if (index != IntPtr.Zero)
- {
- SetWindowText(index, abortretryignore[1]);
- }
- index = (IntPtr)GetDlgItem(hChildWnd, 5);
- if (index != IntPtr.Zero)
- {
- SetWindowText(index, abortretryignore[2]);
- }
- }
- else
- CallNextHookEx(_nextHookPtr, code, wparam, lparam);
- return IntPtr.Zero;
- }
- private static IntPtr MyHookProc(int code, IntPtr wparam, IntPtr lparam)
- {
- IntPtr hChildWnd;// msgbox is "child"
- // notification that a window is about to be activated
- // window handle is wParam
- if (code == 5)//HCBT_ACTIVATE = 5
- {
- // set window handles of messagebox
- hChildWnd = wparam;
- //to get the text of yes button
- int result;
- int count = 0;
- for(int i=0;i<21;i++)
- {
- if (GetDlgItem(hChildWnd, i) != 0)
- SetDlgItemTextA(hChildWnd,i,string.Format("Item {0}",i));
- }
- }
- else
- {
- CallNextHookEx(_nextHookPtr, code, wparam, lparam);// otherwise, continue with any possible chained hooks
- }
- //return (IntPtr)1; //直接返回了,该消息就处理结束了
- return IntPtr.Zero;//返回,让后面的程序处理该消息
- }
- public static void SetHook()
- {
- if (_nextHookPtr != IntPtr.Zero)//Hooked already
- {
- return;
- }
- _nextHookPtr = SetWindowsHookEx((int)HookType.CBT, myProc, IntPtr.Zero, GetCurrentThreadId());
- }
- public static void UnHook()
- {
- if (_nextHookPtr != IntPtr.Zero)
- {
- UnhookWindowsHookEx(_nextHookPtr);
- _nextHookPtr = IntPtr.Zero;
- }
- }
- }
- }
|