用 C# 打印控件的整个区域 [英] Print the entire area of the control with C#

查看:40
本文介绍了用 C# 打印控件的整个区域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这不是一个重复的问题 - 我的问题和其他问题之间的差异是我的控制器包含一个滚动条,因此无法打印更多信息.

This is not a duplicat question- the diffenece beetween my question an the others one is my Controler contail a scroller, so they are more informations can't be printed.

我有一个包含主表单名称 MainForms 的 C# 应用程序.这个 MainForms 有一个控件 mainDisplay.我想将我们在 mainDisplay 上找到的全部信息打印到打印机.

I have a C# application that contains a main form name MainForms. This MainForms has a control mainDisplay. I want to print the entire information what we found on the mainDisplay to the printer.

问题是控件上的信息太大,我必须滚动才能看到所有信息.

The problem is the information on the the control is too big, and I have to scroll to see all information.

有人有什么功能可以让我打印这个控件MainDisplay,其中包含完整的信息?

Someone have any function that allow me to print this control MainDisplay with entire information in it?

这是我的 MainDisplay 区域的打印屏幕,您可以在右侧看到滚动条:

This the printscreen of the area of my MainDisplay at the right you see the scrollbar:

我使用这个函数(来源:打印控件)

I use this Function (Source : Printing a control)

private static void PrintControl(Control control)
{
    var bitmap = new Bitmap(control.Width, control.Height);

    control.DrawToBitmap(bitmap, new Rectangle(0, 0, control.Width, control.Height));

    var pd = new PrintDocument();

    pd.PrintPage += (s, e) => e.Graphics.DrawImage(bitmap, 100, 100);
    pd.Print();
}

但是我的问题还是不能打印我的控件中包含的所有信息,它只是打印了一个小区域,还需要打印更多没有打印的信息.

But my problem still can't print all the informations contain in my control, it's just print a small erea, still need print more informations which are not printed.

推荐答案

我找到了解决方案.这是我做的步骤:

I find the solution. This is the steps i do :

1 - 我们有一个 mainForm,这个主窗体包含一个具有特定尺寸和区域的控件 mainDisplay,假设这个尺寸更小,我们得到滚动.

1 - We have a mainForm, and this main form contain a control mainDisplay with a specific dimension and area, let's say this dimensions is smaller and we get scroll.

2- 我要做的是让这个 mainDisplay 为空.

2- What i do is i make this mainDisplay Empty.

3- 我创建了另一个控件 myControlToDisplay.我绘制并放置所有我想要的字段而不滚动,所以这个 myControlToDisplay 将有一个很大的尺寸.

3- i create an other Control myControlToDisplay. I draw and i put all fields i want without scroll, so this one myControlToDisplay will have a big dimension.

4- 在我的应用程序启动时,我告诉 mainDisplay 加载 myControlToDisplay.这一次 myControlToDisplay 的所有内容都会滚动显示在 mainDisplay 上.因为mainDisplay有一个小区域.

4- on the star-up of my application, i tell to the mainDisplay to load myControlToDisplay. This time all the content of myControlToDisplay will be display on mainDisplay with a scroll. Because mainDisplay have a small area.

5- 我写这个函数:

Bitmap MemoryImage;
PrintDocument printDoc = new PrintDocument();
PrintDialog printDialog = new PrintDialog();
PrintPreviewDialog printDialogPreview = new PrintPreviewDialog();
Control panel = null;

public void Print(Control pnl)
{
    DateTime saveNow = DateTime.Now;
    string datePatt = @"yyyy-M-d_hh-mm-ss tt";

    panel = pnl;
    GetPrintArea(pnl);
    printDialog.AllowSomePages = true;
    printDoc.PrintPage += new PrintPageEventHandler(Print_Details);
    printDialog.Document = printDoc;
    printDialog.Document.DocumentName = "Document Name";

    //printDialog.ShowDialog();
    if (printDialog.ShowDialog() == DialogResult.OK)
    {
        printDoc.Print();
    }
}

