MessageBoxEx.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  1. using System;
  2. using System.Windows.Forms;
  3. using System.Runtime.InteropServices;
  4. using ComPDFKit.Controls.Helper;
  5. namespace ComPDFKit.Controls.Common
  6. {
  7. public class MessageBoxEx
  8. {
  9. private static string[] okstring = new string[] { LanguageHelper.CompressManager.GetString("Main_Ok")};
  10. private static string[] ok = okstring;
  11. private static string[] okcancelstring = new string[] { LanguageHelper.CompressManager.GetString("Main_Ok") , LanguageHelper.CompressManager.GetString("Main_Cancel")};
  12. private static string[] okcancel = okcancelstring;
  13. private static string[] abortretryignorestring = new string[] { LanguageHelper.CompressManager.GetString("Main_MenuHelp_About") , "Retry", "Ignore" };
  14. private static string[] abortretryignore = abortretryignorestring;
  15. private static string[] yesnocancelstring = new string[] { LanguageHelper.CompressManager.GetString("Main_Yes"), LanguageHelper.CompressManager.GetString("Main_No"), LanguageHelper.CompressManager.GetString("Main_Cancel") };
  16. private static string[] yesnocancel = yesnocancelstring;
  17. private static string[] yesnostring = new string[] { LanguageHelper.CompressManager.GetString("Main_Yes"), LanguageHelper.CompressManager.GetString("Main_No") };
  18. private static string[] yesno = yesnostring;
  19. private static string[] retrycancelstring = new string[] { "Retry", LanguageHelper.CompressManager.GetString("Main_Cancel") };
  20. private static string[] retrycancel = retrycancelstring;
  21. public static DialogResult Show(string text,string[] buttonTitles = null)
  22. {
  23. if(buttonTitles!=null&&buttonTitles.Length==1)
  24. {
  25. ok = buttonTitles;
  26. }
  27. else
  28. {
  29. ok = okstring;
  30. }
  31. myProc = new HookProc(OK);
  32. SetHook();
  33. DialogResult result = MessageBox.Show(text, Application.ProductName,MessageBoxButtons.OK);
  34. UnHook();
  35. return result;
  36. }
  37. public static DialogResult Show(string text,string title,string[] buttonTitles = null)
  38. {
  39. if (buttonTitles != null && buttonTitles.Length == 1)
  40. {
  41. ok = buttonTitles;
  42. }
  43. else
  44. {
  45. ok = okstring;
  46. }
  47. myProc = new HookProc(OK);
  48. SetHook();
  49. DialogResult result = MessageBox.Show(text,title);
  50. UnHook();
  51. return result;
  52. }
  53. public static DialogResult Show(string text, string caption, MessageBoxButtons buttons, string[] buttonTitles=null)
  54. {
  55. switch (buttons)
  56. {
  57. case MessageBoxButtons.OK:
  58. if (buttonTitles != null && buttonTitles.Length == 1)
  59. {
  60. ok = buttonTitles;
  61. }
  62. else
  63. {
  64. ok = okstring;
  65. }
  66. myProc = new HookProc(OK);
  67. break;
  68. case MessageBoxButtons.OKCancel:
  69. if (buttonTitles != null && buttonTitles.Length == 2)
  70. {
  71. okcancel = buttonTitles;
  72. }
  73. else
  74. {
  75. okcancel = okcancelstring;
  76. }
  77. myProc = new HookProc(OKCancel);
  78. break;
  79. case MessageBoxButtons.AbortRetryIgnore:
  80. if (buttonTitles != null && buttonTitles.Length == 3)
  81. {
  82. abortretryignore = buttonTitles;
  83. }
  84. else
  85. {
  86. abortretryignore = abortretryignorestring;
  87. }
  88. myProc = new HookProc(AbortRetryIgnore);
  89. break;
  90. case MessageBoxButtons.YesNoCancel:
  91. if (buttonTitles != null && buttonTitles.Length == 3)
  92. {
  93. yesnocancel = buttonTitles;
  94. }
  95. else
  96. {
  97. yesnocancel = yesnocancelstring;
  98. }
  99. myProc = new HookProc(YesNoCancel);
  100. break;
  101. case MessageBoxButtons.YesNo:
  102. if (buttonTitles != null && buttonTitles.Length == 2)
  103. {
  104. yesno = buttonTitles;
  105. }
  106. else
  107. {
  108. yesno = yesnostring;
  109. }
  110. myProc = new HookProc(YesNo);
  111. break;
  112. case MessageBoxButtons.RetryCancel:
  113. if (buttonTitles != null && buttonTitles.Length == 2)
  114. {
  115. retrycancel = buttonTitles;
  116. }
  117. else
  118. {
  119. retrycancel = retrycancelstring;
  120. }
  121. myProc = new HookProc(RetryCancel);
  122. break;
  123. default:
  124. break;
  125. }
  126. SetHook();
  127. DialogResult result = MessageBox.Show(text, string.IsNullOrEmpty(caption)?Application.ProductName:caption, buttons);
  128. UnHook();
  129. return result;
  130. }
  131. public static DialogResult Show(string text, string caption, MessageBoxButtons buttons,MessageBoxIcon icons,string[] buttonTitles = null)
  132. {
  133. switch (buttons)
  134. {
  135. case MessageBoxButtons.OK:
  136. if (buttonTitles != null && buttonTitles.Length == 1)
  137. {
  138. ok = buttonTitles;
  139. }
  140. else
  141. {
  142. ok = okstring;
  143. }
  144. myProc = new HookProc(OK);
  145. break;
  146. case MessageBoxButtons.OKCancel:
  147. if (buttonTitles != null && buttonTitles.Length == 2)
  148. {
  149. okcancel = buttonTitles;
  150. }
  151. else
  152. {
  153. okcancel = okcancelstring;
  154. }
  155. myProc = new HookProc(OKCancel);
  156. break;
  157. case MessageBoxButtons.AbortRetryIgnore:
  158. if (buttonTitles != null && buttonTitles.Length == 3)
  159. {
  160. abortretryignore = buttonTitles;
  161. }
  162. else
  163. {
  164. abortretryignore = abortretryignorestring;
  165. }
  166. myProc = new HookProc(AbortRetryIgnore);
  167. break;
  168. case MessageBoxButtons.YesNoCancel:
  169. if (buttonTitles != null && buttonTitles.Length == 3)
  170. {
  171. yesnocancel = buttonTitles;
  172. }
  173. else
  174. {
  175. yesnocancel = yesnocancelstring;
  176. }
  177. myProc = new HookProc(YesNoCancel);
  178. break;
  179. case MessageBoxButtons.YesNo:
  180. if (buttonTitles != null && buttonTitles.Length == 2)
  181. {
  182. yesno = buttonTitles;
  183. }
  184. else
  185. {
  186. yesno = yesnostring;
  187. }
  188. myProc = new HookProc(YesNo);
  189. break;
  190. case MessageBoxButtons.RetryCancel:
  191. if (buttonTitles != null && buttonTitles.Length == 2)
  192. {
  193. retrycancel = buttonTitles;
  194. }
  195. else
  196. {
  197. retrycancel = retrycancelstring;
  198. }
  199. myProc = new HookProc(RetryCancel);
  200. break;
  201. default:
  202. break;
  203. }
  204. SetHook();
  205. DialogResult result = MessageBox.Show(text, string.IsNullOrEmpty(caption) ? Application.ProductName : caption, buttons, icons);
  206. UnHook();
  207. return result;
  208. }
  209. public static DialogResult Show(string text, string caption, MessageBoxButtons buttons,
  210. MessageBoxIcon icon, MessageBoxDefaultButton defaultButton, string[] buttonTitles=null)
  211. {
  212. switch (buttons)
  213. {
  214. case MessageBoxButtons.OK:
  215. if (buttonTitles != null && buttonTitles.Length == 1)
  216. {
  217. ok = buttonTitles;
  218. }
  219. else
  220. {
  221. ok = okstring;
  222. }
  223. myProc = new HookProc(OK);
  224. break;
  225. case MessageBoxButtons.OKCancel:
  226. if (buttonTitles != null && buttonTitles.Length == 2)
  227. {
  228. okcancel = buttonTitles;
  229. }
  230. else
  231. {
  232. okcancel = okcancelstring;
  233. }
  234. myProc = new HookProc(OKCancel);
  235. break;
  236. case MessageBoxButtons.AbortRetryIgnore:
  237. if (buttonTitles != null && buttonTitles.Length == 3)
  238. {
  239. abortretryignore = buttonTitles;
  240. }
  241. else
  242. {
  243. abortretryignore = abortretryignorestring;
  244. }
  245. myProc = new HookProc(AbortRetryIgnore);
  246. break;
  247. case MessageBoxButtons.YesNoCancel:
  248. if (buttonTitles != null && buttonTitles.Length == 3)
  249. {
  250. yesnocancel = buttonTitles;
  251. }
  252. else
  253. {
  254. yesnocancel = yesnocancelstring;
  255. }
  256. myProc = new HookProc(YesNoCancel);
  257. break;
  258. case MessageBoxButtons.YesNo:
  259. if (buttonTitles != null && buttonTitles.Length == 2)
  260. {
  261. yesno = buttonTitles;
  262. }
  263. else
  264. {
  265. yesno = yesnostring;
  266. }
  267. myProc = new HookProc(YesNo);
  268. break;
  269. case MessageBoxButtons.RetryCancel:
  270. if (buttonTitles != null && buttonTitles.Length == 2)
  271. {
  272. retrycancel = buttonTitles;
  273. }
  274. else
  275. {
  276. retrycancel = retrycancelstring;
  277. }
  278. myProc = new HookProc(RetryCancel);
  279. break;
  280. default:
  281. break;
  282. }
  283. DialogResult result = MessageBox.Show(text, string.IsNullOrEmpty(caption) ? Application.ProductName : caption, buttons, icon, defaultButton);
  284. return result;
  285. }
  286. public enum HookType
  287. {
  288. Keyboard = 2,
  289. CBT = 5,
  290. Mouse = 7,
  291. };
  292. [DllImport("kernel32.dll")]
  293. static extern int GetCurrentThreadId();
  294. [DllImport("user32.dll")]
  295. static extern int GetDlgItem(IntPtr hDlg, int nIDDlgItem);
  296. [DllImport("user32", EntryPoint = "SetDlgItemText")]
  297. static extern int SetDlgItemTextA(IntPtr hDlg, int nIDDlgItem, string lpString);
  298. [DllImport("user32.dll", EntryPoint = "SetWindowTextW", CharSet = CharSet.Unicode)]
  299. private static extern bool SetWindowText(IntPtr hWnd, string lpString);
  300. [DllImport("user32.dll")]
  301. static extern void UnhookWindowsHookEx(IntPtr handle);
  302. [DllImport("user32.dll")]
  303. static extern IntPtr SetWindowsHookEx(int idHook, [MarshalAs(UnmanagedType.FunctionPtr)] HookProc lpfn, IntPtr hInstance, int threadID);
  304. [DllImport("user32.dll")]
  305. static extern IntPtr CallNextHookEx(IntPtr handle, int code, IntPtr wparam, IntPtr lparam);
  306. static IntPtr _nextHookPtr;
  307. ////must be global, or it will be Collected by GC, then no callback func can be used for the Hook
  308. static HookProc myProc = new HookProc(MyHookProc);
  309. private delegate IntPtr HookProc(int code, IntPtr wparam, IntPtr lparam);
  310. private static IntPtr OK(int code, IntPtr wparam, IntPtr lparam)
  311. {
  312. IntPtr hChildWnd;
  313. if (code == 5)//HCBT_ACTIVATE = 5
  314. {
  315. hChildWnd = wparam;
  316. var index = (IntPtr)GetDlgItem(hChildWnd,1);
  317. if (index != IntPtr.Zero)
  318. {
  319. SetWindowText(index, ok[0]);
  320. }
  321. }
  322. else
  323. CallNextHookEx(_nextHookPtr, code, wparam, lparam);
  324. return IntPtr.Zero;
  325. }
  326. private static IntPtr OKCancel(int code, IntPtr wparam, IntPtr lparam)
  327. {
  328. IntPtr hChildWnd;
  329. bool result = false;
  330. if (code == 5)//HCBT_ACTIVATE = 5
  331. {
  332. hChildWnd = wparam;
  333. IntPtr index = (IntPtr)GetDlgItem(hChildWnd, 1);
  334. if (index != IntPtr.Zero)
  335. {
  336. result = SetWindowText(index, okcancel[0]);
  337. }
  338. index = (IntPtr)GetDlgItem(hChildWnd, 2);
  339. if (index != IntPtr.Zero)
  340. {
  341. result = SetWindowText(index, okcancel[1]);
  342. }
  343. }
  344. else
  345. CallNextHookEx(_nextHookPtr, code, wparam, lparam);
  346. return IntPtr.Zero;
  347. }
  348. private static IntPtr RetryCancel(int code, IntPtr wparam, IntPtr lparam)
  349. {
  350. IntPtr hChildWnd;
  351. if (code == 5)//HCBT_ACTIVATE = 5
  352. {
  353. hChildWnd = wparam;
  354. var index = (IntPtr)GetDlgItem(hChildWnd,4);
  355. if (index!=IntPtr.Zero)
  356. {
  357. SetWindowText(index, retrycancel[0]);
  358. }
  359. index = (IntPtr)GetDlgItem(hChildWnd, 2);
  360. if (GetDlgItem(hChildWnd, 2) != 0)
  361. {
  362. SetWindowText(index, retrycancel[1]);
  363. }
  364. }
  365. else
  366. CallNextHookEx(_nextHookPtr, code, wparam, lparam);
  367. return IntPtr.Zero;
  368. }
  369. private static IntPtr YesNo(int code, IntPtr wparam, IntPtr lparam)
  370. {
  371. IntPtr hChildWnd;
  372. if (code == 5)//HCBT_ACTIVATE = 5
  373. {
  374. hChildWnd = wparam;
  375. var index = (IntPtr)GetDlgItem(hChildWnd, 6);
  376. if (index!=IntPtr.Zero)
  377. {
  378. SetWindowText(index, yesno[0]);
  379. }
  380. index = (IntPtr)GetDlgItem(hChildWnd, 7);
  381. if (index != IntPtr.Zero)
  382. {
  383. SetWindowText(index, yesno[1]);
  384. }
  385. }
  386. else
  387. CallNextHookEx(_nextHookPtr, code, wparam, lparam);
  388. return IntPtr.Zero;
  389. }
  390. private static IntPtr YesNoCancel(int code, IntPtr wparam, IntPtr lparam)
  391. {
  392. IntPtr hChildWnd;
  393. if (code == 5)//HCBT_ACTIVATE = 5
  394. {
  395. hChildWnd = wparam;
  396. var index = (IntPtr)GetDlgItem(hChildWnd, 6);
  397. if (index != IntPtr.Zero)
  398. {
  399. SetWindowText(index,yesnocancel[0]);
  400. }
  401. index = (IntPtr)GetDlgItem(hChildWnd, 7);
  402. if (index != IntPtr.Zero)
  403. {
  404. SetWindowText(index, yesnocancel[1]);
  405. }
  406. index = (IntPtr)GetDlgItem(hChildWnd, 2);
  407. if (index != IntPtr.Zero)
  408. {
  409. SetWindowText(index, yesnocancel[2]);
  410. }
  411. }
  412. else
  413. CallNextHookEx(_nextHookPtr, code, wparam, lparam);
  414. return IntPtr.Zero;
  415. }
  416. private static IntPtr AbortRetryIgnore(int code, IntPtr wparam, IntPtr lparam)
  417. {
  418. IntPtr hChildWnd;
  419. if (code == 5)//HCBT_ACTIVATE = 5
  420. {
  421. hChildWnd = wparam;
  422. var index = (IntPtr)GetDlgItem(hChildWnd, 3);
  423. if (index != IntPtr.Zero)
  424. {
  425. SetWindowText(index, abortretryignore[0]);
  426. }
  427. index = (IntPtr)GetDlgItem(hChildWnd, 4);
  428. if (index != IntPtr.Zero)
  429. {
  430. SetWindowText(index, abortretryignore[1]);
  431. }
  432. index = (IntPtr)GetDlgItem(hChildWnd, 5);
  433. if (index != IntPtr.Zero)
  434. {
  435. SetWindowText(index, abortretryignore[2]);
  436. }
  437. }
  438. else
  439. CallNextHookEx(_nextHookPtr, code, wparam, lparam);
  440. return IntPtr.Zero;
  441. }
  442. private static IntPtr MyHookProc(int code, IntPtr wparam, IntPtr lparam)
  443. {
  444. IntPtr hChildWnd;// msgbox is "child"
  445. // notification that a window is about to be activated
  446. // window handle is wParam
  447. if (code == 5)//HCBT_ACTIVATE = 5
  448. {
  449. // set window handles of messagebox
  450. hChildWnd = wparam;
  451. //to get the text of yes button
  452. for(int i=0;i<21;i++)
  453. {
  454. if (GetDlgItem(hChildWnd, i) != 0)
  455. SetDlgItemTextA(hChildWnd,i,string.Format("Item {0}",i));
  456. }
  457. }
  458. else
  459. {
  460. CallNextHookEx(_nextHookPtr, code, wparam, lparam);// otherwise, continue with any possible chained hooks
  461. }
  462. return IntPtr.Zero;
  463. }
  464. public static void SetHook()
  465. {
  466. try
  467. {
  468. if (_nextHookPtr != IntPtr.Zero)//Hooked already
  469. {
  470. return;
  471. }
  472. _nextHookPtr = SetWindowsHookEx((int)HookType.CBT, myProc, IntPtr.Zero, GetCurrentThreadId());
  473. }
  474. catch { }
  475. }
  476. public static void UnHook()
  477. {
  478. if (_nextHookPtr != IntPtr.Zero)
  479. {
  480. UnhookWindowsHookEx(_nextHookPtr);
  481. _nextHookPtr = IntPtr.Zero;
  482. }
  483. }
  484. }
  485. }