Home > Net >  For help! C # and matlab mixed programming problems
For help! C # and matlab mixed programming problems

Time:09-20

Use vs2017 and matlab2017a solution platform is x64, MCR and environment variables are configured,
Wrote a simple. M file mapping, according to the online tutorial generated DLL files for reference, and the figure nested in c # form a panel control, according to
But running figure image has not been displayed, the form has become very small, there is no error, set a breakpoint found matlab functions can be normal call,
Stick procedures below, bosses answer
# region//Windows API
[DllImport (" user32. DLL)]
Public static extern IntPtr FindWindow (string lpClassName, string lpWindowName);//
[DllImport (" user32. DLL)]
Public static extern IntPtr SetParent (IntPtr hWndChild, IntPtr hWndNewParent);
[DllImport (" user32. DLL, "CharSet=CharSet. Auto)]
Public static extern int MoveWindow (IntPtr hWnd, int x, int y, int nWidth, int nHeight, bool BRePaint);

Const int GWL_STYLE=- 16;
Const int WS_CAPTION=0 x00c00000;
Const int WS_THICKFRAME=0 x00040000;
Const int WS_SYSMENU=0 x00080000;
[DllImport (" user32 ")]
Private static extern int GetWindowLong (System. IntPtr HWND, int nIndex);

[DllImport (" user32 ")]
Private static extern int SetWindowLong (System. IntPtr HWND, int index, int newLong);

///& lt; Summary> Maximize window, minimize window, normal size window
Maximize hidden///nCmdShow: 0, 3, 6, 5 normal display
///& lt;/summary>
//[DllImport (" user32. DLL EntryPoint="ShowWindow")]
//public static extern int ShowWindow (IntPtr HWND, int nCmdShow);
# endregion
Public _click ()
{
InitializeComponent ();
}
Public delegate void UpdateUI ();//used to update the UI
Thread startload;//thread used to deal with the matlab form
Class1 cl;
IntPtr figure1.//handle to the image
Private void Form1_Load (object sender, EventArgs e)
{

}
Private void button1_Click (object sender, EventArgs e)
{

Try
{
Startload=new Thread (new ThreadStart (startload_run));
Startload. Start ();

}
The catch (Exception ex)
{
MessageBox. Show (ex. ToString ());
}
}
Void startload_run ()
{
Int count50ms=0;
//instantiate the matlab object

Cl=new Class1 ();
Cl. Drew ();

//loop through figure1 form
While (figure1==IntPtr. Zero)
{
//find the matlab Figure 1 form
Figure1=FindWindow (" SunAwtFrame Figure ", "1");
//delay 50 ms
Thread.sleep (50);
Count50ms + +;
//20 s timeout
//if (count50ms & gt;
=400)//{
//label1. Text="matlab resource loading time is too long!" ;
//return;
//}
}

//across threads, entrust executed
UpdateUI update=delegate
{
//hide the tag
Label1. Visible=false;
//sets the parent form at the matlab image form a panel
SetParent (figure1, panel1. Handle);
//to get form the style of the original
Var style=GetWindowLong (figure1, GWL_STYLE);
//set up the new style, get rid of the title, can change through the border size
The SetWindowLong (figure1, GWL_STYLE, style & amp; ~ WS_CAPTION & amp; ~ WS_THICKFRAME);
//move into place equal emphasis on the panel painting
MoveWindow (figure1, 0, 0, panel1. Width + 20, panel1. Height + 40, true);
//display the form function called, hide and show equivalent to refresh the form


};
Panel1. Invoke (update);
//move again, to prevent error
Thread.Sleep(100);
MoveWindow (figure1, 0, 0, panel1. Width + 20, panel1. Height + 40, true);
}
  •  Tags:  
  • C#
  • Related