Example Code

Calling Win32 Print Dialogue from a CVI program

Code and Documents

Attachment

Overview

This sample shows how to use Win32 APIs in CVI to get a list of printers installed in the system or to show the Print Dialog.

Description

Many printing functions in CVI, but not all, permit you to show a Print Options dialog box to let the user chose the printer to use and other details of printing. However, this is not practical when you have to print several documents / objects since  you should handle that option box every time (there is no way to get back user choices after the dialog has been dismissed). Additionally, functions like PrintTextBuffer () do not have this ability.

For these reasons some way to show the Print Dialog or offering similar facilities is needed in CVI.

In the simplest scenario, you may simply want to let the user choose the printer to use without need to set additional options to get the report. In this case EnumPrinters () API can be of help: the list of printers cna be loaded into a list control on a CVI panel to let the user choose which one to use and manage the rest of the process entirely with CVI native functions.

In a more complex scenario, you may want to let the user more freedom as to select which page(s) to print or other details beyond the mere choice of the printer to use. If this is the case, PrintDlg () or PrinttDlgEx () APIs come in handy. Unfortunately, using this function can be a little tricky. Nevertheless, the more complex is the API used, the more flexible it is: Print Options dialog is the more customizable and the example project shows some of the possibilities. You may want to look into Win32 API documentation to discover additional possibilties and experiment with them.

In this sample I show how to deal with these three APIs and handle user choices in each case.

Steps to Implement Code

Donwload and uncompress the attached project and load it into CVI IDE: the project is made in CVI2012; see the note in Requirements regarding the use of previous CVI releases.

The project can be executed as-is: it shows buttons to launch different commands and shows command results in a message box at call end. No actual printing is issued. A help panel shows some remarks on every method used in the project.

The code is mainly taken from Win32 API help section in CVI, which mirrors Microsoft's technical documentation. I adapted it to CVI characteristics and sewed it up in a finite project. It is intended as a mere programming example on the use of these functins. Feel free to take the relevant part and integrate the corresponding code in your own projects.

To integrate Win32 APIs into a CVI project you should:

  1. #include windows.h file to get function and constant definitions
  2. Add CommDlg32.lib (for PrintDlg and PrintDlgEx) or WinSpool.lib (for EnumPrinters) to your project

Additional informations can be found in this KnowledgeBase entry: Using the Interface to the Win32 API in LabWindows/CVI

Requirements

The project is developed in CVI2012; however, it should be possible to execute it in previous releases simply downgrading the UIR file. A copy of the UIR save in CVI2009 is supplied for those who are using that version: I have tested it in that release and it works. It should be possible to run it in older releases, though I haven't tested till now.

If you want to test this example in previous releases place a post in the comments asking for the release of interest: I can downgrade and test down to CVI6.

No additional software is needed to run this example.

No hardware is needed to run this example.

Notes

I have developed and tested this code using CVI Full Release. I have no Base Package installation to test it against: for this reason I cannot guarantee it can be run in CVI Base version too; I know Base package includes only a subset of the Win32 APIs and I do not know whether some or all the functions I am using are missing in that package.



Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?

Example code from the Example Code Exchange in the NI Community is licensed with the MIT license.

Comments
HeRams
Member
Member
on

As the NIReport tool is not supported anymore, I was urgently looking for a solution to select the printer and store / load it in my program settings. This seems to solve the problem, I will try it!
The only thing is: it took me lots of time to find this example, it should be available with serching for words like "CVI printer selection" or similar on the website.

 

Thank you very much!

Contributors