gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 03:12:20 27 4
gpt4 key购买 nike

如何将 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);

最佳答案

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

程序说明:

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

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


ScrollableControlToBitmap() 方法仅采用 ScrollableControl 类型作为参数:您无法传递 TextBox 控件,即使它使用 ScrollBars。

▶ 将 fullSize 参数设置为 truefalse 以包含容器内的所有子控件或仅包含那些可见的。如果设置为 true,容器的 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://learn.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://learn.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)
}
}
}
}

它是这样工作的:

Scrollable Control Draw To Bitmap

VB.Net version of the same procedure

关于c# - 如何将 PrintDocument 与可滚动面板一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57247750/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com