大侠小侠们,如何用代码设置自定义纸张???

liaoshangfa 2002-01-11 07:47:30
我现在有一个问题对于我来很棘手------代码中设置自定义纸张。
我目前正在写一些单据类报表,一般来说,这些报表所用到的纸张都比较特殊。我发现在打印时用手工设置能正常打印,但是很麻烦,当然用户也不会接受。如果用代码控制,页数不多不觉得,一旦页数多,每页的打印位置就不正常了。真不知如何处理?
我当前测试数据所用是HP网络(针式)打印机。
希望各位在百忙之中指点迷津,或提供一个相关例程。不胜感激!
...全文
176 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
Soft21 2002-01-13
  • 打赏
  • 举报
回复
大家好啊,我学到了不少东西
killerdanny 2002-01-11
  • 打赏
  • 举报
回复
http://www.huazhi.com.cn/index7.htm
Methodor 2002-01-11
  • 打赏
  • 举报
回复
给你个不完整的代码参考参考~~
$PBExportHeader$n_printer.sru
$PBExportComments$关于打印机的封装
forward
global type n_printer from nonvisualobject
end type
end forward

type str_printer_info_1 from structure
long flags
long Description
long name
long comment
end type

type str_printer_info_2 from structure
long servername
long printername
long sharename
long portname
long drivername
long comment
long location
long devmode
long sepfile
long printprocessor
long datatype
long parameters
long securitydescriptor
long attributes
long priority
long defaultpriority
long starttime
long untiltime
long status
long cjobs
long averageppm
character reserve[500]
end type

type str_devmode from structure
character dmdevicename[32]
unsignedinteger dmspecversion
unsignedinteger dmdriverversion
unsignedinteger dmsize
unsignedinteger dmdriverextra
unsignedlong dmfields
unsignedinteger dmorientation
unsignedinteger dmpapersize
unsignedinteger dmpaperlength
unsignedinteger dmpaperwidth
integer dmscale
integer dmcopies
integer dmdefaultsource
integer dmprintquality
integer dmcolor
integer dmduplex
integer dmyresolution
integer dmttoption
integer dmcollate
character dmformname[32]
unsignedinteger dmlogpixels
unsignedlong dmbitsperpel
unsignedlong dmpelswidth
unsignedlong dmpelsheight
unsignedlong dmdisplayflags
unsignedlong dmdisplayfrequency
unsignedlong dmicmmethod
unsignedlong dmicmintent
unsignedlong dmmediatype
unsignedlong dmdithertype
unsignedlong dmreserved1
unsignedlong dmreserved2
unsignedlong dmreserved3
end type

type str_printer_defaults from structure
long Datatype
long DevMode
long DesiredAccess
end type

type str_printdlg from structure
unsignedlong lstructsize
unsignedlong hwndowner
unsignedlong hdevmode
unsignedlong hdevnames
unsignedlong hdc
unsignedlong flags
unsignedinteger nfrompage
unsignedinteger ntopage
unsignedinteger nminpage
unsignedinteger nmaxpage
unsignedinteger ncopies
unsignedlong hinstance
unsignedlong lcustdata
unsignedlong lpfnprinthook
unsignedlong lpfnsetuphook
unsignedlong lpprinttemplatename
unsignedlong lpsetuptemplatename
unsignedlong hprinttemplate
unsignedlong hsetuptemplate
uint reserve1
end type

type str_devnames from structure
UINT wDriverOffset
UINT wDeviceOffset
UINT wOutputOffset
UINT wDefault
character drivername[32]
character devicename[32]
character portname[32]
end type

type str_point from structure
long x
long y
end type

type str_rect from structure
long left
long top
long right
long bottom
end type

type str_PAGESETUPDLG from structure
ulong lStructSize
ulong hwndOwner
ulong hDevMode
ulong hDevNames
ulong Flags
str_point ptPaperSize
str_rect rtMinMargin
str_rect rtMargin
ulong hInstance
ulong lCustData
ulong lpfnPageSetupHook
ulong lpfnPagePaintHook
ulong lpPageSetupTemplateName
ulong hPageSetupTemplate
end type

global type n_printer from nonvisualobject autoinstantiate
end type

type prototypes
function boolean EnumPrinters(ulong flags, string name, ulong level, ref blob buffer, ulong cbbuf, ref ulong cbneeded, ref ulong returned) library "winspool.drv" alias for "EnumPrintersA"
function ulong CopyMem ( ref str_printer_info_1 dest, ref blob source, ulong length ) library "kernel32" alias for "RtlMoveMemory"
function ulong CopyMem ( ref str_printer_info_2 dest, ref blob source, ulong length ) library "kernel32" alias for "RtlMoveMemory"
function ulong CopyMem ( ref blob source, ref str_printer_info_2 dest, ulong length ) library "kernel32" alias for "RtlMoveMemory"

function boolean OpenPrinter ( string PrinterName, ref ulong hPrinter, ulong null) library "winspool.drv" alias for "OpenPrinterA"
function boolean OpenPrinter ( string PrinterName, ref ulong hPrinter, ref str_printer_defaults null) library "winspool.drv" alias for "OpenPrinterA"
function boolean ClosePrinter ( ulong hPrinter ) library "winspool.drv"
function boolean GetPrinter ( ulong hPrinter, ulong level, ref blob Printer, ulong cbbuf, ref ulong cbNeeded ) library "winspool.drv" alias for "GetPrinterA"
function boolean SetPrinter ( ulong hPrinter, ulong level, ref blob Printer, ulong command ) library "winspool.drv" alias for "SetPrinterA"
function boolean PrinterProperties ( ulong hwnd, ulong hprinter ) library "winspool.drv"
function ulong CopyMem ( ref str_devmode dest, ref blob src, ulong length ) library "kernel32" alias for "RtlMoveMemory"
function ulong CopyMem ( ref str_devmode dest, ulong src, ulong length ) library "kernel32" alias for "RtlMoveMemory"
function ulong CopyMem ( ref blob src, ref str_devmode dest, ulong length ) library "kernel32" alias for "RtlMoveMemory"
function ulong CopyMem ( ulong src, ref str_devmode dest, ulong length ) library "kernel32" alias for "RtlMoveMemory"

function long DocumentProperties ( ulong hwnd, ulong hprinter, string null, ref blob DevModeOutput, long null_1, ulong fMode ) library "winspool.drv" alias for "DocumentPropertiesA"
function long DocumentProperties ( ulong hwnd, ulong hprinter, string null, ulong null_1, ref str_devmode pDevModeInput , ulong fMode ) library "winspool.drv" alias for "DocumentPropertiesA"
function long DocumentProperties ( ulong hwnd, ulong hprinter, string null, ulong null_1, ref blob pDevModeInput , ulong fMode ) library "winspool.drv" alias for "DocumentPropertiesA"

function long DeviceCapabilities ( string Device, string Port, uint fwCapability, ref blob pOutput, long devmode ) library "winspool.drv" alias for "DeviceCapabilitiesA"
function long DeviceCapabilities ( string Device, string Port, uint fwCapability, long null, long devmode ) library "winspool.drv" alias for "DeviceCapabilitiesA"

function ulong CreateDC ( long driver, string device, long output, long devmode ) library "gdi32" alias for "CreateDCA"
function boolean DeleteDC ( ulong hdc ) library "gdi32"
function int Escape ( ulong hdc, int nEscape, int cbInput, long inData, ref blob outdata ) library "gdi32"

