Jo Muncher's Blog

the way to my Dream

原创 Window Form Printer收藏

新一篇: 如何才能正确地激励程序员 | 旧一篇: Base Point@What's the heap and stack?

前日浏览网页偶有所得,修改了一下贴了上来。

 

Imports System.Drawing
Imports System.Drawing.Printing

Public Class 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()
        
MyBase.New()
    
End Sub


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


    
Private 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(ByVal g As Graphics)
        
Try
            
'Fill the drawing area with form's property(backcolor,width,height)
            g.FillRectangle(New SolidBrush(_winForm.BackColor), 00, _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

发表于 @ 2007年07月16日 14:49:00|评论(loading...)|编辑

新一篇: 如何才能正确地激励程序员 | 旧一篇: Base Point@What's the heap and stack?

评论:没有评论。

发表评论  


当前用户设置只有注册用户才能发表评论。如果你没有登录,请点击登录
Csdn Blog version 3.1a
Copyright © Jomunk