1

I'm making a C# program which is supposed to dock a matlab program onto a C# form. The reason for this is that my company uses a matlab program for very complex calculations.

When docked, this program will then be integrated into an intouch wonderware environment, which will make the user interactions between the HMI and the matlab program much more efficient.

Using the following code:

    [DllImport("user32.dll")]
    static extern IntPtr SetFocus(IntPtr hWnd);
    [DllImport("user32.dll")]
    public static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
    [DllImport("user32.dll", SetLastError = true)]
    public static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);
    [DllImport("user32.dll", SetLastError = true)]
    public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
    [MonitoringDescriptionAttribute("ProcessMainWindowHandle")]
    public IntPtr MainWindowHandle { get; set; }

    public void getWinHandle(int parentWidth, int parentHeight)
    {

        hWnd = IntPtr.Zero;
        hWnd = FindWindow(null, "TSplot 2.3.1 (Build: 5626)");
        matlabPTR MatlabNativeWin = new matlabPTR();
        MatlabNativeWin.AssignHandle(hWnd);
        SetParent(hWnd, formiLuring.Handle);
        MoveWindow(hWnd, -8, -33, parentWidth+16, parentHeight+41, false);

    }
public class matlabPTR : NativeWindow
{
    protected override void WndProc(ref Message message)
    {
        const int WM_SYSCOMMAND = 0x0112;
        const int SC_MOVE = 0xF010;
        const int SC_SIZING = 0x0214;

        switch (message.Msg)
        {
            case WM_SYSCOMMAND:
                int command = message.WParam.ToInt32() & 0xfff0;

                if (command == SC_MOVE)
                {
                    return;
                }

                break;
        }

        base.WndProc(ref message);
    }
}

I have successfully created a window handle and added it to my C# form. I've also frozen it so that a user cannot move this matlab window.

The problem however, is that the matlab program dosn't seem to get full focus event though it should have. A keyboard press on Z would normally generate a zoom inside the matlab interface, but somehow the matlab program does not register this.

You might think that the reason for this is that the C# form takes the key press event for itself, but I've tried overridden the C# event and this event does not trigger when the matlab program is highlighted.

I figured that either I've missed something or I'm doing something wrong.

Thanks for your time

Jakob Danielsson
  • 767
  • 1
  • 8
  • 16

0 Answers0