function ulong GlobalAlloc(uint uFlags, ulong dwBytes ) library "kernel32"
function ulong GlobalFree ( ulong hmem ) library "kernel32"
function ulong GlobalLock ( ulong hmem ) library "kernel32"
function boolean GlobalUnlock (ulong hmem ) library "kernel32"
function ulong GlobalSize ( ulong hem ) library "kernel32"

function long PrintDlg(ref str_printdlg printdlg) library "comdlg32" alias for "PrintDlgA"
function ulong CopyMem ( ref str_devnames dest, ulong src, ulong length ) library "kernel32" alias for "RtlMoveMemory"
function ulong CopyMem ( ref blob dest, ulong src, ulong length ) library "kernel32" alias for "RtlMoveMemory"

function boolean PageSetupDlg(ref str_pagesetupdlg ll ) library "comdlg32" alias for "PageSetupDlgA"
end prototypes

type variables
constant ulong PRINTER_ENUM_DEFAULT = 1
constant ulong PRINTER_ENUM_LOCAL = 2
constant ulong PRINTER_ENUM_CONNECTIONS = 4
constant ulong PRINTER_ENUM_FAVORITE = 4
constant ulong PRINTER_ENUM_NAME = 8
constant ulong PRINTER_ENUM_REMOTE = 16
constant ulong PRINTER_ENUM_SHARED = 32
constant ulong PRINTER_ENUM_NETWORK = 64

constant ulong PRINTER_ENUM_EXPAND = 16384
constant ulong PRINTER_ENUM_CONTAINER = 32768

constant ulong PRINTER_ENUM_ICONMASK = 16711680
constant ulong PRINTER_ENUM_ICON1 = 65536
constant ulong PRINTER_ENUM_ICON2 = 131072
constant ulong PRINTER_ENUM_ICON3 = 262144
constant ulong PRINTER_ENUM_ICON4 = 545288
constant ulong PRINTER_ENUM_ICON5 = 1048576
constant ulong PRINTER_ENUM_ICON6 = 2097152
constant ulong PRINTER_ENUM_ICON7 = 4194304
constant ulong PRINTER_ENUM_ICON8 = 8388608

constant integer PRINTER_INFO_1_LEN = 16
constant integer PRINTER_INFO_2_LEN = 84
constant integer DEVMODE_LEN = 148

constant ulong DM_ORIENTATION = 00000001
constant ulong DM_PAPERSIZE = 00000002
constant ulong DM_PAPERLENGTH = 00000004
constant ulong DM_PAPERWIDTH = 00000008
constant ulong DM_SCALE = 00000016
constant ulong DM_COPIES = 00000256
constant ulong DM_DEFAULTSOURCE = 00000512
constant ulong DM_PRINTQUALITY = 000001024
constant ulong DM_COLOR = 000002048
constant ulong DM_DUPLEX = 00004096
constant ulong DM_YRESOLUTION = 00008192
constant ulong DM_TTOPTION = 000016384
constant ulong DM_COLLATE = 32768
constant ulong DM_FORMNAME = 65536
constant ulong DM_LOGPIXELS = 131072
constant ulong DM_BITSPERPEL = 262144
constant ulong DM_PELSWIDTH = 524288
constant ulong DM_PELSHEIGHT = 1048576
constant ulong DM_DISPLAYFLAGS = 2097152
constant ulong DM_DISPLAYFREQUENCY = 4194304
constant ulong DM_ICMMETHOD = 8388608
constant ulong DM_ICMINTENT = 16777216
constant ulong DM_MEDIATYPE = 33554432
constant ulong DM_DITHERTYPE = 67108864
constant ulong DM_PANNINGWIDTH = 134217728
constant ulong DM_PANNINGHEIGHT = 268435456

/* orientation selections */
constant ulong DMORIENT_PORTRAIT = 1
constant ulong DMORIENT_LANDSCAPE = 2

constant ulong PRINTER_ATTRIBUTE_QUEUED = 0001
constant ulong PRINTER_ATTRIBUTE_DIRECT = 0002
constant ulong PRINTER_ATTRIBUTE_DEFAULT = 0004
constant ulong PRINTER_ATTRIBUTE_SHARED = 0008
constant ulong PRINTER_ATTRIBUTE_NETWORK = 0016
constant ulong PRINTER_ATTRIBUTE_HIDDEN = 0032
constant ulong PRINTER_ATTRIBUTE_LOCAL = 0064

constant ulong PRINTER_ATTRIBUTE_ENABLE_DEVQ = 128
constant ulong PRINTER_ATTRIBUTE_KEEPPRINTEDJOBS = 256
constant ulong PRINTER_ATTRIBUTE_DO_COMPLETE_FIRST = 512

constant ulong PRINTER_ATTRIBUTE_WORK_OFFLINE = 1024
constant ulong PRINTER_ATTRIBUTE_ENABLE_BIDI = 2048
constant ulong PRINTER_ATTRIBUTE_RAW_ONLY = 4096

constant ulong DM_IN_BUFFER = 8
constant ulong DM_IN_PROMPT = 4
constant ulong DM_OUT_BUFFER = 2
constant ulong DM_OUT_DEFAULT = 1

constant ulong DC_FIELDS = 1
constant ulong DC_PAPERS = 2
constant ulong DC_PAPERSIZE = 3
constant ulong DC_MINEXTENT = 4
constant ulong DC_MAXEXTENT = 5
constant ulong DC_BINS = 6
constant ulong DC_DUPLEX = 7
constant ulong DC_SIZE = 8
constant ulong DC_EXTRA = 9
constant ulong DC_VERSION = 10
constant ulong DC_DRIVER = 11
constant ulong DC_BINNAMES = 12
constant ulong DC_ENUMRESOLUTIONS = 13
constant ulong DC_FILEDEPENDENCIES= 14
constant ulong DC_TRUETYPE = 15
constant ulong DC_PAPERNAMES = 16
constant ulong DC_ORIENTATION = 17
constant ulong DC_COPIES = 18

