繁体   English   中英

C#中MS Word的加载项TextChange事件

[英]MS Word's Add-in TextChange Event in C#

我有一个Microsoft Word加载项,可以在文本中找到相似的单词(但是当我单击一个按钮时!)

我的问题是:如何在用户输入单词时调用函数?

换句话说,当用户键入以获取当前单词并处理它并获得类似单词时,我想要一个类似“TextChange”或“Keypress”的事件。

有些事情像这样:

private void TextChangeEventOfCurrentActiveDocument(object sender, System.EventArgs e)
{
    ...
}

任何其他想法,我可以得到用户输入的新单词?

谢谢。

最后,经过很长一段时间,我使用Windows钩子创建了这个加载项。

(特别感谢@Reg编辑)

在这里,我写的整个代码,这对我来说很好。 (删除了一些可选的代码部分。)

ThisAddIn.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using Word = Microsoft.Office.Interop.Word;
using Office = Microsoft.Office.Core;
using Microsoft.Office.Tools.Word;
using System.Windows.Forms;

using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Threading;

namespace PersianWords
{
    public partial class ThisAddIn
    {

        private const int WH_KEYBOARD_LL = 13;
        private const int WM_KEYDOWN = 0x0100;

        private static IntPtr hookId = IntPtr.Zero;
        private delegate IntPtr HookProcedure(int nCode, IntPtr wParam, IntPtr lParam);
        private static HookProcedure procedure = HookCallback;

        [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        private static extern IntPtr GetModuleHandle(string lpModuleName);

        [DllImport("user32.dll", SetLastError = true)]
        private static extern bool UnhookWindowsHookEx(IntPtr hhk);

        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        private static extern IntPtr SetWindowsHookEx(int idHook, HookProcedure lpfn, IntPtr hMod, uint dwThreadId);

        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        private static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, IntPtr lParam);

        private static IntPtr SetHook(HookProcedure procedure)
        {
            using (Process process = Process.GetCurrentProcess())
            using (ProcessModule module = process.MainModule)
                return SetWindowsHookEx(WH_KEYBOARD_LL, procedure, GetModuleHandle(module.ModuleName), 0);
        }


        private static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
        {

            if (nCode >= 0 && wParam == (IntPtr)WM_KEYDOWN)
            {
                int pointerCode = Marshal.ReadInt32(lParam);

                if (pointerCode == 162 || pointerCode == 160)
                {
                    return CallNextHookEx(hookId, nCode, wParam, lParam);
                }

                string pressedKey = ((Keys)pointerCode).ToString();

                //Do some sort of processing on key press
                var thread = new Thread(() =>
                {

                    MyClass.WrdApp.CustomizationContext = MyClass.WrdApp.ActiveDocument;

                    //do something with current document


                });
                thread.Start();
            }



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


        private void ThisAddIn_Startup(object sender, System.EventArgs e)
        {
            hookId = SetHook(procedure);

            MyClass.WrdApp = Application;

            MyClass.WrdApp.CustomizationContext = MyClass.WrdApp.ActiveDocument;

        }


        private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
        {
            UnhookWindowsHookEx(hookId);
        }

    #region VSTO generated code

    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InternalStartup()
    {
        this.Startup += new System.EventHandler(ThisAddIn_Startup);
        this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);


    }

    #endregion
    }
}

很抱歉,但没有这样的事件。 什么都没有接近它。

所以你被一个按钮困住,或者使用某种计时器每隔一段时间检查一次内容( Timer类可能是一个选项)。

您可以使用Windows挂钩拦截来自另一个窗口的键击(在本例中为Word)。

或者,Word Application有一个WindowSelectionChange事件,该事件在键入时不会触发,但如果用户使用箭头键移动光标或单击一个单词,则会触发。 这将允许您对单击的单词做出反应,而不是用户必须移动到屏幕上的其他位置才能单击按钮。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM