Tuesday, August 12, 2014

Dynamic Controls in LabVIEW at Runtime: Fun with LabVIEW

Adding controls to a LabVIEW VI front panel at runtime is a very intriguing idea. We began to explore this a couple years ago and found many rich resources online in blogs and discussion forums using which we were able to build a complete solution. So, first things first! We would like to give credit to a post from Jhambhekar Automation we used as our main approach for this fun project.

Problem: How can we add controls to a LabVIEW front panel at runtime?

Solution: Using the "Call Library Node" block in LabVIEW build a wrapper around the Window user32.dll to use function call for windows programming.

Demo Video:


There are several posts on LabVIEW forums including LAVA and NI discussions where a wrapper library for user32.dll can be downloaded. A post on how to call a DLL from LabVIEW can be found here.

1. Grab handle to Main VI window: The main idea here, is to use the user32 dll function "Find Window" and passing the FP title to get the handle to the VI front panel window. This handle should be used through your code as the parent handle. Figure 1 shows the use of this user32 function.
Figure1: Use FindWindow to grab parent VI handle
 2.  Programmatically launch second VI: Each control to be added to the main VI at run time is actually another VI. You may simply add a control to the front panel of a second VI, reduce the size of the front panel to fit the displayed control. Programmatically launch and run the VI with the control.
3. Set Main VI as Parent to new VI: Next, in the VI launched in step 2, use the same FindWindow function to grab its own handle and then use the "SetParent" user32 function to set the main VI as its parent. Figure 2 shows this,
Figure 2: Add second VI as child window to Main VI

This will add the new VI as an embedded window in the main VI.
4. Reshape VI from step 2: We made use of  the "getWindowRegion" to get the region of an irregular shape (device/control to be displayed in the second VI) and then passed it to the "setWindowRegion" user32 function to reshape the child window (in this case the devices) we want to add in the Main VI.
5. Use custom cursors: We also used cursor functions from the user 32 to add drag and drop effects. This can now be done using the cursor utilities that are available with the LabVIEW base package here.

Note: Step 4 is to be implemented in the "child" VI and step 5 is to be implemented in the Main VI.

Conclusion: We aim to demonstrate that the discussion forums can be a powerful resource for your LabVIEW applications. The project described here is a result of several ideas we came across online as well as some of our own. The other point we want to stress on is that programming in LabVIEW can be made more enjoyable that it already is!!!

Note: We tried our best to give credit to posts and blogs from where we gathered resources. Please excuse us if we missed out any.

-Wakefield Engineering Team