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() \n";
functionText += "MsgBox \"Undo Happened\"\n";
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.
Unfortunately, it doesn't look like there is an easy solution to this problem. From what I've seen, you have two options:
You could clear the entire undo stack using Document.UndoClear. However, this won't be very user friendly, since all previous actions aren't in the undo list anymore.
Work with the UndoRecord class. Between your calls to StartCustomRecord and EndCustomRecord, all actions in your code will only generate one item in the undo stack. CTRL+Z will still affect your language analysis, but the whole undo stack won't be as polluted as before.
I know this isn't what you want, but even Microsoft MVPs don't have a better solution.
Related
How do I create a resource that I can reference and use in various parts of my program easily?
My specific problem is that I have a NotifyIcon that I want to change the icon of depending on the state of the program. A common problem, but one I've been struggling with for a long time.
Well, after searching around and cobbling together various points from around StackOverflow (gee, I love this place already), most of the problems were already past this stage. I did manage to work out an answer to my problem though.
How to create a resource:
In my case, I want to create an icon. It's a similar process, no matter what type of data you want to add as a resource though.
Right click the project you want to add a resource to. Do this in the Solution Explorer. Select the "Properties" option from the list.
Click the "Resources" tab.
The first button along the top of the bar will let you select the type of resource you want to add. It should start on string. We want to add an icon, so click on it and select "Icons" from the list of options.
Next, move to the second button, "Add Resource". You can either add a new resource, or if you already have an icon already made, you can add that too. Follow the prompts for whichever option you choose.
At this point, you can double click the newly added resource to edit it. Note, resources also show up in the Solution Explorer, and double clicking there is just as effective.
How to use a resource:
Great, so we have our new resource and we're itching to have those lovely changing icons... How do we do that? Well, lucky us, C# makes this exceedingly easy.
There is a static class called Properties.Resources that gives you access to all your resources, so my code ended up being as simple as:
paused = !paused;
if (paused)
notifyIcon.Icon = Properties.Resources.RedIcon;
else
notifyIcon.Icon = Properties.Resources.GreenIcon;
Done! Finished! Everything is simple when you know how, isn't it?
The above didn't actually work for me as I had expected with Visual Studio 2010. It wouldn't let me access Properties.Resources, said it was inaccessible due to permission issues. I ultimately had to change the Persistence settings in the properties of the resource and then I found how to access it via the Resources.Designer.cs file, where it had an automatic getter that let me access the icon, via MyNamespace.Properties.Resources.NameFromAddingTheResource. That returns an object of type Icon, ready to just use.
The above method works well.
Another method (I am assuming web here) is to create your page. Add controls to the page. Then while in design mode go to: Tools > Generate Local Resource. A resource file will automatically appear in the solution with all the controls in the page mapped in the resource file.
To create resources for other languages, append the 4 character language to the end of the file name, before the extension (Account.aspx.en-US.resx, Account.aspx.es-ES.resx...etc).
To retrieve specific entries in the code-behind, simply call this method: GetLocalResourceObject([resource entry key/name]).
Code posted by Matthew Scharley has a memory leak:
paused = !paused;
if (paused)
notifyIcon.Icon = Properties.Resources.RedIcon;
else
notifyIcon.Icon = Properties.Resources.GreenIcon;
You should Dispose() notifyIcon.Icon before replacing it, because Properties.Resources.SOME_ICON creates a new Icon each time it is used.
This can be observed in the log, with this code:
Console.WriteLine(Properties.Resources.RedIcon.GetHashCode());
Console.WriteLine(Properties.Resources.RedIcon.GetHashCode());
Console.WriteLine(Properties.Resources.RedIcon.GetHashCode());
You will see 3 different Hash Codes in the log. This means these are different Objects.
So, the simple fix will be:
paused = !paused;
notifyIcon.Icon?.Dispose();
notifyIcon.Icon = paused
? Properties.Resources.RedIcon;
: Properties.Resources.GreenIcon;
So I have tooltips on a variety of checkboxes and sliders in my application. This is partially necessary because the app is intended to be used by specific people at my company to create files (a large number of proprietary file formats, each with a variety of options). I decided at one point that tooltips were a good stopgap measure until there's a full set of documentation, plus it makes it easier on the fly to see what is out there.
I've considered using a global configuration checkbox that would enable or disable tooltips, but I was wondering if there was an elegant way to only display tooltips on mouseover while CTRL was being held down. I couldn't readily find anything on this. Any comments are appreciated.
Edit: The reason being that having tooltips pop up all the time as you move through the interface isn't really ideal, but being able to quickly access tooltips is very helpful.
Also, can someone please explain why this kind of question gets down-voted? It's frustrating to come here and see questions like "What is .NET" remain positive, and questions where I'm trying to learn something specific that isn't readily available on the Internet-at-large gets down-voted.
Here are all of the locations where the tooltip tipFRBx9P446 exists in my actual code.
Form1.Designer.cs:
this.tipFRBx9P446 = new System.Windows.Forms.ToolTip(this.components);
this.tipFRBx9P446.SetToolTip(this.cbFRBx9P446, "This will force P44 fields to reflect a value of \'6\'. This can be used to ensure" + " proper testing of TFS WI 75062.");
//
// tipFRBx9P446
//
this.tipFRBx9P446.UseAnimation = false;
this.tipFRBx9P446.UseFading = false;
//
private System.Windows.Forms.ToolTip tipFRBx9P446;
Form1.resx:
<metadata name="tipFRBx9P446.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>762, 17</value>
You can use the Control.Modifier Key to check if CRTL is pressed:
if (Control.ModifierKeys == Keys.Control)
{
//CRTL is being pressed, show tooltip
}
You should be able to use this code in your "Mouse_Hover"-event of the respective checkboxes.
Hope this will help!
I'm building a "WPF Application" which is made to be run in the background (minimised state) and detects KeyStrokes of each and Every key on the keyboard & every Mouse Clicks.
So, my question is how to detect every keyStrokes whether app (Window) is minimised or not.
Simply, if my app is in focus then i use this code to count keystrokes.
Public int count;
protected override void OnKeyDown(System.Windows.Input.KeyEventArgs e)
{
//base.OnKeyDown(e);
count++;
tBlockCount.Text = count.ToString();
}
I just want to do the same even if my app is minimised.
I've searched a lot and come across many suggestions like..
http://www.pinvoke.net/default.aspx/user32/registerhotkey.html
http://social.msdn.microsoft.com/Forums/vstudio/en-US/87d66b1c-330c-42fe-8a40-81f82012575c/background-hotkeys-wpf?forum=wpf
Detecting input keystroke during WPF processing
Detect if any key is pressed in C# (not A, B, but any)
Most of those are indicating towards Registering HotKeys. But I'm unable to match scenario with mine.
Any kind of suggestion are most welcome.
Although I'm not really condoning the use of a keylogger (This is what you are trying to do). I would recommend taking a look at this q/a, the section near the bottom of this article, and this article for some inspiration. These should help point in the right direction for the coding side.
What you essentially need to do is just set up an event to intercept any keys that come in from the computer, then you can gather the key and do whatever you like with it (in your case, record it)
Edit: In fact, reading the third article, it actually gives a full code snippet on how to implement and use it in WPF, so I recommend just reading that one.
I'm trying to implement the automation test via UIAutomation for our project. But lots of the controls are not standrad, and proper patterns are also not implemented for that controls. How should I to manipulate the controls via UIAutomation framework in this case?
For example, a button in our product is implemented via a Pane, and the invoked pattern is not implemented as well. How should I click the button? (To avoid installing VS on the test machine, I don't want to use Mouse.Click() in Microsoft.VisiualStudio.TestTools.UITesting namespace) Is there a way to do that only using UIAutomation framework or something else embedded in .net framework? Thanks in advance! (If the proper pattern is implemented, Below code will work. And as a new user, I cannot post the screenshot for your reference, sorry!)
object temp = null;
if (btnTest.TryGetCurrentPattern(InvokePattern.Pattern, out temp))
{
InvokePattern btnTestPattern = temp as InvokePattern;
btnTestPattern.Invoke();
}
The only way to interact when Control Patterns are not implemented is to go clicking around stuff.
I would suggest try following to avoid maximum errors.
Before sending the click, make sure the parent of button(pane or window is set to foreground)
Instead of sending the click to corner of the AutomationElement, try sending it in midpoint, of the element,
Also, try hovering over the element first, the wait like 200ms, and then send click, So that you are sure to see execution.[Trust me, this helps debugging a lot and avoids many issues.]
The best thing would be, if those guys who implement the system would implement server-side UIA provider to their UI Elements!
But often that's not possible..., I used the following workaround (at least for clicking/toggling):
AutomationElement yourAE = ...// some code to find the right AutomationElement (AE)
clickablePoint = yourAE.GetClickablePoint();
also BoundingRectangleProperty could be of help
If you receive that clickable point you can use
System.Windows.Forms.Cursor.Position = new System.Drawing.Point((int)clickablePoint.X, (int)clickablePoint.Y);
to move to the location, and than click it via InputSimulator or some win32 (user32.dll) commands.
(note: of course you can also use InputSimulator or win32 to move the mouse - but I had some problems with the InputSimulator when it came to several screens with different locations or resolutions - so Cursor.Position was the easiest approach, which is also very reliable)
I recently started getting acquainted with Visual Studio 2010 and C# for an internship. C# doesn't include a built-in InputBox function, so I made my own form, with a text box, two buttons and a simple label.
I have a function set up to allow the programmer to call the form in regular format (where the user enters input via the textbox) or yes/no format (where the form just displays a question and the yes and no buttons).
When I switch over to yes/no format, I want to center the label programmatically. I've been using the code:
labelNote.Left = inputBox.Left + (inputBox.Width / 2) - (labelNote.Width / 2);
This should put the center of the note in the center of the form. However, if the contents of the label change (making the new label longer or shorter) the properties don't update to reflect the new size. It won't center unless it includes the original text. Is there some way to force an update? I foresee this becoming a problem with positioning objects for scalability in the future.
Thank you for your time
I'm assuming you have the sizing within an actionlistener. Specifically the forms' resize action listener. Then whenever the form is resized it is called and all of you code is recalled. Then to force an update from somewhere else you just have to call the actionlistener.
Actionlistener:
Private Sub formName_Resize(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Resize
Calls actionlistener:
formName_Resize(sender, e)
Well, you could attach an event to Label.TextChanged. Frankly it would be better to change the TextAlign or something like that though: try to perform the layout in a declarative fashion instead of doing it explicitly through code. That tends to make things work rather better.
I've found the [TableLayoutPanel]1 control to be reasonably easy to work with - most of the time (and occasionally a complete pain).
It turns out that I made a stupid mistake (a common theme for me in debugging. The really small stuff goes unnoticed for the longest amount of time).
The label resizing was not the issue. The issue was the order in which I changed the contents of the label and then called the function to calculate its new location. I was calling the location calculation first, so it found where to center the label based on the old contents. I didn't notice for so long, because the text was changing properly. I took it for granted that the functions were being called in the correct order.
So, when it doubt, check the order in which you're writing your code. Thanks for you help anyway, everyone. I ended up finding out some neat things that could be applicable to other scenarios (such as the MeasureString function in the Graphics class).