English 中文(简体)
Help me convert the following VB/C++ code to C#
原标题:

I have been trying to get the following VB code running in C# for hours now. I keep getting a Value does not fall within the expected range. exception on the CreateStroke() call. Also, here is the Microsoft documentation with a C++ version as well.

Option Explicit
Dim theInkCollector As InkCollector

Private Sub Form_Load()
    Set theInkCollector = New InkCollector
    theInkCollector.hWnd = Me.hWnd
    theInkCollector.Enabled = True

    //Create a set of three points, stored as x1, y1, x2, y2, x3, y3
    //in an array of six Long elements, starting at index 0.
    Dim ptStrokePoints(5) As Long
    ptStrokePoints(0) = 200
    ptStrokePoints(1) = 200
    ptStrokePoints(2) = 400
    ptStrokePoints(3) = 600
    ptStrokePoints(4) = 900
    ptStrokePoints(5) = 300

    //The description value is an unused placeholder.
    Dim theDescription As Variant 
    Dim theStroke As IInkStrokeDisp
  Set theStroke = theInkCollector.Ink.CreateStroke(ptStrokePoints, theDescription)
End Sub

Here is what I have:

MSINKAUTLib.InkCollector collector = new MSINKAUTLib.InkCollector();
collector.hWnd = (int)(this.Handle);
collector.Enabled = true;

long[] pts = new long[6];
pts[0] = 200;
pts[1] = 200;
pts[2] = 400;
pts[3] = 600;
pts[4] = 900;
pts[5] = 300;

collector.Ink.CreateStroke(pts, new object());
问题回答

That looks like the following error from the docs:

E_INVALIDARG - Invalid VARIANT type (only VT_ARRAY | VT_I4 supported).

The C# long type is a 64-bit integer, so you are passing a VT_ARRAY | VT_I8 (not VT_I4).

Change your pts declaration to:

int[] pts = new int[6];

and you should be good to go. (int is the C# 32-bit integer type.)





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签