C # Winfoms Printing - PullRequest
       14

C # Winfoms Printing

0 голосов
/ 30 января 2010

У меня есть winform, я хочу напечатать все доступные данные в winform, скажем, у меня есть форма, полная этикеток, как ее распечатать.

Ответы [ 2 ]

3 голосов
/ 30 января 2010

Следующий пример кода взят из Как: распечатать форму Windows (MSDN) , найденную в вопросе SO под названием "Print Winform / visual element" , который включен первая страница результатов поиска для "winforms printing" :

using System;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Printing;

public class Form1 : Form
{
    private Button printButton = new Button();
    private PrintDocument printDocument1 = new PrintDocument();

    public Form1()
    {
        printButton.Text = "Print Form";
        printButton.Click += printButton_Click;
        printDocument1.PrintPage += printDocument1_PrintPage;
        this.Controls.Add(printButton);
    }

    void printButton_Click(object sender, EventArgs e)
    {
        CaptureScreen();
        printDocument1.Print();
    }

    Bitmap memoryImage;

    private void CaptureScreen()
    {
        Graphics myGraphics = this.CreateGraphics();
        Size s = this.Size;
        memoryImage = new Bitmap(s.Width, s.Height, myGraphics);
        Graphics memoryGraphics = Graphics.FromImage(memoryImage);
        memoryGraphics.CopyFromScreen(Location.X, Location.Y, 0, 0, s);
    }

    private void printDocument1_PrintPage(System.Object sender,  
           System.Drawing.Printing.PrintPageEventArgs e)
    {
        e.Graphics.DrawImage(memoryImage, 0, 0);
    }

    public static void Main()
    {
        Application.Run(new Form1());
    }
}
0 голосов
/ 30 января 2010

псевдопользователей

string printString = "";    
foreach(Label lbl in this.Controls){
     printString += lbl.Text + "\n";
}

Print(printString);
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...