【问题标题】:VSTO Word AddIn : Capture Paste operation by Ctrl+VVSTO Word AddIn : 通过 Ctrl+V 捕获粘贴操作
【发布时间】:2021-12-07 16:42:21
【问题描述】:

我正在开发一个 VSTO Word AddIn 项目,我从外部源(一些其他 word doc)复制了一些段落,并希望在 word addin 中使用 Ctrl + V 将其粘贴。

复制一些文本并将复制的数据粘贴到 word 文档后,它应该粘贴复制的数据 + 我想添加更多自定义功能,例如保持目标 word doc 的相同格式。该怎么做?

【问题讨论】:

标签: c# vsto


【解决方案1】:

要控制将某些数据粘贴到文档中的键盘快捷键,您可以使用SetWindowsHookEx Windows API 函数设置键盘挂钩。例如:

        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);
       

        [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 void ThisAddIn_Startup(object sender, System.EventArgs e)
        {
            hookId = SetHook(HookCallback);
        }

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

        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 IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
        {
            try
            {
                if (nCode >= 0 && wParam == (IntPtr)WM_KEYDOWN)
                {
                    int pointerCode = Marshal.ReadInt32(lParam);
                    string pressedKey = ((Keys)pointerCode).ToString();

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

                        if (Control.ModifierKeys != 0 && pointerCode == 48 && Keys.Shift != 0)
                        {
                            //    
                            Microsoft.Office.Interop.Word.Selection currentSelection = Application.Selection;
                            if (currentSelection.Type == Word.WdSelectionType.wdSelectionIP)
                            {

                                currentSelection.TypeBackspace();
                                currentSelection.TypeText("()");
                                currentSelection.MoveLeft(1);

                                pointerCode = 0;
                            }
                            else
                                if (currentSelection.Type == Word.WdSelectionType.wdSelectionNormal)
                            {
                                currentSelection.TypeBackspace();
                                currentSelection.MoveLeft(1);
                                currentSelection.TypeText("()");
                                pointerCode = 0;

                            }
                            else
                            {
                                // Do nothing.
                            }
                        }
                        else
                        {

                        }

                    });
                    thread.Start();
                }
                
            }
            catch
            {

            }

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

要处理功能区控件(包括上下文菜单),您可以重新利用内置控件。请参阅Temporarily Repurpose Commands on the Office Fluent Ribbon 了解更多信息。

【讨论】:

    【解决方案2】:

    您可以使用Detours 修补GetClipboardData Windows API 函数并在新函数中运行您想要的任何代码。

    适用于 Ctrl+V 和从弹出菜单粘贴。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-28
      • 2022-06-17
      • 2020-12-14
      • 2022-12-22
      • 1970-01-01
      • 2021-12-10
      相关资源
      最近更新 更多