Using Caché Direct
Example: Visual Basic Printer Support
InterSystems can provide you with a Caché Direct sample that demonstrates callbacks by using the Windows default printer (the VB Printer object) from ObjectScript. As a by-product, the sample will provide a Visual Basic form that can be included into any project and used or expanded as necessary. The approach can also be easily extended to support the Screen and Clipboard Visual Basic objects.
The sample routine prints itself to the Windows default printer, in several fonts, with comments italicized and page headers and numbers in a different font. This is fairly simple to write and shows most of the printing capabilities you might need. The printing model is that of Visual Basic (and Windows), not Caché. (Slash parameters to the Write
command could be implemented in a custom GBI device, but they would not provide any more capabilities or convenience than this sample.) The one concession to Caché conventions is in the parameters of the Print
method, which follow the ObjectScript model, since it is both simpler and more familiar. Note that the arguments to the Print()
method are quoted strings, because ObjectScript variable names have no meaning to Basic.
A common way to execute Basic code from an ObjectScript routine is to put the code into the Click event of a button and set the Value
property of the button to 1. This causes the Click event to fire and the Basic code to execute. As a variation, we can exploit the fact that there are other events that fire in response to other actions. In particular, the text control fires a Change event in response to a change in the Text
property. This is convenient because we can set the Text
property to an appropriate string in a single call from Caché and have the Change event code interpret it. The result, if any, will be returned in the Tag
property of the same text control. This approach can be used on a separate, hidden form, in a totally general way, and all in Basic.
The sample includes a few simple ObjectScript routines to format command strings and send them to the Printer object. The Text
property contains a string consisting of a command and some number of arguments. The commands correspond to the operations to be performed with the Printer object: setting and getting properties and performing methods.
To set properties, use SetProp()
followed by one or more comma-separated pieces of the form property_name=value
. This allows you to set multiple properties in a single call. For example, to access the Printer1 instance directly, use this:
Or to access the printer through the helper routine, use this:
To get property values, use GetProp()
followed by one or more comma-separated property names. The response to the query will be set into the Tag
property, in the same form as the SetProp()
arguments. For example, to access the printer directly, use this:
Or to access it through the helper routine, use this:
property is then set to a string of the following form:
This property would also returned as the function result.
The arguments to the Print()
method follow ObjectScript format control conventions (as in the Write
command), not Basic conventions. For example, to access the printer directly, use the following:
Set _Printer1!txtPrint.Text="Scale:0,0,80,60" or "Scale"
Set _Printer1!txtPrint.Text="Print:!,?8,""Some text"""
To access it through the helper routine, use this:
Do Scale^%CDPrint(0,0,80,60) or Do Scale^%CDPrint( )
Do Print^%CDPrint("!,?8,""Some text""")
The arguments to the Print()
method must be quoted strings, because ObjectScript variable names have no meaning to Basic. See quote^CDPrTest
in the sample code for a routine that properly quotes strings, doubling up internal quotes.