Hi,
I'm printing a VB.NET form using BitBlt API my code as follows:
' 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 GetFormImageToPrint()
Dim g As Graphics = Me.CreateGraphics()
Dim formSize As Size = Me.Size
formImage = New Bitmap(formSize.Width, formSize.Height, g)
Dim mg As Graphics = Graphics.FromImage(formImage)
Dim dc1 As IntPtr = g.GetHdc
Dim dc2 As IntPtr = mg.GetHdc
' added code to compute and capture the form
' title bar and borders
Dim widthDiff As Integer = _
(Me.Width - Me.ClientRectangle.Width)
Dim heightDiff As Integer = _
(Me.Height - Me.ClientRectangle.Height)
Dim borderSize As Integer = widthDiff \ 2
Dim heightTitleBar As Integer = heightDiff - borderSize
BitBlt(dc2, 0, 0, _
Me.ClientRectangle.Width + widthDiff, _
Me.ClientRectangle.Height + heightDiff, dc1, _
0 - borderSize, 0 - heightTitleBar, 13369376)
g.ReleaseHdc(dc1)
mg.ReleaseHdc(dc2)
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
Dim sz As SizeF = formImage.GetBounds(GraphicsUnit.Pixel).Size
'scale image if it unscaled doesn't fit marginbounds
If sz.Height > e.MarginBounds.Size.Height Or sz.Width > e.MarginBounds.Size.Width Then
Dim scaleheight As Double = sz.Height / e.MarginBounds.Size.Height
Dim scalewidth As Double = sz.Width / e.MarginBounds.Size.Width
Dim scale As Double = Math.Max(scalewidth, scaleheight)
scalewidth = sz.Width / scale
scaleheight = sz.Height / scale
sz = New SizeF(CSng(scalewidth), CSng(scaleheight))
Dim scalebounds As New RectangleF(e.MarginBounds.Location, sz)
'draw scaled image
e.Graphics.DrawImage(formImage, scalebounds)
Else
'draw unscaled image
e.Graphics.DrawImageUnscaled(formImage, e.MarginBounds)
End If
'e.Graphics.DrawImage(formImage, 0, 0)
End Sub
Private Sub mnuPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuPrint.Click
PrintScr()
End Sub
Private Sub PrintScr()
Try
GetFormImageToPrint()
' create an instance of the PrintDocument component
pd = New Printing.PrintDocument
If PrintDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
pd.PrinterSettings = PrintDialog1.PrinterSettings
pd.Print()
End If
Catch ex As Exception
ErrorMsg(" Error number:" & Err.Number & Chr(13) & "Error Description:" & Err.Description, ParentForm.Text)
End Try
Exit Sub
End Sub
the problem I have is the File menu (mnuFile which contains mnuPrint and about 5 other mnu's) remains on screen thus being also printed. Any ideas how I can close the menu before GetFormImageToPrint is executed.
Cheers
Darren