The previous day, When I browsed the web page, I made some changes and posted them.
Imports system. Drawing
Imports system. Drawing. Printing
Public class winformprinterclass winformprinter
Inherits printdocument
'Descare 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