C#でRTF形式データの印刷処理を作成したい

Jugemu 20 評価のポイント
2024-01-28T01:46:41.22+00:00

TXT形式データの印刷処理を下記のように記述し、結果を得ました。次にRTF形式データの印刷処理を作りたいと考え、RichTextBoxクラスのRtfプロパティを使用することを考えましたが、データの受け渡し先クラス・方法がわかりません。printDocument、Graphics、Streamクラス、その他を当たってみましたがはっきりしませんでした。どなたかご存知の方がいらっしゃいましたらご教示いただけないでしょうか。よろしくお願いします。

{
Windows 10
Windows 10
パーソナル コンピューターとタブレットで実行される Microsoft オペレーティング システム。
53 件の質問
{count} 件の投票

承認済みの回答
  1. gekka 6,926 評価のポイント MVP
    2024-01-28T16:56:18.82+00:00

    .NET RichTextBox から WYSIWYG 印刷結果を取得する」を改造して、

    namespace WinFormsApp1
    {
        using System;
        using System.Windows.Forms;
        using System.Drawing;
    
        public partial class Form1 : System.Windows.Forms.Form
        {
            private RichTextBox richText1;
            private Button button1;
    
            public Form1()
            {
                InitializeComponent();
    
                button1 = new Button();
                button1.Text = "Test";
                button1.Click += button1_Click;
                this.Controls.Add(button1);
    
    
                this.richText1 = new RichTextBox();
                this.richText1.Location = new Point(0, button1.Height);
                this.richText1.Size = new Size(this.ClientSize.Width, this.ClientSize.Height - this.button1.Height);
                this.richText1.Anchor = AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top | AnchorStyles.Bottom;
                this.Controls.Add(richText1);
    
                var data = Clipboard.GetDataObject();
                if (data.GetDataPresent(DataFormats.Rtf))
                {
                    var oRtf = data.GetData(DataFormats.Rtf);
                    if (oRtf is string rtf)
                    {
                        this.richText1.Rtf = rtf;
                    }
                }
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                PrintDialog dlg = new PrintDialog();
                if (dlg.ShowDialog() == DialogResult.OK)
                {
                    System.Drawing.Printing.PrintDocument printDoc
                    = new System.Drawing.Printing.PrintDocument();
    
                    printDoc.PrinterSettings = dlg.PrinterSettings;
    
                    var richPrint = new RichTextBoxPrint(this.richText1);
                    richPrint.PrintRichTextContents(printDoc);
                }
            }
        }
    }
    
    namespace WinFormsApp1
    {
        using System;
        using System.Windows.Forms;
        using System.Runtime.InteropServices;
        using System.Drawing.Printing;
    
        public class RichTextBoxPrint
        {
            public RichTextBoxPrint(RichTextBox rich)
            {
                this.richTextBox = rich;
            }
    
            private RichTextBox richTextBox;
            private int m_nFirstCharOnPage;
    
            public void PrintRichTextContents(PrintDocument printDoc)
            {
                printDoc.BeginPrint += new PrintEventHandler(printDoc_BeginPrint);
                printDoc.PrintPage += new PrintPageEventHandler(printDoc_PrintPage);
                try
                {
                    printDoc.Print();
                }
                finally
                {
                    printDoc.BeginPrint -= new PrintEventHandler(printDoc_BeginPrint);
                    printDoc.PrintPage -= new PrintPageEventHandler(printDoc_PrintPage);
                }
            }
    
    
            private void printDoc_BeginPrint(object sender, System.Drawing.Printing.PrintEventArgs e)
            {
                m_nFirstCharOnPage = 0;
            }
    
            private void printDoc_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
            {
                m_nFirstCharOnPage = this.FormatRange(false, e, m_nFirstCharOnPage, this.richTextBox.TextLength);
                e.HasMorePages = (m_nFirstCharOnPage < this.richTextBox.TextLength);
            }
    
    
            private int FormatRange(bool measureOnly, PrintPageEventArgs e, int charFrom, int charTo)
            {
                STRUCT_CHARRANGE cr;
                cr.cpMin = charFrom;
                cr.cpMax = charTo;
    
                STRUCT_RECT rc;
                rc.top = HundredthInchToTwips(e.MarginBounds.Top);
                rc.bottom = HundredthInchToTwips(e.MarginBounds.Bottom);
                rc.left = HundredthInchToTwips(e.MarginBounds.Left);
                rc.right = HundredthInchToTwips(e.MarginBounds.Right);
    
                STRUCT_RECT rcPage;
                rcPage.top = HundredthInchToTwips(e.PageBounds.Top);
                rcPage.bottom = HundredthInchToTwips(e.PageBounds.Bottom);
                rcPage.left = HundredthInchToTwips(e.PageBounds.Left);
                rcPage.right = HundredthInchToTwips(e.PageBounds.Right);
                IntPtr hdc = e.Graphics.GetHdc();
    
                STRUCT_FORMATRANGE fr;
                fr.chrg = cr;
                fr.hdc = hdc;
                fr.hdcTarget = hdc;
                fr.rc = rc;
                fr.rcPage = rcPage;
    
                Int32 wParam = (measureOnly ? 0 : 1);
                IntPtr lParam = Marshal.AllocCoTaskMem(Marshal.SizeOf(fr));
                Marshal.StructureToPtr(fr, lParam, false);
                int res = SendMessage(this.richTextBox.Handle, EM_FORMATRANGE, wParam, lParam);
                Marshal.FreeCoTaskMem(lParam);
                e.Graphics.ReleaseHdc(hdc);
    
                return res;
            }
    
            private Int32 HundredthInchToTwips(int n)
            {
                return (Int32)(n * 14.4);
            }
    
            #region Win32
            [StructLayout(LayoutKind.Sequential)]
            private struct STRUCT_RECT
            {
                public Int32 left;
                public Int32 top;
                public Int32 right;
                public Int32 bottom;
            }
    
            [StructLayout(LayoutKind.Sequential)]
            private struct STRUCT_CHARRANGE
            {
                public Int32 cpMin;
                public Int32 cpMax;
            }
    
            [StructLayout(LayoutKind.Sequential)]
            private struct STRUCT_FORMATRANGE
            {
                public IntPtr hdc;
                public IntPtr hdcTarget;
                public STRUCT_RECT rc;
                public STRUCT_RECT rcPage;
                public STRUCT_CHARRANGE chrg;
            }
    
            [DllImport("user32.dll")]
            private static extern Int32 SendMessage(IntPtr hWnd, Int32 msg, Int32 wParam, IntPtr lParam);
    
            private const Int32 WM_USER = 0x400;
            private const Int32 EM_FORMATRANGE = WM_USER + 57;
    
            #endregion
        }
    }
    
    1 人がこの回答が役に立ったと思いました。

0 件の追加の回答

並べ替え方法: 最も役に立つ