public void PrintPreview(Control pnl)
{
    DateTime saveNow = DateTime.Now;
    string datePatt = @"yyyy-M-d_hh-mm-ss tt";

    panel = pnl;
    GetPrintArea(pnl);           
    printDoc.PrintPage += new PrintPageEventHandler(Print_Details);
    printDialogPreview.Document = printDoc;
    printDialogPreview.Document.DocumentName = "Document Name";

    //printDialog.ShowDialog();
    if (printDialogPreview.ShowDialog() == DialogResult.OK)
    {
        printDoc.Print();
    }
}

private void Print_Details(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
    RectangleF marginBounds = e.MarginBounds;

    DateTime saveNow = DateTime.Now;
    string datePatt = @"M/d/yyyy hh:mm:ss tt";
    //String dtString = saveNow.ToString(datePatt);

    // create header and footer
    string header = "Put all information you need to display on the Header";
    string footer = "Print date : " + saveNow.ToString(datePatt);

    Font font = new Font("times new roman", 10, System.Drawing.FontStyle.Regular);
    Brush brush = new SolidBrush(Color.Black);

    // measure them
    SizeF headerSize = e.Graphics.MeasureString(header, font);
    SizeF footerSize = e.Graphics.MeasureString(footer, font);

    // draw header
    RectangleF headerBounds = new RectangleF(marginBounds.Left-80, marginBounds.Top-80, marginBounds.Width, headerSize.Height);
    e.Graphics.DrawString(header, font, brush, headerBounds);

    // draw footer
    RectangleF footerBounds = new RectangleF(marginBounds.Left-80, marginBounds.Bottom - footerSize.Height+80, marginBounds.Width, footerSize.Height);
    e.Graphics.DrawString(footer, font, brush, footerBounds);

    // dispose objects
    font.Dispose();
    brush.Dispose();
}

public void GetPrintArea(Control pnl)
{
    MemoryImage = new Bitmap(pnl.Width, pnl.Height);
    Rectangle rect = new Rectangle(0, 0, pnl.Width, pnl.Height);
    pnl.DrawToBitmap(MemoryImage, new Rectangle(0, 0, pnl.Width, pnl.Height));
}

protected override void OnPaint(PaintEventArgs e)
{
    if (MemoryImage != null)
    {
        e.Graphics.DrawImage(MemoryImage, 0, 0);
        base.OnPaint(e);
    }
}
void PrintDoc_PrintPage(object sender, PrintPageEventArgs e)
{
    Rectangle pageArea = e.PageBounds;
    Rectangle m = e.MarginBounds;

    if ((double)MemoryImage.Width / (double)MemoryImage.Height > (double)m.Width / (double)m.Height) // image is wider
    {
        m.Height = (int)((double)MemoryImage.Height / (double)MemoryImage.Width * (double)m.Width);
    }
    else
    {
        m.Width = (int)((double)MemoryImage.Width / (double)MemoryImage.Height * (double)m.Height);
    }

    e.Graphics.DrawImage(MemoryImage, m);

}

6 - 最后我们假设我们有两个按钮,一个用于打印,另一个用于打印预览.你只需要调用这个函数:

6 -And finally we suppose that we have two buttons, one to Print and the other one to print preview. You just have to call this function :

private void PrintButton_Click(object sender, EventArgs e)
{
    try
    {
        Print(mainDisplay.getCurentPanel());
    }
    catch (Exception exp)
    {
        MessageBox.Show("Error: \n" + exp.Message);
    }
}

private void PrintPreviewButton_Click(object sender, EventArgs e)
{
    try
    {
        PrintPreview(mainDisplay.getCurentPanel());
    }
    catch (Exception exp)
    {
        MessageBox.Show("Error: \n" + exp.Message);
    }
}

希望它会帮助某人:)

祝你好运

这篇关于用 C# 打印控件的整个区域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