This chapter shows you how to create a custom device settings dialog. The code is provided in reusable form, thus it can be easily integrated into your own applications.
IC Imaging Control also offers a built-in dialog, which can be used to select and set up a device. If, however, the dialog provided by IC Imaging Control does not fit your needs, this chapter shows you how to create a custom dialog. It will first show you how to integrate the sample code into your own projects and then how to actually build the dialog.
The sample code for this example can be found in the samples\VB6\Making Device Settings directory. To integrate the code into your project, you just have to add the file frmDeviceSettings.frm. Select Project from the menu and then select Add Form. In the Add Form dialog click on the Existing tab. Now browse to the sample folder and open the file frmDeviceSettings.frm.
To call the dialog from your application, just 2 lines of code are needed. First, the ICImagingControl instance that you are using in your application (here: ICImagingControl1 ) must be assigned to the dialog. Then the dialog has to be called. To do this, just insert the following lines at an appropriate place into your code.
Set frmDeviceSettings.ImagingControl = ICImagingControl1 frmDeviceSettings.Show vbModal
In this chapter, we will focus on the device settings dialog. In addition to the dialog, you will of course need an application that calls it. The sample code of this chapter contains a ready-to-run project with a small application that calls the dialog.
Assuming you have already set up a main project, you now have to add a new form to the project. This form will contain the elements of the custom dialog. To add a new form, select Project from the menu and then choose Add Form. Choose the Dialog type of form. Name the form frmDeviceSettings.
Now add 5 combo boxes to the form. Name them cboDevice, cboVideoNorm, cboVideoFormat, cboFrameRate and cboInputChannel. Also add a text box, called txtSerial and two check boxes named chkFlipV and chkFlipH.
In addition to the controls mentioned above, the sample dialog also contains a label named lblErrorMessage. The Visible property of this label is set to false, so that it will not be displayed when the dialog is called. The reason for this is explained when the Form_Load event is discussed.
First, we need a global data structure that will store the state of the device. This is necessary since that state has to be restored, if the user clicks the Cancel button of the dialog. For this sample, the data structure looks as follows:
Private Type Settings DeviceAvail As Boolean Device As String VideoNormAvail As Boolean VideoNorm As String VideoFormat As String FrameRateAvail As Boolean FrameRate As Double InputChannelAvail As Boolean InputChannel As String FlipVAvail As Boolean FlipV As Boolean FlipHAvail As Boolean FlipH As Boolean End Type
Now add some global variables:
Public ImagingControl As ICImagingControl Private DeviceState As Settings Const NOT_AVAILABLE = "n/a"
ImagingControl must be public since it is assigned from the main program before the dialog is called.
Now add a Form_Load event and insert the following code:
Private Sub Form_Load() If ImagingControl.DeviceValid Then If ImagingControl.LiveVideoRunning Then lblDevice.Visible = False lblVideoNorm.Visible = False lblVideoFormat.Visible = False lblFrameRate.Visible = False lblInputChannel.Visible = False lblSerial.Visible = False cboDevice.Visible = False cboVideoNorm.Visible = False cboVideoFormat.Visible = False cboFrameRate.Visible = False cboInputChannel.Visible = False txtSerial.Visible = False chkFlipV.Visible = False chkFlipH.Visible = False CancelButton.Visible = False Me.Caption = "Error" Me.Width = 3465 Me.Height = 2175 OKButton.Top = 85 OKButton.Left = 134 lblErrorMessage.Caption = "The device settings dialog is not available while the live video is running." _ & vbCrLf & vbCrLf & "Stop the live video first." lblErrorMessage.Visible = True Exit Sub End If End If SaveDeviceSettings UpdateDevices End Sub
The Form_Load event checks, whether the live video is running. If it is, it reformats the dialog box to display a message to tell you that the dialog will not be displayed, while the live video is running. This behavior was implemented to point out that changing the device will cause side effects. The ring buffer collection ICImagingControl.ImageBuffers is reset when the device is changed. Therefore, image data in the collection is lost.
Now add the click events of the OK and the Cancel button and insert the following code accordingly:
Private Sub OKButton_Click() Unload Me End Sub
Private Sub CancelButton_Click() RestoreDeviceSettings Unload Me End Sub
As can be seen above, the Form_Load event calls a procedure SaveDeviceSettings and the CancelButton_Click event calls RestoreDeviceSettings. These are two helper procedures that fill and read out the global data structure DeviceState to save\restore the state of the device.
The UpdateDevices procedure that is called on the Form_Load event updates the controls of the dialog, but this will be discussed later.
Now add the click events for all 5 combo boxes and the 2 check boxes. On the click event of the device combo box, the currently selected device must be changed and all other combo boxes, the check boxes and the display of the serial number text field must be updated. This is done with the following code:
Private Sub cboDevice_Click() Dim Serial As String On Error GoTo err_cboDevice_Click 'Open the device If cboDevice.Enabled Then Dim Item As Variant Dim index As Integer index = 1 ImagingControl.Device = cboDevice.Text txtSerial.Text = NOT_AVAILABLE For Each Item In ImagingControl.Devices If Item.Name = cboDevice.Text Then If ImagingControl.Devices.Item(index).GetSerialNumber(Serial) Then txtSerial.Text = Serial Exit For End If End If index = index + 1 Next ' Get supported video norms, formats and inputs UpdateVideoNorms UpdateInputChannels UpdateFlip End If Exit Sub err_cboDevice_Click: MsgBox Err.Description End Sub
After the new device is selected, its serial number is retrieved. If a serial number is not available, n/a is displayed. The procedures UpdateVideoNorms, UpdateInputChannels and UpdateFlip update the controls. They will be discussed later. Please note that the video format and the frame rate are updated implicitly by UpdateVideoNorms and UpdateVideoFormat.
The click events for the other combo boxes are quite similar. The click event for the cboVideoNorm combo box includes a procedure UpdateVideoFormats, which is used to update the video format combo box according to the selected video norm. The click event for the cboVideoFormat combo box includes a procedure UpdateFrameRates that updates the frame rate combo box after the video format has changed. The other combo boxes just set the currently selected value. Here is the code for the video norm combo box:
Private Sub cboVideoNorm_Click() On Error GoTo err_cboVideoNorm_Click If cboVideoNorm.Enabled Then ImagingControl.VideoNorm = cboVideoNorm.Text End If UpdateVideoFormats Exit Sub err_cboVideoNorm_Click: MsgBox Err.Description End Sub
The click events for the other combo boxes are implemented accordingly, except a minor detail, regarding the frame rate combo box. If you assign the new value for the frame rate, make sure you do a typecast, since cboFrameRate.Text returns a string, but ICImagingControl.DeviceFrameRate needs a floating point value.
Finally, add click events for the check boxes. They just toggle the horizontal and vertical flip:
Private Sub chkFlipV_Click() If ImagingControl.DeviceFlipVerticalAvailable Then ImagingControl.DeviceFlipVertical = (chkFlipV.Value = 1) End If End Sub
The event for chkFlipH is implemented the same way.
As mentioned before, there is an Update procedure for every combo box and one for the two check boxes. The Update procedures for the combo boxes just fill the appropriate combo box with currently valid values. If the value that was selected before the update is still contained in the combo box, it is selected automatically. The code for the UpdateDevice procedure looks as follows:
Private Sub UpdateDevices() cboDevice.Clear If ImagingControl.Devices.Count > 0 Then Dim Item As Variant Dim index As Long For Each Item In ImagingControl.Devices cboDevice.AddItem Item.Name Next If ImagingControl.DeviceValid Then index = ImagingControl.Devices.FindIndex(ImagingControl.Device) cboDevice.ListIndex = index - 1 Else cboDevice.ListIndex = 0 End If cboDevice.Enabled = True Else cboDevice.AddItem NOT_AVAILABLE cboDevice.Enabled = False cboDevice.ListIndex = 0 End If End Sub
The Update procedures for the other combo boxes are implemented accordingly, but note that ImagingControl.DeviceFrameRates returns a collection that contains floating point values. Therefore, in the UpdateFrameRates procedure, you must typecast them into a string before you can add them to the combo box.
The UpdateFlip procedure checks whether there is a vertical or horizontal flip available with the current device and sets their current state if possible:
Private Sub UpdateFlip() If ImagingControl.DeviceFlipHorizontalAvailable Then chkFlipH.Enabled = True If ImagingControl.DeviceFlipHorizontal Then chkFlipH.Value = 1 Else chkFlipH.Value = 0 End If Else chkFlipH.Enabled = False chkFlipH.Value = 0 End If If ImagingControl.DeviceFlipVerticalAvailable Then chkFlipV.Enabled = True If ImagingControl.DeviceFlipVertical Then chkFlipV.Value = 1 Else chkFlipV.Value = 0 End If Else chkFlipV.Enabled = False chkFlipV.Value = 0 End If End Sub
Established in 1990, The Imaging Source is one of the leading manufacturers of industrial cameras, frame grabbers and video converters for production automation, quality assurance, logistics, medicine, science and security.
Our comprehensive range of cameras with USB 3.1, USB 3.0, USB 2.0, GigE interfaces and other innovative machine vision products are renowned for their high quality and ability to meet the performance requirements of demanding applications.