Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- partial class Form1
- {
- private System.ComponentModel.IContainer components = null;
- protected override void Dispose(bool disposing)
- {
- if (disposing && (components != null))
- {
- components.Dispose();
- }
- base.Dispose(disposing);
- }
- private void InitializeComponent()
- {
- this.btnOpenFile = new System.Windows.Forms.Button();
- this.lblPages = new System.Windows.Forms.Label();
- this.btnPagePrev = new System.Windows.Forms.Button();
- this.btnPageFirst = new System.Windows.Forms.Button();
- this.btnPageLast = new System.Windows.Forms.Button();
- this.btnPageNext = new System.Windows.Forms.Button();
- this.SuspendLayout();
- //
- // btnOpenFile
- //
- this.btnOpenFile.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(128)))), ((int)(((byte)(0)))));
- this.btnOpenFile.FlatAppearance.MouseDownBackColor = System.Drawing.Color.FromArgb(((int)(((byte)(128)))), ((int)(((byte)(64)))), ((int)(((byte)(0)))));
- this.btnOpenFile.FlatAppearance.MouseOverBackColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(192)))), ((int)(((byte)(128)))));
- this.btnOpenFile.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
- this.btnOpenFile.Font = new System.Drawing.Font("Segoe UI", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
- this.btnOpenFile.ForeColor = System.Drawing.Color.Black;
- this.btnOpenFile.Location = new System.Drawing.Point(12, 12);
- this.btnOpenFile.Name = "btnOpenFile";
- this.btnOpenFile.Size = new System.Drawing.Size(122, 28);
- this.btnOpenFile.TabIndex = 2;
- this.btnOpenFile.Text = "Open Document";
- this.btnOpenFile.UseVisualStyleBackColor = false;
- this.btnOpenFile.Click += new System.EventHandler(this.btnOpenFile_Click);
- //
- // lblPages
- //
- this.lblPages.BackColor = System.Drawing.Color.Transparent;
- this.lblPages.Font = new System.Drawing.Font("Segoe UI", 12.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
- this.lblPages.ForeColor = System.Drawing.Color.Black;
- this.lblPages.Location = new System.Drawing.Point(301, 16);
- this.lblPages.Name = "lblPages";
- this.lblPages.Size = new System.Drawing.Size(41, 27);
- this.lblPages.TabIndex = 4;
- this.lblPages.Text = "0";
- this.lblPages.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
- //
- // btnPagePrev
- //
- this.btnPagePrev.Enabled = false;
- this.btnPagePrev.FlatAppearance.BorderColor = System.Drawing.SystemColors.Control;
- this.btnPagePrev.FlatAppearance.BorderSize = 0;
- this.btnPagePrev.FlatAppearance.MouseDownBackColor = System.Drawing.SystemColors.Control;
- this.btnPagePrev.FlatAppearance.MouseOverBackColor = System.Drawing.SystemColors.Control;
- this.btnPagePrev.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
- this.btnPagePrev.Font = new System.Drawing.Font("Segoe UI Symbol", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
- this.btnPagePrev.ForeColor = System.Drawing.Color.Black;
- this.btnPagePrev.Location = new System.Drawing.Point(275, 14);
- this.btnPagePrev.Name = "btnPagePrev";
- this.btnPagePrev.Size = new System.Drawing.Size(25, 27);
- this.btnPagePrev.TabIndex = 5;
- this.btnPagePrev.Text = "◀";
- this.btnPagePrev.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
- this.btnPagePrev.UseVisualStyleBackColor = true;
- this.btnPagePrev.Click += new System.EventHandler(this.btnPagePrev_Click);
- //
- // btnPageFirst
- //
- this.btnPageFirst.Enabled = false;
- this.btnPageFirst.FlatAppearance.BorderColor = System.Drawing.SystemColors.Control;
- this.btnPageFirst.FlatAppearance.BorderSize = 0;
- this.btnPageFirst.FlatAppearance.MouseDownBackColor = System.Drawing.SystemColors.Control;
- this.btnPageFirst.FlatAppearance.MouseOverBackColor = System.Drawing.SystemColors.Control;
- this.btnPageFirst.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
- this.btnPageFirst.Font = new System.Drawing.Font("Segoe UI Symbol", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
- this.btnPageFirst.ForeColor = System.Drawing.Color.Black;
- this.btnPageFirst.Location = new System.Drawing.Point(244, 11);
- this.btnPageFirst.Name = "btnPageFirst";
- this.btnPageFirst.Size = new System.Drawing.Size(25, 27);
- this.btnPageFirst.TabIndex = 6;
- this.btnPageFirst.Text = "⏪";
- this.btnPageFirst.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
- this.btnPageFirst.UseVisualStyleBackColor = true;
- this.btnPageFirst.Click += new System.EventHandler(this.btnPageFirst_Click);
- //
- // btnPageLast
- //
- this.btnPageLast.Enabled = false;
- this.btnPageLast.FlatAppearance.BorderColor = System.Drawing.SystemColors.Control;
- this.btnPageLast.FlatAppearance.BorderSize = 0;
- this.btnPageLast.FlatAppearance.MouseDownBackColor = System.Drawing.SystemColors.Control;
- this.btnPageLast.FlatAppearance.MouseOverBackColor = System.Drawing.SystemColors.Control;
- this.btnPageLast.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
- this.btnPageLast.Font = new System.Drawing.Font("Segoe UI Symbol", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
- this.btnPageLast.ForeColor = System.Drawing.Color.Black;
- this.btnPageLast.Location = new System.Drawing.Point(370, 11);
- this.btnPageLast.Name = "btnPageLast";
- this.btnPageLast.Size = new System.Drawing.Size(25, 27);
- this.btnPageLast.TabIndex = 8;
- this.btnPageLast.Text = "⏩";
- this.btnPageLast.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
- this.btnPageLast.UseVisualStyleBackColor = true;
- this.btnPageLast.Click += new System.EventHandler(this.btnPageLast_Click);
- //
- // btnPageNext
- //
- this.btnPageNext.Enabled = false;
- this.btnPageNext.FlatAppearance.BorderColor = System.Drawing.SystemColors.Control;
- this.btnPageNext.FlatAppearance.BorderSize = 0;
- this.btnPageNext.FlatAppearance.MouseDownBackColor = System.Drawing.SystemColors.Control;
- this.btnPageNext.FlatAppearance.MouseOverBackColor = System.Drawing.SystemColors.Control;
- this.btnPageNext.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
- this.btnPageNext.Font = new System.Drawing.Font("Segoe UI Symbol", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
- this.btnPageNext.ForeColor = System.Drawing.Color.Black;
- this.btnPageNext.Location = new System.Drawing.Point(339, 14);
- this.btnPageNext.Name = "btnPageNext";
- this.btnPageNext.Size = new System.Drawing.Size(25, 27);
- this.btnPageNext.TabIndex = 7;
- this.btnPageNext.Text = "▶";
- this.btnPageNext.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
- this.btnPageNext.UseVisualStyleBackColor = true;
- this.btnPageNext.Click += new System.EventHandler(this.btnPageNext_Click);
- //
- // Form1
- //
- this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
- this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
- this.ClientSize = new System.Drawing.Size(412, 290);
- this.Controls.Add(this.btnPageLast);
- this.Controls.Add(this.btnPageNext);
- this.Controls.Add(this.btnPageFirst);
- this.Controls.Add(this.btnPagePrev);
- this.Controls.Add(this.lblPages);
- this.Controls.Add(this.btnOpenFile);
- this.Name = "Form1";
- this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
- this.Text = "Form1";
- this.ResumeLayout(false);
- }
- private System.Windows.Forms.Button btnOpenFile;
- private System.Windows.Forms.Label lblPages;
- private System.Windows.Forms.Button btnPagePrev;
- private System.Windows.Forms.Button btnPageFirst;
- private System.Windows.Forms.Button btnPageLast;
- private System.Windows.Forms.Button btnPageNext;
- }
- -----------------------------------------------------------------------------------------------------------------------------
- using System;
- using System.Collections.Generic;
- using System.Drawing;
- using System.Drawing.Text;
- using System.IO;
- using System.Linq;
- using System.Runtime.InteropServices;
- using System.Text;
- using System.Windows.Forms;
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- public class RTBReader : RichTextBox
- {
- [DllImport("user32", CharSet = CharSet.Auto)]
- private static extern int SendMessage(HandleRef hWnd, int _msg, int wParam, ref PARAFORMAT2 _pf);
- [DllImport("user32", CharSet = CharSet.Auto)]
- private static extern int SendMessage(HandleRef hWnd, int _msg, int wParam, int lParam);
- public enum TextAlignment
- {
- Left = 1,
- Right,
- Center,
- Justify
- }
- private string text;
- public RTBReader()
- : this(null, Color.Black, Color.White) { }
- public RTBReader(Font Font, Color Forecolor, Color Backcolor)
- {
- this.Name = "Reader";
- this.ScrollBars = RichTextBoxScrollBars.None;
- this.SelectionAlignment = TextAlignment.Justify;
- this.Font = Font ?? new Font("Segoe UI", 10F, FontStyle.Regular, GraphicsUnit.Point);
- this.ForeColor = Forecolor;
- this.BackColor = Backcolor;
- }
- public List<string> Pages { get; private set; }
- public string DocumentText
- {
- get => this.text;
- set => this.Initialize(value);
- }
- public void ReaderPage(int Page) => Text = Pages[Page];
- private void Initialize(string Document)
- {
- if (string.IsNullOrEmpty(Document))
- {
- this.text = string.Empty;
- return;
- }
- this.text = Document;
- this.Pages = new List<string>();
- TextFormatFlags flags = TextFormatFlags.Top | TextFormatFlags.Left |
- TextFormatFlags.WordBreak | TextFormatFlags.TextBoxControl |
- TextFormatFlags.NoPadding;
- Size textSize = TextRenderer.MeasureText(this.text, this.Font, this.ClientSize, flags);
- int NumberOfPages = textSize.Height / this.ClientSize.Height;
- if (textSize.Height > this.Height)
- {
- this.Text = Document;
- this.Update();
- //Number of shown lines
- int FirstCharOfLastShownLine = this.GetCharIndexFromPosition(new Point(0, this.ClientSize.Height));
- int ShownLines = this.GetLineFromCharIndex(FirstCharOfLastShownLine);
- int TotalLines = this.GetLineFromCharIndex(this.Text.Length - 1);
- for (int p = 0; p < NumberOfPages; p++)
- {
- int FirstLineOfPage = (p * ShownLines);
- int FirstCharOfPage = this.GetFirstCharIndexFromLine(FirstLineOfPage);
- int FirstLineOfNextPage = (p + 1) * ShownLines;
- FirstLineOfNextPage = (FirstLineOfNextPage > TotalLines) ? TotalLines : FirstLineOfNextPage;
- int LastCharOfPage = (FirstLineOfNextPage < TotalLines)
- ? this.GetFirstCharIndexFromLine(FirstLineOfNextPage) - 1
- : this.Text.Length;
- this.Pages.Add(this.Text.Substring(FirstCharOfPage, LastCharOfPage - FirstCharOfPage));
- }
- }
- else
- {
- this.Pages.Add(this.text);
- }
- this.Text = this.Pages.First();
- }
- private const int EM_SETEVENTMASK = 1073;
- private const int EM_GETPARAFORMAT = 1085;
- private const int EM_SETPARAFORMAT = 1095;
- private const int EM_SETTYPOGRAPHYOPTIONS = 1226;
- private const int WM_SETREDRAW = 11;
- private const int TO_ADVANCEDTYPOGRAPHY = 0x1;
- private const int PFM_ALIGNMENT = 8;
- private const int SCF_SELECTION = 1;
- [StructLayout(LayoutKind.Sequential)]
- private struct PARAFORMAT2
- {
- public int cbSize;
- public uint dwMask;
- public short wNumbering;
- public short wReserved;
- public int dxStartIndent;
- public int dxRightIndent;
- public int dxOffset;
- public short wAlignment;
- public short cTabCount;
- [MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)]
- public int[] rgxTabs;
- public int dySpaceBefore;
- public int dySpaceAfter;
- public int dyLineSpacing;
- public short sStyle;
- public byte bLineSpacingRule;
- public byte bOutlineLevel;
- public short wShadingWeight;
- public short wShadingStyle;
- public short wNumberingStart;
- public short wNumberingStyle;
- public short wNumberingTab;
- public short wBorderSpace;
- public short wBorderWidth;
- public short wBorders;
- }
- public new TextAlignment SelectionAlignment
- {
- get
- {
- PARAFORMAT2 _pf = new PARAFORMAT2();
- _pf.cbSize = Marshal.SizeOf(_pf);
- SendMessage(new HandleRef(this, Handle), EM_GETPARAFORMAT, SCF_SELECTION, ref _pf);
- // If 0, defaults to TextAlignment.Left
- if ((_pf.dwMask & PFM_ALIGNMENT) == 0) return TextAlignment.Left;
- return (TextAlignment)_pf.wAlignment;
- }
- set
- {
- PARAFORMAT2 _pf = new PARAFORMAT2();
- _pf.cbSize = Marshal.SizeOf(_pf);
- _pf.dwMask = PFM_ALIGNMENT;
- _pf.wAlignment = (short)value;
- SendMessage(new HandleRef(this, Handle), EM_SETPARAFORMAT, SCF_SELECTION, ref _pf);
- }
- }
- // Overrides OnHandleCreated to enable RTB advances options
- protected override void OnHandleCreated(EventArgs e)
- {
- base.OnHandleCreated(e);
- // EM_SETTYPOGRAPHYOPTIONS allows to enable RTB (RichEdit) Advanced Typography
- SendMessage(new HandleRef(this, Handle), EM_SETTYPOGRAPHYOPTIONS,
- TO_ADVANCEDTYPOGRAPHY,
- TO_ADVANCEDTYPOGRAPHY);
- }
- }
- RTBReader DocumentViewer = null;
- private int CurrentPage = 0;
- private int FirstPage = 0;
- private int LastPage = 0;
- private void btnOpenFile_Click(object sender, EventArgs e)
- {
- DocumentViewer = new RTBReader();
- string Document = File.ReadAllText(@"..\..\Resources\Document.txt", Encoding.Default);
- DocumentViewer.Location = new Point(12, 46);
- DocumentViewer.Size = new Size(385, 230);
- DocumentViewer.DocumentText = Document;
- this.Controls.Add(DocumentViewer);
- this.lblPages.Text = "1/" + DocumentViewer.Pages.Count.ToString();
- LastPage = DocumentViewer.Pages.Count - 1;
- btnPageNext.Enabled = true;
- btnPageLast.Enabled = true;
- }
- private void btnPageNext_Click(object sender, EventArgs e)
- {
- if (CurrentPage < LastPage)
- {
- btnPageFirst.Enabled = true;
- btnPagePrev.Enabled = true;
- CurrentPage += 1;
- DocumentViewer.ReaderPage(CurrentPage);
- this.lblPages.Text = (CurrentPage + 1).ToString() + "/" + DocumentViewer.Pages.Count.ToString();
- if (CurrentPage == LastPage)
- {
- btnPageNext.Enabled = false;
- btnPageLast.Enabled = false;
- }
- }
- }
- private void btnPagePrev_Click(object sender, EventArgs e)
- {
- if (CurrentPage > FirstPage)
- {
- btnPageLast.Enabled = true;
- btnPageNext.Enabled = true;
- CurrentPage -= 1;
- DocumentViewer.ReaderPage(CurrentPage);
- this.lblPages.Text = (CurrentPage + 1).ToString() + "/" + DocumentViewer.Pages.Count.ToString();
- if (CurrentPage == 0)
- {
- btnPagePrev.Enabled = false;
- btnPageFirst.Enabled = false;
- }
- }
- }
- private void btnPageFirst_Click(object sender, EventArgs e)
- {
- btnPagePrev.Enabled = false;
- btnPageFirst.Enabled = false;
- btnPageNext.Enabled = true;
- btnPageLast.Enabled = true;
- CurrentPage = FirstPage;
- DocumentViewer.ReaderPage(FirstPage);
- this.lblPages.Text = (CurrentPage + 1).ToString() + "/" + DocumentViewer.Pages.Count.ToString();
- }
- private void btnPageLast_Click(object sender, EventArgs e)
- {
- btnPageNext.Enabled = false;
- btnPageLast.Enabled = false;
- btnPagePrev.Enabled = true;
- btnPageFirst.Enabled = true;
- CurrentPage = LastPage;
- DocumentViewer.ReaderPage(LastPage);
- this.lblPages.Text = (CurrentPage + 1).ToString() + "/" + DocumentViewer.Pages.Count.ToString();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement