如何在打印时放大表格。 [英] How to scale up a form on printing.
问题描述
Printform只打印屏幕上可以看到的内容。
我需要在A4上打印我的表格。
如果我缩放屏幕上的表格,因此在分辨率较低的机器上使用时会被截断。
因此我认为答案可能是缩放表格以适应最低的屏幕分辨率然后打印到适合A4使用自动调整或缩放以适合。
可以这样做,如果是,怎么做?
< blockquote>
Hi Dave the Golfer,
感谢您在此发帖。
根据您的描述,您希望缩放表单以适应最低的屏幕分辨率然后使用
打印以适合A4,例如自动调整或缩放以适合。
也就是说,它必须自适应预览窗口大小。所以,你的目标分为两部分。
1.打印
公共类Form1
Private Sub preview_Click(sender As Object,e As EventArgs)处理预览。点击
PrintPreviewDialog1.Document = PrintDocument1
lineReader = New StringReader(TextBox1.Text)
试试
PrintPreviewDialog1.ShowDialog()
Catch ee As Exception
MessageBox.Show(ee.Message," print error",MessageBoxButtons.OK,MessageBoxIcon。[Error])
End Try
End Sub
Dim asc As New AutoSizeFormClass()
Private Sub Form1_Load(sender As Object,e As EventArgs)Handles MyBase.Load
asc.controllInitializeSize(Me)
AddHandler PrintDocument1.PrintPage,AddressOf PrintDocument1_PrintPage
Me.PrintDialog1.UseEXDialog = True
Me.PrintPreviewDialog1.AutoScrollMargin = New System.Drawing.Size(0,0)
Me.PrintPreviewDialog1.AutoScrollMinSize = New System.Drawing.Size(0,0)
Me.PrintPreviewDialog1.ClientSize = New System.Drawing.Size(400,300)
Me.PrintPreviewDialog1.Enabled = True
Me.PrintPreviewDialog1.Name =" printPreviewDialog1"
Me.PrintPreviewDialog1.Visible = False
End Sub
Private Sub Form1_SizeChanged(sender As Object,e As EventArgs)
asc.controlAutoSize(Me)
End Sub
Dim lineReader As StringReader = Nothing
Private Sub PrintDocument1_PrintPage(sender As Object,e As Printing.PrintPageEventArgs)Handles PrintDocument1.PrintPage
Dim g As Graphics = e.Graphics
Dim yposition As Single = 0
Dim count As Integer = 0
Dim lefmargin As Single = e.MarginBounds.Left
Dim topmargin As Single = e。 MarginBounds.Top
Dim line As String =""
Dim printFont As New Font("",10.5F,FontStyle.Bold,GraphicsUnit.Point,CByte(134))
Dim mybrush As New SolidBrush(Color.Black)
Dim linesperpage As Single = e.MarginBounds.Height / printFont.GetHeight(g)
line = lineReader.ReadLine()
while count< linesperpage AndAlso String.IsNullOrEmpty(line)= False
yposition = topmargin +(count * printFont.GetHeight(g))
g.DrawString(line,printFont,mybrush,lefmargin,yposition,New StringFormat() )
count = count + 1
line = lineReader.ReadLine()
End while
如果line IsNot Nothing则
e.HasMorePages = True
Else
e.HasMorePages = False
End if
lineReader = New StringReader(TextBox1.Text)
End Sub
Private Sub pagesetup_Click(sender As Object, e作为EventArgs)处理pagesetup.Click
PageSetupDialog1.Document = PrintDocument1
PageSetupDialog1.ShowDialog()
End Sub
End Class
2. Windows自适应。&NBSP;&NBSP;&NBSP;
公共类AutoSizeFormClass
公共结构controlRect
Public Left As Integer
Public Top As Integer
Public Width As Integer
Public Height As Integer
End Structure
Public oldCtrl As New List(Of controlRect)()
Private ctrlNo As Integer = 0
Public Sub controllInitializeSize(mForm As Control)
Dim cR As controlRect
cR.Left = mForm.Left
cR.Top = mForm.Top
cR.Width = mForm.Width
cR.Height = mForm.Height
oldCtrl.Add(cR)
AddControl(mForm)
End Sub
Private Sub AddControl(ctl As Control )
For Each c As Control in ctl.Controls
Dim objCtrl As controlRect
objCtrl.Left = c.Left
objCtrl.Top = c.Top
objCtrl。宽度= c.Width
objCtrl.Height = c.Height
oldCtrl.Add(objCtrl)
如果c.Controls.Count> 0然后
AddControl(c)
结束如果
下一个
结束Sub
Public Sub controlAutoSize(mForm As Control)
如果ctrlNo = 0则
Dim cR As controlRect
cR.Left = 0
cR.Top = 0
cR.Width = mForm.PreferredSize.Width
cR.Height = mForm.PreferredSize.Height
oldCtrl.Add(cR)
AddControl(mForm)
结束如果
Dim wScale As Single = CSng(mForm.Width)/ CSng(oldCtrl(0).Width)
Dim hScale As Single = CSng(mForm.Height)/ CSng(oldCtrl(0).Height)
ctrlNo = 1
AutoScaleControl(mForm,wScale,hScale)
End Sub
Private Sub AutoScaleControl(ctl As Control,wScale As Single,hScale As Single)
Dim ctrLeft0 As Integer,ctrTop0 As Integer,ctrWidth0 As Integer,ctrHeight0 As Integer
For each c as Control in ctl .Controls
ctrLe ft0 = oldCtrl(ctrlNo).Left
ctrTop0 = oldCtrl(ctrlNo).Top
ctrWidth0 = oldCtrl(ctrlNo).Width
ctrHeight0 = oldCtrl(ctrlNo).Height
c。左= CInt((ctrLeft0)* wScale)
c.Top = CInt((ctrTop0)* hScale)
c.Width = CInt(ctrWidth0 * wScale)
c.Height = CInt( ctrHeight0 * hScale)
ctrlNo + = 1
如果c.Controls.Count> 0然后
AutoScaleControl(c,wScale,hScale)
结束如果
如果TypeOf ctl是DataGridView那么
Dim dgv As DataGridView = TryCast(ctl,DataGridView)
Cursor .Current = Cursors.WaitCursor
Dim widths As Integer = 0
For i As Integer = 0 to dgv.Columns.Count - 1
dgv.AutoResizeColumn(i,DataGridViewAutoSizeColumnMode.AllCells)
widths + = dgv.Columns(i).Width
Next
如果widths> = ctl.Size.Width那么
dgv.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.DisplayedCells
Else
dgv.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill
End if
Cursor.Current = Cursors。[Default]
End if
Next
End Sub
结束等级
最好的问候,
Wendy
Printform only prints what is viewable on the screen.
I need my forms to be printed on A4.
If I scale the forms on the screen accordingly they get truncated when used on machines with lower resolution.
Therefore I thought the answer maybe to scale the forms to fit the lowest screen resolution and then print to fit A4 using something like autofit or scale to fit.
Can this be done and if so, how?
Hi Dave the Golfer,
Thank you for posting here.
According to your description, you’d like to scale the forms to fit the lowest screen resolution and then print to fit A4 using something like auto fit or scale to fit.
That is to say, it have to self-adaptation preview windows size. So, you goal is divide into two part.
1. Printing
Public Class Form1 Private Sub preview_Click(sender As Object, e As EventArgs) Handles preview.Click PrintPreviewDialog1.Document = PrintDocument1 lineReader = New StringReader(TextBox1.Text) Try PrintPreviewDialog1.ShowDialog() Catch ee As Exception MessageBox.Show(ee.Message, "print error", MessageBoxButtons.OK, MessageBoxIcon.[Error]) End Try End Sub Dim asc As New AutoSizeFormClass() Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load asc.controllInitializeSize(Me) AddHandler PrintDocument1.PrintPage, AddressOf PrintDocument1_PrintPage Me.PrintDialog1.UseEXDialog = True Me.PrintPreviewDialog1.AutoScrollMargin = New System.Drawing.Size(0, 0) Me.PrintPreviewDialog1.AutoScrollMinSize = New System.Drawing.Size(0, 0) Me.PrintPreviewDialog1.ClientSize = New System.Drawing.Size(400, 300) Me.PrintPreviewDialog1.Enabled = True Me.PrintPreviewDialog1.Name = "printPreviewDialog1" Me.PrintPreviewDialog1.Visible = False End Sub Private Sub Form1_SizeChanged(sender As Object, e As EventArgs) asc.controlAutoSize(Me) End Sub Dim lineReader As StringReader = Nothing Private Sub PrintDocument1_PrintPage(sender As Object, e As Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage Dim g As Graphics = e.Graphics Dim yposition As Single = 0 Dim count As Integer = 0 Dim lefmargin As Single = e.MarginBounds.Left Dim topmargin As Single = e.MarginBounds.Top Dim line As String = "" Dim printFont As New Font("", 10.5F, FontStyle.Bold, GraphicsUnit.Point, CByte(134)) Dim mybrush As New SolidBrush(Color.Black) Dim linesperpage As Single = e.MarginBounds.Height / printFont.GetHeight(g) line = lineReader.ReadLine() While count < linesperpage AndAlso String.IsNullOrEmpty(line) = False yposition = topmargin + (count * printFont.GetHeight(g)) g.DrawString(line, printFont, mybrush, lefmargin, yposition, New StringFormat()) count = count + 1 line = lineReader.ReadLine() End While If line IsNot Nothing Then e.HasMorePages = True Else e.HasMorePages = False End If lineReader = New StringReader(TextBox1.Text) End Sub Private Sub pagesetup_Click(sender As Object, e As EventArgs) Handles pagesetup.Click PageSetupDialog1.Document = PrintDocument1 PageSetupDialog1.ShowDialog() End Sub End Class2.Windows self-adaptation.
Public Class AutoSizeFormClass Public Structure controlRect Public Left As Integer Public Top As Integer Public Width As Integer Public Height As Integer End Structure Public oldCtrl As New List(Of controlRect)() Private ctrlNo As Integer = 0 Public Sub controllInitializeSize(mForm As Control) Dim cR As controlRect cR.Left = mForm.Left cR.Top = mForm.Top cR.Width = mForm.Width cR.Height = mForm.Height oldCtrl.Add(cR) AddControl(mForm) End Sub Private Sub AddControl(ctl As Control) For Each c As Control In ctl.Controls Dim objCtrl As controlRect objCtrl.Left = c.Left objCtrl.Top = c.Top objCtrl.Width = c.Width objCtrl.Height = c.Height oldCtrl.Add(objCtrl) If c.Controls.Count > 0 Then AddControl(c) End If Next End Sub Public Sub controlAutoSize(mForm As Control) If ctrlNo = 0 Then Dim cR As controlRect cR.Left = 0 cR.Top = 0 cR.Width = mForm.PreferredSize.Width cR.Height = mForm.PreferredSize.Height oldCtrl.Add(cR) AddControl(mForm) End If Dim wScale As Single = CSng(mForm.Width) / CSng(oldCtrl(0).Width) Dim hScale As Single = CSng(mForm.Height) / CSng(oldCtrl(0).Height) ctrlNo = 1 AutoScaleControl(mForm, wScale, hScale) End Sub Private Sub AutoScaleControl(ctl As Control, wScale As Single, hScale As Single) Dim ctrLeft0 As Integer, ctrTop0 As Integer, ctrWidth0 As Integer, ctrHeight0 As Integer For Each c As Control In ctl.Controls ctrLeft0 = oldCtrl(ctrlNo).Left ctrTop0 = oldCtrl(ctrlNo).Top ctrWidth0 = oldCtrl(ctrlNo).Width ctrHeight0 = oldCtrl(ctrlNo).Height c.Left = CInt((ctrLeft0) * wScale) c.Top = CInt((ctrTop0) * hScale) c.Width = CInt(ctrWidth0 * wScale) c.Height = CInt(ctrHeight0 * hScale) ctrlNo += 1 If c.Controls.Count > 0 Then AutoScaleControl(c, wScale, hScale) End If If TypeOf ctl Is DataGridView Then Dim dgv As DataGridView = TryCast(ctl, DataGridView) Cursor.Current = Cursors.WaitCursor Dim widths As Integer = 0 For i As Integer = 0 To dgv.Columns.Count - 1 dgv.AutoResizeColumn(i, DataGridViewAutoSizeColumnMode.AllCells) widths += dgv.Columns(i).Width Next If widths >= ctl.Size.Width Then dgv.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.DisplayedCells Else dgv.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill End If Cursor.Current = Cursors.[Default] End If Next End Sub End Class
Best Regards,
Wendy
这篇关于如何在打印时放大表格。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文