求助高手:在打印之前,如何直接获取打印对象Printer 中的内容,传送给PictureBox预览?
用Picture1替换打印对象并不适用于一些特殊情况,我需要的是直接捕捉 Printer 中的图片。
用Picture1替换打印对象并不适用于一些特殊情况,我需要的是直接捕捉 Printer 中的图片。
解决方案 »
- 谁有读取考勤机数据的代码,在线等待!
- 帮忙修改COMBO控件支持文字增量,急急急!
- 请问直接访问sql数据库,打包时还要加入什么组件,才能使在客户机上能访问到服务器上的数据库?
- 帮帮我呀,谁知道gsm modem 怎么开发发送中文短信!!!
- 请指教:如何选中datagrid中的一条纪录,双击触发一个on_click事件
- 请问在哪位有ActiveResizer 有解版!
- 枚举主窗口内子窗体的标题名
- 请问一个关于在vb中实现类似于ie中的后退的功能的解决方法,可高分相送!急!
- richtextbox添加文本图片的问题
- 谁会钩子?
- 如何在多窗口中使指定程序窗口轮流得到焦点?
- 如果通过窗体句柄获得窗体对象
兼容 Printer 对象,完全免费.
下载地址: http://d.download.csdn.net/down/1077508/rwj
不知道对你是否有用
Option Explicit
Private Type Rect
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Private Type CharRange
cpMin As Long ' First character of range (0 for start of doc)
cpMax As Long ' Last character of range (-1 for end of doc)
End Type
Private Type FormatRange
hdc As Long ' Actual DC to draw on
hdcTarget As Long ' Target DC for determining text formatting
rc As Rect ' Region of the DC to draw to (in twips)
rcPage As Rect ' Region of the entire DC (page size) (in twips)
chrg As CharRange ' Range of text to draw (see above declaration)
End Type
Private Const WM_USER As Long = &H400
Private Const EM_FORMATRANGE As Long = WM_USER + 57
Private Const EM_SETTARGETDEVICE As Long = WM_USER + 72
Private Const PHYSICALOFFSETX As Long = 112
Private Const PHYSICALOFFSETY As Long = 113
Private Declare Function GetDeviceCaps Lib "gdi32" (ByVal hdc As Long, ByVal nIndex As Long) As Long
Private Declare Function SendMessage Lib "USER32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal msg As Long, ByVal wp As Long, lp As Any) As Long
Private Declare Function CreateDC Lib "gdi32" Alias "CreateDCA" (ByVal lpDriverName As String, ByVal lpDeviceName As String, ByVal lpOutput As Long, ByVal lpInitData As Long) As Long Public Sub PrintRTF(RTF As RichTextBox, LeftMarginWidth As Long, _
TopMarginHeight, RightMarginWidth, BottomMarginHeight)
Dim LeftOffset As Long, TopOffset As Long
Dim LeftMargin As Long, TopMargin As Long
Dim RightMargin As Long, BottomMargin As Long
Dim fr As FormatRange
Dim rcDrawTo As Rect
Dim rcPage As Rect
Dim TextLength As Long
Dim NextCharPosition As Long
Dim r As Long
' Start a print job to get a valid Printer.hDC
Printer.Print Space(1)
Printer.ScaleMode = vbTwips
' Get the offsett to the printable area on the page in twips
LeftOffset = Printer.ScaleX(GetDeviceCaps(Printer.hdc, PHYSICALOFFSETX), vbPixels, vbTwips)
TopOffset = Printer.ScaleY(GetDeviceCaps(Printer.hdc, PHYSICALOFFSETY), vbPixels, vbTwips)
' Calculate the Left, Top, Right, and Bottom margins
LeftMargin = LeftMarginWidth - LeftOffset
TopMargin = TopMarginHeight - TopOffset
RightMargin = (Printer.Width - RightMarginWidth) - LeftOffset
BottomMargin = (Printer.Height - BottomMarginHeight) - TopOffset
' Set printable area rect
rcPage.Left = 0
rcPage.Top = 0
rcPage.Right = Printer.ScaleWidth
rcPage.Bottom = Printer.ScaleHeight
' Set rect in which to print (relative to printable area)
rcDrawTo.Left = LeftMargin
rcDrawTo.Top = TopMargin
rcDrawTo.Right = RightMargin
rcDrawTo.Bottom = BottomMargin
' Set up the print instructions
fr.hdc = Printer.hdc ' Use the same DC for measuring and rendering
fr.hdcTarget = Printer.hdc ' Point at printer hDC
fr.rc = rcDrawTo ' Indicate the area on page to draw to
fr.rcPage = rcPage ' Indicate entire size of page
fr.chrg.cpMin = 0 ' Indicate start of text through
fr.chrg.cpMax = -1 ' end of the text
' Get length of text in RTF
TextLength = Len(RTF.Text)
' Loop printing each page until done
Do
' Print the page by sending EM_FORMATRANGE message
NextCharPosition = SendMessage(RTF.hWnd, EM_FORMATRANGE, True, fr)
If NextCharPosition >= TextLength Then Exit Do 'If done then exit
fr.chrg.cpMin = NextCharPosition ' Starting position for next page
Printer.NewPage ' Move on to next page
Printer.Print Space(1) ' Re-initialize hDC
fr.hdc = Printer.hdc
fr.hdcTarget = Printer.hdc
Loop
' Commit the print job
Printer.EndDoc
' Allow the RTF to free up memory
r = SendMessage(RTF.hWnd, EM_FORMATRANGE, False, ByVal CLng(0))
End Sub '窗体中:
Private Sub Command1_Click()
' Print the contents of the RichTextBox with a one inch margin
PrintRTF RichTextBox1, 1440, 1440, 1440, 1440 ' 1440 Twips = 1 Inch
End Sub
用对象直接画PicutreBox,可以预览,预览好后,用对象再传给printer不就行了.给你个简单的例子(picture的放大缩小,自动重画的打开自动做吧):Option Explicit
Const UNIT_CM = 567 '-------------------------设置每厘米等于567个缇
Const TABLE_TOP = 800 '-----------------------表上边距
Const hs = 34 '-------------------------------表的总行数Private Sub Command1_Click()
drawTable Picture1, "高压电网远程监控参数" '送图片框
End SubPrivate Sub Command2_Click()
drawTable Printer, "高压电网远程监控参数" '送打印机
Printer.EndDoc '立即打印
End SubPrivate Sub drawTable(ByVal obj As Object, ByVal table_Header As String)
'*----------------------------------------------------------*
'| 入口参数: |
'| obj 发送的对象 |
'| table_Header 发送的表头 |
'| 出口参数: |
'| 无 |
'*----------------------------------------------------------*
If obj Is Picture1 Then
Picture1.Cls
End If
'*===================== 画报表 ========================* Dim iRows As Single '------------------------表行总高度
Dim iRow(0 To hs + 1) As Single '------------表的每行高
Dim iCols As Single '-----------------------表列总宽度
Dim iCol(0 To 14) As Single '---------------表的每列宽
Dim table_Col(0 To 11) As String '----------表头列变量
Dim iCol_centre As Single '------------------表头居中处理
Dim j As Byte
table_Col(0) = "电网电压(V)": table_Col(1) = "电网功率因数"
table_Col(2) = "单级电容容量(KVar)": table_Col(3) = "电容投切状态"
table_Col(4) = "投切延时时间(s)": table_Col(5) = "日 期 时 间"
table_Col(6) = " 站 点 名 称 ": table_Col(7) = "站点代码"
'-----初始化列宽变量
For j = 0 To 7
iCol(j) = UNIT_CM * 2.8
DoEvents
Next j
iCol(2) = UNIT_CM * 3.7: iCol(4) = UNIT_CM * 3.2 '特殊列处理
iCol(5) = UNIT_CM * 3.7: iCol(6) = UNIT_CM * 6 '特殊列处理
iCol(7) = UNIT_CM * 1.5
'-----统计总列宽
For j = 0 To 7
iCols = iCols + iCol(j)
DoEvents
Next j
'-----初始化行高变量
For j = 0 To hs
iRow(j) = UNIT_CM * 0.5
DoEvents
Next j
'-----统计总行高
iRows = TABLE_TOP
For j = 0 To hs
iRows = iRows + iRow(j)
DoEvents
Next j'-----决定表的大小---------------
If obj Is Picture1 Then
obj.ScaleMode = 1
obj.Width = 14991
obj.ScaleWidth = 16361
obj.Height = 9260
obj.ScaleHeight = 11366
ElseIf obj Is Printer Then
obj.PaperSize = vbPRPSA4
obj.Orientation = 2 '横向打印End If'---========画表的列开始========----
Dim table_Centre As Single
table_Centre = (obj.ScaleWidth - iCols) / 2
iCols = table_Centre
For j = 0 To 13
obj.Line (iCols, TABLE_TOP)-(iCols, iRows), QBColor(0)
iCols = iCols + iCol(j)
DoEvents
Next j'---========画表的行开始========---
iRows = TABLE_TOP
For j = 0 To hs + 1
obj.Line (table_Centre, iRows)-(iCols, iRows), QBColor(0)
iRows = iRows + iRow(j)
DoEvents
Next j
'---========打印表头标题=======-----
obj.FontName = "宋体"
obj.FontSize = 12
obj.FontBold = 1
obj.CurrentX = (obj.ScaleWidth - obj.TextWidth(table_Header)) / 2
obj.CurrentY = (TABLE_TOP - obj.TextHeight(table_Header)) / 2
obj.Print table_Header
obj.FontSize = 9
obj.FontBold = 0
'--------- 打印表头列 ------------
iCols = table_Centre
Dim Char_Y As Long
Char_Y = TABLE_TOP + (iRow(0) - obj.TextHeight(table_Col(0))) / 2 + 15
For j = 0 To 11
iCol_centre = (iCol(j) - obj.TextWidth(table_Col(j))) / 2 + 15
obj.CurrentX = iCols + iCol_centre
obj.CurrentY = Char_Y
obj.Print table_Col(j) '/*---------------------------prn*/
iCols = iCols + iCol(j)
DoEvents
Next j
'--------- 决定表在屏幕上的位置 -----------
If obj Is Picture1 Then
If obj.Width < Form1.Width Then
obj.Left = (Form1.Width - obj.Width) / 2
Else
obj.Left = 0
End If
If obj.Height < Form1.Height Then
obj.Top = (Form1.Height - obj.Height) / 2
Else
obj.Top = 0
End If
End If
End Sub