VSTO Word 2016:波浪形下划线而不影响撤消 [英] VSTO Word 2016: Squiggly underline without affecting undo

查看:33
本文介绍了VSTO Word 2016:波浪形下划线而不影响撤消的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一种实时语言分析工具,该工具需要突出显示单词以在 Word 2016 中使用 VSTO 加载项引起作者的注意,该加载项使用 C# 以 .NET4.6.1 编写.想想语法/拼写检查,它会在单词下方添加一条波浪线,以表明该单词存在语法或拼写错误.我正在为我自己定义的一些规则添加类似的功能.

我四处寻找添加波浪线的方法,偶然发现了 Font.UnderlineFont.UnderlineColor.我把它设置在一个词的范围内,它似乎提供了我想要引起注意的视觉效果.但是,有一个问题.我添加的每个下划线或我更改的下划线颜色都会向撤消堆栈添加撤消操作.

我不希望这种情况发生,或者我想要一种方法从堆栈中弹出我刚刚在代码中执行的操作.目的是让用户能够使用CTRL+Z删除他更改的文本,并且不影响我的语言分析结果.

我该怎么做?

解决方案

我偶然发现了这个问题,它提出了完全相同的问题:

因此,如果您担心捕获按钮命令(我认识的每个人都使用 Ctrl+Z & Y),您可能会注入 VBA 代码以访问 EditUndoEditRedo 事件,例如:

VB._VBComponent vbModule = VBProj.VBE.ActiveVBProject.VBComponents.Add(VB.vbext_ComponentType.vbext_ct_StdModule);String functionText = "Public Sub EditUndo() 
";functionText += "MsgBox "撤消发生"
";functionText += "结束子";vbModule.CodeModule.AddFromString(functionText);

这种方法的主要问题是需要授予信任.

<小时>

同一 QA 中的另一个答案我可以在 Word 或 Excel 中创建撤消事务吗?(VSTO) 是 Mike Regan 的,他在 Dirk 之后 3 个月回答了.他使用了一个隐藏文档,并在需要时将其放置在真实文档中,以便一次性撤消任何数量的 VSTO 操作.

仍然没有解决阻止操作被记录在撤消历史记录中的问题.

<小时>

我确实尝试过使用注册表项将 UndoHistory 限制为 0 并将其重置为 100(以便在添加操作时禁用历史记录),但它似乎仅适用于 Excelhttps://support.microsoft.com/en-gb/kb/211922

可能有一个未记录的 reg 键可以完全禁用撤消/重做历史记录,但它只能在启动时由 Word 读取.我以为每次撤消/重做之前都会读取包含数字的 UndoHistory 键,但这种方法根本没有运气.

<小时>

这不是一个容易解决的问题,有很大的限制,所以可能更容易:

a) 接受您的拼写/语法检查插件包含在撤消/重做列表中(失败).

b) 找出行/文本在屏幕上的位置并显示突出显示问题的透明工具提示.这比看起来要困难得多,也不理想,这里有两个很好的答案可以指导您使用此方法:从 VSTO 加载项检测 Word 2016 中的文本更改 或更简单的方法来检测来自此 Microsoft 电子邮件线程的 XY 位置:https://groups.google.com/forum/#!topic/microsoft.public.word.vba.general/pKq4PsqD3cM

//定位对话框相对于单词插入点(插入符号)int 左 = 0;int 顶部 = 0;整数宽度 = 0;整数高度 = 0;MSWord.Range r = Globals.ThisDocument.Application.Selection.Range;MSWord.Window w = Globals.ThisDocument.ActiveWindow;w.GetPoint(out left, out top, out width, out height, r);frmPopUp newForm = 新 frmPopUp();newForm.SetDesktopLocation( left + width + 2, top - newForm.Height + height );

c) 仅通过键盘使用事务性撤消/重做捕获撤消/重做事件,并让用户使用按钮查看原子撤消/重做.删除撤消/重做按钮将是非常狡猾的,这会导致我的撤消按钮去哪儿了?支持案例的堆.所以不要这样做,如果你好奇,可以从.qat"文件快速访问工具栏定制.参考:https://support.microsoft.com/en-us/kb/926805

d) 使用鼠标挂钩并检测何时单击撤消/重做按钮.这是连接鼠标的代码,注意这不会与企业防病毒产品很好地配合,我不推荐它:https://blogs.msdn.microsoft.com/andreww/2009/02/24/message-hooks-in-add-ins/https://github.com/gmamaladze/globalmousekeyhook.

e) 尝试拦截原始 Windows 消息,例如 Excel带有 WebBrowser 控件的 CustomTaskPane - 键盘/焦点问题 只是 注意 根据我的评论引用 BUG:无法在 DatePicker 上选择超出浮动 VSTO 加载项 的日期,该加载项将消息输入办公室有时会表现出奇怪的行为.

I am working on a real-time language analysis tool that needs to highlight words to draw attention from the writer in Word 2016 using a VSTO add-in, written in .NET4.6.1 with C#. Think of the grammar/spelling check, which adds a squiggly line underneath a word to show you that the word has grammatical or spelling errors. I'm adding a similar feature for some of my own defined rules.

