TechSapphire Saturday, 2024-04-20, 3:22 AM
Site menu
Login form
News
Play Games
  • Deep Freeze
  • Ice Slide
  • Gyroball
  • Fat Fish
  • Bush Royal Rampage
  • Main » Files » Programming

    Demonstrates printing a Windows Form at Run Time, Printing Windows form directly using .Net, Printing without crystal report .Net
    2011-10-09, 8:08 AM

    Printing Windows form directly using .Net Or Printing without crystal report .Net

    This following code is to make your window form print itself without moving to crystal reports for printing.

    Solution code in VB.Net:

    Imports System.Drawing.Printing

    Imports System.Drawing.Graphics

    Imports System.Drawing.Imaging

    Imports System.Runtime.InteropServices

    Public Class Form1

     

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

            Dim prd As PrintDocument

            prd = New PrintDocument

     

            AddHandler prd.PrintPage, AddressOf OnPrintPage

     

            prd.Print()

        End Sub

        Private Sub OnPrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs)

            Dim hwndForm As IntPtr

            hwndForm = Me.Handle

     

            Dim hdcDIBSection As IntPtr

            Dim hdcRef As IntPtr

            Dim hbmDIBSection As IntPtr

            Dim hbmDIBSectionOld As IntPtr

            Dim BMPheader As Win32APICall.BITMAPINFOHEADER

     

            hdcRef = Win32APICall.GetDC(IntPtr.Zero)

            hdcDIBSection = Win32APICall.CreateCompatibleDC(hdcRef)

            Win32APICall.ReleaseDC(IntPtr.Zero, hdcRef)

     

            BMPheader.biBitCount = 24

            BMPheader.biClrImportant = 0

            BMPheader.biClrUsed = 0

            BMPheader.biCompression = Win32APICall.BI_RGB

            BMPheader.biSize = 40

            BMPheader.biHeight = Me.Height

            BMPheader.biPlanes = 1

            BMPheader.biSizeImage = 0

            BMPheader.biWidth = Me.Width

            BMPheader.biXPelsPerMeter = 0

            BMPheader.biYPelsPerMeter = 0

     

            hbmDIBSection = Win32APICall.CreateDIBSection(hdcDIBSection, BMPheader, Win32APICall.DIB_RGB_COLORS, _

            IntPtr.Zero, IntPtr.Zero, 0)

     

            hbmDIBSectionOld = Win32APICall.SelectObject(hdcDIBSection, hbmDIBSection)

            Win32APICall.PatBlt(hdcDIBSection, 0, 0, Me.Width, Me.Height, Win32APICall.WHITENESS)

            Win32APICall.PrintWindow(hwndForm, hdcDIBSection, 0)

            Win32APICall.SelectObject(hdcDIBSection, hbmDIBSectionOld)

     

            Dim imageFrm As Bitmap

            imageFrm = Image.FromHbitmap(hbmDIBSection)

            e.Graphics.DrawImage(imageFrm, 0, 0)

     

            Win32APICall.DeleteDC(hdcDIBSection)

            Win32APICall.DeleteObject(hbmDIBSection)

        End Sub

     

    End Class

    Public Class Win32APICall

     

        Public Const DIB_RGB_COLORS = 0

        Public Const BI_RGB = 0

        Public Const WHITENESS = 16711778

     

        <DllImport("user32.dll", EntryPoint:="PrintWindow", _

        SetLastError:=True, CharSet:=CharSet.Unicode, _

        ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)> _

        Public Shared Function PrintWindow(ByVal hWnd As IntPtr, ByVal hDC As IntPtr, ByVal dwFlags As Integer) As UInt32

        End Function

     

        <StructLayout(LayoutKind.Sequential, pack:=8, CharSet:=CharSet.Auto)> _

        Structure BITMAPINFOHEADER

            Dim biSize As Int32

            Dim biWidth As Int32

            Dim biHeight As Int32

            Dim biPlanes As Int16

            Dim biBitCount As Int16

            Dim biCompression As Int32

            Dim biSizeImage As Int32

            Dim biXPelsPerMeter As Int32

            Dim biYPelsPerMeter As Int32

            Dim biClrUsed As Int32

            Dim biClrImportant As Int32

        End Structure

     

        <DllImport("gdi32.dll", EntryPoint:="CreateDIBSection", _

        SetLastError:=True, CharSet:=CharSet.Unicode, _

        ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)> _

        Public Shared Function CreateDIBSection(ByVal hdc As IntPtr, ByRef pbmi As BITMAPINFOHEADER, _

        ByVal iUsage As Int32, ByVal ppvBits As IntPtr, ByVal hSection As IntPtr, _

        ByVal dwOffset As Int32) As IntPtr

        End Function

     

        <DllImport("gdi32.dll", EntryPoint:="PatBlt", _

        SetLastError:=True, CharSet:=CharSet.Unicode, _

        ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)> _

        Public Shared Function PatBlt(ByVal hDC As IntPtr, ByVal nXLeft As Int32, _

            ByVal nYLeft As Int32, ByVal nWidth As Int32, ByVal nHeight As Int32, _

            ByVal dwRop As Int32) As Boolean

        End Function

     

        <DllImport("gdi32.dll", EntryPoint:="SelectObject", _

        SetLastError:=True, CharSet:=CharSet.Unicode, _

        ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)> _

        Public Shared Function SelectObject(ByVal hDC As IntPtr, ByVal hObj As IntPtr) As IntPtr

        End Function

     

        <DllImport("GDI32.dll", EntryPoint:="CreateCompatibleDC", SetLastError:=True, CharSet:=CharSet.Unicode, _

        ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)> _

        Public Shared Function CreateCompatibleDC(ByVal hRefDC As IntPtr) As IntPtr

        End Function

     

        <DllImport("GDI32.dll", EntryPoint:="DeleteDC", SetLastError:=True, CharSet:=CharSet.Unicode, _

        ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)> _

        Public Shared Function DeleteDC(ByVal hDC As IntPtr) As Boolean

        End Function

     

        <DllImport("GDI32.dll", EntryPoint:="DeleteObject", SetLastError:=True, CharSet:=CharSet.Unicode, _

        ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)> _

        Public Shared Function DeleteObject(ByVal hObj As IntPtr) As Boolean

        End Function

     

        <DllImport("User32.dll", EntryPoint:="ReleaseDC", SetLastError:=True, CharSet:=CharSet.Unicode, _

        ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)> _

        Public Shared Function ReleaseDC(ByVal hWnd As IntPtr, ByVal hDC As IntPtr) As Boolean

        End Function

     

        <DllImport("User32.dll", EntryPoint:="GetDC", SetLastError:=True, CharSet:=CharSet.Unicode, _

        ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)> _

        Public Shared Function GetDC(ByVal hWnd As IntPtr) As IntPtr

        End Function

     

     

    End Class

    Explanation:

    You need form with name form1 and button on it with name button1.

    Category: Programming | Added by: Admin | Tags: Demonstrates printing a Windows For, PrintDocument, PrintPage, PrintForm, BitBlt, Printing a Windows Form
    Views: 1789 | Downloads: 0 | Comments: 2 | Rating: 2.0/1
    Total comments: 0
    Only registered users can add comments.
    [ Registration | Login ]
    Categories
    Programming [3]
    .Net solution,.net project,vb project,vb,c#,.net,c language,c++,projects,project,college project,html template,free template,java
    .Net [0]
    .Net program,vb.net,c#.net,asp.net,projects,.net project,programming solution,free projects
    Mystic Programs [0]
    programming challenges,computer quiz question,secret program
    Security [0]
    Security,window security,web security,web vulnerabilities,virus,virus programming,software security,validation
    Google [0]
    Google search,Google tricks,restrict Google search,Free book search,search Ftp directly,search directory,search direct link rapidshare,search torrent directly
    Project [0]
    College projects,free project,c language project,vb project,vb.net project,.net project,c# project,html project
    Video Tutorial [2]
    Free Video Tutorial
    Database [1]
    SQL Server
    Copyright MyCorp © 2024