constant uint DMPAPER_LETTER = 1 /* Letter 8 1/2 x 11 in */
constant uint DMPAPER_LETTERSMALL = 2 /* Letter Small 8 1/2 x 11 in */
constant uint DMPAPER_TABLOID = 3 /* Tabloid 11 x 17 in */
constant uint DMPAPER_LEDGER = 4 /* Ledger 17 x 11 in */
constant uint DMPAPER_LEGAL = 5 /* Legal 8 1/2 x 14 in */
constant uint DMPAPER_STATEMENT = 6 /* Statement 5 1/2 x 8 1/2 in */
constant uint DMPAPER_EXECUTIVE = 7 /* Executive 7 1/4 x 10 1/2 in */
constant uint DMPAPER_A3 = 8 /* A3 297 x 420 mm */
constant uint DMPAPER_A4 = 9 /* A4 210 x 297 mm */
constant uint DMPAPER_A4SMALL = 10 /* A4 Small 210 x 297 mm */
constant uint DMPAPER_A5 = 11 /* A5 148 x 210 mm */
constant uint DMPAPER_B4 = 12 /* B4 (JIS) 250 x 354 */
constant uint DMPAPER_B5 = 13 /* B5 (JIS) 182 x 257 mm */
constant uint DMPAPER_FOLIO = 14 /* Folio 8 1/2 x 13 in */
constant uint DMPAPER_QUARTO = 15 /* Quarto 215 x 275 mm */
constant uint DMPAPER_10X14 = 16 /* 10x14 in */
constant uint DMPAPER_11X17 = 17 /* 11x17 in */
constant uint DMPAPER_NOTE = 18 /* Note 8 1/2 x 11 in */
constant uint DMPAPER_ENV_9 = 19 /* Envelope #9 3 7/8 x 8 7/8 */
constant uint DMPAPER_ENV_10 = 20 /* Envelope #10 4 1/8 x 9 1/2 */
constant uint DMPAPER_ENV_11 = 21 /* Envelope #11 4 1/2 x 10 3/8 */
constant uint DMPAPER_ENV_12 = 22 /* Envelope #12 4 \276 x 11 */
constant uint DMPAPER_ENV_14 = 23 /* Envelope #14 5 x 11 1/2 */
constant uint DMPAPER_CSHEET = 24 /* C size sheet */
constant uint DMPAPER_DSHEET = 25 /* D size sheet */
constant uint DMPAPER_ESHEET = 26 /* E size sheet */
constant uint DMPAPER_ENV_DL = 27 /* Envelope DL 110 x 220mm */
constant uint DMPAPER_ENV_C5 = 28 /* Envelope C5 162 x 229 mm */
constant uint DMPAPER_ENV_C3 = 29 /* Envelope C3 324 x 458 mm */
constant uint DMPAPER_ENV_C4 = 30 /* Envelope C4 229 x 324 mm */
constant uint DMPAPER_ENV_C6 = 31 /* Envelope C6 114 x 162 mm */
constant uint DMPAPER_ENV_C65 = 32 /* Envelope C65 114 x 229 mm */
constant uint DMPAPER_ENV_B4 = 33 /* Envelope B4 250 x 353 mm */
constant uint DMPAPER_ENV_B5 = 34 /* Envelope B5 176 x 250 mm */
constant uint DMPAPER_ENV_B6 = 35 /* Envelope B6 176 x 125 mm */
constant uint DMPAPER_ENV_ITALY = 36 /* Envelope 110 x 230 mm */
constant uint DMPAPER_ENV_MONARCH = 37 /* Envelope Monarch 3.875 x 7.5 in */
constant uint DMPAPER_ENV_PERSONAL = 38 /* 6 3/4 Envelope 3 5/8 x 6 1/2 in */
constant uint DMPAPER_FANFOLD_US = 39 /* US Std Fanfold 14 7/8 x 11 in */
constant uint DMPAPER_FANFOLD_STD_GERMAN = 40 /* German Std Fanfold 8 1/2 x 12 in */
constant uint DMPAPER_FANFOLD_LGL_GERMAN = 41 /* German Legal Fanfold 8 1/2 x 13 in */
constant uint DMPAPER_ISO_B4 = 42 /* B4 (ISO) 250 x 353 mm */
constant uint DMPAPER_JAPANESE_POSTCARD = 43 /* Japanese Postcard 100 x 148 mm */
constant uint DMPAPER_9X11 = 44 /* 9 x 11 in */
constant uint DMPAPER_10X11 = 45 /* 10 x 11 in */
constant uint DMPAPER_15X11 = 46 /* 15 x 11 in */
constant uint DMPAPER_ENV_INVITE = 47 /* Envelope Invite 220 x 220 mm */
constant uint DMPAPER_RESERVED_48 = 48 /* RESERVED--DO NOT USE */
constant uint DMPAPER_RESERVED_49 = 49 /* RESERVED--DO NOT USE */
constant uint DMPAPER_LETTER_EXTRA = 50 /* Letter Extra 9 \275 x 12 in */
constant uint DMPAPER_LEGAL_EXTRA = 51 /* Legal Extra 9 \275 x 15 in */
constant uint DMPAPER_TABLOID_EXTRA = 52 /* Tabloid Extra 11.69 x 18 in */
constant uint DMPAPER_A4_EXTRA = 53 /* A4 Extra 9.27 x 12.69 in */
constant uint DMPAPER_LETTER_TRANSVERSE = 54 /* Letter Transverse 8 \275 x 11 in */
constant uint DMPAPER_A4_TRANSVERSE = 55 /* A4 Transverse 210 x 297 mm */
constant uint DMPAPER_LETTER_EXTRA_TRANSVERSE =56 /* Letter Extra Transverse 9\275 x 12 in */
constant uint DMPAPER_A_PLUS = 57 /* SuperA/SuperA/A4 227 x 356 mm */
constant uint DMPAPER_B_PLUS = 58 /* SuperB/SuperB/A3 305 x 487 mm */
constant uint DMPAPER_LETTER_PLUS = 59 /* Letter Plus 8.5 x 12.69 in */
constant uint DMPAPER_A4_PLUS = 60 /* A4 Plus 210 x 330 mm */
constant uint DMPAPER_A5_TRANSVERSE = 61 /* A5 Transverse 148 x 210 mm */
constant uint DMPAPER_B5_TRANSVERSE = 62 /* B5 (JIS) Transverse 182 x 257 mm */
constant uint DMPAPER_A3_EXTRA = 63 /* A3 Extra 322 x 445 mm */
constant uint DMPAPER_A5_EXTRA = 64 /* A5 Extra 174 x 235 mm */
constant uint DMPAPER_B5_EXTRA = 65 /* B5 (ISO) Extra 201 x 276 mm */
constant uint DMPAPER_A2 = 66 /* A2 420 x 594 mm */
constant uint DMPAPER_A3_TRANSVERSE = 67 /* A3 Transverse 297 x 420 mm */
constant uint DMPAPER_A3_EXTRA_TRANSVERSE = 68 /* A3 Extra Transverse 322 x 445 mm */
constant uint DMPAPER_USER = 256

constant uint GMEM_MOVEABLE = 2

constant int PRINTDLG_LEN = 66

constant ulong PD_ALLPAGES = 000000
constant ulong PD_SELECTION = 000001
constant ulong PD_PAGENUMS = 000002
constant ulong PD_NOSELECTION = 000004
constant ulong PD_NOPAGENUMS = 000008
constant ulong PD_COLLATE = 000016
constant ulong PD_PRINTTOFILE = 000032
constant ulong PD_PRINTSETUP = 000064
constant ulong PD_NOWARNING = 0000128
constant ulong PD_RETURNDC = 256
constant ulong PD_RETURNIC = 512
constant ulong PD_RETURNDEFAULT = 1024
constant ulong PD_SHOWHELP = 2048
constant ulong PD_ENABLEPRINTHOOK = 4096
constant ulong PD_ENABLESETUPHOOK = 8192
constant ulong PD_ENABLEPRINTTEMPLATE = 16384
constant ulong PD_ENABLESETUPTEMPLATE = 32768
constant ulong PD_ENABLEPRINTTEMPLATEHANDLE = 65536
constant ulong PD_ENABLESETUPTEMPLATEHANDLE = 131072
constant ulong PD_USEDEVMODECOPIES = 262144
constant ulong PD_USEDEVMODECOPIESANDCOLLATE = 524288
constant ulong PD_DISABLEPRINTTOFILE = 1048576
constant ulong PD_HIDEPRINTTOFILE = 2097152
constant ulong PD_NONETWORKBUTTON = 4194304

constant int DEVNAMES_LEN = 32 * 3 +8