I searched around on adding squiggly lines, and stumbled on Font.Underline and Font.UnderlineColor. I set this on the range of a word, and it appears to provided that visual stumili I was after to draw attention. There is a problem, though. Every underline I add or underline color I change adds an undo action to the undo stack.

I don't want this to happen, or I want a way to pop the action I just did in code from the stack. The aim is to have the user be able to use CTRL+Z to remove text he changed, and not affect my language anlysis result.

How would I go about doing this?

解决方案

I stumbled on this question, which is asking exactly the same thing: Prevent actions to be added to the word undo redo OR Remove actions from the undo redo CommandBarComboBox

As @Manu pointed out that same question was also asked over at MSDN where the answer was:

The UndoClear method will empty the list. That's the best you can do.

There is also a similar question on here Word VSTO override CTRL+Z / CTRL+Y which suggests the Microsoft.Office.Interop.Word.UndoRecord route or MessageHooks in AddIns.


After more research I noticed a great idea at the end of this thread: MSDN Draw my own squigglies on Word document where you keep track of your actions and then skip past them in Undo and Redo operations.

Here is an excellent example of code to do "transactional undo/redo's" Can I create an undo transaction in Word or Excel? (VSTO) . You can do this same method in VSTO except for one big problem, as noted by Dirk Vollmar in his answer:

I don't think that overwriting built-in Word commands is possible using VSTO alone, though

I have overwritten some built-in commands in VSTO using keyboard hooking events to intercept commands: How to perform .Onkey Event in an Excel Add-In created with Visual Studio 2010? However I'm not sure if you can recreate the Ribbon to intercept button commands. More specifically, the Undo and Redo are built-in galleries in the Ribbon UI and you can do nothing with a built-in Ribbon gallery. And in versions like 2010 the Undo/Redo buttons are in the title bar - and you cannot add/edit buttons on the title bar using VSTO:

So if you're concerned with trapping the button commands (everyone I know uses Ctrl+Z & Y), you might inject VBA code to get access to EditUndo and EditRedo events, eg:

VB._VBComponent vbModule = VBProj.VBE.ActiveVBProject.VBComponents.Add(VB.vbext_ComponentType.vbext_ct_StdModule);
String functionText = "Public Sub EditUndo() 
";
functionText += "MsgBox "Undo Happened"
";
functionText += "End Sub";
vbModule.CodeModule.AddFromString(functionText);

Main problem with this approach is Trust needs to be granted.


Another answer in this same QA Can I create an undo transaction in Word or Excel? (VSTO) is by Mike Regan who answered 3 months after Dirk. He used a hidden document and placed it in the real document when needed to make any amount of VSTO actions a single undo.

Still doesn't solve the problem of preventing an action being recorded in the Undo History.


I did try a Registry key to limit the UndoHistory to 0 and reset it back to 100 (in order to disable the History while adding an action), but it appears its only for Excel https://support.microsoft.com/en-gb/kb/211922

There maybe an undocumented reg key to disable the Undo/Redo history altogether but it would only be read by Word on startup. I thought the UndoHistory key containing a number would be read before each Undo/Redo, but no luck with this approach at all.


It's not an easy problem to solve there are big limitations, so it might be easier to:

a) Accept that your spell/grammer checker Add-In is included in the Undo/Redo list (defeat).

b) Work out where the line/text is on screen and show a transparent tooltip highlighting the problem. This is a lot harder than it seems and is less than ideal, here are two great answers to guide you on this method: Detecting text changes in Word 2016 from VSTO add-in or a much simpler approach to detect XY positions from this Microsoft email thread: https://groups.google.com/forum/#!topic/microsoft.public.word.vba.general/pKq4PsqD3cM

//position dialog relative to word insertion point (caret)
int left = 0;
int top = 0;
int width = 0;
int height = 0;
MSWord.Range r = Globals.ThisDocument.Application.Selection.Range;
MSWord.Window w = Globals.ThisDocument.ActiveWindow;

w.GetPoint(out left, out top, out width, out height, r);

frmPopUp newForm = new frmPopUp();
newForm.SetDesktopLocation( left + width + 2, top - newForm.Height + height );

c) Only trap Undo/Redo events by the Keyboard with Transactional Undo/Redo's and let users see the atomic Undo/Redo's using the buttons. It would be extremely dodgy to remove the Undo/Redo buttons and that will cause heaps of Where's my Undo button gone? support cases. So don't do this, if you're curious, quick access toolobar customization can be done from ".qat" files. Ref: https://support.microsoft.com/en-us/kb/926805

d) Use a Mouse Hook and detect when the Undo/Redo buttons are clicked. Here is the code to hook up the mouse, note this wont play nice with corporate Anti-Virus products and I dont recommend it: https://blogs.msdn.microsoft.com/andreww/2009/02/24/message-hooks-in-add-ins/ or https://github.com/gmamaladze/globalmousekeyhook.

e) Try to intercept raw windows messages, eg Excel CustomTaskPane with WebBrowser control - keyboard/focus issues just beware as per my comment referencing BUG: Cant choose dates on a DatePicker that fall outside a floating VSTO Add-In that message pumps in Office sometimes exhibit weird behaviour.

这篇关于VSTO Word 2016:波浪形下划线而不影响撤消的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