怎么打印Form1?
.NET 2003里怎么打印Form1? 问题点数:10、回复次数:10Top
1 楼chinahth(淡若浮云)回复于 2006-03-05 22:43:13 得分 0
Dim line As System.IO.StringReader
Private frm_print As Form2
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub printdoc_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles printdoc.PrintPage
Dim g As Graphics
Dim linesperpage As Long
Dim current As Long
Dim y As Integer
Dim left As Integer
Dim top As Integer
Dim stroutput As String
Dim printfont As System.Drawing.Font
Dim brush As New System.Drawing.SolidBrush(Color.Black)
Dim activechild As Form = Me.ActiveMdiChild
Dim thebox As RichTextBox
Dim redPen As New Pen(Color.Red, 1)
activechild = Me.ActiveMdiChild
thebox = CType(activechild.ActiveControl, RichTextBox)
printfont = thebox.Font
g = e.Graphics
' left = e.MarginBounds.Left
'top = e.MarginBounds.Top
left = 200
top = 500
linesperpage = e.MarginBounds.Height / printfont.GetHeight(g) - 4
y = top
g.DrawString(activechild.Text, printfont, brush, left, y)
g.DrawString("-", printfont, brush, left, y + printfont.GetHeight(g))
g.DrawLine(redPen, left, y + printfont.GetHeight(g), e.MarginBounds.Right, y + printfont.GetHeight(g))
y = top + 2 * printfont.GetHeight(g)
While current < linesperpage
stroutput = line.ReadLine
If Not stroutput Is Nothing Then
y = y + printfont.GetHeight(g)
g.DrawString(stroutput, printfont, brush, left, y)
g.DrawLine(redPen, left, y + printfont.GetHeight(g), e.MarginBounds.Right, y + printfont.GetHeight(g))
Else
Exit While
End If
End While
y = e.MarginBounds.Bottom - printfont.GetHeight(g)
g.DrawString("footer", printfont, brush, e.MarginBounds.Width / 2, y)
If Not stroutput Is Nothing Then
e.HasMorePages = True
Else
e.HasMorePages = False
End If
End Sub
Private Sub MenuItem2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem2.Click
If frm_print Is Nothing OrElse frm_print.IsDisposed Then
frm_print = New Form2
End If
frm_print.MdiParent = Me
frm_print.Activate()
frm_print.Show()
End Sub
Private Sub munprint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles munprint.Click
Dim activechild As Form = Me.ActiveMdiChild
activechild = Me.ActiveMdiChild
If Not activechild Is Nothing Then
Dim printersetup As New PrintDialog
printersetup.Document = printdoc
If printersetup.ShowDialog = DialogResult.OK Then
Dim thebox As RichTextBox
thebox = CType(activechild.ActiveControl, RichTextBox)
line = New System.IO.StringReader(thebox.Text)
Try
printdoc.Print()
Catch ex As Exception
MsgBox(ex.Message)
printdoc.PrintController.OnEndPrint(printdoc, e)
End Try
End If
End If
End Sub
Private Sub munpreview_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles munpreview.Click
Dim activechild As Form = Me.ActiveMdiChild
activechild = Me.ActiveMdiChild
If Not activechild Is Nothing Then
Dim preview As New PrintPreviewDialog
preview.Document = printdoc
Dim thebox As RichTextBox
thebox = CType(activechild.ActiveControl, RichTextBox)
line = New System.IO.StringReader(thebox.Text)
Try
preview.StartPosition = FormStartPosition.CenterScreen
preview.ShowDialog()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End If
End Sub
Private Sub munpagesetup_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles munpagesetup.Click
'页面设置
Dim pagesetup As New PageSetupDialog
pagesetup.Document = printdoc
pagesetup.ShowDialog()
End Sub
Private Sub munprintersetup_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles munprintersetup.Click
'打印设置
Dim printersetup As New PrintDialog
printersetup.Document = printdoc
printersetup.ShowDialog()
End SubTop
2 楼chaoming(chao)回复于 2006-03-06 08:30:18 得分 0
楼上,将原理讲一下好吗?Top
3 楼sz_lgp(longguoping)回复于 2006-03-06 11:14:25 得分 0
此事重要,关注!!!!Top
4 楼intern9901()回复于 2006-03-06 12:49:47 得分 0
chinahth(淡若浮云) 提供的代码不是打印Form2,而是打印Form2中的RichBox中的内容,另外字定义了一些打印格式。Top
5 楼alan001(星岛孤裔林郎君)回复于 2006-03-07 07:33:00 得分 0
真的没有高手能解决???Top
6 楼wuyazhe(wyz&xyl)回复于 2006-03-07 08:39:53 得分 0
Mark
没接触过。Top
7 楼intern9901()回复于 2006-03-07 09:27:40 得分 0
其实chinahth(淡若浮云) 的方法是比较好的方法。
但如果你非要拷贝屏幕,类似于VB6中的printform,在.NET中你必须使用第三方的屏幕捕获工具,或者是使用API,如BitBlt。
代码其实很简单的,只要你会用BitBlt。你先试试吧。Top
8 楼lye2000000_super(我自横刀香甜笑)回复于 2006-03-07 09:50:56 得分 0
打印?没明白?在哪里打印?Top
9 楼TSD(智之选,商欲达--智商购物系统zhishop.com)回复于 2006-03-07 10:25:31 得分 0
MARKTop
10 楼sx_lxh(路漫漫)回复于 2006-03-07 14:08:00 得分 10
net实现把窗体的界面打印出来
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
End Class
Top




