📄 form1.vb
字号:
Imports System.Runtime.InteropServices
Public Class Form1
Inherits System.Windows.Forms.Form
Friend WithEvents PictureBox1 As System.Windows.Forms.PictureBox
Friend WithEvents btnPrint As System.Windows.Forms.Button
Friend WithEvents MainMenu1 As System.Windows.Forms.MainMenu
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
MyBase.Dispose(disposing)
End Sub
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents chkFit As System.Windows.Forms.CheckBox
Private Sub InitializeComponent()
Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(Form1))
Me.MainMenu1 = New System.Windows.Forms.MainMenu
Me.PictureBox1 = New System.Windows.Forms.PictureBox
Me.btnPrint = New System.Windows.Forms.Button
Me.chkFit = New System.Windows.Forms.CheckBox
'
'PictureBox1
'
Me.PictureBox1.Image = CType(resources.GetObject("PictureBox1.Image"), System.Drawing.Image)
Me.PictureBox1.Location = New System.Drawing.Point(0, 8)
Me.PictureBox1.Size = New System.Drawing.Size(240, 192)
'
'btnPrint
'
Me.btnPrint.Location = New System.Drawing.Point(80, 232)
Me.btnPrint.Text = "Print"
'
'chkFit
'
Me.chkFit.Location = New System.Drawing.Point(44, 200)
Me.chkFit.Size = New System.Drawing.Size(152, 20)
Me.chkFit.Text = "Stretch to Fit to Page"
'
'Form1
'
Me.Controls.Add(Me.chkFit)
Me.Controls.Add(Me.btnPrint)
Me.Controls.Add(Me.PictureBox1)
Me.Menu = Me.MainMenu1
Me.Text = "Form1"
End Sub
#End Region
Private Sub btnPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrint.Click
PrintScreen(Me, chkFit.Checked)
End Sub
Private Function PrintScreen(ByVal ctl As Control, ByVal autoscale As Boolean) As Boolean
Dim hOldWnd As IntPtr = GetCapture()
ctl.Capture = True
Dim hWnd As IntPtr = GetCapture()
ctl.Capture = False
SetCapture(hOldWnd)
Return PrintScreen(hWnd, ctl, autoscale)
End Function
Private Function PrintScreen(ByVal hWnd As IntPtr, ByVal ctl As Control, ByVal autoscale As Boolean) As Boolean
Dim result As Boolean = False
'Get DC for entire screen (be sure to destroy later!)
Dim hdcInput As IntPtr = GetWindowDC(hWnd)
If (hdcInput.Equals(IntPtr.Zero)) Then
Return False
End If
Dim screenWidth As Integer
Dim screenHeight As Integer
If (Not TypeOf (ctl) Is Form) Then
'Create bitmap for whole screen
screenWidth = GetSystemMetrics(SM_CXSCREEN)
screenHeight = GetSystemMetrics(SM_CYSCREEN)
Else
'Just size it for the control
screenWidth = ctl.Width
screenHeight = ctl.Height
End If
Dim hBitmap As IntPtr = CreateCompatibleBitmap(hdcInput, screenWidth, screenHeight)
If (hdcInput.Equals(IntPtr.Zero)) Then
Return False
End If
Dim hdcOutput As IntPtr = CreateCompatibleDC(hdcInput)
If (hdcInput.Equals(IntPtr.Zero)) Then
DeleteDC(hdcInput)
Return False
End If
Dim hbmOld As IntPtr = SelectObject(hdcOutput, hBitmap)
'Copy the image into the bitmap....
If (Not BitBlt(hdcOutput, 0, 0, screenWidth, screenHeight, hdcInput, 0, 0, SRCCOPY)) Then
SelectObject(hdcOutput, hbmOld)
DeleteDC(hdcOutput)
DeleteDC(hdcInput)
Return False
End If
'Disconnect bitmap from DCs, and clean up DCs...
SelectObject(hdcOutput, hbmOld)
DeleteDC(hdcOutput)
DeleteDC(hdcInput)
Dim hPrinterDC As IntPtr = CreateDC("pcl.dll", "PCL Inkjet", "LPT1", IntPtr.Zero)
'dim hPrinterDC as IntPtr = CreateDC("pcl.dll", "PCL Inkjet", "LPT1", ref devMode)
If (Not hPrinterDC.Equals(IntPtr.Zero)) Then
Dim hGenericMemoryDC As IntPtr = CreateCompatibleDC(IntPtr.Zero)
Dim pageSizeX As Integer = GetDeviceCaps(hPrinterDC, HORZRES) ' page width in pixels
Dim pageSizeY As Integer = GetDeviceCaps(hPrinterDC, VERTRES) ' page height in pixels
'Select bitmap into the generic DC.
Dim hOldObj As IntPtr = SelectObject(hGenericMemoryDC, hBitmap)
'Notify GDI of document and page start.
Dim lpszDocName As String = "\Image.bmp"
Dim lpszOutput As String = "\Image.prn"
Dim lpszDataType As String = Nothing
Dim cbMemSize As Integer = 20 + GetStringAllocSize(lpszDocName) + _
GetStringAllocSize(lpszOutput) + GetStringAllocSize(lpszDataType)
Dim p As IntPtr = LocalAlloc(&H40, cbMemSize)
Dim di As DOCINFO = New DOCINFO
di.cbSize = 20
di.fwType = 0
di.lpszDocName = New IntPtr(p.ToInt32() + 20)
di.lpszOutput = New IntPtr(di.lpszDocName.ToInt32() + GetStringAllocSize(lpszDocName))
di.lpszDataType = IntPtr.Zero
Marshal.StructureToPtr(lpszDocName, di.lpszDocName, False)
Marshal.StructureToPtr(lpszOutput, di.lpszOutput, False)
If (StartDoc(hPrinterDC, di) > 0) Then
'Tell printer to start page.
StartPage(hPrinterDC)
Dim bmpInfo As BITMAP_STRUC = New BITMAP_STRUC
GetObject(hBitmap, System.Runtime.InteropServices.Marshal.SizeOf(bmpInfo), bmpInfo)
'Copy generic DC to the printer (destination) DC.
Dim scale As Integer = pageSizeX / bmpInfo.bmWidth
If (scale = 0) Then
scale = 4
End If
If (Not autoscale) Then
scale = 1
End If
StretchBlt(hPrinterDC, 0, 0, bmpInfo.bmWidth * scale, bmpInfo.bmHeight * scale, _
hGenericMemoryDC, 0, 0, bmpInfo.bmWidth, bmpInfo.bmHeight, SRCCOPY)
'Call end of page and end of document.
EndPage(hPrinterDC)
EndDoc(hPrinterDC)
result = True
End If
'Clean up.
SelectObject(hGenericMemoryDC, hOldObj)
DeleteDC(hGenericMemoryDC)
LocalFree(p)
'Clean up printer DC (destination).
DeleteDC(hPrinterDC)
End If
DeleteObject(hBitmap)
Return result
End Function
Private Function GetStringAllocSize(ByVal s As String) As Integer
If (s Is Nothing) Then
Return 0
End If
Return (s.Length + 1) * Marshal.SystemDefaultCharSize
End Function
'//////////////////////////////////////////////////////////////////////////////////////////
'// Import API Constants
'//////////////////////////////////////////////////////////////////////////////////////////
'Screen size flags
Private Const SM_CXSCREEN As Int32 = 0
Private Const SM_CYSCREEN As Int32 = 1
'Used in the dwROP parameter for BitBlt.
'Copies the source rectangle directly to the destination rectangle.
Private Const SRCCOPY As Integer = &HCC0020
'GetDeviceCaps flags
Private Const HORZRES As Int32 = 8 'Horizontal width in pixels
Private Const VERTRES As Int32 = 10 'Vertical height in pixels
Structure DOCINFO
Public cbSize As Integer
Public lpszDocName As IntPtr
Public lpszOutput As IntPtr
Public lpszDataType As IntPtr
Public fwType As Integer
End Structure
Structure BITMAP_STRUC
Public bmType As Integer
Public bmWidth As Integer
Public bmHeight As Integer
Public bmWidthBytes As Integer
Public bmPlanes As UInt16
Public bmBitsPixel As UInt16
Public bmBits As Integer
End Structure
'//////////////////////////////////////////////////////////////////////////////////////////
'// Import API Methods
'//////////////////////////////////////////////////////////////////////////////////////////
' <summary>
' PInvoke for GetCapture()
' </summary>
' <returns>The return value is a handle to the capture window associated with the
' current thread. If no window in the thread has captured the mouse, the return
' value is NULL.</returns>
<DllImport("coredll.dll")> _
Private Shared Function GetCapture() As IntPtr
End Function
<DllImport("coredll.dll")> _
Private Shared Function SetCapture(ByVal hwnd As IntPtr) As IntPtr
End Function
'<summary>
' PInvoke for GetWindowDC()
'</summary>
'<param name="hwnd">Handle to the window whose DC is to be retrieved.</param>
'<returns>A handle to the DC for the specified window's client area</returns>
<DllImport("coredll.dll")> _
Private Shared Function GetWindowDC(ByVal hwnd As IntPtr) As IntPtr
End Function
'<summary>
'PInvoke for ReleaseDC
'</summary>
'<param name="hwnd">Handle to the window whose DC is to be released.</param>
'<param name="hdc">Handle to the DC to be released</param>
'<returns>The return value indicates whether the DC was released. If the
'DC was released, the return value is 1. If the DC was not released, the return
'value is zero.</returns>
<DllImport("coredll.dll")> _
Private Shared Function ReleaseDC(ByVal hwnd As IntPtr, ByVal hdc As IntPtr) As Integer
End Function
'<summary>
'PInvoke for CreateDC
'</summary>
'<param name="hdc">Handle to an existing DC.</param>
'<returns>The handle to a memory DC.</returns>
<DllImport("coredll.dll")> _
Private Shared Function CreateDC(ByVal lpszDriver As String, ByVal lpszDevice As String, _
ByVal lpszOutput As String, ByRef deviceMode As IntPtr) As IntPtr
End Function
'<summary>
'PInvoke for CreateCompatibleDC
'</summary>
'<param name="hdc">Handle to an existing DC.</param>
'<returns>The handle to a memory DC.</returns>
<DllImport("coredll.dll")> _
Private Shared Function CreateCompatibleDC(ByVal hdc As IntPtr) As IntPtr
End Function
'<summary>
'PInvoke for DeleteDC
'</summary>
'<param name="hdc">Handle to the device context</param>
'<returns>True if the function succeeds; false otherwise</returns>
<DllImport("coredll.dll")> _
Private Shared Function DeleteDC(ByVal hdc As IntPtr) As Boolean
End Function
<DllImport("coredll.dll")> _
Private Shared Function GetSystemMetrics(ByVal nIndex As Int32) As Integer
End Function
<DllImport("coredll.dll")> _
Private Shared Function GetDeviceCaps(ByVal hdc As IntPtr, ByVal nIndex As Integer) As Integer
End Function
'<summary>
'PInvoke for DeleteObject
'</summary>
'<param name="hObject">HAndle to a logical pen, brush, font, bitmap, region or palette</param>
'<returns>True if the function succeedes; false otherwise</returns>
<DllImport("coredll.dll")> _
Private Shared Function DeleteObject(ByVal hObject As IntPtr) As Boolean
End Function
'<summary>
'PInvoke for SelectObject
'</summary>
'<param name="hdc">Handle to the DC</param>
'<param name="hgdiobj">Handle to the object (bitmap, brush, font, pen or region)
'to be selected.</param>
'<returns>If the function succeeds, the return value is a handle to the object
'being replaced.</returns>
<DllImport("coredll.dll")> _
Private Shared Function SelectObject(ByVal hdc As IntPtr, ByVal hgdiobj As IntPtr) As IntPtr
End Function
<DllImport("coredll.dll")> _
Private Shared Function GetObject(ByVal hgdiobj As IntPtr, ByVal cbBuffer As Integer, _
ByRef lpvObject As BITMAP_STRUC) As Integer
End Function
<DllImport("coredll.dll")> _
Private Shared Function CreateCompatibleBitmap(ByVal hdc As IntPtr, _
ByVal nWidth As Integer, ByVal nHeight As Integer) As IntPtr
End Function
'<summary>
'PInvoke for BitBlt
'</summary>
'<param name="hdcDest">Handle to the destination device context</param>
'<param name="nXDest">Specifies the x-coordinate, in logical units, of
'the upper-left corner of the destination rectangle.</param>
'<param name="nYDest">Specifies the y-coordinate, in logical units, of
'the upper-left corner of the destination rectangle.</param>
'<param name="nWidth">Specifies the width, in logical units, of the
'source and destination rectangles.</param>
'<param name="nHeight">Specifies the height, in logical units, of the
'source and destination rectangles.</param>
'<param name="hdcSrc">Handle to the source device context.</param>
'<param name="nXSrc">Specifies the x-coordinate, in logical units, of
'the upper-left corner of the source rectangle.</param>
'<param name="nYSrc">Specifies the y-coordinate, in logical units, of
'the upper-left corner of the source rectangle.</param>
'<param name="dwROP">Specifies a raster-operation code. These codes define
'how the color data for the source rectangle is to be combined with the
'color data for the destination rectangle to achieve the final color.</param>
'<returns>If the function succeeds, the return value is nonzero.</returns>
<DllImport("coredll.dll")> _
Private 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 Int32) As Boolean
End Function
' <summary>
' PInvoke for StretchBlt
' </summary>
' <param name="hdcDest"></param>
' <param name="nXOriginDest"></param>
' <param name="nYOriginDest"></param>
' <param name="nWidthDest"></param>
' <param name="nHeightDest"></param>
' <param name="hdcSrc"></param>
' <param name="nXOriginSrc"></param>
' <param name="nYOriginSrc"></param>
' <param name="nWidthSrc"></param>
' <param name="nHeightSrc"></param>
' <param name="dwRop"></param>
' <returns></returns>
<DllImport("coredll.dll")> _
Private Shared Function StretchBlt(ByVal hdcDest As IntPtr, ByVal nXOriginDest As Integer, _
ByVal nYOriginDest As Integer, ByVal nWidthDest As Integer, ByVal nHeightDest As Integer, _
ByVal hdcSrc As IntPtr, ByVal nXOriginSrc As Integer, ByVal nYOriginSrc As Integer, ByVal nWidthSrc As Integer, _
ByVal nHeightSrc As Integer, ByVal dwRop As Int32) As Boolean
End Function
<DllImport("coredll.dll")> _
Private Shared Function StartDoc(ByVal hdc As IntPtr, ByRef di As DOCINFO) As Int32
End Function
<DllImport("coredll.dll")> _
Private Shared Function StartPage(ByVal hdc As IntPtr) As Int32
End Function
<DllImport("coredll.dll")> _
Private Shared Function EndDoc(ByVal hdc As IntPtr) As Int32
End Function
<DllImport("coredll.dll")> _
Private Shared Function EndPage(ByVal hdc As IntPtr) As Int32
End Function
<DllImport("coredll.dll")> _
Private Shared Function LocalAlloc(ByVal flags As Integer, ByVal size As Integer) As IntPtr
End Function
<DllImport("coredll.dll")> _
Private Shared Sub LocalFree(ByVal p As IntPtr)
End Sub
End Class
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -