.netc++matrixmemsetmagnification-api

Windows Magnification API, .NET and matrices


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 Internet 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 multidimensional array/matrix and how I would handle this in .NET also.

How can I do it?


Solution

  • 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.