constant ulong PSD_DEFAULTMINMARGINS = 0000000 // default (printer's)
constant ulong PSD_INWININIINTLMEASURE = 0000000 // 1st of 4 possible
constant ulong PSD_MINMARGINS = 0000001 // use caller's
constant ulong PSD_MARGINS = 0000002 // use caller's
constant ulong PSD_INTHOUSANDTHSOFINCHES = 0000004 // 2nd of 4 possible
constant ulong PSD_INHUNDREDTHSOFMILLIMETERS = 0000008 // 3rd of 4 possible
constant ulong PSD_DISABLEMARGINS = 16
constant ulong PSD_DISABLEPRINTER = 32
constant ulong PSD_NOWARNING = 128 // must be same as PD_*
constant ulong PSD_DISABLEORIENTATION = 256
constant ulong PSD_RETURNDEFAULT = 1024 // must be same as PD_*
constant ulong PSD_DISABLEPAPER = 512
constant ulong PSD_SHOWHELP = 2048 // must be same as PD_*
constant ulong PSD_ENABLEPAGESETUPHOOK = 8192 // must be same as PD_*
constant ulong PSD_ENABLEPAGESETUPTEMPLATE = 32768 // must be same as PD_*
constant ulong PSD_ENABLEPAGESETUPTEMPLATEHANDLE = 131072 // must be same as PD_*
constant ulong PSD_ENABLEPAGEPAINTHOOK = 262144
constant ulong PSD_DISABLEPAGEPAINTING = 524288
constant ulong PSD_NONETWORKBUTTON = 2097152 // must be same as PD_*

constant int PAGESETUPDLG_LEN = 84
end variables

forward prototypes
public function integer of_getprinterlist (ref string printerlist[])
public function integer of_direct (string PrinterName, string data)
public function integer of_printer (string printername, integer command, ref any outpara, ref integer papersize, ref integer paperwidth, ref integer paperlength)
public function string of_getdrivername (string PrinterName)
public function string of_getportname (string printername)
public function int of_getattribute (string printername)
public function long of_getstatus (string printername)
public function integer of_getpapersize (string printername, ref integer papersize, ref integer paperwidth, ref integer paperheight)
public function string of_getpbprinter ()
public function integer of_setdefault (string printername)
private function string of_getstringfromblob (blob source, integer offset)
private function integer of_modifyprinter (string printername, str_devmode devmode)
public function integer of_printsetup (unsignedlong hwnd)
public function integer of_setpapersize (string printername, integer papersize, integer paperwidth, integer paperheight)
public function integer of_pagesetup (unsignedlong hwnd, ref long left, ref long right, ref long top, ref long bottom)
public function integer of_setdwpapersize (readonly datawindow adw_dw, integer ai_w, integer ai_h)
public function integer of_setdspapersize (readonly datastore adw_dw, integer ai_w, integer ai_h)
end prototypes

public function integer of_getprinterlist (ref string printerlist[]);// 取打印机列表
// 在printerlist[]中返回
// 返回值为打印机个数,返回-1,错误
integer li_Count , li_Size, li_i
String ls_Name
Blob lbl_buffer, lbl_tmp
ulong ll_cbNeeded, ll_Returned
str_Printer_info_2 lstr_info

li_Size = 1

l_GetPrinter:
lbl_buffer = Blob ( Space ( li_size ) )
SetNull ( ls_Name )
If Not EnumPrinters( PRINTER_ENUM_LOCAL, ls_Name, 2, ref lbl_buffer, li_size, ll_cbNeeded, ll_Returned ) Then
If li_size < ll_cbNeeded Then
li_size = ll_cbNeeded
Goto l_GetPrinter
Else
Return -1
End If
End If

string ls_desc, ls_pname, ls_comment
For li_i = 1 To integer ( ll_Returned )
lbl_tmp = BlobMid ( lbl_buffer, (li_i -1 ) * PRINTER_INFO_2_LEN + 1 )
CopyMem ( lstr_info, lbl_tmp, PRINTER_INFO_2_LEN )
printerlist [ li_i ] = String ( lstr_info.PrinterName, "address" )
ls_pname = string ( lstr_info.PortName, "address" )
Next

Return Integer ( ll_Returned )
end function

public function integer of_direct (string PrinterName, string data);// 绕过windows直接向打印机发送字符
// PrinterName: 打印机名称
// data: 需要发送的数据
// 返回: 1 = 成功, 2 = 失败
ulong ll_handle
// 建立打印机handle
ll_handle = CreateDC ( 0, PrinterName, 0, 0 )

DeleteDC ( ll_handle )
Return 1
end function

public function integer of_printer (string printername, integer command, ref any outpara, ref integer papersize, ref integer paperwidth, ref integer paperlength);// 与打印机有关的操作
// PrinterName:打印机名称
// Command:命令
// =1: 取打印机驱动名称
// = 2: 取打印机端口名称
// = 3: 取打印机状态
// = 4: 取打印机属性
// = 5: 取打印机纸张
// = 6: 取打印机自定义纸张的大小
// = 7: 取打印方向
// = 8: 设置为缺省打印机
// = 101: 设置打印机的纸张
// = 102: 将打印机设为自定义纸张并设定自定义纸张的大小
// = 103: 设置打印方向

// outpara: 1,2,3,4, 打印机参数由此返回
// papersize: 打印纸大小
// pagerwidth, pagerheight:自定义打印纸尺
// 取得打印机打印纸
// return 1: 成功
// return -1: 不成功

str_Printer_info_2 lstr_info
str_Printer_defaults lstr_default
str_devmode lstr_devmode
Integer li_rc, li_i
string ls_DriverName, ls_PortName
ulong ll_handle, ll_cbbuf, ll_cbNeeded
blob lbl_buffer

// 打开打印机
If Not OpenPrinter ( printername, ll_handle, ref lstr_default ) Then Return -1
ll_cbbuf = 0

// 取打印机驱动名称
ll_cbBuf = 0

l_SetBuffer:
lbl_buffer = Blob ( Space ( ll_cbBuf ) )
If Not GetPrinter ( ll_handle, 2, ref lbl_buffer, ll_cbBuf, ll_cbNeeded ) Then
If ll_cbNeeded > ll_cbBuf Then
ll_cbBuf = ll_cbNeeded
Goto l_SetBuffer
Else
Return -1
End If
End If

// copy 至结构
CopyMem ( lstr_info, lbl_buffer, PRINTER_INFO_2_LEN )

// 驱动名、端口
ls_DriverName = String ( lstr_info.DriverName, "address" )
ls_PortName = String ( lstr_info.PortName, "address" )

Choose Case Command
Case 1
// 驱动名称
OutPara = ls_DriverName
li_rc = 1
Goto l_quit
Case 2
// 端口名称
OutPara = ls_PortName
li_rc = 1
Goto l_quit
Case 3
// 打印机状态
OutPara = lstr_info.Status
li_rc = 1
Goto l_quit
Case 4
// 打印机属性
OutPara = lstr_info.Attributes
li_rc = 1
Goto l_quit
Case 5,6
// 打印纸大小
CopyMem ( lstr_devmode, lstr_info.DevMode, DEVMODE_LEN )
papersize = lstr_devMode.dmPaperSize
paperwidth = lstr_devMode.dmPaperWidth
paperlength = lstr_devMode.dmPaperLength
li_rc = 1
Goto l_quit
Case 7
// 打印方向
CopyMem ( lstr_devmode, lstr_info.DevMode, DEVMODE_LEN )
OutPara = lstr_devMode.dmOrientation
li_rc = 1
Goto l_quit
Case 105
// 设置为缺省打印机
lstr_info.Attributes += PRINTER_ATTRIBUTE_DEFAULT
CopyMem ( lbl_buffer, lstr_info, PRINTER_INFO_2_LEN )
If SetPrinter ( ll_handle , 2, lbl_buffer, 0 ) Then
li_rc = 1
Else
li_rc = -1
end If
Goto l_quit
End Choose

