Advertisement
Jimi2000

RichTexBox page Reader

Apr 23rd, 2018
694
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 19.25 KB | None | 0 0
  1.     partial class Form1
  2.     {
  3.         private System.ComponentModel.IContainer components = null;
  4.  
  5.         protected override void Dispose(bool disposing)
  6.         {
  7.             if (disposing && (components != null))
  8.             {
  9.                 components.Dispose();
  10.             }
  11.             base.Dispose(disposing);
  12.         }
  13.  
  14.         private void InitializeComponent()
  15.         {
  16.             this.btnOpenFile = new System.Windows.Forms.Button();
  17.             this.lblPages = new System.Windows.Forms.Label();
  18.             this.btnPagePrev = new System.Windows.Forms.Button();
  19.             this.btnPageFirst = new System.Windows.Forms.Button();
  20.             this.btnPageLast = new System.Windows.Forms.Button();
  21.             this.btnPageNext = new System.Windows.Forms.Button();
  22.             this.SuspendLayout();
  23.             //
  24.             // btnOpenFile
  25.             //
  26.             this.btnOpenFile.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(128)))), ((int)(((byte)(0)))));
  27.             this.btnOpenFile.FlatAppearance.MouseDownBackColor = System.Drawing.Color.FromArgb(((int)(((byte)(128)))), ((int)(((byte)(64)))), ((int)(((byte)(0)))));
  28.             this.btnOpenFile.FlatAppearance.MouseOverBackColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(192)))), ((int)(((byte)(128)))));
  29.             this.btnOpenFile.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
  30.             this.btnOpenFile.Font = new System.Drawing.Font("Segoe UI", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
  31.             this.btnOpenFile.ForeColor = System.Drawing.Color.Black;
  32.             this.btnOpenFile.Location = new System.Drawing.Point(12, 12);
  33.             this.btnOpenFile.Name = "btnOpenFile";
  34.             this.btnOpenFile.Size = new System.Drawing.Size(122, 28);
  35.             this.btnOpenFile.TabIndex = 2;
  36.             this.btnOpenFile.Text = "Open Document";
  37.             this.btnOpenFile.UseVisualStyleBackColor = false;
  38.             this.btnOpenFile.Click += new System.EventHandler(this.btnOpenFile_Click);
  39.             //
  40.             // lblPages
  41.             //
  42.             this.lblPages.BackColor = System.Drawing.Color.Transparent;
  43.             this.lblPages.Font = new System.Drawing.Font("Segoe UI", 12.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
  44.             this.lblPages.ForeColor = System.Drawing.Color.Black;
  45.             this.lblPages.Location = new System.Drawing.Point(301, 16);
  46.             this.lblPages.Name = "lblPages";
  47.             this.lblPages.Size = new System.Drawing.Size(41, 27);
  48.             this.lblPages.TabIndex = 4;
  49.             this.lblPages.Text = "0";
  50.             this.lblPages.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
  51.             //
  52.             // btnPagePrev
  53.             //
  54.             this.btnPagePrev.Enabled = false;
  55.             this.btnPagePrev.FlatAppearance.BorderColor = System.Drawing.SystemColors.Control;
  56.             this.btnPagePrev.FlatAppearance.BorderSize = 0;
  57.             this.btnPagePrev.FlatAppearance.MouseDownBackColor = System.Drawing.SystemColors.Control;
  58.             this.btnPagePrev.FlatAppearance.MouseOverBackColor = System.Drawing.SystemColors.Control;
  59.             this.btnPagePrev.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
  60.             this.btnPagePrev.Font = new System.Drawing.Font("Segoe UI Symbol", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
  61.             this.btnPagePrev.ForeColor = System.Drawing.Color.Black;
  62.             this.btnPagePrev.Location = new System.Drawing.Point(275, 14);
  63.             this.btnPagePrev.Name = "btnPagePrev";
  64.             this.btnPagePrev.Size = new System.Drawing.Size(25, 27);
  65.             this.btnPagePrev.TabIndex = 5;
  66.             this.btnPagePrev.Text = "◀";
  67.             this.btnPagePrev.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
  68.             this.btnPagePrev.UseVisualStyleBackColor = true;
  69.             this.btnPagePrev.Click += new System.EventHandler(this.btnPagePrev_Click);
  70.             //
  71.             // btnPageFirst
  72.             //
  73.             this.btnPageFirst.Enabled = false;
  74.             this.btnPageFirst.FlatAppearance.BorderColor = System.Drawing.SystemColors.Control;
  75.             this.btnPageFirst.FlatAppearance.BorderSize = 0;
  76.             this.btnPageFirst.FlatAppearance.MouseDownBackColor = System.Drawing.SystemColors.Control;
  77.             this.btnPageFirst.FlatAppearance.MouseOverBackColor = System.Drawing.SystemColors.Control;
  78.             this.btnPageFirst.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
  79.             this.btnPageFirst.Font = new System.Drawing.Font("Segoe UI Symbol", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
  80.             this.btnPageFirst.ForeColor = System.Drawing.Color.Black;
  81.             this.btnPageFirst.Location = new System.Drawing.Point(244, 11);
  82.             this.btnPageFirst.Name = "btnPageFirst";
  83.             this.btnPageFirst.Size = new System.Drawing.Size(25, 27);
  84.             this.btnPageFirst.TabIndex = 6;
  85.             this.btnPageFirst.Text = "⏪";
  86.             this.btnPageFirst.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
  87.             this.btnPageFirst.UseVisualStyleBackColor = true;
  88.             this.btnPageFirst.Click += new System.EventHandler(this.btnPageFirst_Click);
  89.             //
  90.             // btnPageLast
  91.             //
  92.             this.btnPageLast.Enabled = false;
  93.             this.btnPageLast.FlatAppearance.BorderColor = System.Drawing.SystemColors.Control;
  94.             this.btnPageLast.FlatAppearance.BorderSize = 0;
  95.             this.btnPageLast.FlatAppearance.MouseDownBackColor = System.Drawing.SystemColors.Control;
  96.             this.btnPageLast.FlatAppearance.MouseOverBackColor = System.Drawing.SystemColors.Control;
  97.             this.btnPageLast.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
  98.             this.btnPageLast.Font = new System.Drawing.Font("Segoe UI Symbol", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
  99.             this.btnPageLast.ForeColor = System.Drawing.Color.Black;
  100.             this.btnPageLast.Location = new System.Drawing.Point(370, 11);
  101.             this.btnPageLast.Name = "btnPageLast";
  102.             this.btnPageLast.Size = new System.Drawing.Size(25, 27);
  103.             this.btnPageLast.TabIndex = 8;
  104.             this.btnPageLast.Text = "⏩";
  105.             this.btnPageLast.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
  106.             this.btnPageLast.UseVisualStyleBackColor = true;
  107.             this.btnPageLast.Click += new System.EventHandler(this.btnPageLast_Click);
  108.             //
  109.             // btnPageNext
  110.             //
  111.             this.btnPageNext.Enabled = false;
  112.             this.btnPageNext.FlatAppearance.BorderColor = System.Drawing.SystemColors.Control;
  113.             this.btnPageNext.FlatAppearance.BorderSize = 0;
  114.             this.btnPageNext.FlatAppearance.MouseDownBackColor = System.Drawing.SystemColors.Control;
  115.             this.btnPageNext.FlatAppearance.MouseOverBackColor = System.Drawing.SystemColors.Control;
  116.             this.btnPageNext.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
  117.             this.btnPageNext.Font = new System.Drawing.Font("Segoe UI Symbol", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
  118.             this.btnPageNext.ForeColor = System.Drawing.Color.Black;
  119.             this.btnPageNext.Location = new System.Drawing.Point(339, 14);
  120.             this.btnPageNext.Name = "btnPageNext";
  121.             this.btnPageNext.Size = new System.Drawing.Size(25, 27);
  122.             this.btnPageNext.TabIndex = 7;
  123.             this.btnPageNext.Text = "▶";
  124.             this.btnPageNext.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
  125.             this.btnPageNext.UseVisualStyleBackColor = true;
  126.             this.btnPageNext.Click += new System.EventHandler(this.btnPageNext_Click);
  127.             //
  128.             // Form1
  129.             //
  130.             this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
  131.             this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
  132.             this.ClientSize = new System.Drawing.Size(412, 290);
  133.             this.Controls.Add(this.btnPageLast);
  134.             this.Controls.Add(this.btnPageNext);
  135.             this.Controls.Add(this.btnPageFirst);
  136.             this.Controls.Add(this.btnPagePrev);
  137.             this.Controls.Add(this.lblPages);
  138.             this.Controls.Add(this.btnOpenFile);
  139.             this.Name = "Form1";
  140.             this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
  141.             this.Text = "Form1";
  142.             this.ResumeLayout(false);
  143.         }
  144.  
  145.         private System.Windows.Forms.Button btnOpenFile;
  146.         private System.Windows.Forms.Label lblPages;
  147.         private System.Windows.Forms.Button btnPagePrev;
  148.         private System.Windows.Forms.Button btnPageFirst;
  149.         private System.Windows.Forms.Button btnPageLast;
  150.         private System.Windows.Forms.Button btnPageNext;
  151.     }
  152.  
  153. -----------------------------------------------------------------------------------------------------------------------------
  154.     using System;
  155.     using System.Collections.Generic;
  156.     using System.Drawing;
  157.     using System.Drawing.Text;
  158.     using System.IO;
  159.     using System.Linq;
  160.     using System.Runtime.InteropServices;
  161.     using System.Text;
  162.     using System.Windows.Forms;
  163.  
  164.     public partial class Form1 : Form
  165.     {
  166.         public Form1()
  167.         {
  168.             InitializeComponent();
  169.         }
  170.  
  171.         public class RTBReader : RichTextBox
  172.         {
  173.             [DllImport("user32", CharSet = CharSet.Auto)]
  174.             private static extern int SendMessage(HandleRef hWnd, int _msg, int wParam, ref PARAFORMAT2 _pf);
  175.  
  176.             [DllImport("user32", CharSet = CharSet.Auto)]
  177.             private static extern int SendMessage(HandleRef hWnd, int _msg, int wParam, int lParam);
  178.  
  179.             public enum TextAlignment
  180.             {
  181.                 Left = 1,
  182.                 Right,
  183.                 Center,
  184.                 Justify
  185.             }
  186.  
  187.             private string text;
  188.  
  189.             public RTBReader()
  190.                 : this(null, Color.Black, Color.White) { }
  191.             public RTBReader(Font Font, Color Forecolor, Color Backcolor)
  192.             {
  193.                 this.Name = "Reader";
  194.                 this.ScrollBars = RichTextBoxScrollBars.None;
  195.                 this.SelectionAlignment = TextAlignment.Justify;
  196.                 this.Font = Font ?? new Font("Segoe UI", 10F, FontStyle.Regular, GraphicsUnit.Point);
  197.                 this.ForeColor = Forecolor;
  198.                 this.BackColor = Backcolor;
  199.             }
  200.  
  201.             public List<string> Pages { get; private set; }
  202.             public string DocumentText
  203.             {
  204.                 get => this.text;
  205.                 set => this.Initialize(value);
  206.             }
  207.  
  208.             public void ReaderPage(int Page) => Text = Pages[Page];
  209.  
  210.             private void Initialize(string Document)
  211.             {
  212.                 if (string.IsNullOrEmpty(Document))
  213.                 {
  214.                     this.text = string.Empty;
  215.                     return;
  216.                 }
  217.  
  218.                 this.text = Document;
  219.                 this.Pages = new List<string>();
  220.                 TextFormatFlags flags = TextFormatFlags.Top | TextFormatFlags.Left |
  221.                                         TextFormatFlags.WordBreak | TextFormatFlags.TextBoxControl |
  222.                                         TextFormatFlags.NoPadding;
  223.  
  224.  
  225.                 Size textSize = TextRenderer.MeasureText(this.text, this.Font, this.ClientSize, flags);
  226.                 int NumberOfPages = textSize.Height / this.ClientSize.Height;
  227.  
  228.                 if (textSize.Height > this.Height)
  229.                 {
  230.                     this.Text = Document;
  231.                     this.Update();
  232.  
  233.                     //Number of shown lines
  234.                     int FirstCharOfLastShownLine  = this.GetCharIndexFromPosition(new Point(0, this.ClientSize.Height));
  235.                     int ShownLines = this.GetLineFromCharIndex(FirstCharOfLastShownLine);
  236.                     int TotalLines = this.GetLineFromCharIndex(this.Text.Length - 1);
  237.  
  238.  
  239.                     for (int p = 0; p < NumberOfPages; p++)
  240.                     {
  241.                         int FirstLineOfPage = (p * ShownLines);
  242.                         int FirstCharOfPage = this.GetFirstCharIndexFromLine(FirstLineOfPage);
  243.  
  244.                         int FirstLineOfNextPage = (p + 1) * ShownLines;
  245.                         FirstLineOfNextPage = (FirstLineOfNextPage > TotalLines) ? TotalLines : FirstLineOfNextPage;
  246.                         int LastCharOfPage = (FirstLineOfNextPage < TotalLines)
  247.                                            ? this.GetFirstCharIndexFromLine(FirstLineOfNextPage) - 1
  248.                                            : this.Text.Length;
  249.                         this.Pages.Add(this.Text.Substring(FirstCharOfPage, LastCharOfPage - FirstCharOfPage));
  250.                     }
  251.                 }
  252.                 else
  253.                 {
  254.                     this.Pages.Add(this.text);
  255.                 }
  256.                 this.Text = this.Pages.First();
  257.             }
  258.  
  259.             private const int EM_SETEVENTMASK = 1073;
  260.             private const int EM_GETPARAFORMAT = 1085;
  261.             private const int EM_SETPARAFORMAT = 1095;
  262.             private const int EM_SETTYPOGRAPHYOPTIONS = 1226;
  263.             private const int WM_SETREDRAW = 11;
  264.             private const int TO_ADVANCEDTYPOGRAPHY = 0x1;
  265.             private const int PFM_ALIGNMENT = 8;
  266.             private const int SCF_SELECTION = 1;
  267.  
  268.             [StructLayout(LayoutKind.Sequential)]
  269.             private struct PARAFORMAT2
  270.             {
  271.                 public int cbSize;
  272.                 public uint dwMask;
  273.                 public short wNumbering;
  274.                 public short wReserved;
  275.                 public int dxStartIndent;
  276.                 public int dxRightIndent;
  277.                 public int dxOffset;
  278.                 public short wAlignment;
  279.                 public short cTabCount;
  280.                 [MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)]
  281.                 public int[] rgxTabs;
  282.                 public int dySpaceBefore;
  283.                 public int dySpaceAfter;
  284.                 public int dyLineSpacing;
  285.                 public short sStyle;
  286.                 public byte bLineSpacingRule;
  287.                 public byte bOutlineLevel;
  288.                 public short wShadingWeight;
  289.                 public short wShadingStyle;
  290.                 public short wNumberingStart;
  291.                 public short wNumberingStyle;
  292.                 public short wNumberingTab;
  293.                 public short wBorderSpace;
  294.                 public short wBorderWidth;
  295.                 public short wBorders;
  296.             }
  297.  
  298.             public new TextAlignment SelectionAlignment
  299.             {
  300.                 get
  301.                 {
  302.                     PARAFORMAT2 _pf = new PARAFORMAT2();
  303.                     _pf.cbSize = Marshal.SizeOf(_pf);
  304.  
  305.                     SendMessage(new HandleRef(this, Handle), EM_GETPARAFORMAT, SCF_SELECTION, ref _pf);
  306.  
  307.                     // If 0, defaults to TextAlignment.Left
  308.                     if ((_pf.dwMask & PFM_ALIGNMENT) == 0) return TextAlignment.Left;
  309.                     return (TextAlignment)_pf.wAlignment;
  310.                 }
  311.  
  312.                 set
  313.                 {
  314.                     PARAFORMAT2 _pf = new PARAFORMAT2();
  315.                     _pf.cbSize = Marshal.SizeOf(_pf);
  316.                     _pf.dwMask = PFM_ALIGNMENT;
  317.                     _pf.wAlignment = (short)value;
  318.  
  319.                     SendMessage(new HandleRef(this, Handle), EM_SETPARAFORMAT, SCF_SELECTION, ref _pf);
  320.                 }
  321.             }
  322.  
  323.             // Overrides OnHandleCreated to enable RTB advances options
  324.             protected override void OnHandleCreated(EventArgs e)
  325.             {
  326.                 base.OnHandleCreated(e);
  327.  
  328.                 // EM_SETTYPOGRAPHYOPTIONS allows to enable RTB (RichEdit) Advanced Typography
  329.                 SendMessage(new HandleRef(this, Handle), EM_SETTYPOGRAPHYOPTIONS,
  330.                                                          TO_ADVANCEDTYPOGRAPHY,
  331.                                                          TO_ADVANCEDTYPOGRAPHY);
  332.             }
  333.         }
  334.  
  335.         RTBReader DocumentViewer = null;
  336.         private int CurrentPage = 0;
  337.         private int FirstPage = 0;
  338.         private int LastPage = 0;
  339.         private void btnOpenFile_Click(object sender, EventArgs e)
  340.         {
  341.             DocumentViewer = new RTBReader();
  342.             string Document = File.ReadAllText(@"..\..\Resources\Document.txt", Encoding.Default);
  343.             DocumentViewer.Location = new Point(12, 46);
  344.             DocumentViewer.Size = new Size(385, 230);
  345.             DocumentViewer.DocumentText = Document;
  346.             this.Controls.Add(DocumentViewer);
  347.             this.lblPages.Text = "1/" + DocumentViewer.Pages.Count.ToString();
  348.             LastPage = DocumentViewer.Pages.Count - 1;
  349.             btnPageNext.Enabled = true;
  350.             btnPageLast.Enabled = true;
  351.         }
  352.  
  353.         private void btnPageNext_Click(object sender, EventArgs e)
  354.         {
  355.             if (CurrentPage < LastPage)
  356.             {
  357.                 btnPageFirst.Enabled = true;
  358.                 btnPagePrev.Enabled = true;
  359.  
  360.                 CurrentPage += 1;
  361.                 DocumentViewer.ReaderPage(CurrentPage);
  362.                 this.lblPages.Text = (CurrentPage + 1).ToString() + "/" + DocumentViewer.Pages.Count.ToString();
  363.  
  364.                 if (CurrentPage == LastPage)
  365.                 {
  366.                     btnPageNext.Enabled = false;
  367.                     btnPageLast.Enabled = false;
  368.                 }
  369.             }
  370.         }
  371.  
  372.         private void btnPagePrev_Click(object sender, EventArgs e)
  373.         {
  374.             if (CurrentPage > FirstPage)
  375.             {
  376.                 btnPageLast.Enabled = true;
  377.                 btnPageNext.Enabled = true;
  378.  
  379.                 CurrentPage -= 1;
  380.                 DocumentViewer.ReaderPage(CurrentPage);
  381.                 this.lblPages.Text = (CurrentPage + 1).ToString() + "/" + DocumentViewer.Pages.Count.ToString();
  382.                 if (CurrentPage == 0)
  383.                 {
  384.                     btnPagePrev.Enabled = false;
  385.                     btnPageFirst.Enabled = false;
  386.                 }
  387.             }
  388.         }
  389.  
  390.         private void btnPageFirst_Click(object sender, EventArgs e)
  391.         {
  392.             btnPagePrev.Enabled = false;
  393.             btnPageFirst.Enabled = false;
  394.             btnPageNext.Enabled = true;
  395.             btnPageLast.Enabled = true;
  396.             CurrentPage = FirstPage;
  397.             DocumentViewer.ReaderPage(FirstPage);
  398.             this.lblPages.Text = (CurrentPage + 1).ToString() + "/" + DocumentViewer.Pages.Count.ToString();
  399.         }
  400.  
  401.         private void btnPageLast_Click(object sender, EventArgs e)
  402.         {
  403.             btnPageNext.Enabled = false;
  404.             btnPageLast.Enabled = false;
  405.             btnPagePrev.Enabled = true;
  406.             btnPageFirst.Enabled = true;
  407.             CurrentPage = LastPage;
  408.             DocumentViewer.ReaderPage(LastPage);
  409.             this.lblPages.Text = (CurrentPage + 1).ToString() + "/" + DocumentViewer.Pages.Count.ToString();
  410.         }
  411.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement