Using Caché Direct
Example: Visual Basic Printer Support
[Back] 
   
Server:docs2
Instance:LATEST
User:UnknownUser
 
-
Search:    

InterSystems can provide you with a Caché Direct sample that demonstrates callbacks by using the Windows default printer (the VB Printer object) from Caché 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 Caché 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.
Overview
A common way to execute Basic code from an Caché 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.
Internal Command Syntax
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.
Setting Properties
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:
Set _Printer1!txtPrint.Text="SetProp:FontName=Arial,FontSize=12,FontBold=1"
Or to access the printer through the helper routine, use this:
Do SetProp^%CDPrt("FontName=Arial,FontSize=12,FontBold=1")
Getting Properties
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:
Set _Printer1!txtPrint.Text="GetProp:FontName,FontSize,FontBold"
Or to access it through the helper routine, use this:
Set prop=$$GetProp^%CDPrint("FontName,FontSize,FontBold")
The Tag property is then set to a string of the following form:
"FontName=Arial,FontSize=12,FontBold=1"
This property would also returned as the function result.
Executing Methods
The following methods are available in this sample: NewPage(), Scale(), TextHeight(), TextWidth(), Print(), and EndDoc(). Each of these methods has an equivalent command that you can execute through the helper routine.
The arguments to the Print() method follow Caché 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="NewPage"
Set _Printer1!txtPrint.Text="Scale:0,0,80,60"  or  "Scale"
Set _Printer1!txtPrint.Text="TextHeight:"12""
Set _Printer1!txtPrint.Text="TextWidth:"50"" 
Set _Printer1!txtPrint.Text="Print:!,?8,""Some text"""
Set _Printer1!txtPrint.Text="EndDoc"  
To access it through the helper routine, use this:
Do NewPage^%CDPrint
Do Scale^%CDPrint(0,0,80,60)  or  Do Scale^%CDPrint( )
Set ht=TextHeight^%CDPrint("12")
Set wid=TextWidth^%CDPrint("50")
Do Print^%CDPrint("!,?8,""Some text""")
Do EndDoc^%CDPrint  
The arguments to the Print() method must be quoted strings, because Caché 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.