MATLAB 6.x & vc6.0: display the plot image in the window of the MFC Program
Lab environment:
Pwin2k SP3, Matlab 6.0, Visual C ++ sp?
1. Question proposal
By calling MCC, You can compile the MATLAB program into a C/C ++ or library file and embed it into your own VC application. This method is not only suitable for pure computational code, but also for graphical functions, such as plot. However, the problem is that every time the plot function is called, a new window will be generated, which makes the style of our VC program not uniform and looks awkward. In addition, the hop-out window features are too obvious, so that people can recognize MATLAB at once, in some cases, your customers, bosses, colleagues, students, GG, and mm may feel very disdainful: lazy!
In order to make the program look more professional, can we embed the image in our VC window or in a dialog box without letting it jump out?
2 Analysis
In fact, after executing plot, execute the following command:
Set (GCA)
You will find a property: parent, which is the parent window of the window, that is, in which parent window is displayed. Set it to form a complex gui. Let's take a look at the demo provided by Matlab. If this attribute is empty, the plot window is displayed as an independent window.
Therefore, the drawing function of MATLAB itself supports display in a specific parent window. Unfortunately, the parent attribute is not specified by Win32 handle, but an ID in the form of a MATLAB string. It is difficult for us to use this function in VC.
3 Reverse Thinking
Since the image cannot be directly stored in the specified window, can the image be painted and moved to the window?
The idea is that simple. Check Win32 API and find two functions: findwindow and setparent. As the name suggests, you have guessed points for what I want to do.
4. Code Implementation
The core code is as follows:
M_hmat =: findwindow (null, "Figure No. 1 ");
: Setparent (m_hmat, m_hwnd );
First, find the handle of the window named "Figure No. 1" and set its parent window to a window. This code is included in the View class of an MFC program with a DOC/view structure. m_hwnd is the window handle of the view.
5. Application Instances
Download the application instance: sglmfc.zip
This program is restructured based on the example sglmfc in Chapter 9 of "Matlab extended programming". The menu item embed and its callback function onsglembed are added to sglmfcview. cpp. For how to compile the project, refer to the relevant content of this book.
6. Other Details
Note the following:
■ This program is a simple demonstration program, and the programming is not strict. Execute the program containing the debug version. Run the program in strict accordance with the following process:
(1) SGL | start
(2) SGL | demo. A plot window is displayed.
(3) SGL | embed, and the plot window runs to the view
(4) double-click the window bar to fill the entire view
(5) Close the window.
(6) SGL-stop
(7) Exit the program
Click here to view the demo result.
■ This method will still display the pop-up Plot window. to disable the display of plot outside the VC window, use the figure command to display an implicit window, after drawing and moving to the inside of the VC window, it is displayed, for example
> Figure ('visible ', 'off ')
> Plot ([])
> Figure (1)
■ To remove the menu and button of the plot, set (GCF, 'menubar ', 'None') should be executed in the corresponding MATLAB program ');
■ If a large number of images are displayed, you cannot use "Figure No. 1" to uniquely identify each window. It should be 2 or 3. This requires you to use MATLAB code and VC code to program and control it.
■ With WIN32API, you can also change the name and icon of the window to completely remove the marks of Matlab.
5 Statement
The program itself is not complete. If you have any problems, try to solve them by yourself.
Energy
2002.12.3