PDA

View Full Version : پرینت محتوی یک textbox یا form در vb2003.net



ashkan2005
جمعه 29 دی 1385, 22:09 عصر
با سلام خدمت دوستان و اساتیدمحترم
من امروز از صبح تو سایت دنبال برنامه ای در vb2003.net می گردم تا بتونم محتوی یک textbox یا form رو برای چاپ به چاپگر بفرستم تمام کد های رو هم که دوستان نوشتند امتحان کردم اما جواب نداد از همه عزیزان خواهش می کنم اگه اطلاعاتی در این مورد دارن به طور کامل از imports تا آخر اینجا بگن تا همه استفاده کنیم .:متفکر:
به امید موفقیت همه عزیزان

ashkan2005
شنبه 30 دی 1385, 21:54 عصر
با سلام خدمت دوستان
پس از تلاش بسیار تونستم شکل صحیح کد رو پیدا کنم می تونین استفاده کنین.


OptionStrictOn
Imports System.Drawing.Printing
PublicClass Form1
Inherits System.Windows.Forms.Form
PrivateWithEvents pdoc AsNew PrintDocument
PrivateSub Print1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Print1.Click
Dim dialog AsNew PrintDialog
dialog.Document = pdoc
If dialog.ShowDialog = DialogResult.OK Then
pdoc.Print()
EndIf
EndSub
PrivateSub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) HandlesMyBase.Load
TextBox1.Text = "nhm rain group"

