none
Imprimir form sem margens/barra de rolagem RRS feed

  • Pergunta

  • Olá amigos,

    Gostaria de imprimir uma form, sem margens.

    Já tentei várias coisas, mas só consigo o que se vê na imagem.

    Para além disso gostaria de colocar uma barra de rolagem no form para trabalhar no formato de uma folha A4.

    Posso contar com a vossa ajuda?

    Cumprimentos.
    sábado, 7 de abril de 2012 23:17

Respostas

  • 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
    

    sábado, 21 de abril de 2012 00:23

Todas as Respostas

  • sábado, 7 de abril de 2012 23:19
  • Imports System.Drawing.Printing

    Public Class Form1
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Shown

            Dim ps As New PageSettings
            Dim area = ps.PrintableArea
            Dim ar = area.Width / area.Height

            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
        

        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

      
    End Class
    sábado, 7 de abril de 2012 23:19
  • Prezado(a),
    Estou migrando seu post para o fórum de Windows Forms.
    Por favor, das próximas vezes que tiver alguma dúvida relacionada a esse assunto, poste por lá.
    Obrigado.

    André Alves de Lima
    Microsoft MVP - Client App Dev
    Visite o meu site: http://www.andrealveslima.com.br
    Me siga no Twitter: @andrealveslima

    quinta-feira, 19 de abril de 2012 10:56
    Moderador
  • 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
    

    sábado, 21 de abril de 2012 00:23
  • Meu Amigo como ficaria se eu fosse passar a pagina em aberto como referencia em vez de passar o Panel1 ?

    Desde já Obrigado.

    quinta-feira, 2 de outubro de 2014 22:19