티스토리 뷰

MessageBox의 버튼 텍스트 변경하기


기본 메시지박스의 텍스트를 변경할 수 있다.




데브피아 C#마을 조동현님께서 올리신 팁입니다.

개인 라이브러리에 추가해서 유용하게 잘 사용하고 있습니다.

 



아래 내용을 참고하면 아래와 같은 메세지 박스를 생성할 수 있습니다.

 

 

 

    using System;
    using System.Windows.Forms;
    using System.Runtime.InteropServices;

    public class MessageBoxEx
    {
        delegate int HookProc(int code, IntPtr wParam, IntPtr lParam);

        [DllImport("user32.dll", SetLastError = true)]
        static extern IntPtr SetWindowsHookEx(int hook, HookProc callback, IntPtr hMod, uint dwThreadId);

        [DllImport("user32.dll")]
        static extern bool UnhookWindowsHookEx(IntPtr hhk);

        [DllImport("user32.dll")]
        static extern int CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, IntPtr lParam);

        [DllImport("user32.dll")]
        static extern IntPtr GetDlgItem(IntPtr hDlg, DialogResult nIDDlgItem);

        [DllImport("user32.dll")]
        static extern bool SetDlgItemText(IntPtr hDlg, DialogResult nIDDlgItem, string lpString);

        [DllImport("kernel32.dll")]
        static extern uint GetCurrentThreadId();

        static IntPtr g_hHook;

        static string yes;
        static string cancel;
        static string no;

 

        /// <summary>
        /// 메시지 박스를 띠웁니다.
        /// </summary>
        /// <param name="text">텍스트 입니다.</param>
        /// <param name="caption">캡션 입니다.</param>
        /// <param name="yes">예 문자열 입니다.</param>
        /// <param name="no">아니오 문자열 입니다.</param>
        /// <param name="cancel">취소 문자열 입니다.</param>
        /// <returns></returns>
        public static DialogResult Show(string text, string caption, string yes, string no, string cancel)
        {
            MessageBoxEx.yes = yes;
            MessageBoxEx.cancel = cancel;
            MessageBoxEx.no = no;
            g_hHook = SetWindowsHookEx(5, new HookProc(HookWndProc), IntPtr.Zero, GetCurrentThreadId());
            return MessageBox.Show(text, caption, MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
        }

        static int HookWndProc(int nCode, IntPtr wParam, IntPtr lParam)
        {
            IntPtr hChildWnd;

            if (nCode == 5)
            {
                hChildWnd = wParam;

                if (GetDlgItem(hChildWnd, DialogResult.Yes) != null)
                    SetDlgItemText(hChildWnd, DialogResult.Yes, MessageBoxEx.yes);

                if (GetDlgItem(hChildWnd, DialogResult.No) != null)
                    SetDlgItemText(hChildWnd, DialogResult.No, MessageBoxEx.no);

                if (GetDlgItem(hChildWnd, DialogResult.Cancel) != null)
                    SetDlgItemText(hChildWnd, DialogResult.Cancel, MessageBoxEx.cancel);

                UnhookWindowsHookEx(g_hHook);
            }
            else
                CallNextHookEx(g_hHook, nCode, wParam, lParam);

            return 0;
        }
    }

 

엄청난 DllImport와 static이지요...

 

ex)

MessageBoxEx.Show("정지 하시겠습니까?", "정지", "야스", "노노", "취소닷!");

댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2024/04   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30
글 보관함