Load ink to a MathInputControl in C#
I'm trying to use use the LoadInk method of an mathinputcontrol but I can't figure out where to create the IIDispInk object from, as it just appears to be an interface.
http://msdn.microsoft.com/en-us/library/dd372605(VS.85).aspx
Any guidance would be highly appreciated.
Thanks :)
Edit: for clarity, here is my code so far [edit 2: by "so far", I mean of what's been added. Pretty much all the rest of my code can be found on SO under how to create the MIC in C#] (thanks to Hans Passant)
MSINKAUTLib.InkDispClass loadInkTest = new MSINKAUTLib.InkDispClass();
Stream stream = File.Open("C:\\Tim\\bytes.isf", FileMode.Open);
byte[] bytes = new byte[stream.Length];
stream.Read(bytes, 0, bytes.Length);
loadInkTest.Load(bytes);
ctrl.LoadInk((micautLib.IInkDisp)loadInkTest);
Unfortunately this throws exactly the same exception
Catastrophic failure (Exception from HRESULT: 0x8000FFFF (E_UNEXPECTED))
C:\Tim\bytes.isf contain bytes saved from an InkPicture control which lo开发者_开发百科ads and saves this file file OK, so I assume that because the loadInkTest.Load() method did not throw an exception (normally it is not shy to do so) that it loaded the data OK. If there is a suggestion on a better (or more obvious) place to get the bytes, please let me know.
Use Project + Add Reference, Browse tab. Navigate to c:\program files\common files\microsoft shared\ink and select InkObj.dll. You can now create an instance of MSINKAUTLib.InkDispClass. It implements IInkDisp and has Save and Load methods.
You will have to cast the object to micautLib.IInkDisp, the interfaces come from different type libraries. And the clincher, you have to call the MathInputControl's Show() method first before using LoadInk(). Error reporting is miserable, everything is E_UNEXPECTED. The code I got to work:
var ctl = new micautLib.MathInputControl();
var ink = new MSINKAUTLib.InkDisp();
ink.Load(System.IO.File.ReadAllBytes("c:\\temp\\test.isf"));
var iink = (micautLib.IInkDisp)ink;
ctl.Show();
ctl.LoadInk(iink);
Plus event handlers for the Insert and Close event. And the glue to get the window in the right place.
Also beware that the micautLib type library has a dependency on the bit-ness of the machine. The troublemaker is the SetOwnerWindow() method, you really want to use it to prevent the dialog from disappearing behind another window. Its argument is declared as LONG_PTR, a type that is 32-bits on a 32-bit operating system, 64-bits on x64. The window handle. When you use Visual Studio, you will always get the 32-bit version of that method since VS is a 32-bit program.
If you plan on supporting 64-bit operating systems then you will have to build a separate version of your program. Starting with running the 64-bit version of Tlbimp.exe (not Visual Studio) to create the interop wrapper. So that the argument will be a 64-bit value and compatible with the window Handle you pass to the method.
Ah, the joys of COM. No real accident that this wasn't wrapped by Microsoft.Ink.dll :)
精彩评论