2010-11-24 82 views
47

我需要将消息框控件按钮Yes改为ContinueNo改为Close。 如何更改按钮文本?如何在MessageBox.Show对话框中更改“是”和“否”按钮的按钮文本?

这里是我的代码:

DialogResult dlgResult = MessageBox.Show("Patterns have been logged successfully", "Logtool", MessageBoxButtons.YesNo, MessageBoxIcon.Information); 
+1

http://stackoverflow.com/a/12413958/2930727是正确答案。创造奇迹! – Harpreet 2013-11-22 07:54:49

回答

24

只需添加一个新的形式,并添加按钮和标签。在其构造函数中给出要显示的值和按钮的文本等,并从项目中的任何位置调用它。

In project -> Add Component -> Windows Form and select a form 

添加一些标签和按钮。

初始化构造函数中的值并从任何地方调用它。

public class form1:System.Windows.Forms.Form 
{ 
    public form1() 
    { 
    } 

    public form1(string message,string buttonText1,string buttonText2) 
    { 
     lblMessage.Text = message; 
     button1.Text = buttonText1; 
     button2.Text = buttonText2; 
    } 
} 

// Write code for button1 and button2 's click event in order to call 
// from any where in your current project. 

// Calling 

Form1 frm = new Form1("message to show", "buttontext1", "buttontext2"); 
frm.ShowDialog(); 
+7

您还需要为添加的按钮设置对话框结果属性。更多细节在这里:http://www.switchonthecode.com/tutorials/csharp-dialogs-part-2-custom-dialogs – CEPA 2012-09-20 00:28:56

+2

是的,然后你用DialogResult检查ShowDialog()。 – kbvishnu 2012-09-20 06:35:18

+0

这是一种实用的方法吗? – 2015-09-19 16:30:54

70

我不认为这会很简单! 转到此链接: https://www.codeproject.com/Articles/18399/Localizing-System-MessageBox

下载此源码。采取MessageBoxManager.cs文件,将其添加到您的项目。现在只要在你的代码(例如在您的Program.cs文件中的Main()方法)注册了一次,它的工作每次调用MessageBox.Show(时间):

MessageBoxManager.OK = "Alright"; 
    MessageBoxManager.Yes = "Yep!"; 
    MessageBoxManager.No = "Nope"; 
    MessageBoxManager.Register(); 

为源见this answer代码为MessageBoxManager.cs

13

下面是该文件的内容MessageBoxManager.cs

#pragma warning disable 0618 

using System; 

using System.Text; 

using System.Runtime.InteropServices; 

using System.Security.Permissions; 

[assembly: SecurityPermission(SecurityAction.RequestMinimum, UnmanagedCode = true)] 

namespace System.Windows.Forms 

{ 

    public class MessageBoxManager 
    { 
     private delegate IntPtr HookProc(int nCode, IntPtr wParam, IntPtr lParam); 
     private delegate bool EnumChildProc(IntPtr hWnd, IntPtr lParam); 

     private const int WH_CALLWNDPROCRET = 12; 
     private const int WM_DESTROY = 0x0002; 
     private const int WM_INITDIALOG = 0x0110; 
     private const int WM_TIMER = 0x0113; 
     private const int WM_USER = 0x400; 
     private const int DM_GETDEFID = WM_USER + 0; 

     private const int MBOK = 1; 
     private const int MBCancel = 2; 
     private const int MBAbort = 3; 
     private const int MBRetry = 4; 
     private const int MBIgnore = 5; 
     private const int MBYes = 6; 
     private const int MBNo = 7; 


     [DllImport("user32.dll")] 
     private static extern IntPtr SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, IntPtr lParam); 

