【问题标题】:Printing a scroll-able windows form. [duplicate]打印可滚动的窗体。 [复制]
【发布时间】:2012-12-31 16:23:13
【问题描述】:

可能重复:
How can I take a screenshot of a Winforms control/form in C#?

我有一个包含名称和图片列表的 Windows 窗体。该列表很长,因此有一个滚动面板。现在,我想打印此表单,但我不能,因为打印功能只打印“可见”部分,因为当您向下滚动时会看到不可见部分。那么,有没有办法一次打印整个表格?

【问题讨论】:

  • 这不是一个完全的骗局,但问题本质上是相同的。这里没有简单的解决方案。您要打印的内容实际上并不存在(因为尚未绘制)。在打印之前,您必须滚动表单并捕获多个图像。只打印数据可能更容易,而不必担心与表单完全匹配。
  • @JonB - 好吧,这类问题可以帮助找到这个反复出现的问题的一般答案。可能的重复项似乎并不适用。在 SO 上,“不可能”是唯一的答案吗?

标签: c# .net


【解决方案1】:

在 Visual Basic PowerPacks 工具箱中查找打印表单控件

要打印可滚动表单的完整客户区,试试这个...

1.在工具箱中,单击 Visual Basic PowerPacks 选项卡,然后将 PrintForm 组件拖到窗体上。

PrintForm 组件将被添加到组件托盘中。

2.在“属性”窗口中,将 PrintAction 属性设置为 PrintToPrinter。

3.在适当的事件处理程序中添加以下代码(例如,在打印按钮的 Click 事件处理程序中)。

1.PrintForm1.Print(Me, PowerPacks.Printing.PrintForm.PrintOption.Scrollable)

试一试,让我知道你的效果如何。

【讨论】:

  • 确定我的表单高度不能超过 764,这真的很恶心。我尝试了你的解决方案。它不适用于大型表格。您是否有任何其他方法可以让表单通过滚动获得更高的高度。谢谢
【解决方案2】:

这不是一个完整的答案,但这里有一段代码截取窗体上可滚动面板控件的屏幕截图(位图)。最大的缺点是截屏时屏幕闪烁。我已经在简单的应用程序上对其进行了测试,因此它可能不适用于所有情况,但这可能是一个开始。

这里是如何使用它:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent(); // create a scrollable panel1 component
    }

    private void button1_Click(object sender, EventArgs e)
    {
        TakeScreenshot(panel1, "C:\\mypanel.bmp");
    }
}

这里是实用程序:

    public static void TakeScreenshot(Panel panel, string filePath)
    {
        if (panel == null)
            throw new ArgumentNullException("panel");

        if (filePath == null)
            throw new ArgumentNullException("filePath");

        // get parent form (may not be a direct parent)
        Form form = panel.FindForm();
        if (form == null)
            throw new ArgumentException(null, "panel");

        // remember form position
        int w = form.Width;
        int h = form.Height;
        int l = form.Left;
        int t = form.Top;

        // get panel virtual size
        Rectangle display = panel.DisplayRectangle;

        // get panel position relative to parent form
        Point panelLocation = panel.PointToScreen(panel.Location);
        Size panelPosition = new Size(panelLocation.X - form.Location.X, panelLocation.Y - form.Location.Y);

        // resize form and move it outside the screen
        int neededWidth = panelPosition.Width + display.Width;
        int neededHeight = panelPosition.Height + display.Height;
        form.SetBounds(0, -neededHeight, neededWidth, neededHeight, BoundsSpecified.All);

        // resize panel (useless if panel has a dock)
        int pw = panel.Width;
        int ph = panel.Height;
        panel.SetBounds(0, 0, display.Width, display.Height, BoundsSpecified.Size);

        // render the panel on a bitmap
        try
        {
            Bitmap bmp = new Bitmap(display.Width, display.Height);
            panel.DrawToBitmap(bmp, display);
            bmp.Save(filePath);
        }
        finally
        {
            // restore
            panel.SetBounds(0, 0, pw, ph, BoundsSpecified.Size);
            form.SetBounds(l, t, w, h, BoundsSpecified.All);
        }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-02
    • 1970-01-01
    • 1970-01-01
    • 2016-12-18
    • 2020-01-11
    相关资源
    最近更新 更多