views:

217

answers:

1

I'm trying to create a magnifier app in .net using the Windows Magnification API. I've pretty much got everything working except for actually setting the magnification level (which defaults to 100%). The problem is, I can't find any examples anywhere on the net and all the documentation for the API is C++ code. This is the particular function I'm having trouble with.

bool SetMagFactor(float magfactor)
{
    MAGTRANSFORM matrix;
    memset(&matrix, 0, sizeof(matrix));
    matrix.v[0][0] = magfactor;
    matrix.v[1][1] = magfactor;
    matrix.v[2][2] = 1.0f;
    return MagSetWindowTransform(hwndMag, &matrix);  
}

The MAGTRANSFORM structure is defined as follows:

typedef struct tagMAGTRANSFORM {
    float v[3] [3];
} MAGTRANSFORM, *PMAGTRANSFORM;

The most confusing part of this is the memset - I'm not sure what it does or what its equivalent is in .NET, but what's also confusing is the multi-demensional array/matrix and how I would handle this in .NET also.

Any help would be greatly appreciated.

+2  A: 

The memset is just clearing out the matrix to start with. You wouldn't need to do this in .NET. I suspect the simplest way of defining the struct in C# would be to specify each element individually:

public struct MagTransform
{
    readonly float m00;
    readonly float m10;
    readonly float m20;
    readonly float m01;
    readonly float m11;
    readonly float m21;
    readonly float m02;
    readonly float m12;
    readonly float m22;

    public MagTransform(float magnificationFactor) : this()
    {
        m00 = magnificationFactor;
        m11 = magnificationFactor;
        m22 = 1.0f;
    }
}

You may also need to specify the layout - I'm afraid I'm not so hot on marshalling.

As you can see, I've assumed the values you want based on the sample code. There will be warnings about unused values, but that's okay.

You could use a fixed buffer instead, and unsafe code... but I think I'd probably use the above. Basically you just need 9 floats.

Jon Skeet
unfortunately it fails when I pass the structure to the MagSetWindowTransform function. I get a weird exception - "System.ExecutionEngineException thrown" if I try and martial it as LayoutKind.Auto and "The specified record cannot be mapped to a managed value class".
Andy E
LayoutKind.Explicit?
Jon Skeet
Same as specifying no layout - System.ExecutionEngineException thrown
Andy E