如何使用带有可滚动面板的 PrintDocument?

Posted

技术标签:

【中文标题】如何使用带有可滚动面板的 PrintDocument?【英文标题】:How to use PrintDocument with a scrollable Panel? 【发布时间】:2019-12-06 10:25:33 【问题描述】:

如何将PrintDocument 与可滚动面板一起使用?

这是我的一些代码:

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));

Rectangle pagearea = e.PageBounds;
e.Graphics.DrawImage(MemoryImage, (pagearea.Width / 2) - 
(pannel.Width / 2), pannel.Location.Y);

【问题讨论】:

c# panel for drawing graphics and scrolling的可能重复 欢迎来到 Stack Overflow。请阅读How to Ask 和minimal reproducible example,获取有关如何以清晰、可回答的方式提出问题的建议。请edit your question 包含一个 MCVE,说明您迄今为止尝试过的内容、您拥有的代码、与您希望它执行的操作有何不同以及具体您需要什么帮助与。 @AnantDabhi:您提出的副本与 printing 毫无关系,这个问题显然是要问的。 【参考方案1】:

这些方法集允许将ScrollableControl 的内容打印到位图。

过程说明:

    控件首先滚动回原点(control.AutoScrollPosition = new Point(0, 0);(否则会引发异常:位图大小错误。您可能希望存储当前滚动位置并在之后恢复)。 验证并存储由PreferredSize 或DisplayRectangle 属性返回的容器的实际大小(取决于方法参数设置的条件和打印的容器类型)。此属性考虑容器的全部范围。 这将是位图的大小。 使用容器的背景颜色清除位图。 迭代ScrollableControl.Controls集合并打印所有第一级子控件的相对位置(子控件的Bounds矩形相对于容器ClientArea。) 如果一级控件有子控件,则调用 DrawNestedControls 递归方法,该方法将枚举并绘制所有嵌套的子容器/控件,同时保留内部剪辑边界。

包括对 RichTextBox 控件的支持RichEditPrinter 类包含打印 RichTextBox/RichEdit 控件内容所需的逻辑。该类使用正在打印控件的位图的设备上下文向 RichTextBox 发送EM_FORMATRANGE 消息。 MSDN 文档中提供了更多详细信息:How to Print the Contents of Rich Edit Controls。


ScrollableControlToBitmap() 方法只接受 ScrollableControl 类型作为参数:您不能传递 TextBox 控件,即使它使用 ScrollBars。

▶ 将 fullSize 参数设置为 truefalse 以包含容器内的所有子控件或仅包含可见的子控件。如果设置为 true,则 Container 的 ClientRectangle 将扩展为包含并打印其所有子控件。

▶ 将 includeHidden 参数设置为 truefalse 以包含或排除隐藏控件(如果有)。


注意:此代码使用Control.DeviceDpi 属性来评估容器设备上下文的当前Dpi。此属性需要 .Net Framework 4.7+。如果此版本不可用,您可以删除:

bitmap.SetResolution(canvas.DeviceDpi, canvas.DeviceDpi);

或通过其他方式得出该值。见GetDeviceCaps。 可能,更新项目的框架版本:)


// Prints the content of the current Form instance, 
// include all child controls and also those that are not visible
var bitmap = ControlPrinter.ScrollableControlToBitmap(this, true, true);

// Prints the content of a ScrollableControl inside a Form
// include all child controls except those that are not visible
var bitmap = ControlPrinter.ScrollableControlToBitmap(this.panel1, true, false);
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
using System.Windows.Forms;

