大多数人在学习编程的过程中,往往都会通过不断的代码编写来积累经验达到一个运用熟练的地步。那么对于VB.NET语言的学习同样需要遵循这样一个规律。下面就让我们一起来看看VB.NET窗体打印的实现代码,从中增加我们的代码编写经验。
VB.NET窗体打印代码示例:
- Imports System
- Imports System.Drawing.Printing
- Public Class Form1
- < System.Runtime.InteropServices.
DllImport("gdi32.dll")>- _Public Shared Function BitBlt
(ByVal hdcDest As IntPtr, ByVal
nXDest As Integer, ByVal nYDest As
Integer, ByVal nWidth As Integer,
ByVal nHeight As Integer, ByVal hdcSrc
As IntPtr, ByVal nXSrc As Integer,
ByVal nYSrc As Integer, ByVal dwRop
As Integer) As Boolean- End Function
- < System.Runtime.InteropServices.
DllImport("User32.dll")> _- Public Shared Function GetWindowDC
(ByVal hwd As IntPtr) As IntPtr- End Function
- < System.Runtime.InteropServices.
DllImport("User32.dll")> _- Public Shared Function ReleaseDC
(ByVal hwd As IntPtr, ByVal dc As
IntPtr) As Integer- End Function
- < System.Runtime.InteropServices.
DllImport("user32.dll")> _- Public Shared Function GetWindowRect
(ByVal hWnd As IntPtr, ByRef lpRect
As RECT) As Boolean- End Function
- < Serializable(), System.Runtime.
InteropServices.StructLayout(System.
Runtime.InteropServices.LayoutKind.
Sequential)> _- Public Structure RECT
- Public Left As Integer
- Public Top As Integer
- Public Right As Integer
- Public Bottom As Integer
- End Structure
- Private memoryImage As Bitmap
- Private Sub CaptureScreen()
- Dim mygraphics As Graphics = Me.
CreateGraphics()- Dim r As RECT
- GetWindowRect(Me.Handle, r)
- Dim s As RectangleRectangle =
Rectangle.FromLTRB(r.Left, r.Top,
r.Right, r.Bottom)- memoryImage = New Bitmap(s.Width,
s.Height, mygraphics)- Dim memoryGraphics As Graphics
Graphics = Graphics.FromImage(memoryImage)- Dim dc1 As IntPtr = GetWindowDC(Me.Handle)
- Dim dc2 As IntPtr = memoryGraphics.GetHdc()
- BitBlt(dc2, 0, 0, s.Width, s.Height,
dc1, 0, 0, 13369376)- ReleaseDC(Me.Handle, dc1)
- memoryGraphics.ReleaseHdc(dc2)
- End Sub
- Private Sub Document_PrintPage(ByVal
sender As System.Object, ByVal e As
System.Drawing.Printing.PrintPageEventArgs)- e.Graphics.DrawImage(memoryImage, 0, 0)
- End Sub
- Private Sub Button1_Click_1(ByVal sender
As System.Object, ByVal e As System.EventArgs)
Handles Button1.Click- CaptureScreen()
- PrintDialog1.ShowDialog()
- Dim pd As PrintDocument = New
PrintDocument- pd.PrinterSettings = PrintDialog1.
PrinterSettings- AddHandler pd.PrintPage, AddressOf
Document_PrintPage- pd.Print()
- End Sub
怎么样,通过本文介绍的VB.NET窗体打印的实现方法,大家应该可以充分掌握这一应用技巧了。
【编辑推荐】