سلام
می خواستم ببینم راهی هست که بدون باز شدن print preview و پنجره های دیگر فرم برنامه رو پرینت کرد
سلام
می خواستم ببینم راهی هست که بدون باز شدن print preview و پنجره های دیگر فرم برنامه رو پرینت کرد
خوب priview نگیرید مستقیم printdocumnet رو چاپ کنید
ممنون میشم اگه یه مثال ساده برام بزنید
Private Declare Function PrintWindow Lib "user32.dll" (ByVal _
hwnd As IntPtr, ByVal hdcBlt As IntPtr, ByVal nFlags As _
UInt32) As Boolean
Dim screenGrab As Bitmap
Private Sub CaptureScreen()
'captures Form1 into screenGrab
screenGrab = New Bitmap(GroupBox1.Width, GroupBox1.Height)
Dim g As Graphics = Graphics.FromImage(screenGrab)
Dim hdc As IntPtr = g.GetHdc
test.PrintWindow(GroupBox1.Handle, hdc, Nothing)
g.ReleaseHdc(hdc)
g.Flush()
g.Dispose()
End Sub
Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, _
ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
' page bounds are in 100ths of an inch...
Dim pagewidth As Single = e.PageBounds.Width / 100
Dim pageheight As Single = e.PageBounds.Height / 100
' how many pixels in the print area?
Dim pagewidthpixels As Integer = pagewidth * e.PageSettings.PrinterResolution.X
Dim pageheightpixels As Integer = pageheight * e.PageSettings.PrinterResolution.Y
' how many times does the screengrab image fit into the print area?
Dim width_ratio As Double = pagewidthpixels / screenGrab.Width
Dim height_ratio As Double = pageheightpixels / screenGrab.Height
' If the screengrab fits x times, then reduce the resolution of the printer by x.
' Only use one scaling_factor to keep it in proportion
' use the smaller of the two to keep it on the page
Dim scaling_factor As Double
If width_ratio > height_ratio Then
scaling_factor = height_ratio
Else
scaling_factor = width_ratio
End If
' set the resolution of the image - this will scale it to fit on the printed page
screenGrab.SetResolution(e.PageSettings.PrinterRes olution.X / scaling_factor, e.PageSettings.PrinterResolution.Y / scaling_factor)
' send it to the printer
e.Graphics.DrawImage(screenGrab, 0, 0)
End Sub
کد زیر را مطالعه کنید . اگر متوجه نشدید source را برایتان ایمیل می کنم متاسفانه نتوانستم در این قسمت source را upload کنم
Private Declare Function PrintWindow Lib "user32.dll" (ByVal _
hwnd As IntPtr, ByVal hdcBlt As IntPtr, ByVal nFlags As _
UInt32) As Boolean
Dim screenGrab As Bitmap
Private Sub CaptureScreen()
'captures Form1 into screenGrab
screenGrab = New Bitmap(GroupBox1.Width, GroupBox1.Height)
Dim g As Graphics = Graphics.FromImage(screenGrab)
Dim hdc As IntPtr = g.GetHdc
test.PrintWindow(GroupBox1.Handle, hdc, Nothing)
g.ReleaseHdc(hdc)
g.Flush()
g.Dispose()
End Sub
Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, _
ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
' page bounds are in 100ths of an inch...
Dim pagewidth As Single = e.PageBounds.Width / 100
Dim pageheight As Single = e.PageBounds.Height / 100
' how many pixels in the print area?
Dim pagewidthpixels As Integer = pagewidth * e.PageSettings.PrinterResolution.X
Dim pageheightpixels As Integer = pageheight * e.PageSettings.PrinterResolution.Y
' how many times does the screengrab image fit into the print area?
Dim width_ratio As Double = pagewidthpixels / screenGrab.Width
Dim height_ratio As Double = pageheightpixels / screenGrab.Height
' If the screengrab fits x times, then reduce the resolution of the printer by x.
' Only use one scaling_factor to keep it in proportion
' use the smaller of the two to keep it on the page
Dim scaling_factor As Double
If width_ratio > height_ratio Then
scaling_factor = height_ratio
Else
scaling_factor = width_ratio
End If
' set the resolution of the image - this will scale it to fit on the printed page
screenGrab.SetResolution(e.PageSettings.PrinterRes olution.X / scaling_factor, e.PageSettings.PrinterResolution.Y / scaling_factor)
' send it to the printer
e.Graphics.DrawImage(screenGrab, 0, 0)
End Sub
به جای استفاده از دستور ShowDialog از دستور زیر استفاده کنید:
rpt.PrintToPrinter(1, True, 1, 1)
اولین پارامتر مربوط به تعداد چاپ می شود.
اگه ممکنه این سه خط کد را در اینجا قرار دهیدنوشته شده توسط ali_kolahdoozan
با تشکر