如何得到打印机实际可打印区域范围
(2008-08-08 16:35:13)
标签:
vb打印机打印区域教育 |
分类: VisualBasic |
功能如下:
1.得到打印机实际可打印范围的X,Y向起始空白大小(Physical Printable Area x/y margin),单位是打印机单元(device units)。
GetDeviceCaps(Printer.hdc, PHYSICALOFFSETX)
GetDeviceCaps(Printer.hdc, PHYSICALOFFSETY)
2.得到打印机实际可打印的宽度和高度值(Physical Printable Area x/y margin),单位是打印机单元(device units)。
GetDeviceCaps(Printer.hdc, PHYSICALWIDTH)
GetDeviceCaps(Printer.hdc, PHYSICALHEIGHT)
因为不同打印机的打印精度是不同的,因此对device units就没有一个固定的转换比例。但是我们可以利用上面得到的两个结果求出一个比例来确定实际可打印范围。示范源码如下:
Option Explicit
Private Declare Function GetDeviceCaps Lib "gdi32" (ByVal hdc As Long, ByVal nIndex As Long) As Long
'不同打印机的打印单元精度不同
Private Const PHYSICALWIDTH = 110 'Physical Width in device units
Private Const PHYSICALHEIGHT = 111 'Physical Height in device units
Private Const PHYSICALOFFSETX = 112 'Physical Printable Area x margin
Private Const PHYSICALOFFSETY = 113 'Physical Printable Area y margin
Private Const SCALINGFACTORX = 114 'Scaling factor x
Private Const SCALINGFACTORY = 115 'Scaling factor y
Private Const DRIVERVERSION = 0 'Device driver version
Private Sub Form_Load()
Dim dblW As Double, dblH As Double
With Me.Picture1
.Appearance = 0
.BorderStyle = 0
.AutoRedraw = True
End With
'不可打印部分所占纸张的宽度和高度比例。
dblW = GetDeviceCaps(Printer.hdc, PHYSICALOFFSETX) / GetDeviceCaps(Printer.hdc, PHYSICALWIDTH)
dblH = GetDeviceCaps(Printer.hdc, PHYSICALOFFSETY) / GetDeviceCaps(Printer.hdc, PHYSICALHEIGHT)
'画出可打印区域
With Printer
Picture1.Line (.ScaleLeft + .ScaleWidth * dblW, .ScaleTop + .ScaleHeight * dblH)-(.ScaleLeft + .ScaleWidth - .ScaleWidth * dblW, .ScaleTop + .ScaleHeight - .ScaleHeight * dblH), vbBlue, B
End With
End Sub