EndSub
PrivateSub Preview1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Preview1.Click
Dim ppd AsNew PrintPreviewDialog
Try
ppd.Document = pdoc
ppd.ShowDialog()
Catch exp As Exception
MessageBox.Show("An error occurred while trying to load the " & _
"document for Print Preview. Make sure you currently have " & _
"access to a printer. A printer must be connected and " & _
"accessible for Print Preview to work.", Me.Text, _
MessageBoxButtons.OK, MessageBoxIcon.Error)
EndTry
EndSub
PrivateSub PageSetup1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PageSetup1.Click
Dim psd AsNew PageSetupDialog
With psd
.Document = pdoc
.PageSettings = pdoc.DefaultPageSettings
EndWith
If psd.ShowDialog = DialogResult.OK Then
pdoc.DefaultPageSettings = psd.PageSettings
EndIf
EndSub
' PrintPage is the foundational printing event. This event gets fired for every
' page that will be printed. You could also handle the BeginPrint and EndPrint
' events for more control.
'
' The following is very
' fast and useful for plain text as MeasureString calculates the text that
' can be fitted on an entire page. This is not that useful, however, for
' formatted text. In that case you would want to have word-level (vs page-level)
' control, which is more complicated.
PrivateSub pdoc_PrintPage(ByVal sender AsObject, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles pdoc.PrintPage
' Declare a variable to hold the position of the last printed char. Declare
' as static so that subsequent PrintPage events can reference it.
Static intCurrentChar As Int32
' Initialize the font to be used for printing.
Dim font AsNew Font("Microsoft Sans Serif", 24)
Dim intPrintAreaHeight, intPrintAreaWidth, marginLeft, marginTop As Int32
With pdoc.DefaultPageSettings
' Initialize local variables that contain the bounds of the printing
' area rectangle.
intPrintAreaHeight = .PaperSize.Height - .Margins.Top - .Margins.Bottom
intPrintAreaWidth = .PaperSize.Width - .Margins.Left - .Margins.Right
' Initialize local variables to hold margin values that will serve
' as the X and Y coordinates for the upper left corner of the printing
' area rectangle.
marginLeft = .Margins.Left ' X coordinate
marginTop = .Margins.Top ' Y coordinate
EndWith
' If the user selected Landscape mode, swap the printing area height
' and width.
If pdoc.DefaultPageSettings.Landscape Then
Dim intTemp As Int32
intTemp = intPrintAreaHeight
intPrintAreaHeight = intPrintAreaWidth
intPrintAreaWidth = intTemp
EndIf
' Calculate the total number of lines in the document based on the height of
' the printing area and the height of the font.
Dim intLineCount As Int32 = CInt(intPrintAreaHeight / font.Height)
' Initialize the rectangle structure that defines the printing area.
Dim rectPrintingArea AsNew RectangleF(marginLeft, marginTop, intPrintAreaWidth, intPrintAreaHeight)
' Instantiate the StringFormat class, which encapsulates text layout
' information (such as alignment and line spacing), display manipulations
' (such as ellipsis insertion and national digit substitution) and OpenType
' features. Use of StringFormat causes MeasureString and DrawString to use
' only an integer number of lines when printing each page, ignoring partial
' lines that would otherwise likely be printed if the number of lines per
' page do not divide up cleanly for each page (which is usually the case).
' See further discussion in the SDK documentation about StringFormatFlags.
Dim fmt AsNew StringFormat(StringFormatFlags.LineLimit)
' Call MeasureString to determine the number of characters that will fit in
' the printing area rectangle. The CharFitted Int32 is passed ByRef and used
' later when calculating intCurrentChar and thus HasMorePages. LinesFilled
' is not needed for this sample but must be passed when passing CharsFitted.
' Mid is used to pass the segment of remaining text left off from the
' previous page of printing (recall that intCurrentChar was declared as
' static.
Dim intLinesFilled, intCharsFitted As Int32
e.Graphics.MeasureString(Mid(TextBox1.Text, intCurrentChar + 1), font, _
New SizeF(intPrintAreaWidth, intPrintAreaHeight), fmt, _
intCharsFitted, intLinesFilled)
' Print the text to the page.
e.Graphics.DrawString(Mid(TextBox1.Text, intCurrentChar + 1), font, _
Brushes.Black, rectPrintingArea, fmt)
' Advance the current char to the last char printed on this page. As
' intCurrentChar is a static variable, its value can be used for the next
' page to be printed. It is advanced by 1 and passed to Mid() to print the
' next page (see above in MeasureString()).
intCurrentChar += intCharsFitted
' HasMorePages tells the printing module whether another PrintPage event
' should be fired.
If intCurrentChar < TextBox1.Text.Length Then
e.HasMorePages = True
Else
e.HasMorePages = False
' You must explicitly reset intCurrentChar as it is static.
intCurrentChar = 0
EndIf
EndSub
EndClass

اگه خواستین می تونم کل برنامه رو رو سایت بذارم.

forozeshfard
یک شنبه 01 بهمن 1385, 05:55 صبح
سلام
ممنون می شوم اگر برنامه را اینجا قرار دهید تا همه استفاده کنند

ashkan2005
چهارشنبه 04 بهمن 1385, 21:33 عصر
با سلام خدمت شما دوست عزیز
ببخشید که نمی تونم برنامه رو تو سایت بذارم هر جا مشکلی داشتین بگین اگه بتونم حتما" کمک می کنم.

ashkan2005
چهارشنبه 04 بهمن 1385, 21:46 عصر
با سلام خدمت دوستان
این هم کد پرینت از کل فرم
ابتدا یک button روی صفحه بیاوردید و در event on click آن دستور زیر را بنویسید.

CaptureScreen()
PrintDocument1.Print()

حال دستورات تابع() CaptureScreen را به صورت زیر بنویسید.




Private Sub CaptureScreen()
Dim mygraphics As Graphics = Me.CreateGraphics()
Dim s As Size = Me.Size
memoryImage = New Bitmap(s.Width, s.Height, mygraphics)
Dim memoryGraphics As Graphics = Graphics.FromImage(memoryImage)
Dim dc1 As IntPtr = mygraphics.GetHdc
Dim dc2 As IntPtr = memoryGraphics.GetHdc
BitBlt(dc2, 0, 0, Me.ClientRectangle.Width, _
Me.ClientRectangle.Height, dc1, 0, 0, 13369376)
mygraphics.ReleaseHdc(dc1)
memoryGraphics.ReleaseHdc(dc2)
End Sub

حال یک
PrintDocument1 روی فرم اضافه کنید و دستور زیر را برای آن بنویسید.

(e.Graphics.DrawImage(memoryImage, 0, 0
در آخر
Function زیر را نیز به اول برنامه اضافه کنید.



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

Dim memoryImage As Bitmap

موفق باشید.
سلام بر حسین
یاشاسین آذربایجان