Window Form Printer

來源:互聯網
上載者:User

前日瀏覽網頁偶有所得,修改了一下貼了上來。

 

Imports System.Drawing
Imports System.Drawing.Printing

Public Class WinFormPrinterClass WinFormPrinter
    Inherits PrintDocument

    'declare a variable of form 
    Private _winForm As Form


    'Const 
    Const c_button As String = "System.Windows.Forms.Button"
    Const c_textbox As String = "System.Windows.Forms.TextBox"
    Const c_checkbox As String = "System.Windows.Forms.CheckBox"
    Const c_picturebox As String = "System.Windows.Forms.PictureBox"

    'var
    Private ctl_Obj As Object

    Sub New()Sub New()
        MyBase.New()
    End Sub

    Sub New()Sub New(ByVal win As Form)
        _winForm = win
        AddHandler MyBase.PrintPage, AddressOf PrintPageHandler
    End Sub

    Private Sub PrintPageHandler()Sub PrintPageHandler(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs)
        DrawWinForm(e.Graphics)
    End Sub

    'Draw controls on window forms 
    Public Sub DrawWinForm()Sub DrawWinForm(ByVal g As Graphics)
        Try
            'Fill the drawing area with form's property(backcolor,width,height)
            g.FillRectangle(New SolidBrush(_winForm.BackColor), 0, 0, _winForm.Width, _winForm.Height)
            'Put necessary controls into drawing area
            For Each ctl As Control In _winForm.Controls
                'Check the type of control
                Select Case ctl.GetType().ToString()
                    Case c_button
                        ctl_Obj = CType(ctl, Button)
                        ControlPaint.DrawButton(g, ctl_Obj.Left, ctl_Obj.Top, ctl_Obj.Width, ctl_Obj.Height, ButtonState.Normal)
                        g.DrawString(ctl_Obj.Text _
                                        , ctl_Obj.Font _
                                        , New SolidBrush(ctl_Obj.ForeColor) _
                                        , ctl_Obj.Left + ctl_Obj.Width / 2 - g.MeasureString(ctl_Obj.Text, ctl_Obj.Font).Width / 2 _
                                        , ctl_Obj.Top + ctl_Obj.Height / 2 - g.MeasureString("a", ctl_Obj.Font).Height / 2 _
                                        , New StringFormat)
                    Case c_textbox
                        ctl_Obj = CType(ctl, TextBox)
                        ControlPaint.DrawButton(g, ctl_Obj.Left + 1, ctl_Obj.Top + 1, ctl_Obj.Width + 2, ctl_Obj.Height - 2, ButtonState.Pushed)
                        g.DrawString(ctl_Obj.Text _
                                        , ctl_Obj.Font _
                                        , New SolidBrush(ctl_Obj.ForeColor) _
                                        , ctl_Obj.Left + 2 _
                                        , ctl_Obj.Top + ctl_Obj.Height / 2 - g.MeasureString("a", ctl_Obj.Font).Height / 2 _
                                        , New StringFormat)
                    Case c_checkbox
                        ctl_Obj = CType(ctl, CheckBox)
                        If ctl_Obj.Checked Then
                            ControlPaint.DrawCheckBox(g, ctl_Obj.Left, ctl_Obj.Top + 1, ctl_Obj.Width, ctl_Obj.Height / 2, ButtonState.Checked)
                        Else
                            ControlPaint.DrawCheckBox(g, ctl_Obj.Left, ctl_Obj.Top + 1, ctl_Obj.Width, ctl_Obj.Height / 2, ButtonState.Normal)
                        End If
                        g.DrawString(ctl_Obj.Text _
                                        , ctl_Obj.Font _
                                        , New SolidBrush(ctl_Obj.ForeColor) _
                                        , ctl_Obj.Right - ctl_Obj.Height - g.MeasureString(ctl_Obj.Text, ctl_Obj.Font).Width _
                                        , ctl_Obj.Top _
                                        , New StringFormat)
                    Case c_picturebox
                        ctl_Obj = CType(ctl, PictureBox)
                        g.DrawImage(ctl_Obj.Image _
                                        , New Rectangle(ctl_Obj.Left, ctl_Obj.Top, ctl_Obj.Width, ctl_Obj.Height) _
                                        , New Rectangle(ctl_Obj.Left, ctl_Obj.Top, ctl_Obj.Width, ctl_Obj.Height) _
                                        , GraphicsUnit.Pixel)
                End Select
            Next
        Catch ex As Exception
            Throw New Exception("Error occured at DrawWinForm(Sub):" + ex.Message)
        End Try
    End Sub
End Class

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.