在保存按钮上更改defaut文本

时间:2015-07-31 12:23:15

标签: c# windows button openfiledialog savefiledialog

这是我在这里的第一个留言,而且我不是母语为英语的人,所以请提前感谢你不要轻视我的错误。

我的问题是我想要更改OpenFileDialogSaveFileDialog的默认文本,因为在我的应用程序中有一个更改语言设置的选项。 按钮文本是" Save"和"打开"。

我可以通过MessageBoxManager为其他按钮执行此操作,但是我也需要为按钮执行此操作" Save"按钮"打开"。

这些按钮必须通过其他方式处理,因为我无法找到他们的" ID" (ID_ok = 1ID_cancel = 2),我无法找到"保存"。 所以,如果您对解决方案有任何想法,我将非常感激。

#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;
        private const int MBSave = 8;


        [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";
        /// No text
        /// </summary>
        public static string Save = "&Save";


    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;
                case MBSave:
                    SetWindowText(hWnd, Save);
                    break;
                case 1038:
                    SetWindowText(hWnd, Retry);
                    break;
            }
            nButton++;
        }

        return true;
    }
}

编辑: 首先,感谢纠正我的错误(其中很多)^^ ....原因,我想翻译按钮很简单。我的程序必须翻译成5种语言,可以通过菜单选择。我没有&#34;窗口终极&#34;改变我想要的设置语言。

但无论如何,我不希望每次点击都改变操作系统的语言。我只有两个解决方案由我自己创建所有的messageBox和openDialog并更改程序调用messageBox.show由我的新表单(将花费很多时间......)或正确翻译按钮的开关/保存对话框的1500倍。所使用的计算机适用于该程序,不应使用任何其他程序。

目前我能够正确翻译所有的邮箱,但我的问题是按钮&#34;保存&#34;,&#34;打开&#34;(此文字更改为我的按钮&#34 ;好的&#34;)如果你想我可以告诉你在哪里可以下载项目以更好地了解实现的功能

0 个答案:

没有答案