If Command = 101 Or Command = 102 Then
// 取打印机支持的纸张
SetNull ( lbl_buffer )
ll_cbbuf = DeviceCapabilities ( printername, ls_PortName, DC_PAPERS, 0, 0 )
lbl_buffer = Blob ( Space ( 1024 ) ) // ll_cbbuf ) )
ll_cbbuf = DeviceCapabilities ( PrinterName, ls_PortName, DC_PAPERS, lbl_buffer, 0 )
If ll_cbbuf < 0 Then Return -1

// 检查是否支持用户指定纸张
For li_i =1 To Integer ( ll_cbbuf )
If integer ( BlobMid ( lbl_buffer, ( li_i - 1 ) * 2 + 1, 2 ) ) = papersize Then Exit
Next

If li_i > Integer ( ll_cbbuf ) Then
// 不支持自定久纸张
Return -1
End If
End If

// 取需要的空间
ll_cbbuf =DocumentProperties ( 0, ll_handle, ls_DriverName, ref lbl_buffer, 0 , 0 )
If ll_cbbuf < 0 Then
li_rc = -1
Goto l_quit
End If

// 分配空间
lbl_buffer = Blob ( Space ( ll_cbbuf ) )
li_rc = DocumentProperties ( 0, ll_handle, ls_DriverName, ref lbl_buffer, 0, DM_OUT_BUFFER )

CopyMem ( lstr_devmode, lbl_buffer, DEVMODE_LEN )

Choose Case command
Case 101,102
lstr_devMode.dmPaperSize = papersize
lstr_devMode.dmFields = DM_PAPERSIZE
If PaperSize = DMPAPER_USER Then
lstr_devMode.dmPaperWidth = paperwidth
lstr_devMode.dmPaperLength = paperlength
lstr_devMode.dmFields += DM_PAPERLENGTH + DM_PAPERWIDTH
End If
Case 103
lstr_devMode.dmOrientation = outpara
lstr_devMode.dmFields = DM_ORIENTATION
End Choose

li_rc = DocumentProperties ( 0, ll_handle, ls_DriverName, 0, lstr_devmode, DM_IN_BUFFER + DM_OUT_DEFAULT )
If li_rc > 0 Then li_rc = 1

l_quit:
ClosePrinter ( ll_handle )
Return li_rc
end function

public function string of_getdrivername (string PrinterName);// 取打印机驱动程序名称
String ls_Driver
Integer li_1, li_2, li_3, li_rc
Any la_para
li_rc = of_Printer ( PrinterName, 1, la_para, li_1, li_2, li_3 )
If li_rc = 1 Then
ls_Driver = String ( la_para )
Else
ls_Driver = ''
End If

Return ls_Driver

end function

public function string of_getportname (string printername);// 取打印机端口名称
String ls_port
Integer li_1, li_2, li_3, li_rc
Any la_para
li_rc = of_Printer ( PrinterName, 2, la_para, li_1, li_2, li_3 )
If li_rc = 1 Then
ls_port = String ( la_para )
Else
ls_port = ''
End If

Return ls_port

end function

public function int of_getattribute (string printername);// 取打印机属性,下为打印机属性定义
/*
#define PRINTER_ATTRIBUTE_QUEUED 0x00000001
#define PRINTER_ATTRIBUTE_DIRECT 0x00000002
#define PRINTER_ATTRIBUTE_DEFAULT 0x00000004
#define PRINTER_ATTRIBUTE_SHARED 0x00000008
#define PRINTER_ATTRIBUTE_NETWORK 0x00000010
#define PRINTER_ATTRIBUTE_HIDDEN 0x00000020
#define PRINTER_ATTRIBUTE_LOCAL 0x00000040

#define PRINTER_ATTRIBUTE_ENABLE_DEVQ 0x00000080
#define PRINTER_ATTRIBUTE_KEEPPRINTEDJOBS 0x00000100
#define PRINTER_ATTRIBUTE_DO_COMPLETE_FIRST 0x00000200

#define PRINTER_ATTRIBUTE_WORK_OFFLINE 0x00000400
#define PRINTER_ATTRIBUTE_ENABLE_BIDI 0x00000800
#define PRINTER_ATTRIBUTE_RAW_ONLY 0x00001000
#define PRINTER_ATTRIBUTE_PUBLISHED 0x00002000
*/

int li_attrib
Integer li_1, li_2, li_3, li_rc
Any la_para
li_rc = of_Printer ( PrinterName, 2, la_para, li_1, li_2, li_3 )
If li_rc = 1 Then
li_attrib = la_para
Else
li_attrib = -1
End If

Return li_attrib

end function

public function long of_getstatus (string printername);// 取打印机状态,下为打印机状态定义
/*
#define PRINTER_STATUS_PAUSED 0x00000001
#define PRINTER_STATUS_ERROR 0x00000002
#define PRINTER_STATUS_PENDING_DELETION 0x00000004
#define PRINTER_STATUS_PAPER_JAM 0x00000008
#define PRINTER_STATUS_PAPER_OUT 0x00000010
#define PRINTER_STATUS_MANUAL_FEED 0x00000020
#define PRINTER_STATUS_PAPER_PROBLEM 0x00000040
#define PRINTER_STATUS_OFFLINE 0x00000080
#define PRINTER_STATUS_IO_ACTIVE 0x00000100
#define PRINTER_STATUS_BUSY 0x00000200
#define PRINTER_STATUS_PRINTING 0x00000400
#define PRINTER_STATUS_OUTPUT_BIN_FULL 0x00000800
#define PRINTER_STATUS_NOT_AVAILABLE 0x00001000
#define PRINTER_STATUS_WAITING 0x00002000
#define PRINTER_STATUS_PROCESSING 0x00004000
#define PRINTER_STATUS_INITIALIZING 0x00008000
#define PRINTER_STATUS_WARMING_UP 0x00010000
#define PRINTER_STATUS_TONER_LOW 0x00020000
#define PRINTER_STATUS_NO_TONER 0x00040000
#define PRINTER_STATUS_PAGE_PUNT 0x00080000
#define PRINTER_STATUS_USER_INTERVENTION 0x00100000
#define PRINTER_STATUS_OUT_OF_MEMORY 0x00200000
#define PRINTER_STATUS_DOOR_OPEN 0x00400000
#define PRINTER_STATUS_SERVER_UNKNOWN 0x00800000
#define PRINTER_STATUS_POWER_SAVE 0x01000000
*/

long ll_status
Integer li_1, li_2, li_3, li_rc
Any la_para
li_rc = of_Printer ( PrinterName, 3, la_para, li_1, li_2, li_3 )
If li_rc = 1 Then
ll_status = la_para
Else
ll_status = -1
End If

Return ll_status

end function

