请教高手一个打印的问题?

yu_gh 2004-01-09 02:44:28
小弟现在要把在DataGrid表中的信息在页面直接打印出来,小弟不知道怎么实现希望高手指教,谢谢!
...全文
139 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhuangcrui 2004-01-12
  • 打赏
  • 举报
回复
Option Strict On
Imports System.Drawing.Printing

Public Class frmMain
Inherits System.Windows.Forms.Form
Private WithEvents pdoc As New PrintDocument()
Private Sub btnPrintDialog_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrintDialog.Click
Dim dialog As New PrintDialog()
dialog.Document = pdoc

If dialog.ShowDialog = DialogResult.OK Then
pdoc.Print()
End If
End Sub
Private Sub btnPrintPreview_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrintPreview.Click
Dim ppd As New PrintPreviewDialog()
Try
ppd.Document = pdoc
ppd.ShowDialog()
Catch exp As Exception
MessageBox.Show("An error occurred while trying to load the " & _
"document for Print Preview. Make sure you currently have " & _
"access to a printer. A printer must be connected and " & _
"accessible for Print Preview to work.", Me.Text, _
MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
Private Sub btnPageSetup_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPageSetup.Click
Dim psd As New PageSetupDialog()
With psd
.Document = pdoc
.PageSettings = pdoc.DefaultPageSettings
End With

If psd.ShowDialog = DialogResult.OK Then
pdoc.DefaultPageSettings = psd.PageSettings
End If
End Sub
Private Sub pdoc_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles pdoc.PrintPage
Static intCurrentChar As Int32
Dim intPrintAreaHeight, intPrintAreaWidth, marginLeft, marginTop As Int32
With pdoc.DefaultPageSettings
intPrintAreaHeight = .PaperSize.Height - .Margins.Top - .Margins.Bottom
intPrintAreaWidth = .PaperSize.Width - .Margins.Left - .Margins.Right
marginLeft = .Margins.Left ' X coordinate
marginTop = .Margins.Top ' Y coordinate
End With
If pdoc.DefaultPageSettings.Landscape Then
Dim intTemp As Int32
intTemp = intPrintAreaHeight
intPrintAreaHeight = intPrintAreaWidth
intPrintAreaWidth = intTemp
End If
Dim intLineCount As Int32 = CInt(intPrintAreaHeight / font.Height)
Dim rectPrintingArea As New RectangleF(marginLeft, marginTop, intPrintAreaWidth, intPrintAreaHeight)
Dim fmt As New StringFormat(StringFormatFlags.LineLimit)
打印出东西

' static.
Dim intLinesFilled, intCharsFitted As Int32
e.Graphics.MeasureString(Mid(txtDocument.Text, intCurrentChar + 1), font, _
New SizeF(intPrintAreaWidth, intPrintAreaHeight), fmt, _
intCharsFitted, intLinesFilled)

' Print the text to the page.
e.Graphics.DrawString(Mid(txtDocument.Text, intCurrentChar + 1), font, _
Brushes.Black, rectPrintingArea, fmt)
intCurrentChar += intCharsFitted
If intCurrentChar < txtDocument.Text.Length Then
e.HasMorePages = True
Else
e.HasMorePages = False
' You must explicitly reset intCurrentChar as it is static.
intCurrentChar = 0
End If
End Sub
tinghuyang 2004-01-11
  • 打赏
  • 举报
回复
给我也发一份!我的email:desnoc@yahoo.com.cn
yu_gh 2004-01-09
  • 打赏
  • 举报
回复
是直接打印出来,我的Mail:ygh0998@sina.com.cn谢谢
zhuangcrui 2004-01-09
  • 打赏
  • 举报
回复
是不是直接打印出来,留个地址给你样例代码
weixinzhu 2004-01-09
  • 打赏
  • 举报
回复
可以直接打印FORM,是不是你的意思?如下:
Imports System.Drawing.Printing

' Specifies what happens when the PrintPage event is raised.
Private Sub pd_PrintPage(ByVal sender As Object, ByVal ev As PrintPageEventArgs)
Dim obj As Bitmap
Dim iData As IDataObject = Clipboard.GetDataObject()
' Determines whether the data is in a format you can use.
If iData.GetDataPresent(DataFormats.Bitmap) Then
obj = iData.GetData(DataFormats.Bitmap)
ev.Graphics.DrawImage(obj, _
obj.GetBounds(System.Drawing.GraphicsUnit.Pixel)) 'ev.Graphics.VisibleClipBounds)
End If
ev.HasMorePages = False
End Sub

Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Try
'Printscreen
SendKeys.SendWait("%{PRTSC}")
' Assumes the default printer.
Dim pd As New PrintDocument()
AddHandler pd.PrintPage, AddressOf Me.pd_PrintPage
pd.DefaultPageSettings.Landscape = True
pd.Print()
Catch ex As Exception
MessageBox.Show("An error occurred while printing" & vbCrLf & _
ex.ToString(), "Error")
End Try
End Sub
LVOLCANO 2004-01-09
  • 打赏
  • 举报
回复
GZ
dzliang21 2004-01-09
  • 打赏
  • 举报
回复
http://www.c-sharpcorner.com/Graphics/DataGridPrinterMG.asp
szch 2004-01-09
  • 打赏
  • 举报
回复
参看drawing类
dongbeiren 2004-01-09
  • 打赏
  • 举报
回复
为什么不用报表?

16,555

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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