Jawab PrintOption

  • 13 April 2012 23:39
     
      Memiliki Kode

    Hello friends,

    I need your help.

    What is the full code (PrintOption) I write to add this:

    Imports System.Drawing.Printing
    
    Public Class Form1
        
        
    
        Private Sub Button2_Click(ByVal sender As System.Object, _
                ByVal e As System.EventArgs) Handles Button1.Click
            ' If you don't want the print button to print the print button
            Button1.Visible = True
            ' Set the PrintAction to display a Print Preview dialog
            PrintForm1.PrintAction = Printing.PrintAction.PrintToPreview
            ' This prints a copy of the form as it appears in the PrintPreview
            PrintForm1.Print()
            ' Display the print button again on the form
            Button1.Visible = True
    
        End Sub
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Shown
    
            Using g = Graphics.FromHwnd(IntPtr.Zero)
                Size = New Size(CInt(21 / 2.54 * g.DpiX), CInt(29.7 / 2.54 * g.DpiY))
            End Using
    
        End Sub
    
    End Class

Semua Balasan

  • 14 April 2012 0:11
     
     
    i don't understand the question

    thanks for any help

  • 14 April 2012 22:39
     
     

    How to enter PrintOption the block of code that I mentioned first?

    objective:

    Expand margins, covering the red.

  • 16 April 2012 22:54
     
      Memiliki Kode

    the PrintForm component doesn't have that option.

    you could use a printdocument component instead:

    Imports System.Drawing.Printing
    Imports System.Drawing
    
    Public Class Form1
    
        Private WithEvents PrintDocument1 As New PrintDocument
        Dim img As Bitmap
    
        Private Enum area
            wholeForm
            clientRectangle
        End Enum
    
        Private Function captureForm(ByVal frm As Form, ByVal printArea As area) As Bitmap
            frm.BringToFront()
            Dim img As Bitmap = Nothing
            Dim gr As Graphics
            If printArea = area.clientRectangle Then
                img = New Bitmap(frm.ClientSize.Width, frm.ClientSize.Height)
                gr = Graphics.FromImage(img)
                gr.CopyFromScreen(frm.PointToScreen(frm.ClientRectangle.Location), Point.Empty, frm.ClientSize)
            ElseIf printArea = area.wholeForm Then
                img = New Bitmap(frm.Width, frm.Height)
                gr = Graphics.FromImage(img)
                gr.CopyFromScreen(frm.Location, Point.Empty, frm.Size)
            End If
            Return img
        End Function
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            img = captureForm(Me, area.clientRectangle)
            PrintDocument1.Print()
        End Sub
    
        Private Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
            PrintDocument1.OriginAtMargins = True
            e.PageSettings.Margins = New Margins(0, PrintDocument1.DefaultPageSettings.PaperSize.Width, 0, PrintDocument1.DefaultPageSettings.PaperSize.Height)
    
            e.Graphics.DrawImage(img, PrintDocument1.DefaultPageSettings.PrintableArea, img.GetBounds(GraphicsUnit.Pixel), GraphicsUnit.Pixel)
    
        End Sub
    
    End Class
    


    thanks for any help

  • 21 April 2012 0:27
     
     Jawab Memiliki Kode
    Imports System.Drawing.Printing
    Public Class Form1
        Private WithEvents pd As New PrintDocument
        Private WithEvents Button1 As New Button With {.Location = New Point(50, 50), .Text = "print", .Parent = Me}
    
        Private bmp As Bitmap
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
            bmp = New Bitmap(Panel1.ClientSize.Width, Panel1.ClientSize.Height)
            Panel1.DrawToBitmap(bmp, Panel1.ClientRectangle)
    
            pd.Print()
    
        End Sub
        Private Sub pd_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles pd.PrintPage
    
            Dim area = e.PageSettings.PrintableArea
            Dim ar1 = area.Width / area.Height
            Dim ar2 = CSng(bmp.Width / bmp.Height)
    
            If ar1 > ar2 Then
                Dim NewWidth = bmp.Width * area.Height / bmp.Height
                Dim x = (area.Width - NewWidth) / 2
                e.Graphics.DrawImage(bmp, x, area.Top, NewWidth, area.Height)
            Else
                Dim NewHeight = bmp.Height * area.Width / bmp.Width
                Dim y = (area.Height - NewHeight) / 2
                e.Graphics.DrawImage(bmp, area.Left, y, area.Width, NewHeight)
            End If
    
            e.HasMorePages = False
    
        End Sub
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Using g = Graphics.FromHwnd(IntPtr.Zero)
                Panel1.Size = New Size(CInt(21 / 2.54 * g.DpiX), CInt(29.7 / 2.54 * g.DpiY))
            End Using
          End Sub
    End Class