-2

私の問題は、タイプされた2つの文字の組み合わせを動的に変更し、それらを新しい1つの文字に置き換えることです。「cx」と入力すると、すぐに「ĉ」に置き換えられます。誰かがこれに関するコードを教えてくれませんか? ありがとう

4

1 に答える 1

0

WinFormsを想定して...

簡単な例を次に示します。

public partial class Form1 : Form
{

    private const int WM_SETREDRAW = 0xB;

    [System.Runtime.InteropServices.DllImport("user32.dll")]
    public static extern int SendMessage(IntPtr hWnd, Int32 wMsg, bool wParam, Int32 lParam);

    private Dictionary<string, string> replacements = new Dictionary<string, string>();

    public Form1()
    {
        InitializeComponent();

        replacements.Add("cx", "ĉ");
        replacements.Add("ae", "æ");
        // etc...
    }

    private void richTextBox1_TextChanged(object sender, EventArgs e)
    {
        if (cbAutoReplacements.Checked)
        {
            int prevStart = richTextBox1.SelectionStart;
            int prevLength = richTextBox1.SelectionLength;
            SendMessage(richTextBox1.Handle, WM_SETREDRAW, false, 0);

            int index;
            int start;
            foreach (KeyValuePair<string, string> pair in replacements)
            {
                start = 0;
                index = richTextBox1.Find(pair.Key, start, RichTextBoxFinds.MatchCase);
                while (index != -1)
                {
                    richTextBox1.Select(index, pair.Key.Length);
                    richTextBox1.SelectedText = pair.Value;
                    index = richTextBox1.Find(pair.Key, ++start, RichTextBoxFinds.MatchCase);
                }
            }

            richTextBox1.Select(prevStart, prevLength);
            SendMessage(richTextBox1.Handle, WM_SETREDRAW, true, 0);
            richTextBox1.Invalidate();
        }
    }

}
于 2015-10-18T20:23:05.843 に答える