public class ControlPrinter

    public static Bitmap ScrollableControlToBitmap(ScrollableControl canvas, bool fullSize, bool includeHidden)
    
        canvas.AutoScrollPosition = new Point(0, 0);
        if (includeHidden) 
            canvas.SuspendLayout();
            foreach (Control child in canvas.Controls) 
                child.Visible = true;
            
            canvas.ResumeLayout(true);
        

        canvas.PerformLayout();
        Size containerSize = canvas.DisplayRectangle.Size;
        if (fullSize) 
            containerSize.Width = Math.Max(containerSize.Width, canvas.ClientSize.Width);
            containerSize.Height = Math.Max(containerSize.Height, canvas.ClientSize.Height);
        
        else 
            containerSize = canvas.ClientSize;;
        
         
        var bitmap = new Bitmap(containerSize.Width, containerSize.Height, PixelFormat.Format32bppArgb);
        bitmap.SetResolution(canvas.DeviceDpi, canvas.DeviceDpi);

        var graphics = Graphics.FromImage(bitmap);
        if (canvas.BackgroundImage != null) 
            graphics.DrawImage(canvas.BackgroundImage, new Rectangle(Point.Empty, containerSize));
        
        else 
            graphics.Clear(canvas.BackColor);
        

        var rtfPrinter = new RichEditPrinter(graphics);

        try 
            DrawNestedControls(canvas, canvas, new Rectangle(Point.Empty, containerSize), bitmap, rtfPrinter);
            return bitmap;
        
        finally 
            rtfPrinter.Dispose();
            graphics.Dispose();
        
    

    private static void DrawNestedControls(Control outerContainer, Control parent, Rectangle parentBounds, Bitmap bitmap, RichEditPrinter rtfPrinter)
    
        for (int i = parent.Controls.Count - 1; i >= 0; i--) 
            var ctl = parent.Controls[i];
            if (!ctl.Visible || (ctl.Width < 1 || ctl.Height < 1)) continue;

            var clipBounds = Rectangle.Empty;
            if (parent.Equals(outerContainer))  clipBounds = ctl.Bounds; 
            else 
                Size scrContainerSize = parentBounds.Size;
                if ((parent != ctl) && parent is ScrollableControl scrctl) 
                    if (scrctl.VerticalScroll.Visible) scrContainerSize.Width -= (SystemInformation.VerticalScrollBarWidth + 1);
                    if (scrctl.HorizontalScroll.Visible) scrContainerSize.Height -= (SystemInformation.HorizontalScrollBarHeight + 1);
                
                clipBounds = Rectangle.Intersect(new Rectangle(Point.Empty, scrContainerSize), ctl.Bounds);
            

            if (clipBounds.Width < 1 || clipBounds.Height < 1) continue;
            var bounds = outerContainer.RectangleToClient(parent.RectangleToScreen(clipBounds));

            if (ctl is RichTextBox rtb) 
                rtfPrinter.DrawRtf(rtb.Rtf, outerContainer.Bounds, bounds, ctl.BackColor);
            
            else 
                ctl.DrawToBitmap(bitmap, bounds);
            
            if (ctl.HasChildren) 
                DrawNestedControls(outerContainer, ctl, clipBounds, bitmap, rtfPrinter);
            
        
    

    internal class RichEditPrinter : IDisposable
    
        Graphics dc = null;
        RTBPrinter rtb = null;

        public RichEditPrinter(Graphics graphics)
        
            this.dc = graphics;
            this.rtb = new RTBPrinter()  ScrollBars = RichTextBoxScrollBars.None ;
        

        public void DrawRtf(string rtf, Rectangle canvas, Rectangle layoutArea, Color color)
        
            rtb.Rtf = rtf;
            rtb.Draw(dc, canvas, layoutArea, color);
            rtb.Clear();
        

        public void Dispose() => this.rtb.Dispose();

        private class RTBPrinter : RichTextBox
        
            public void Draw(Graphics g, Rectangle hdcArea, Rectangle layoutArea, Color color)
            
                using (var brush = new SolidBrush(color)) 
                    g.FillRectangle(brush, layoutArea);
                ;

                IntPtr hdc = g.GetHdc();
                var canvasAreaTwips = new RECT().ToInches(hdcArea);
                var layoutAreaTwips = new RECT().ToInches(layoutArea);

                var formatRange = new FORMATRANGE() 
                    charRange = new CHARRANGE()  cpMax = -1, cpMin = 0 ,
                    hdc = hdc,
                    hdcTarget = hdc,
                    rect = layoutAreaTwips,
                    rectPage = canvasAreaTwips
                ;

                IntPtr lParam = Marshal.AllocCoTaskMem(Marshal.SizeOf(formatRange));
                Marshal.StructureToPtr(formatRange, lParam, false);

                SendMessage(this.Handle, EM_FORMATRANGE, (IntPtr)1, lParam);
                Marshal.FreeCoTaskMem(lParam);
                g.ReleaseHdc(hdc);
            

            [DllImport("User32.dll", CharSet = CharSet.Auto, SetLastError = true)]
            internal static extern int SendMessage(IntPtr hWnd, int uMsg, IntPtr wParam, IntPtr lParam);

            internal const int WM_USER = 0x0400;
            // https://docs.microsoft.com/en-us/windows/win32/controls/em-formatrange
            internal const int EM_FORMATRANGE = WM_USER + 57;

            [StructLayout(LayoutKind.Sequential)]
            internal struct RECT
            
                public int Left;
                public int Top;
                public int Right;
                public int Bottom;

                public Rectangle ToRectangle() => Rectangle.FromLTRB(Left, Top, Right, Bottom);
                public RECT ToInches(Rectangle rectangle)
                
                    float inch = 14.92f;
                    return new RECT() 
                        Left = (int)(rectangle.Left * inch),
                        Top = (int)(rectangle.Top * inch),
                        Right = (int)(rectangle.Right * inch),
                        Bottom = (int)(rectangle.Bottom * inch)
                    ;
                
            

            // https://docs.microsoft.com/en-us/windows/win32/api/richedit/ns-richedit-formatrange?
            [StructLayout(LayoutKind.Sequential)]
            internal struct FORMATRANGE
            
                public IntPtr hdcTarget;      // A HDC for the target device to format for
                public IntPtr hdc;            // A HDC for the device to render to, if EM_FORMATRANGE is being used to send the output to a device
                public RECT rect;             // The area within the rcPage rectangle to render to. Units are measured in twips.
                public RECT rectPage;         // The entire area of a page on the rendering device. Units are measured in twips.
                public CHARRANGE charRange;   // The range of characters to format (see CHARRANGE)
            

            [StructLayout(LayoutKind.Sequential)]
            internal struct CHARRANGE
            
                public int cpMin;           // First character of range (0 for start of doc)
                public int cpMax;           // Last character of range (-1 for end of doc)
            
        
    

这就是它的工作原理:

VB.Net version of the same procedure

【讨论】:

两个小修复:[1] 根据 z-index 绘制子项(首先在 Controls 集合中,最后绘制。)[2]检查孩子是否可见。 @Jimi 在canvas.DeviceDpi 上给了我错误:(找不到DeviceDpi @Inside Man Control.DeviceDpi 需要 .Net Framework 4.7+。是时候更新了:)但我会添加注释。您可以同时删除bitmap.SetResolution(canvas.DeviceDpi, canvas.DeviceDpi);,这不是严格必要的。如果你的应用是 DpiAware,它就是。

以上是关于如何使用带有可滚动面板的 PrintDocument?的主要内容,如果未能解决你的问题,请参考以下文章

如何使用角度和离子制作带有固定标题的可滚动表格

如何在不给定高度的情况下使内容在弹性框中可滚动?

禁用带有复选框的网格面板

如何限制图例大小并使其可使用饼图滚动?和 javafx 布局

C#将所有可滚动面板保存为图像

如何使带有选项卡视图的视图可滚动?