123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532 |
- using System;
- using System.Windows.Forms;
- using System.Runtime.InteropServices;
- using ComPDFKit.Controls.Helper;
- namespace ComPDFKit.Controls.Common
- {
- public class MessageBoxEx
- {
- private static string[] okstring = new string[] { LanguageHelper.CompressManager.GetString("Main_Ok")};
- private static string[] ok = okstring;
- private static string[] okcancelstring = new string[] { LanguageHelper.CompressManager.GetString("Main_Ok") , LanguageHelper.CompressManager.GetString("Main_Cancel")};
- private static string[] okcancel = okcancelstring;
- private static string[] abortretryignorestring = new string[] { LanguageHelper.CompressManager.GetString("Main_MenuHelp_About") , "Retry", "Ignore" };
- private static string[] abortretryignore = abortretryignorestring;
- private static string[] yesnocancelstring = new string[] { LanguageHelper.CompressManager.GetString("Main_Yes"), LanguageHelper.CompressManager.GetString("Main_No"), LanguageHelper.CompressManager.GetString("Main_Cancel") };
- private static string[] yesnocancel = yesnocancelstring;
- private static string[] yesnostring = new string[] { LanguageHelper.CompressManager.GetString("Main_Yes"), LanguageHelper.CompressManager.GetString("Main_No") };
- private static string[] yesno = yesnostring;
- private static string[] retrycancelstring = new string[] { "Retry", LanguageHelper.CompressManager.GetString("Main_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,MessageBoxButtons.OK);
-
- 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();
- [DllImport("user32.dll")]
- static extern int GetDlgItem(IntPtr hDlg, int nIDDlgItem);
- [DllImport("user32", EntryPoint = "SetDlgItemText")]
- static extern int SetDlgItemTextA(IntPtr hDlg, int nIDDlgItem, string lpString);
- [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
- 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.Zero;
- }
- public static void SetHook()
- {
- try
- {
- if (_nextHookPtr != IntPtr.Zero)//Hooked already
- {
- return;
- }
- _nextHookPtr = SetWindowsHookEx((int)HookType.CBT, myProc, IntPtr.Zero, GetCurrentThreadId());
- }
- catch { }
- }
- public static void UnHook()
- {
- if (_nextHookPtr != IntPtr.Zero)
- {
- UnhookWindowsHookEx(_nextHookPtr);
- _nextHookPtr = IntPtr.Zero;
- }
- }
- }
- }
|