我正在开发一个VB.NET(windows窗体)项目。我有一个表格,我必须在其中打印内容。 [英] I am working on a VB.NET (windows form) project. I have a form and I have to print the contents in it.

查看:120
本文介绍了我正在开发一个VB.NET(windows窗体)项目。我有一个表格,我必须在其中打印内容。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个表单,其中包含标签,带有文本的文本框,带有图片的PictureBox,按钮,DateTimePicker和作为表单背景的图像。我想打印所有这些东西,即除了按钮和表格边框之外的表格。

我使用过PrintDocunt Control,PrintDialog Control和PrintForm Control。

PrintDocument Control打印一个空白文档,PrintForm Control打印一个带有按钮和表格边框的裁剪表格。结果按钮和表单边框出现在文档中并且图片框被裁剪,这是我不想要的。



注意:*我正在使用2017年的Visual Studio社区Access Database 2016.

* VB作为编程语言。

*要使用PrintForm控件我安装了Visual Studio PowerPacks 12.0。

*应用程序没有任何错误和/或例外。所以我没有发布完整的代码。

*唯一的问题是打印空白文档和裁剪文档。



我尝试过:



'此代码打印一份空白文件

I have created a Form which has Labels, TextBoxes with text, PictureBox with a picture, Buttons, DateTimePicker and a image as Form Background. I want to print all these things i.e. the form except buttons and Form Border.
I have used PrintDocunt Control, PrintDialog Control and PrintForm Control.
The PrintDocument Control prints a blank document and PrintForm Control prints a cropped form with buttons and form borders. As a result buttons and form borders are appeared in document and picturebox got cropped, which I don't want.

Note: *I am using Visual Studio Community 2017 with Access Database 2016.
*VB as programming language.
*To use PrintForm control I have installed Visual Studio PowerPacks 12.0.
*The application doesn't have any error and/or exceptions. So I didn't post the full code.
*The only problem is that Printing a blank document and a cropped document.

What I have tried:

'This Code Prints A blank Document

Private Sub Print_btn_Click(sender As Object, e As EventArgs) Handles Print_btn.Click
        PrintDialog1.Document = PrintDocument1
        PrintDialog1.PrinterSettings = PrintDocument1.PrinterSettings
        PrintDialog1.AllowSomePages = True

        If PrintDialog1.ShowDialog = DialogResult.OK Then
            PrintDocument1.PrinterSettings = PrintDialog1.PrinterSettings
            PrintDocument1.Print()
        End If
    End Sub



'此代码打印带有按钮和表格边框的裁剪文档


'This Code Prints A cropped document with buttons and Form Borders

Private Sub Print_btn_Click(sender As Object, e As EventArgs) Handles Print_btn.Click
        If PrintDialog1.ShowDialog = DialogResult.OK Then
            PrintDocument1.PrinterSettings = PrintDialog1.PrinterSettings
            PrintForm1.Print()
        End If
    End Sub

推荐答案

是的,它会 - 因为你没有为 PrintDocument.PrintPage Event(System.Drawing.Printing) [ ^ ]这是打印文档内容的实际工作发生了。



该链接包含一个基本示例。
Yes, it will - because you haven't set a handler for the PrintDocument.PrintPage Event (System.Drawing.Printing)[^] which is where the actual work of printing the document content takes place.

The link includes a basic example.


我从OriginalGriff的使用处理程序的想法得到了解决方案PrintDocument.PrintPage事件和谷歌。

步骤1:获取整个表格的图像。

Step2。裁剪生成的图像并获得没有边框的客户区/表单图像。

Step3。打印图像。

代码打印按钮和PrintDocument.PrintPage事件

I got the a solution from OriginalGriff's Idea of using a Handler to PrintDocument.PrintPage Event and Google.
Step1: Get the image of the whole form.
Step2. Crop the generated image and get image of client area/ form without border.
Step3. Print The Image.
Code For Print Button and PrintDocument.PrintPage Event
Private Sub Print_btn_Click(sender As Object, e As EventArgs) Handles Print_btn.Click
        PrintPreviewDialog1.Document = PrintDocument1
        PrintDocument1.OriginAtMargins = False
        AddHandler PrintDocument1.PrintPage, AddressOf PrintDocument1_PrintPage
        PrintPreviewDialog1.ShowDialog()
    End Sub

    Private Sub PrintDocument1_PrintPage(sender As Object, e As Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
        Dim print_bmp As Bitmap = GetClientareaImage(Me)
        e.Graphics.DrawImage(print_bmp, 0, 0)
    End Sub





代码用于获取客户区域的图像。

**我正在使用模块来声明此项目全局所需的功能和Subs ..



Code For getting image of client area.
**I am using modules to declare functions and Subs that are required globally for this project..

Module Image_processor

    Public Function GetControlImage(ByVal ctrl As Control) As Bitmap
        Dim ctrl_bmp As New Bitmap(ctrl.Width, ctrl.Height)
        ctrl.DrawToBitmap(ctrl_bmp, New Rectangle(0, 0, ctrl.Width, ctrl.Height))
        Return ctrl_bmp
    End Function

    Public Function GetClientareaImage(ByVal frm As Form) As Bitmap
        Using Cropped_frm As Bitmap = GetControlImage(frm)
            Dim Border As Point = frm.PointToScreen(New Point(0, 0))
            Dim client_x As Integer = Border.X - frm.Left
            Dim client_y As Integer = Border.Y - frm.Top
            Dim client_wd As Integer = frm.ClientSize.Width
            Dim client_ht As Integer = frm.ClientSize.Height

            Dim client_bmp As New Bitmap(client_wd, client_ht)
            Using client_graph As Graphics = Graphics.FromImage(client_bmp)
                client_graph.DrawImage(Cropped_frm, 0, 0, New Rectangle(client_x, client_y, client_wd, client_ht), GraphicsUnit.Pixel)
            End Using
            Return client_bmp
        End Using

    End Function
End Module





想了很多CodeProject,OriginalGriff和Coders



Thaks a lot CodeProject, OriginalGriff and Coders


这篇关于我正在开发一个VB.NET(windows窗体)项目。我有一个表格,我必须在其中打印内容。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