     [DllImport("user32.dll")] 
     private static extern IntPtr SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hInstance, int threadId); 

     [DllImport("user32.dll")] 
     private static extern int UnhookWindowsHookEx(IntPtr idHook); 

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

     [DllImport("user32.dll", EntryPoint = "GetWindowTextLengthW", CharSet = CharSet.Unicode)] 
     private static extern int GetWindowTextLength(IntPtr hWnd); 

     [DllImport("user32.dll", EntryPoint = "GetWindowTextW", CharSet = CharSet.Unicode)] 
     private static extern int GetWindowText(IntPtr hWnd, StringBuilder text, int maxLength); 

     [DllImport("user32.dll")] 
     private static extern int EndDialog(IntPtr hDlg, IntPtr nResult); 

     [DllImport("user32.dll")] 
     private static extern bool EnumChildWindows(IntPtr hWndParent, EnumChildProc lpEnumFunc, IntPtr lParam); 

     [DllImport("user32.dll", EntryPoint = "GetClassNameW", CharSet = CharSet.Unicode)] 
     private static extern int GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount); 

     [DllImport("user32.dll")] 
     private static extern int GetDlgCtrlID(IntPtr hwndCtl); 

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

     [DllImport("user32.dll", EntryPoint = "SetWindowTextW", CharSet = CharSet.Unicode)] 
     private static extern bool SetWindowText(IntPtr hWnd, string lpString); 


     [StructLayout(LayoutKind.Sequential)] 
     public struct CWPRETSTRUCT 
     { 
      public IntPtr lResult; 
      public IntPtr lParam; 
      public IntPtr wParam; 
      public uint message; 
      public IntPtr hwnd; 
     }; 

     private static HookProc hookProc; 
     private static EnumChildProc enumProc; 
     [ThreadStatic] 
     private static IntPtr hHook; 
     [ThreadStatic] 
     private static int nButton; 

     /// <summary> 
     /// OK text 
     /// </summary> 
     public static string OK = "&OK"; 
     /// <summary> 
     /// Cancel text 
     /// </summary> 
     public static string Cancel = "&Cancel"; 
     /// <summary> 
     /// Abort text 
     /// </summary> 
     public static string Abort = "&Abort"; 
     /// <summary> 
     /// Retry text 
     /// </summary> 
     public static string Retry = "&Retry"; 
     /// <summary> 
     /// Ignore text 
     /// </summary> 
     public static string Ignore = "&Ignore"; 
     /// <summary> 
     /// Yes text 
     /// </summary> 
     public static string Yes = "&Yes"; 
     /// <summary> 
     /// No text 
     /// </summary> 
     public static string No = "&No"; 

     static MessageBoxManager() 
     { 
      hookProc = new HookProc(MessageBoxHookProc); 
      enumProc = new EnumChildProc(MessageBoxEnumProc); 
      hHook = IntPtr.Zero; 
     } 

     /// <summary> 
     /// Enables MessageBoxManager functionality 
     /// </summary> 
     /// <remarks> 
     /// MessageBoxManager functionality is enabled on current thread only. 
     /// Each thread that needs MessageBoxManager functionality has to call this method. 
     /// </remarks> 
     public static void Register() 
     { 
      if (hHook != IntPtr.Zero) 
       throw new NotSupportedException("One hook per thread allowed."); 
      hHook = SetWindowsHookEx(WH_CALLWNDPROCRET, hookProc, IntPtr.Zero, AppDomain.GetCurrentThreadId()); 
     } 

     /// <summary> 
     /// Disables MessageBoxManager functionality 
     /// </summary> 
     /// <remarks> 
     /// Disables MessageBoxManager functionality on current thread only. 
     /// </remarks> 
     public static void Unregister() 
     { 
      if (hHook != IntPtr.Zero) 
      { 
       UnhookWindowsHookEx(hHook); 
       hHook = IntPtr.Zero; 
      } 
     } 

     private static IntPtr MessageBoxHookProc(int nCode, IntPtr wParam, IntPtr lParam) 
     { 
      if (nCode < 0) 
       return CallNextHookEx(hHook, nCode, wParam, lParam); 

      CWPRETSTRUCT msg = (CWPRETSTRUCT)Marshal.PtrToStructure(lParam, typeof(CWPRETSTRUCT)); 
      IntPtr hook = hHook; 

      if (msg.message == WM_INITDIALOG) 
      { 
       int nLength = GetWindowTextLength(msg.hwnd); 
       StringBuilder className = new StringBuilder(10); 
       GetClassName(msg.hwnd, className, className.Capacity); 
       if (className.ToString() == "#32770") 
       { 
        nButton = 0; 
        EnumChildWindows(msg.hwnd, enumProc, IntPtr.Zero); 
        if (nButton == 1) 
        { 
         IntPtr hButton = GetDlgItem(msg.hwnd, MBCancel); 
         if (hButton != IntPtr.Zero) 
          SetWindowText(hButton, OK); 
        } 
       } 
      } 

      return CallNextHookEx(hook, nCode, wParam, lParam); 
     } 

     private static bool MessageBoxEnumProc(IntPtr hWnd, IntPtr lParam) 
     { 
      StringBuilder className = new StringBuilder(10); 
      GetClassName(hWnd, className, className.Capacity); 
      if (className.ToString() == "Button") 
      { 
       int ctlId = GetDlgCtrlID(hWnd); 
       switch (ctlId) 
       { 
        case MBOK: 
         SetWindowText(hWnd, OK); 
         break; 
        case MBCancel: 
         SetWindowText(hWnd, Cancel); 
         break; 
        case MBAbort: 
         SetWindowText(hWnd, Abort); 
         break; 
        case MBRetry: 
         SetWindowText(hWnd, Retry); 
         break; 
        case MBIgnore: 
         SetWindowText(hWnd, Ignore); 
         break; 
        case MBYes: 
         SetWindowText(hWnd, Yes); 
         break; 
        case MBNo: 
         SetWindowText(hWnd, No); 
         break; 

       } 
       nButton++; 
      } 

      return true; 
     } 


    } 
} 
0