public function integer of_getpapersize (string printername, ref integer papersize, ref integer paperwidth, ref integer paperheight);// 取打印机纸张大小
/* 打印机尺寸定义
#define DMPAPER_LETTER 1 /* Letter 8 1/2 x 11 in */
#define DMPAPER_LETTERSMALL 2 /* Letter Small 8 1/2 x 11 in */
#define DMPAPER_TABLOID 3 /* Tabloid 11 x 17 in */
#define DMPAPER_LEDGER 4 /* Ledger 17 x 11 in */
#define DMPAPER_LEGAL 5 /* Legal 8 1/2 x 14 in */
#define DMPAPER_STATEMENT 6 /* Statement 5 1/2 x 8 1/2 in */
#define DMPAPER_EXECUTIVE 7 /* Executive 7 1/4 x 10 1/2 in */
#define DMPAPER_A3 8 /* A3 297 x 420 mm */
#define DMPAPER_A4 9 /* A4 210 x 297 mm */
#define DMPAPER_A4SMALL 10 /* A4 Small 210 x 297 mm */
#define DMPAPER_A5 11 /* A5 148 x 210 mm */
#define DMPAPER_B4 12 /* B4 (JIS) 250 x 354 */
#define DMPAPER_B5 13 /* B5 (JIS) 182 x 257 mm */
#define DMPAPER_FOLIO 14 /* Folio 8 1/2 x 13 in */
#define DMPAPER_QUARTO 15 /* Quarto 215 x 275 mm */
#define DMPAPER_10X14 16 /* 10x14 in */
#define DMPAPER_11X17 17 /* 11x17 in */
#define DMPAPER_NOTE 18 /* Note 8 1/2 x 11 in */
#define DMPAPER_ENV_9 19 /* Envelope #9 3 7/8 x 8 7/8 */
#define DMPAPER_ENV_10 20 /* Envelope #10 4 1/8 x 9 1/2 */
#define DMPAPER_ENV_11 21 /* Envelope #11 4 1/2 x 10 3/8 */
#define DMPAPER_ENV_12 22 /* Envelope #12 4 \276 x 11 */
#define DMPAPER_ENV_14 23 /* Envelope #14 5 x 11 1/2 */
#define DMPAPER_CSHEET 24 /* C size sheet */
#define DMPAPER_DSHEET 25 /* D size sheet */
#define DMPAPER_ESHEET 26 /* E size sheet */
#define DMPAPER_ENV_DL 27 /* Envelope DL 110 x 220mm */
#define DMPAPER_ENV_C5 28 /* Envelope C5 162 x 229 mm */
#define DMPAPER_ENV_C3 29 /* Envelope C3 324 x 458 mm */
#define DMPAPER_ENV_C4 30 /* Envelope C4 229 x 324 mm */
#define DMPAPER_ENV_C6 31 /* Envelope C6 114 x 162 mm */
#define DMPAPER_ENV_C65 32 /* Envelope C65 114 x 229 mm */
#define DMPAPER_ENV_B4 33 /* Envelope B4 250 x 353 mm */
#define DMPAPER_ENV_B5 34 /* Envelope B5 176 x 250 mm */
#define DMPAPER_ENV_B6 35 /* Envelope B6 176 x 125 mm */
#define DMPAPER_ENV_ITALY 36 /* Envelope 110 x 230 mm */
#define DMPAPER_ENV_MONARCH 37 /* Envelope Monarch 3.875 x 7.5 in */
#define DMPAPER_ENV_PERSONAL 38 /* 6 3/4 Envelope 3 5/8 x 6 1/2 in */
#define DMPAPER_FANFOLD_US 39 /* US Std Fanfold 14 7/8 x 11 in */
#define DMPAPER_FANFOLD_STD_GERMAN 40 /* German Std Fanfold 8 1/2 x 12 in */
#define DMPAPER_FANFOLD_LGL_GERMAN 41 /* German Legal Fanfold 8 1/2 x 13 in */
#if(WINVER >= 0x0400)
#define DMPAPER_ISO_B4 42 /* B4 (ISO) 250 x 353 mm */
#define DMPAPER_JAPANESE_POSTCARD 43 /* Japanese Postcard 100 x 148 mm */
#define DMPAPER_9X11 44 /* 9 x 11 in */
#define DMPAPER_10X11 45 /* 10 x 11 in */
#define DMPAPER_15X11 46 /* 15 x 11 in */
#define DMPAPER_ENV_INVITE 47 /* Envelope Invite 220 x 220 mm */
#define DMPAPER_RESERVED_48 48 /* RESERVED--DO NOT USE */
#define DMPAPER_RESERVED_49 49 /* RESERVED--DO NOT USE */
#define DMPAPER_LETTER_EXTRA 50 /* Letter Extra 9 \275 x 12 in */
#define DMPAPER_LEGAL_EXTRA 51 /* Legal Extra 9 \275 x 15 in */
#define DMPAPER_TABLOID_EXTRA 52 /* Tabloid Extra 11.69 x 18 in */
#define DMPAPER_A4_EXTRA 53 /* A4 Extra 9.27 x 12.69 in */
#define DMPAPER_LETTER_TRANSVERSE 54 /* Letter Transverse 8 \275 x 11 in */
#define DMPAPER_A4_TRANSVERSE 55 /* A4 Transverse 210 x 297 mm */
#define DMPAPER_LETTER_EXTRA_TRANSVERSE 56 /* Letter Extra Transverse 9\275 x 12 in */
#define DMPAPER_A_PLUS 57 /* SuperA/SuperA/A4 227 x 356 mm */
#define DMPAPER_B_PLUS 58 /* SuperB/SuperB/A3 305 x 487 mm */
#define DMPAPER_LETTER_PLUS 59 /* Letter Plus 8.5 x 12.69 in */
#define DMPAPER_A4_PLUS 60 /* A4 Plus 210 x 330 mm */
#define DMPAPER_A5_TRANSVERSE 61 /* A5 Transverse 148 x 210 mm */
#define DMPAPER_B5_TRANSVERSE 62 /* B5 (JIS) Transverse 182 x 257 mm */
#define DMPAPER_A3_EXTRA 63 /* A3 Extra 322 x 445 mm */
#define DMPAPER_A5_EXTRA 64 /* A5 Extra 174 x 235 mm */
#define DMPAPER_B5_EXTRA 65 /* B5 (ISO) Extra 201 x 276 mm */
#define DMPAPER_A2 66 /* A2 420 x 594 mm */
#define DMPAPER_A3_TRANSVERSE 67 /* A3 Transverse 297 x 420 mm */
#define DMPAPER_A3_EXTRA_TRANSVERSE 68 /* A3 Extra Transverse 322 x 445 mm */
#endif /* WINVER >= 0x0400 */

