Winforms - 如何打印整个表格,包括看不见的部分? [英] Winforms - how to print the whole of a form, including unseen parts?

查看:17
本文介绍了Winforms - 如何打印整个表格,包括看不见的部分?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用 C# 编写的 Windows 窗体项目.主窗体上有一个 TabControl,并且要求其中一位用户能够打印 TabPages 之一.表单很长,我使用垂直滚动条.需要能够打印整个表单.

I have a windows forms project written in C#. The main form has a TabControl on it and there is a requirement for one of the users to be able to print one of the TabPages. The form is very long and I use a vertical scroll bar. The whole of the form needs to be able to be printed.

我曾尝试先使用 DrawToBitmap 方法转换为位图,但这只会包括用户可以看到的表单部分.我尝试过的其他一些解决方案涉及屏幕捕获,也有同样的问题.

I have tried using the DrawToBitmap method to convert to a bitmap first, but this will only include the portion of the form that the user can see. Some other solutions I have tried involve screen capturing, which has the same issue.

如何打印或获取整个标签页的图像,包括用户仅在向下滚动时才能看到的部分?

How can I print out, or get an image, of the whole of the tab page, including the parts the user only sees when they scroll down?

推荐答案

这对于任何控件都相当简单,包括 TabControlsTabPages 但不包括 Forms.

This is rather simple for any control including TabControls and TabPages but not Forms.

您需要做的就是将相关控件放大到足以显示其所有内容.(它们不必必须在屏幕上可见.)

All you need to do is enlarge the relevant controls enough to show all their content. (They don't have to be actually visible on screen.)

这是一个例子:

tabControl1.Height = 10080;
tabPage2.Height = 10050;
dataGridView1.Height = 10000;

dataGridView1.Rows.Add(3000);
for (int i = 0; i < dataGridView1.Rows.Count; i++)  dataGridView1[0, i].Value = i;

using (Bitmap bmp = new Bitmap(tabControl1.Width , tabControl1.Height ))
{
    tabControl1.DrawToBitmap(bmp, tabControl1.ClientRectangle);
    bmp.Save("D:\xxxx.png", ImageFormat.Png);
}

这将保存 DataGridViewTabPageTabControl 的全部内容..

This saves the full content of the DataGridView, the TabPage and the TabControl..

注意:这不适用于不能超过屏幕尺寸的表单..

Note: that this will not work with forms, which can't much exceed the screen dimensions..

更新:这里的代码通过将几个位图修补在一起来保存具有垂直滚动的表单.当然,它也可以扩展为包括水平滚动.我已经为更大的面板编写了一个类似的解决方案 此处.

Update: Here is code that saves a form with vertical scrolling by patching several bitmaps together. It can, of course be expanded to include horizontal scrolling as well. I have coded a similar solution for larger Panels here.

static void saveLargeForm(Form form, string fileName)
{
    // yes it may take a while
    form.Cursor = Cursors.WaitCursor;

    // allocate target bitmap and a buffer bitmap
    Bitmap target = new Bitmap(form.DisplayRectangle.Width, form.DisplayRectangle.Height);
    Bitmap buffer = new Bitmap(form.Width, form.Height);
    // the vertical pointer
    int y = 0;
    var vsc = form.VerticalScroll;
    vsc.Value = 0;
    form.AutoScrollPosition = new Point(0, 0);
    // the scroll amount
    int l = vsc.LargeChange;

    Rectangle srcRect = ClientBounds(form);
    Rectangle destRect = Rectangle.Empty;
    bool done = false;

    // we'll draw onto the large bitmap with G
    using (Graphics G = Graphics.FromImage(target))
    {
        while (!done)
        {
            destRect = new Rectangle(0, y, srcRect.Width, srcRect.Height);
            form.DrawToBitmap(buffer, new Rectangle(0, 0, form.Width, form.Height));
            G.DrawImage(buffer, destRect, srcRect, GraphicsUnit.Pixel);   
            int v = vsc.Value;
            vsc.Value = vsc.Value + l;
            form.AutoScrollPosition = new Point(form.AutoScrollPosition.X, vsc.Value + l);
            int delta = vsc.Value - v;
            done = delta < l;
            y += delta;
        }
        destRect = new Rectangle(0, y, srcRect.Width, srcRect.Height);
        form.DrawToBitmap(buffer, new Rectangle(0, 0, form.Width, form.Height));
        G.DrawImage(buffer, destRect, srcRect, GraphicsUnit.Pixel);
    }
    // write result to disc and clean up
    target.Save(fileName, System.Drawing.Imaging.ImageFormat.Png);
    target.Dispose();   
    buffer.Dispose();      
    GC.Collect();          // not sure why, but it helped
    form.Cursor = Cursors.Default;    
}

它利用辅助函数来确定虚拟客户端矩形的净大小,即不包括边框、标题和滚动条:

It makes use of a helper function to determine the the net size of the virtual client rectangle, ie excluding borders, title and scrollbar:

static Rectangle ClientBounds(Form f)
{
    Rectangle rc = f.ClientRectangle;
    Rectangle rb = f.Bounds;
    int sw = SystemInformation.VerticalScrollBarWidth;
    var vsc = f.VerticalScroll;
    int bw = (rb.Width - rc.Width - (vsc.Visible ? sw : 0) ) / 2;
    int th = (rb.Height - rc.Height) - bw * 2;
    return new Rectangle(bw, th + bw, rc.Width, rc.Height );
}

这篇关于Winforms - 如何打印整个表格,包括看不见的部分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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