LabVIEW calls C#Winform

Interface interaction is not the strength of LabVIEW. When using LabVIEW to create UI, it is always subject to VI limited controls, limited skin and limited control events. When it is necessary to realize the functions of UI, such as multi document window, window floating and docking, animation and so on, it still needs to spend a lot of effort. Therefore, other languages can be used to realize interface expression and interaction, which is supplemented by LabVIEW. At the same time, with the aid of IDE in text programming (such as C#), code is automatically generated through interactive configuration, which makes the development of interface program more efficient.  

C # as an object-oriented language, the window code is encapsulated in system Windows. Forms. In the Form class. LabVIEW is called through the support provided net node, use Show() or ShowDialog() function to run and display the custom window. There are two ways to call the window: mode and non mode. At the same time, you can specify the relationship between the owner Form and the subsidiary Form. The following four Form classes provide functions:

1 public void Show();                         //A non modal call that does not specify the relationship between the owner form and the subordinate form
2 public void Show(IWin32Window owner);       //A non modal call that specifies the relationship between the owner form and the subordinate form
3 public void ShowDialog();                   //Mode call, which implicitly specifies the relationship between the owner form and the subordinate form
4 public void ShowDialog(IWin32Window owner); //Mode call to explicitly specify the relationship between owner form and subordinate form

The developer develops the code of Winform through C# and publishes it net. Then VI can call the provided by LabVIEW net function node. Please pay attention to:

1. calling winform with the ShowDialog method through show needs to be called in a single thread. Please set the VI execution system in the attribute setting of the calling VI to User Interface.

 

2. If you need to display and establish the relationship between the owner form and the subsidiary form, you need to use the overloaded function with IWin32Window owner as the input parameter IWin32Window is an interface type, which essentially implements the function of obtaining Win32 hWnd handles.

 1 namespace System.Windows.Forms
 2 {
 3     // Summary:
 4     //     Provides an interface to expose Win32 HWND handles.
 5     [ComVisible(true)]
 6     [Guid("458AB8A2-A1EA-4d7b-8EBE-DEE5D3D9442C")]
 7     [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
 8     public interface IWin32Window
 9     {
10         // Summary:
11         //     Gets the handle to the window represented by the implementer.
12         //
13         // Returns:
14         //     A handle to the window represented by the implementer.
15         IntPtr Handle { get; }
16     }
17 }

We can get the HWND of the caller's VI form, and then customize an adapter class to implement the interface. First, construct the obtained HWND transfer in class, and then call the Show(IWin32Window owner) method:

 1 public class LabVIEWWin32WindowsAdapter : IWin32Window
 2 {
 3         public LabVIEWWin32WindowsAdapter(int handle)
 4         {
 5             this.handle = (IntPtr)handle;
 6         }
 7 
 8         IntPtr handle;
 9         public IntPtr Handle
10         {
11             get { return handle; }
12         }
13 }

 

 

Posted by kel on Mon, 16 May 2022 23:45:25 +0300