这可能不是最漂亮的,但如果你不想使用MessageBoxManager,(这是真棒):

public static DialogResult DialogBox(string title, string promptText, ref string value, string button1 = "OK", string button2 = "Cancel", string button3 = null) 
    { 
     Form form = new Form(); 
     Label label = new Label(); 
     TextBox textBox = new TextBox(); 
     Button button_1 = new Button(); 
     Button button_2 = new Button(); 
     Button button_3 = new Button(); 

     int buttonStartPos = 228; //Standard two button position 


     if (button3 != null) 
      buttonStartPos = 228 - 81; 
     else 
     { 
      button_3.Visible = false; 
      button_3.Enabled = false; 
     } 


     form.Text = title; 

     // Label 
     label.Text = promptText; 
     label.SetBounds(9, 20, 372, 13); 
     label.Font = new Font("Microsoft Tai Le", 10, FontStyle.Regular); 

     // TextBox 
     if (value == null) 
     { 
     } 
     else 
     { 
      textBox.Text = value; 
      textBox.SetBounds(12, 36, 372, 20); 
      textBox.Anchor = textBox.Anchor | AnchorStyles.Right; 
     } 

     button_1.Text = button1; 
     button_2.Text = button2; 
     button_3.Text = button3 ?? string.Empty; 
     button_1.DialogResult = DialogResult.OK; 
     button_2.DialogResult = DialogResult.Cancel; 
     button_3.DialogResult = DialogResult.Yes; 


     button_1.SetBounds(buttonStartPos, 72, 75, 23); 
     button_2.SetBounds(buttonStartPos + 81, 72, 75, 23); 
     button_3.SetBounds(buttonStartPos + (2 * 81), 72, 75, 23); 

     label.AutoSize = true; 
     button_1.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; 
     button_2.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; 
     button_3.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; 

     form.ClientSize = new Size(396, 107); 
     form.Controls.AddRange(new Control[] { label, button_1, button_2 }); 
     if (button3 != null) 
      form.Controls.Add(button_3); 
     if (value != null) 
      form.Controls.Add(textBox); 

     form.ClientSize = new Size(Math.Max(300, label.Right + 10), form.ClientSize.Height); 
     form.FormBorderStyle = FormBorderStyle.FixedDialog; 
     form.StartPosition = FormStartPosition.CenterScreen; 
     form.MinimizeBox = false; 
     form.MaximizeBox = false; 
     form.AcceptButton = button_1; 
     form.CancelButton = button_2; 

     DialogResult dialogResult = form.ShowDialog(); 
     value = textBox.Text; 
     return dialogResult; 
    }