B
Bhadri
Guest
I have a problem in printing large windows form. I have used following code to print the form. still it's printing like print screen, not complete form. Form is larger than the Screen and Scrollable.
Private Const SRCCOPY As Integer = &HCC0020
' create a printing component
Private WithEvents pd As Printing.PrintDocument
' storage for form image
Dim formImage As Bitmap
' create API prototype
Private Declare Function BitBlt Lib "gdi32.dll" Alias _
"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 System.Int32) As Long
Private Sub BtnPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnPrint.Click
' initiate the printdocument component
GetFormImage(True)
' pd.Print()
End Sub
' Callback from PrintDocument component to
' do the actual printing
Private Sub pd_PrintPage(ByVal sender As Object, _
ByVal e As System.Drawing.Printing.PrintPageEventArgs) _
Handles pd.PrintPage
e.Graphics.DrawImage(formImage, 0, 0)
' e.HasMorePages = True
End Sub
Private Sub GetFormImage(Optional ByVal fullWindow As Boolean = False)
With Me ' This can easily be replaced with a Form parameter
' Create a Graphics object for the form
Dim formGraphics As Graphics = .CreateGraphics
' Create a compatible bitmap and get its Graphics object
If fullWindow Then
formImage = New Bitmap(.Width, .Height, formGraphics)
Else
formImage = New Bitmap(.ClientRectangle.Width, _
.ClientRectangle.Height, _
formGraphics)
End If
Dim memGraphics As Graphics = Graphics.FromImage(formImage)
' Get the target and source device context handles (hDC)
Dim sourceDC As IntPtr = formGraphics.GetHdc
Dim targetDC As IntPtr = memGraphics.GetHdc
' Do the screenshot part of the job
If fullWindow Then
' Consider the border width and the titlebar height
Dim widthDelta As Integer = (.Width - _
.ClientRectangle.Width)
Dim heightDelta As Integer = (.Height - _
.ClientRectangle.Height)
' Copy the form including its titlebar and borders
BitBlt(targetDC, _
0, 0, _
.ClientRectangle.Width + widthDelta, _
.ClientRectangle.Height + heightDelta, _
sourceDC, _
0 - widthDelta \ 2, 0 - (heightDelta - widthDelta \ 2), _
SRCCOPY)
Else
' Copy the form's client area
BitBlt(targetDC, _
0, 0, .ClientRectangle.Width, .ClientRectangle.Height, _
sourceDC, _
.ClientRectangle.X, .ClientRectangle.Y, _
SRCCOPY)
End If
' Release DCs and dispose objects
formGraphics.ReleaseHdc(sourceDC)
formGraphics.Dispose()
memGraphics.ReleaseHdc(targetDC)
memGraphics.Dispose()
' formImage now has the form's image. Print it before disposing it.
pd.Print() ' Invokes PrintDocument1_PrintPage (below)
' Finally dispose the image
formGraphics.Dispose()
End With
Version 1.1.4322SP1
What if the form you have is a vertically scrollable form.
How would you print the hidden area of the form (the area you have to get to by scrolling down)..
The above method only prints the front first screen of the form.
please help me out. Any solution would be great.
Thanks in advance.
Regards
Bhadri
Continue reading...
Private Const SRCCOPY As Integer = &HCC0020
' create a printing component
Private WithEvents pd As Printing.PrintDocument
' storage for form image
Dim formImage As Bitmap
' create API prototype
Private Declare Function BitBlt Lib "gdi32.dll" Alias _
"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 System.Int32) As Long
Private Sub BtnPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnPrint.Click
' initiate the printdocument component
GetFormImage(True)
' pd.Print()
End Sub
' Callback from PrintDocument component to
' do the actual printing
Private Sub pd_PrintPage(ByVal sender As Object, _
ByVal e As System.Drawing.Printing.PrintPageEventArgs) _
Handles pd.PrintPage
e.Graphics.DrawImage(formImage, 0, 0)
' e.HasMorePages = True
End Sub
Private Sub GetFormImage(Optional ByVal fullWindow As Boolean = False)
With Me ' This can easily be replaced with a Form parameter
' Create a Graphics object for the form
Dim formGraphics As Graphics = .CreateGraphics
' Create a compatible bitmap and get its Graphics object
If fullWindow Then
formImage = New Bitmap(.Width, .Height, formGraphics)
Else
formImage = New Bitmap(.ClientRectangle.Width, _
.ClientRectangle.Height, _
formGraphics)
End If
Dim memGraphics As Graphics = Graphics.FromImage(formImage)
' Get the target and source device context handles (hDC)
Dim sourceDC As IntPtr = formGraphics.GetHdc
Dim targetDC As IntPtr = memGraphics.GetHdc
' Do the screenshot part of the job
If fullWindow Then
' Consider the border width and the titlebar height
Dim widthDelta As Integer = (.Width - _
.ClientRectangle.Width)
Dim heightDelta As Integer = (.Height - _
.ClientRectangle.Height)
' Copy the form including its titlebar and borders
BitBlt(targetDC, _
0, 0, _
.ClientRectangle.Width + widthDelta, _
.ClientRectangle.Height + heightDelta, _
sourceDC, _
0 - widthDelta \ 2, 0 - (heightDelta - widthDelta \ 2), _
SRCCOPY)
Else
' Copy the form's client area
BitBlt(targetDC, _
0, 0, .ClientRectangle.Width, .ClientRectangle.Height, _
sourceDC, _
.ClientRectangle.X, .ClientRectangle.Y, _
SRCCOPY)
End If
' Release DCs and dispose objects
formGraphics.ReleaseHdc(sourceDC)
formGraphics.Dispose()
memGraphics.ReleaseHdc(targetDC)
memGraphics.Dispose()
' formImage now has the form's image. Print it before disposing it.
pd.Print() ' Invokes PrintDocument1_PrintPage (below)
' Finally dispose the image
formGraphics.Dispose()
End With
Version 1.1.4322SP1
What if the form you have is a vertically scrollable form.
How would you print the hidden area of the form (the area you have to get to by scrolling down)..
The above method only prints the front first screen of the form.
please help me out. Any solution would be great.
Thanks in advance.
Regards
Bhadri
Continue reading...