自定义MessageBox按钮
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了自定义MessageBox按钮相关的知识,希望对你有一定的参考价值。
[DllImport("kernel32.dll")] static extern uint GetCurrentThreadId(); [DllImport("user32.dll", CharSet = CharSet.Auto)] private static extern int CallNextHookEx(int idHook, int nCode, IntPtr wParam, IntPtr lParam); [DllImport("user32.dll", CharSet = CharSet.Auto)] private static extern bool UnhookWindowsHookEx(int idHook); [DllImport("user32.dll", CharSet = CharSet.Auto)] private static extern int SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hInstance, int threadId); [DllImport("user32.dll")] private static extern bool SetDlgItemText(IntPtr hWnd, int nIDDlgItem, string lpString); delegate int HookProc(int nCode, IntPtr wParam, IntPtr lParam); static HookProc dlgHookProc; private const long WH_CBT = 5; private const long HCBT_ACTIVATE = 5; private const int ID_BUT_OK = 1; private const int ID_BUT_CANCEL = 2; private const int ID_BUT_ABORT = 3; private const int ID_BUT_RETRY = 4; private const int ID_BUT_IGNORE = 5; private const int ID_BUT_YES = 6; private const int ID_BUT_NO = 7; private const string BUT_OK = "Save"; private const string BUT_CANCEL = "Cancel"; private const string BUT_ABORT = "Stop"; private const string BUT_RETRY = "Continue"; private const string BUT_IGNORE = "Ignore"; private const string BUT_YES = "Yeeh"; private const string BUT_NO = "Never"; private static int _hook = 0; private static int DialogHookProc(int nCode, IntPtr wParam, IntPtr lParam) { if (nCode < 0) { return CallNextHookEx(_hook, nCode, wParam, lParam); } if (nCode == HCBT_ACTIVATE) { SetDlgItemText(wParam, ID_BUT_OK, BUT_OK); SetDlgItemText(wParam, ID_BUT_CANCEL, BUT_CANCEL); SetDlgItemText(wParam, ID_BUT_ABORT, BUT_ABORT); SetDlgItemText(wParam, ID_BUT_RETRY, BUT_RETRY); SetDlgItemText(wParam, ID_BUT_IGNORE, BUT_IGNORE); SetDlgItemText(wParam, ID_BUT_YES, BUT_YES); SetDlgItemText(wParam, ID_BUT_NO, BUT_NO); } return CallNextHookEx(_hook, nCode, wParam, lParam); } private void Button_Click(object sender, EventArgs e) { _hook = SetWindowsHookEx((int)WH_CBT, dlgHookProc, (IntPtr)0, (int)GetCurrentThreadId()); DialogResult dlgEmptyCheck = MessageBox.Show("Text", "Caption", MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button3); if (dlgEmptyCheck == DialogResult.Abort) { } UnhookWindowsHookEx(_hook); }
以上是关于自定义MessageBox按钮的主要内容,如果未能解决你的问题,请参考以下文章
C语言怎么自定义MessageBox函数的按钮?要新窗口吗?