本文介绍了如何缩放表格以使其适合A4纸的尺寸进行打印的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 在我的应用程序中,我有许多需要打印的窗口形式。 最终用户都有不同尺寸的屏幕,最低的是1366 x 768. 我尝试使用PrintForm,但程序只打印屏幕上的内容,因此表格必须很小,因此只能填写A4纸的一部分。 因此我希望能够捕获表格和将它扩展到A4以进行打印。 我从互联网上复制了一些代码并将其修改以供我使用,但它似乎没有按比例放大表格,尽管它会打印它。 有任何想法我怎么做。 请注意我是一个完全的业余爱好者。为高尔夫俱乐部的老年人部门制作软件。 我的尝试: 使用的代码如下 Public Class TeamSelect12 '定义你的打印对象私人PrintDoc1 As PrintDocument = New PrintDocument Private PrintPreviewDialog1 As PrintPreviewDialog = New PrintPreviewDialog Private PageSetupDialog1 As PageSetupDialog = New PageSetupDialog '打印按钮代码 Private Sub PrtTSBut_Click( sender As System.Object,e As System.EventArgs)Handles PrtTSBut.Click PDFTeamBut.Visible = False'隐藏按钮 PrtTSBut.Visible = False'隐藏按钮 Me。刷新() 使用PageSetupDialog1 '指定文档使用 .Document = PrintDoc1 '启用打印机按钮 .AllowPrinter = True .EnableM etric = True '初始化对话框的PrinterSettings属性以保存用户'定义的页面设置。 .PageSettings = New System.Drawing.Printing.PageSettings 'Initialize对话框的PrinterSettings属性,用于保存用户'设置打印机设置。 .PrinterSettings = New System.Drawing.Printing.PrinterSettings End with PrintPreviewDialog1.Document = PrintDoc1 AddHandler PrintDoc1.PrintPage,AddressOf PDoc_PrintPage Dim保证金作为新保证金(20,20,20,40) PrintDoc1.DefaultPageSettings.Margins =保证金 PrintPreviewDialog1.ShowDialog() '替换表格 PrtTSBut.Visible = True PDFTeamBut.Visible = True '隐藏形式 Me.Hide() InputForm.Show()'返回输入形式结束Sub '打印处理程序和图形打印方法缩放 Private Sub PDoc_PrintPage(sender As Object,e As PrintPageEventArgs) PrintToGraphics(e.Graphics,e.MarginBounds) End Sub Public Sub PrintToGraphics(图形为图形,边界为矩形)'将控件的视图打印到图形ics对象。 '< param name =graphics>要绘制的图形对象。< / param> '< param name =bounds>要打印的矩形。< / param> '将控件和内容绘制到位图 Dim Bitmap As Bitmap = New Bitmap(Me.Width,Me.Height) Me.DrawToBitmap(Bitmap,New Rectangle( 0,0,Bitmap.Width,Bitmap.Height)) '将打印限制分配给目标矩形 Dim PrtWidth,PrtHeight,PrtLeft,PrtTop As Integer PrtWidth = bounds。宽度 PrtHeight = bounds.Height PrtLeft = bounds.Left PrtTop = bounds.Top Dim target As Rectangle = New Rectangle(PrtLeft,PrtTop,PrtWidth,PrtHeight)'缩放位图以适应目标 Dim xScale As Double = Bitmap.Width / PrtWidth Dim yScale As Double = Bitmap.Height / PrtHeight 如果xScale< yScale然后 target.Width = Int(xScale * target.Width / yScale)否则 target.Height = Int(yScale * target.Height / xScale)结束如果'绘制位图 Graphics.PageUnit = GraphicsUnit.Display Graphics.DrawImage(位图,目标)结束子 解决方案 In my application I have a number of window forms that need to printed.The end users all have different size screens the lowest being 1366 x 768.I tried using PrintForm but the program only prints what is on the screen so the forms have to be small and therefore would only fill part of an A4 sheet.Therefore I want to be able to capture the form and scale it up to A4 for printing.I copied some code from the internet and modified it for my use but it does not seem to scale up the form although it does print it.Has any one any ideas how I can do this.Please be aware I am a total amateur at this. Producing the software for the Seniors Section of a golf club.What I have tried:The code am using is as followsPublic Class TeamSelect12 'Define your print object Private PrintDoc1 As PrintDocument = New PrintDocument Private PrintPreviewDialog1 As PrintPreviewDialog = New PrintPreviewDialog Private PageSetupDialog1 As PageSetupDialog = New PageSetupDialog 'Print Button Code Private Sub PrtTSBut_Click(sender As System.Object, e As System.EventArgs) Handles PrtTSBut.Click PDFTeamBut.Visible = False 'Hides button PrtTSBut.Visible = False 'Hides button Me.Refresh() With PageSetupDialog1 'Assign the document to use .Document = PrintDoc1 'Enable printer button .AllowPrinter = True .EnableMetric = True ' Initialize the dialog's PrinterSettings property to hold user ' defined page settings. .PageSettings = New System.Drawing.Printing.PageSettings ' Initialize dialog's PrinterSettings property to hold user ' set printer settings. .PrinterSettings = New System.Drawing.Printing.PrinterSettings End With PrintPreviewDialog1.Document = PrintDoc1 AddHandler PrintDoc1.PrintPage, AddressOf PDoc_PrintPage Dim margins As New Margins(20, 20, 20, 40) PrintDoc1.DefaultPageSettings.Margins = margins PrintPreviewDialog1.ShowDialog() 'Replace buttons back on form PrtTSBut.Visible = True PDFTeamBut.Visible = True 'Hide form Me.Hide() InputForm.Show() ' Return to input form End Sub 'Print Handler And Graphics print method With scaling Private Sub PDoc_PrintPage(sender As Object, e As PrintPageEventArgs) PrintToGraphics(e.Graphics, e.MarginBounds) End Sub Public Sub PrintToGraphics(Graphics As Graphics, bounds As Rectangle) 'Print the control's view to a Graphics object. '<param name="graphics">Graphics object to draw on.</param> '<param name="bounds">Rectangle to print in.</param> 'Draw the control and contents to a bitmap Dim Bitmap As Bitmap = New Bitmap(Me.Width, Me.Height) Me.DrawToBitmap(Bitmap, New Rectangle(0, 0, Bitmap.Width, Bitmap.Height)) 'Assign Print Bounds to target rectangle Dim PrtWidth, PrtHeight, PrtLeft, PrtTop As Integer PrtWidth = bounds.Width PrtHeight = bounds.Height PrtLeft = bounds.Left PrtTop = bounds.Top Dim target As Rectangle = New Rectangle(PrtLeft, PrtTop, PrtWidth, PrtHeight) 'Scale bitmap to fit target Dim xScale As Double = Bitmap.Width / PrtWidth Dim yScale As Double = Bitmap.Height / PrtHeight If xScale < yScale Then target.Width = Int(xScale * target.Width / yScale) Else target.Height = Int(yScale * target.Height / xScale) End If 'Draw the bitmap Graphics.PageUnit = GraphicsUnit.Display Graphics.DrawImage(Bitmap, target) End Sub 解决方案 这篇关于如何缩放表格以使其适合A4纸的尺寸进行打印的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
09-02 22:13