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

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

问题描述

我正在使用一种实时语言分析工具,该工具需要突出显示单词以使用VSTO外接程序(使用C#在.NET4.6.1中编写)在Word 2016中引起作者的注意.考虑一下语法/拼写检查,该检查会在单词下方添加一条弯曲的线,以向您显示该单词存在语法或拼写错误.我为自己定义的某些规则添加了类似的功能.

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.

我四处搜寻,添加了波浪线,偶然发现了Font.UnderlineFont.UnderlineColor.我将其设置在一个单词的范围内,它看起来是为了使我能引起注意.不过有一个问题.我添加的每个下划线或更改的下划线颜色都会向撤消堆栈中添加撤消操作.

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.

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

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.

我该怎么做?

推荐答案

我偶然发现了这个问题,它问的是完全相同的事情: 禁止将操作添加到撤消重做一词或从撤消重做CommandBarComboBox中删除操作

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

@Manu指出,在 MSDN 答案在哪里:

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

UndoClear方法将清空列表.那是你最好的.

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

这里还有一个类似的问题单词VSTO覆盖CTRL + Z/CTRL + Y 这表明AddIns中的Microsoft.Office.Interop.Word.UndoRecord路由或MessageHooks.

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:

我不认为仅使用VSTO就能覆盖内置的Word命令

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

我已经使用键盘挂钩事件覆盖了VSTO中的一些内置命令来拦截命令:

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:

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

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() \n";
functionText += "MsgBox \"Undo Happened\"\n";
functionText += "End Sub";
vbModule.CodeModule.AddFromString(functionText);

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

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

同一质量检查中的另一个答案 可以在Word或Excel中创建撤消事务? (VSTO)由迈克·雷根(Mike Regan)负责,他在德克(Dirk)发表三个月后回答.他使用了一个隐藏的文档,并在需要进行任何VSTO操作一次撤消时将其放置在真实文档中.

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.

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

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

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

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)接受您的拼写/语法检查器加载项已包含在撤消/重做"列表中(失败).

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

b)找出行/文本在屏幕上的位置,并显示透明的工具提示以突出显示问题.这比看起来要难得多,也不太理想,这里有两个很好的指导来指导您使用此方法:

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)仅通过键盘带有事务性撤消/重做功能的捕获撤消/重做事件,并让用户使用按钮查看原子性的撤消/重做操作.删除撤消/重做"按钮将非常狡猾,这会导致我的撤消"按钮消失了吗?支持案例.因此,请不要执行此操作,如果您感到好奇,可以从".qat"文件中快速访问olobar自定义设置.参考: https://support.microsoft.com/en-us/kb/926805

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)使用鼠标钩并检测何时单击撤消/重做"按钮.这是连接鼠标的代码,请注意,这不能与公司的防病毒产品配合使用,我不建议这样做: https://blogs.msdn.microsoft.com/andreww/2009/02/24/message-hooks-in-add-ins/ https://github.com/gmamaladze/globalmousekeyhook .

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)尝试拦截原始Windows消息,例如 Excel带有WebBrowser控件的CustomTaskPane-键盘/焦点问题只需当心,就像我引用

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天全站免登陆