#if(WINVER >= 0x0500)
#define DMPAPER_DBL_JAPANESE_POSTCARD 69 /* Japanese Double Postcard 200 x 148 mm */
#define DMPAPER_A6 70 /* A6 105 x 148 mm */
#define DMPAPER_JENV_KAKU2 71 /* Japanese Envelope Kaku #2 */
#define DMPAPER_JENV_KAKU3 72 /* Japanese Envelope Kaku #3 */
#define DMPAPER_JENV_CHOU3 73 /* Japanese Envelope Chou #3 */
#define DMPAPER_JENV_CHOU4 74 /* Japanese Envelope Chou #4 */
#define DMPAPER_LETTER_ROTATED 75 /* Letter Rotated 11 x 8 1/2 11 in */
#define DMPAPER_A3_ROTATED 76 /* A3 Rotated 420 x 297 mm */
#define DMPAPER_A4_ROTATED 77 /* A4 Rotated 297 x 210 mm */
#define DMPAPER_A5_ROTATED 78 /* A5 Rotated 210 x 148 mm */
#define DMPAPER_B4_JIS_ROTATED 79 /* B4 (JIS) Rotated 364 x 257 mm */
#define DMPAPER_B5_JIS_ROTATED 80 /* B5 (JIS) Rotated 257 x 182 mm */
#define DMPAPER_JAPANESE_POSTCARD_ROTATED 81 /* Japanese Postcard Rotated 148 x 100 mm */
#define DMPAPER_DBL_JAPANESE_POSTCARD_ROTATED 82 /* Double Japanese Postcard Rotated 148 x 200 mm */
#define DMPAPER_A6_ROTATED 83 /* A6 Rotated 148 x 105 mm */
#define DMPAPER_JENV_KAKU2_ROTATED 84 /* Japanese Envelope Kaku #2 Rotated */
#define DMPAPER_JENV_KAKU3_ROTATED 85 /* Japanese Envelope Kaku #3 Rotated */
#define DMPAPER_JENV_CHOU3_ROTATED 86 /* Japanese Envelope Chou #3 Rotated */
#define DMPAPER_JENV_CHOU4_ROTATED 87 /* Japanese Envelope Chou #4 Rotated */
#define DMPAPER_B6_JIS 88 /* B6 (JIS) 128 x 182 mm */
#define DMPAPER_B6_JIS_ROTATED 89 /* B6 (JIS) Rotated 182 x 128 mm */
#define DMPAPER_12X11 90 /* 12 x 11 in */
#define DMPAPER_JENV_YOU4 91 /* Japanese Envelope You #4 */
#define DMPAPER_JENV_YOU4_ROTATED 92 /* Japanese Envelope You #4 Rotated*/
#define DMPAPER_P16K 93 /* PRC 16K 146 x 215 mm */
#define DMPAPER_P32K 94 /* PRC 32K 97 x 151 mm */
#define DMPAPER_P32KBIG 95 /* PRC 32K(Big) 97 x 151 mm */
#define DMPAPER_PENV_1 96 /* PRC Envelope #1 102 x 165 mm */
#define DMPAPER_PENV_2 97 /* PRC Envelope #2 102 x 176 mm */
#define DMPAPER_PENV_3 98 /* PRC Envelope #3 125 x 176 mm */
#define DMPAPER_PENV_4 99 /* PRC Envelope #4 110 x 208 mm */
#define DMPAPER_PENV_5 100 /* PRC Envelope #5 110 x 220 mm */
#define DMPAPER_PENV_6 101 /* PRC Envelope #6 120 x 230 mm */
#define DMPAPER_PENV_7 102 /* PRC Envelope #7 160 x 230 mm */
#define DMPAPER_PENV_8 103 /* PRC Envelope #8 120 x 309 mm */
#define DMPAPER_PENV_9 104 /* PRC Envelope #9 229 x 324 mm */
#define DMPAPER_PENV_10 105 /* PRC Envelope #10 324 x 458 mm */
#define DMPAPER_P16K_ROTATED 106 /* PRC 16K Rotated */
#define DMPAPER_P32K_ROTATED 107 /* PRC 32K Rotated */
#define DMPAPER_P32KBIG_ROTATED 108 /* PRC 32K(Big) Rotated */
#define DMPAPER_PENV_1_ROTATED 109 /* PRC Envelope #1 Rotated 165 x 102 mm */
#define DMPAPER_PENV_2_ROTATED 110 /* PRC Envelope #2 Rotated 176 x 102 mm */
#define DMPAPER_PENV_3_ROTATED 111 /* PRC Envelope #3 Rotated 176 x 125 mm */
#define DMPAPER_PENV_4_ROTATED 112 /* PRC Envelope #4 Rotated 208 x 110 mm */
#define DMPAPER_PENV_5_ROTATED 113 /* PRC Envelope #5 Rotated 220 x 110 mm */
#define DMPAPER_PENV_6_ROTATED 114 /* PRC Envelope #6 Rotated 230 x 120 mm */
#define DMPAPER_PENV_7_ROTATED 115 /* PRC Envelope #7 Rotated 230 x 160 mm */
#define DMPAPER_PENV_8_ROTATED 116 /* PRC Envelope #8 Rotated 309 x 120 mm */
#define DMPAPER_PENV_9_ROTATED 117 /* PRC Envelope #9 Rotated 324 x 229 mm */
#define DMPAPER_PENV_10_ROTATED 118 /* PRC Envelope #10 Rotated 458 x 324 mm */
#endif /* WINVER >= 0x0500 */

#if (WINVER >= 0x0500)
#define DMPAPER_LAST DMPAPER_PENV_10_ROTATED
#elif (WINVER >= 0x0400)
#define DMPAPER_LAST DMPAPER_A3_EXTRA_TRANSVERSE
#else
#define DMPAPER_LAST DMPAPER_FANFOLD_LGL_GERMAN
#endif

#define DMPAPER_USER 256
*/
Integer li_rc
any la_buff
Return of_Printer ( printername, 5, la_buff, papersize,paperwidth,paperheight )

end function

public function string of_getpbprinter ();String ls_printer
Integer li_Pos
ls_printer = ProfileString ( "win.ini", "windows", "device", "" )
If ls_printer = "" then Return ""

li_Pos = Pos ( ls_printer, "," )
If li_Pos <> 0 Then
ls_printer = Left ( ls_printer, li_Pos - 1 )
End If

Return ls_printer
end function

public function integer of_setdefault (string printername);// 设置为缺省打印机
integer li_size, li_width, li_length
any la_any
Return of_Printer( printername, 105, la_any, li_size, li_width, li_length )
end function

private function string of_getstringfromblob (blob source, integer offset);// 从指定位置得到一个以0结束的字符串
String ls_char, ls_target
Integer li_i
For li_i = offset To Len ( source )
ls_char = String ( BlobMid ( source, li_i, 1 ) )
If Len ( ls_char ) = 0 Then Exit
ls_target += ls_char
Next

Return ls_target
end function

private function integer of_modifyprinter (string printername, str_devmode devmode);// 修改打印机参数
// 仅供内部使用
// 与打印机有关的操作
// return 1: 成功
// return -1: 不成功

str_Printer_info_2 lstr_info
str_Printer_defaults lstr_default
str_devmode lstr_devmode
Integer li_rc, li_i
string ls_DriverName, ls_PortName
ulong ll_handle, ll_cbbuf, ll_cbNeeded
blob lbl_buffer

// 打开打印机
If Not OpenPrinter ( printername, ll_handle, ref lstr_default ) Then Return -1
ll_cbbuf = 0

// 取打印机驱动名称
ll_cbBuf = 0

l_SetBuffer:
lbl_buffer = Blob ( Space ( ll_cbBuf ) )
If Not GetPrinter ( ll_handle, 2, ref lbl_buffer, ll_cbBuf, ll_cbNeeded ) Then
If ll_cbNeeded > ll_cbBuf Then
ll_cbBuf = ll_cbNeeded
Goto l_SetBuffer
Else
li_rc = -1
Goto l_quit
End If
End If

// copy 至结构
CopyMem ( lstr_info, lbl_buffer, PRINTER_INFO_2_LEN )

// 驱动名、端口
ls_DriverName = String ( lstr_info.DriverName, "address" )
ls_PortName = String ( lstr_info.PortName, "address" )

