using System;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.IO;
namespace text
{
?/// <summary>
?/// Form1 的摘要說明。
?/// </summary>
?public class Form1 : System.Windows.Forms.Form
?{
??private System.Windows.Forms.MainMenu mainMenu1;
??private System.Windows.Forms.MenuItem menuItem1;
??private System.Windows.Forms.MenuItem menuItem2;
??private System.Windows.Forms.MenuItem menuItem3;
??private System.Windows.Forms.MenuItem menuItem4;
??private System.Windows.Forms.OpenFileDialog openFileDialog1;
??private System.Windows.Forms.MenuItem menuItem5;
??private System.Windows.Forms.TextBox textBox1;
??public static string pathstr="";
??public static int curRowNo = 0;
??public static long? PageNum = 0;
??static ArrayList pageList = new ArrayList();
??static FileStream fs;
??static int maxPage = int.MaxValue;
??static int pageReadBytes = 700;
??private System.Windows.Forms.MenuItem menuItem6;
??private System.Windows.Forms.Panel panel1;
??private System.Windows.Forms.TextBox PN;
??private System.Windows.Forms.Label label1;
??????? static int pageChar = 300;
??public Form1()
??{
???//
???// Windows 窗體設計器支持所必需的
???//
???InitializeComponent();
???//
???// TODO: 在 InitializeComponent 調用后添加任何構造函數代碼
???//
??}
??/// <summary>
??/// 清理所有正在使用的資源。
??/// </summary>
??protected override void Dispose( bool disposing )
??{
???base.Dispose( disposing );
??}
??#region Windows 窗體設計器生成的代碼
??/// <summary>
??/// 設計器支持所需的方法 - 不要使用代碼編輯器修改
??/// 此方法的內容。
??/// </summary>
??private void InitializeComponent()
??{
???this.mainMenu1 = new System.Windows.Forms.MainMenu();
???this.menuItem1 = new System.Windows.Forms.MenuItem();
???this.menuItem2 = new System.Windows.Forms.MenuItem();
???this.menuItem3 = new System.Windows.Forms.MenuItem();
???this.menuItem4 = new System.Windows.Forms.MenuItem();
???this.menuItem6 = new System.Windows.Forms.MenuItem();
???this.menuItem5 = new System.Windows.Forms.MenuItem();
???this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
???this.textBox1 = new System.Windows.Forms.TextBox();
???this.panel1 = new System.Windows.Forms.Panel();
???this.PN = new System.Windows.Forms.TextBox();
???this.label1 = new System.Windows.Forms.Label();
???//
???// mainMenu1
???//
???this.mainMenu1.MenuItems.Add(this.menuItem1);
???this.mainMenu1.MenuItems.Add(this.menuItem4);
???//
???// menuItem1
???//
???this.menuItem1.MenuItems.Add(this.menuItem2);
???this.menuItem1.MenuItems.Add(this.menuItem3);
???this.menuItem1.Text = "文件";
???//
???// menuItem2
???//
???this.menuItem2.Text = "打開";
???this.menuItem2.Click += new System.EventHandler(this.menuItem2_Click);
???//
???// menuItem3
???//
???this.menuItem3.Text = "關閉";
???this.menuItem3.Click += new System.EventHandler(this.menuItem3_Click);
???//
???// menuItem4
???//
???this.menuItem4.MenuItems.Add(this.menuItem6);
???this.menuItem4.MenuItems.Add(this.menuItem5);
???this.menuItem4.Text = "幫助";
???//
???// menuItem6
???//
???this.menuItem6.Text = "屬性";
???this.menuItem6.Click += new System.EventHandler(this.menuItem6_Click);
???//
???// menuItem5
???//
???this.menuItem5.Text = "關于";
???this.menuItem5.Click += new System.EventHandler(this.menuItem5_Click);
???//
???// textBox1
???//
???this.textBox1.Location = new System.Drawing.Point(0, 32);
???this.textBox1.Multiline = true;
???this.textBox1.ScrollBars = System.Windows.Forms.ScrollBars.Both;
???this.textBox1.Size = new System.Drawing.Size(232, 160);
???this.textBox1.Text = "";
???this.textBox1.KeyDown += new System.Windows.Forms.KeyEventHandler(this.textBox1_KeyDown);
???//
???// panel1
???//
???this.panel1.Controls.Add(this.label1);
???this.panel1.Controls.Add(this.PN);
???this.panel1.Location = new System.Drawing.Point(0, 24);
???this.panel1.Size = new System.Drawing.Size(240, 184);
???this.panel1.Visible = false;
???//
???// PN
???//
???this.PN.Font = new System.Drawing.Font("宋體", 15.75F, System.Drawing.FontStyle.Bold);
???this.PN.Location = new System.Drawing.Point(40, 40);
???this.PN.Size = new System.Drawing.Size(112, 31);
???this.PN.Text = "0";
???this.PN.KeyDown += new System.Windows.Forms.KeyEventHandler(this.PN_KeyDown);
???//
???// label1
???//
???this.label1.Font = new System.Drawing.Font("宋體", 24F, System.Drawing.FontStyle.Bold);
???this.label1.Location = new System.Drawing.Point(32, 96);
???this.label1.Size = new System.Drawing.Size(128, 32);
???this.label1.Text = "ENT跳轉";
???//
???// Form1
???//
???this.ClientSize = new System.Drawing.Size(490, 447);
???this.Controls.Add(this.panel1);
???this.Controls.Add(this.textBox1);
???this.Menu = this.mainMenu1;
???this.Text = "文本查看器";
??}
??#endregion
??/// <summary>
??/// 應用程序的主入口點。
??/// </summary>
??static void Main()
??{
???Application.Run(new Form1());
??}
??private void menuItem2_Click(object sender, System.EventArgs e)
??{
???openFileDialog1.Filter = "txt?? files?? (*.txt)|*.txt";
???openFileDialog1.FilterIndex = 2;
??
???if (openFileDialog1.ShowDialog() == DialogResult.OK)
???{
??
????pathstr = openFileDialog1.FileName;?
????//this.textBox1.Text = displaytext(curRowNo, pathstr);
????this.Text = "文本查看器" + "1/" + PageNum.ToString();
????if (fs != null) fs.Close();
????pageList.Clear();
????fs = File.Open(pathstr, FileMode.Open, FileAccess.Read);
????PageNum = fs.Length / pageReadBytes - 120;
????curRowNo = 0;
????string text = ReadRow(fs, curRowNo);
????if (text != null && text.Length > 0)
????{
?????this.textBox1.Text = text;
?????this.Text = "文本查看器" + (curRowNo + 1).ToString() + "/" + PageNum.ToString();
?????this.Refresh();
????}
?
???}
???this.textBox1.Focus();
??}
??private static string displaytext(int curRowNo,string pathstr)
??{string textstr;
???byte[] rowBytes;
???FileStream fs = File.Open(pathstr, FileMode.Open, FileAccess.Read);
???long fileLength = fs.Length;
???PageNum = fileLength / 150;
???rowBytes = ReadRows(fs, curRowNo);
???? textstr = Encoding.Default.GetString(rowBytes, 0, 150).Trim();??
???fs.Close();
???return textstr;
??}
??private static byte[] ReadRows(FileStream fs, int recno)
??{
???byte[] rowBytes = new byte[150];
???if (recno < 0) return rowBytes;
???int position = recno * 150;
???fs.Seek(position, SeekOrigin.Begin);
???fs.Read(rowBytes, 0, 150);
???return rowBytes;
??}
??private void menuItem3_Click(object sender, System.EventArgs e)
??{
???this.Close();
??}
?
??private void addtext(string pathstr)
??{
???try
???{
??
????StreamReader reader = new StreamReader(pathstr, System.Text.Encoding.Default);
???
????while (reader.Peek() > 0)
????{????
?????string oneLine = reader.ReadLine();?
?????this.textBox1.Text+=oneLine+"/r/n";
????}
????reader.Close();
???}
???catch
???{
????MessageBox.Show("打開請檢查格式!");
???}
??
??}
??private void menuItem5_Click(object sender, System.EventArgs e)
??{
???MessageBox.Show("小航簡單文本閱讀器,版權歸呂順所有!");
??}
??private static string ReadRow(FileStream fs, int pageNo)
??{
???string textstr ;
???byte[] rowBytes;
???int bytecount;
???long startPosition = fs.Position;
???for (int i = 0; i < pageList.Count; i++)
???{
????if (((PageInfo)pageList[i]).PageNo == pageNo)
????{
?????bytecount =((PageInfo)pageList[i]).ByteCount;
?????rowBytes = new byte[bytecount];
?????fs.Position = ((PageInfo)pageList[i]).StartPosition;
?????bytecount = fs.Read(rowBytes,0, bytecount);
?????fs.Position = startPosition;
?????textstr = Encoding.Default.GetString(rowBytes, 0, bytecount);???????????????????
?????return textstr;
????}
???}
???if (fs.Position >= fs.Length - 1) return "";
???
???rowBytes = new byte[pageReadBytes];
???bytecount = fs.Read(rowBytes, 0, pageReadBytes);
???if(bytecount == 0) return "";
???int iDbCharCount = 0;
???if(bytecount == pageReadBytes && rowBytes[pageReadBytes - 1] > 127)
???{
????for (int i = pageReadBytes - 1;i>=0;i--)
????{??
?????if(rowBytes[i] > 127)
??????iDbCharCount ++;
?????else
??????break;
????}
???}
???try
???{
????textstr = Encoding.Default.GetString(rowBytes, 0, bytecount - iDbCharCount );
???}
???catch
???{
????textstr = Encoding.Default.GetString(rowBytes, 0, bytecount- iDbCharCount - 1);
???}
???textstr = textstr.Substring(0, Math.Min(pageChar, textstr.Length));
???int textByteCount = Encoding.Default.GetBytes(textstr, 0, Math.Min(pageChar,textstr.Length) , rowBytes, 0);
???if(bytecount < pageReadBytes - 1)
????fs.Seek(0, SeekOrigin.End);
???else
????fs.Seek(textByteCount - bytecount, SeekOrigin.Current);
???pageList.Add(new PageInfo(pageNo, startPosition,textByteCount));
???return textstr;
??}
??private struct PageInfo
??{
???public PageInfo(int pageNo, long startPosition, int byteCount)
???{
????StartPosition = startPosition;
????ByteCount = byteCount;
????PageNo = pageNo;
???}
???public long StartPosition;
???public int ByteCount;
???public int PageNo;
??}
??private void textBox1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
??{
???if(e.KeyCode==Keys.Left)
???{
????if (curRowNo >= 0)
????{
?????curRowNo--;
?????string text = ReadRow(fs, curRowNo);
?????if (text != null && text.Length > 0)
?????{
??????this.textBox1.Text = text;
??????this.Text = "文本查看器" + (curRowNo + 1).ToString() + "/" + PageNum.ToString();
??????this.Refresh();
?????}
????}
????else
????{
?????MessageBox.Show("已經是第一頁!");
??????this.textBox1.Focus();
????}
???}
???if(e.KeyCode==Keys.Right)
???{
????if (curRowNo <= maxPage)
????{
?????curRowNo++;
?????string text = ReadRow(fs, curRowNo);
?????if (text != null && text.Length > 0)
?????{
??????this.textBox1.Text = text;
??????this.Text = "文本查看器" + (curRowNo + 1).ToString() + "/" + PageNum.ToString();
??????this.Refresh();
?????}
?????else
?????{
??????maxPage = curRowNo-1;
??????PageNum = maxPage + 1;
?????}
????}
????else
????{
?????MessageBox.Show("已經是最后一頁!");
?????this.textBox1.Focus();
????}
???}
??}
??private void SelectPage_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
??{
??
??}
??private void PN_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
??{
???try
???{
??????? curRowNo=Convert.ToInt32(this.PN.Text);????
????curRowNo=curRowNo-1;
????if(e.KeyCode==Keys.Enter)
????{
?????string text = ReadRow(fs, curRowNo);
?????
??????this.textBox1.Text = text;
??????this.Text = "文本查看器" + (curRowNo + 1).ToString() + "/" + PageNum.ToString();
??????this.Refresh();?????
?????MessageBox.Show("哦了!");
?????this.panel1.Visible=false;
?????this.textBox1.Focus();
????}
???}
???catch
???{
???MessageBox.Show("大順啊,請輸入數字!謝謝");
???}
??}
??private void menuItem6_Click(object sender, System.EventArgs e)
??{
???this.panel1.Visible=true;
???this.PN.Focus();
??}
?}
}?