自定义MessageBox按钮

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了自定义MessageBox按钮相关的知识,希望对你有一定的参考价值。

  1. [DllImport("kernel32.dll")]
  2. static extern uint GetCurrentThreadId();
  3.  
  4. [DllImport("user32.dll", CharSet = CharSet.Auto)]
  5. private static extern int CallNextHookEx(int idHook, int nCode, IntPtr wParam, IntPtr lParam);
  6.  
  7. [DllImport("user32.dll", CharSet = CharSet.Auto)]
  8. private static extern bool UnhookWindowsHookEx(int idHook);
  9.  
  10. [DllImport("user32.dll", CharSet = CharSet.Auto)]
  11. private static extern int SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hInstance, int threadId);
  12.  
  13. [DllImport("user32.dll")]
  14. private static extern bool SetDlgItemText(IntPtr hWnd, int nIDDlgItem, string lpString);
  15.  
  16. delegate int HookProc(int nCode, IntPtr wParam, IntPtr lParam);
  17.  
  18. static HookProc dlgHookProc;
  19.  
  20. private const long WH_CBT = 5;
  21. private const long HCBT_ACTIVATE = 5;
  22.  
  23. private const int ID_BUT_OK = 1;
  24. private const int ID_BUT_CANCEL = 2;
  25. private const int ID_BUT_ABORT = 3;
  26. private const int ID_BUT_RETRY = 4;
  27. private const int ID_BUT_IGNORE = 5;
  28. private const int ID_BUT_YES = 6;
  29. private const int ID_BUT_NO = 7;
  30.  
  31. private const string BUT_OK = "Save";
  32. private const string BUT_CANCEL = "Cancel";
  33. private const string BUT_ABORT = "Stop";
  34. private const string BUT_RETRY = "Continue";
  35. private const string BUT_IGNORE = "Ignore";
  36. private const string BUT_YES = "Yeeh";
  37. private const string BUT_NO = "Never";
  38.  
  39. private static int _hook = 0;
  40.  
  41. private static int DialogHookProc(int nCode, IntPtr wParam, IntPtr lParam)
  42. {
  43. if (nCode < 0)
  44. {
  45. return CallNextHookEx(_hook, nCode, wParam, lParam);
  46. }
  47.  
  48. if (nCode == HCBT_ACTIVATE)
  49. {
  50. SetDlgItemText(wParam, ID_BUT_OK, BUT_OK);
  51. SetDlgItemText(wParam, ID_BUT_CANCEL, BUT_CANCEL);
  52. SetDlgItemText(wParam, ID_BUT_ABORT, BUT_ABORT);
  53. SetDlgItemText(wParam, ID_BUT_RETRY, BUT_RETRY);
  54. SetDlgItemText(wParam, ID_BUT_IGNORE, BUT_IGNORE);
  55. SetDlgItemText(wParam, ID_BUT_YES, BUT_YES);
  56. SetDlgItemText(wParam, ID_BUT_NO, BUT_NO);
  57. }
  58.  
  59. return CallNextHookEx(_hook, nCode, wParam, lParam);
  60. }
  61.  
  62. private void Button_Click(object sender, EventArgs e)
  63. {
  64. dlgHookProc = new HookProc(DialogHookProc);
  65.  
  66. _hook = SetWindowsHookEx((int)WH_CBT, dlgHookProc, (IntPtr)0, (int)GetCurrentThreadId());
  67.  
  68. DialogResult dlgEmptyCheck = MessageBox.Show("Text", "Caption", MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button3);
  69.  
  70. if (dlgEmptyCheck == DialogResult.Abort)
  71. {
  72.  
  73. }
  74.  
  75. UnhookWindowsHookEx(_hook);
  76. }

以上是关于自定义MessageBox按钮的主要内容,如果未能解决你的问题,请参考以下文章

C语言怎么自定义MessageBox函数的按钮?要新窗口吗?

如何在 Windows Phone 的 Messagebox 中添加超链接按钮?

Messagebox自定义计时关闭

自定义类似MessageBox小窗体操作

MessageBox

调整显示自定义字体大小的 MessageBox 窗口的大小?