// 设置更新所有属性
devMode.dmFields = 536870911
li_rc = DocumentProperties ( 0, ll_handle, ls_DriverName, 0, devmode, DM_IN_BUFFER + DM_OUT_DEFAULT )
If li_rc > 0 Then li_rc = 1

l_quit:
ClosePrinter ( ll_handle )
Return li_rc
end function

public function integer of_printsetup (unsignedlong hwnd);str_PrintDlg lstr_Dlg
str_devmode lstr_devmode
str_devnames lstr_devNames
String ls_printer, ls_driver, ls_port, ls_char
Blob lbl_buffer
ulong ll_mem, ll_length

// 初始化结构参数
lstr_Dlg.lStructSize = PRINTDLG_LEN
lstr_Dlg.hwndOwner = hwnd
lstr_Dlg.hDevMode = 0
lstr_Dlg.hDevNames = 0
lstr_Dlg.flags = PD_PRINTTOFILE + PD_NOPAGENUMS + PD_NOSELECTION + PD_USEDEVMODECOPIESANDCOLLATE
lstr_Dlg.hInstance = 0
lstr_Dlg.lCustData = 0
lstr_Dlg.lpfnPrintHook = 0
lstr_Dlg.lpfnSetupHook = 0
lstr_Dlg.lpPrintTemplateName = 0
lstr_Dlg.lpSetupTemplateName = 0
lstr_Dlg.hPrintTemplate = 0
lstr_Dlg.hSetupTemplate = 0

If PrintDlg ( lstr_Dlg ) <> 0 Then
// 取devmode
ll_mem = GlobalLock ( lstr_Dlg.hDevMode )
CopyMem ( lstr_devmode, ll_mem, DEVMODE_LEN )
GlobalUnlock ( lstr_Dlg.hDevMode )

// 取devnames
// 这个结构记录用户设置的打印机
// 取长度
ll_Length = GlobalSize ( lstr_Dlg.hDevNames )

ll_mem = GlobalLock ( lstr_Dlg.hDevNames )
CopyMem ( lstr_devNames, ll_mem, DEVNAMES_LEN )

// 分配空间
lbl_buffer = Blob ( Space ( ll_length ) )
CopyMem ( lbl_buffer, ll_mem, ll_length )
GlobalUnlock ( lstr_Dlg.hDevNames )

// 打印机名称
ls_printer = of_GetStringFromBlob ( lbl_buffer, lstr_devNames.wDeviceOffset + 1 )
// 驱动程序,可能是以前的16位系统留下的东西吧?
ls_driver = of_GetStringFromBlob ( lbl_buffer, lstr_devNames.wDriverOffset + 1 )
// 打印端口
ls_port = of_GetStringFromBlob ( lbl_buffer, lstr_devNames.wOutputOffset + 1 )

// 更新打印机属性
of_ModifyPrinter ( ls_printer, lstr_devMode )
// 将打印机设置为缺省打印机
of_SetDefault ( ls_printer )
Else
Return -1
End If

Return 1
end function

public function integer of_setpapersize (string printername, integer papersize, integer paperwidth, integer paperheight);any la_any
Return of_Printer( printername, 102, la_any, papersize,paperwidth,paperheight )
end function

public function integer of_pagesetup (unsignedlong hwnd, ref long left, ref long right, ref long top, ref long bottom);// 打印机页面设置
// 返回1:用户选择确定
// 左右上下边界放在left, right,top,bottom中返回
str_PageSetupDlg lstr_Dlg
str_devmode lstr_devmode
str_devnames lstr_devNames
String ls_printer, ls_driver, ls_port, ls_char
Blob lbl_buffer
ulong ll_mem, ll_length

// 初始化结构参数
lstr_Dlg.lStructSize = PAGESETUPDLG_LEN
lstr_Dlg.hwndOwner = hwnd
lstr_Dlg.hDevMode = 0
lstr_Dlg.hDevNames = 0
lstr_Dlg.flags = PSD_DEFAULTMINMARGINS + PSD_INHUNDREDTHSOFMILLIMETERS
lstr_Dlg.hInstance = 0
lstr_Dlg.lCustData = 0

If PageSetupDlg ( lstr_Dlg ) Then
// 取devmode
ll_mem = GlobalLock ( lstr_Dlg.hDevMode )
CopyMem ( lstr_devmode, ll_mem, DEVMODE_LEN )
GlobalUnlock ( lstr_Dlg.hDevMode )

// 取devnames
// 这个结构记录用户设置的打印机
// 取长度
ll_Length = GlobalSize ( lstr_Dlg.hDevNames )

ll_mem = GlobalLock ( lstr_Dlg.hDevNames )
CopyMem ( lstr_devNames, ll_mem, DEVNAMES_LEN )

// 分配空间
lbl_buffer = Blob ( Space ( ll_length ) )
CopyMem ( lbl_buffer, ll_mem, ll_length )
GlobalUnlock ( lstr_Dlg.hDevNames )

// 打印机名称
ls_printer = of_GetStringFromBlob ( lbl_buffer, lstr_devNames.wDeviceOffset + 1 )
// 驱动程序,可能是以前的16位系统留下的东西吧?
ls_driver = of_GetStringFromBlob ( lbl_buffer, lstr_devNames.wDriverOffset + 1 )
// 打印端口
ls_port = of_GetStringFromBlob ( lbl_buffer, lstr_devNames.wOutputOffset + 1 )

// 更新打印机属性
of_ModifyPrinter ( ls_printer, lstr_devMode )
// 将打印机设置为缺省打印机
of_SetDefault ( ls_printer )

Left = lstr_Dlg.rtMargin.Left
Right = lstr_Dlg.rtMargin.Right
Top = lstr_Dlg.rtMargin.Top
Bottom = lstr_Dlg.rtMargin.Bottom
Else
Return -1
End If

Return 1
end function

public function integer of_setdwpapersize (readonly datawindow adw_dw, integer ai_w, integer ai_h);string ls_printer
ls_printer=adw_dw.object.datawindow.printer
ls_printer=trim(left(ls_printer,pos(ls_printer," on ")))
if of_SetPaperSize ( ls_printer, 256, ai_w, ai_h )<>1 then return -1
return of_SetDefault ( ls_printer )



end function
public function integer of_setdspapersize (readonly datastore adw_dw, integer ai_w, integer ai_h);string ls_printer
ls_printer=adw_dw.object.datawindow.printer
ls_printer=trim(left(ls_printer,pos(ls_printer," on ")))
If of_SetPaperSize ( ls_printer, 256, ai_w, ai_h ) = 1 Then return 1
return -1
end function

on n_printer.create
TriggerEvent( this, "constructor" )
end on

on n_printer.destroy
TriggerEvent( this, "destructor" )
end on

~~
programbcb 2002-01-11
  • 打赏
  • 举报
回复
是啊,这是一个难点,我是这样去解决的,就是安装N台打印机,然后每台打印机设置它的默认张纸,然后用程序动态修改默认打印机的方法。
ideage 2002-01-11
  • 打赏
  • 举报
回复
到陶清网站下个函数集,有自定义纸张的函数。
如果你找不到,留个Mail!
liaoshangfa 2002-01-11
  • 打赏
  • 举报
回复
大家过来帮帮忙吧
liaoshangfa 2002-01-11
  • 打赏
  • 举报
回复
流方:

401

社区成员

发帖
与我相关
我的任务
社区描述
PowerBuilder 非技术版
社区管理员
  • 非技术版社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