(c#) 一个简单的TXT阅读器
*功能:简单TXT阅读器
*
调整字体或界面大小时,自动计算总页数与当前页数(以当前界面大小为一页,以方便阅读)
*
* 要点:
* 1)读取TEXT文件并在TextBox中显示
* 2)消除汉字乱码
* 3)计算总页数,当前页数
*
3)按下按钮时,发送键码(用按钮模拟PGUP,PGDN功能键)
*
4)程序运行中如何变更字体(需用fontFamily类)
*
5)打开文件OpenFileDialog,保存文件SaveFileDialog
* 6)抓屏
* 7)菜单项的Checked
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace ShowReader
{
public
partial class MainForm : Form
{
// int CurrentLine;
int WordIndex;
public MainForm()
{
InitializeComponent();
this.BackgroundImageLayout = ImageLayout.Stretch;
this.KeyPreview = true;
this.labelTitle.Visible = false;
this.textBox1.Visible = false;
this.textBox1.Text = "";
this.panel_bottom.Visible = false;
this.textBox1.BackColor = System.Drawing.Color.White;
}
private void FileOpenMenuItem_Click(object sender, EventArgs e)
{
OpenFileDialog fileDialog = new OpenFileDialog();
// fileDialog.InitialDirectory = "d:\";
fileDialog.Title = "选择文件";
fileDialog.Filter = "TXT files (*.txt)|*.txt";
fileDialog.FilterIndex = 1;
fileDialog.RestoreDirectory = true;
if (fileDialog.ShowDialog() == DialogResult.OK)
{
labelTitle.Visible = true;
panel_bottom.Visible = true;
textCurrentPage.Text = "1";
this.textBox1.Visible = true;
// this.textBox1.ScrollBars.GetType();
FileStream fs = new FileStream(fileDialog.FileName, FileMode.Open,
FileAccess.Read);
StreamReader sr = new StreamReader(fs,
Encoding.Default); //注意第二个参数,若无,汉字会成乱码
string input = sr.ReadToEnd();
this.textBox1.Focus();
textBox1.Text = input;
// string str = new
string();
if (fileDialog.FileName.LastIndexOf(".") -
fileDialog.FileName.LastIndexOf("\") - 1 > 0
& fileDialog.FileName.LastIndexOf("\") + 1
> 1)
{
fileDialog.FileName =
fileDialog.FileName.Substring(fileDialog.FileName.LastIndexOf("\")
+ 1, (fileDialog.FileName.LastIndexOf(".") -
fileDialog.FileName.LastIndexOf("\") - 1));
}
labelTitle.Text = fileDialog.FileName;
SetLabelTitleLeft();
getPages();
getCurrentPage();
}
}
private void MainForm_Resize(object sender, EventArgs e)
{
this.panel_bottom.Height = 50;
this.textBox1.Top = 40;
this.textBox1.Left = 10;
this.textBox1.Width = this.Width - 38;
this.textBox1.Height = this.Height - 80 -
this.panel_bottom.Height;
this.panel_bottom.Top = this.textBox1.Top + this.textBox1.Height +
10;
this.panel_bottom.Left = 0;
this.panel_bottom.Width = this.Width;
this.panel_Button.Left = this.Width - this.panel_Button.Width;
SetLabelTitleLeft();
//
foreach (Control ctl in this.panel_right.Controls)
//
{
//
ctl.Left = -1;
//
ctl.Height = this.textBox1.Height / 10 + 2;
//
ctl.Width = ctl.Parent.Width;
//
ctl.Enabled = false;
// }
//
for (int i = 0; i < this.panel_right.Controls.Count;
i++)
// {
//
this.panel_right.Controls[i].Text = Convert.ToString(i + 1);
//
this.panel_right.Controls[i].Top = (this.textBox1.Top -
this.panel_right.Top) + (i) * this.textBox1.Height / 10;
//
}
getPages();
}
public void getPages()
{
int myPages = (textBox1.GetLineFromCharIndex(textBox1.Text.Length)
+ 1) / ((this.textBox1.Height / this.textBox1.Font.Height) - 0) +
1;
textPages.Text = Convert.ToString(myPages);
}
public void getCurrentPage()
{
WordIndex =
textBox1.GetFirstCharIndexOfCurrentLine();//得到当前行第一个字符的索引
int CurrentLine = textBox1.GetLineFromCharIndex(WordIndex) +
1;//得到当前行的行号,从0开始,习惯是从1开始,所以+1.
int CurrentPage = CurrentLine / (this.textBox1.Height /
this.textBox1.Font.Height) + 1;
if (CurrentPage >
Convert.ToInt32(textPages.Text))
{
CurrentPage = Convert.ToInt32(textPages.Text);
}
textCurrentPage.Text = Convert.ToString(CurrentPage);
}
private void FileCloseMenuItem_Click(object sender, EventArgs
e)
{
this.textBox1.Text = "";
this.textBox1.Visible = false;
this.panel_bottom.Visible = false;
this.labelTitle.Visible
= false;
}
private void SetFont01MenuItem_Click(object sender, EventArgs
e)
{
//公共构造函数 FontFamily 已重载
FontFamily fontFamily = new FontFamily("微软雅黑");//字体为黑体
textBox1.Font = new Font(
fontFamily,
16,//字号为16
FontStyle.Regular,//普通文本
//Bold 为加粗
//Italic 为倾斜
//Strikeout 带删除线
//Underline 带下划线
GraphicsUnit.Pixel); //指定给定数据的度量单位。
//成员说明:
//Display 指定显示设备的度量单位。通常,视频显示使用的单位是像素;
//打印机使用的单位是 1/100 英寸。
//Document 将文档单位(1/300
英寸)指定为度量单位。
//Inch 将英寸指定为度量单位。
//Millimeter 将毫米指定为度量单位。
//Pixel 将设备像素指定为度量单位。
//Point 将打印机点(1/72
英寸)指定为度量单位。
//World 将世界坐标系单位指定为度量单位。
SetFont01MenuItem.Checked = true;
SetFont02MenuItem.Checked = false;
SetFont03MenuItem.Checked = false;
SetFont04MenuItem.Checked = false;
getPages();
getCurrentPage();
}
private void SetFont02MenuItem_Click(object sender, EventArgs
e)
{
//公共构造函数 FontFamily 已重载
FontFamily
fontFamily = new FontFamily("微软雅黑");//字体为黑体
textBox1.Font = new Font(
fontFamily,
19,//字号
FontStyle.Regular,//普通文本
GraphicsUnit.Pixel); //指定给定数据的度量单位。
SetFont01MenuItem.Checked = false;
SetFont02MenuItem.Checked = true;
SetFont03MenuItem.Checked = false;
SetFont04MenuItem.Checked = false;
getPages();
getCurrentPage();
}
private void SetFont03MenuItem_Click(object sender, EventArgs
e)
{
FontFamily fontFamily = new FontFamily("微软雅黑");//字体为黑体
textBox1.Font = new Font(
fontFamily,
24,//字号
FontStyle.Regular,//普通文本
GraphicsUnit.Pixel); //指定给定数据的度量单位。
SetFont01MenuItem.Checked = false;
SetFont02MenuItem.Checked = false;
SetFont03MenuItem.Checked = true;
SetFont04MenuItem.Checked = false;
getPages();
getCurrentPage();
}
private void SetFont04MenuItem_Click(object sender, EventArgs
e)
{
FontFamily fontFamily = new FontFamily("微软雅黑");//字体为黑体
textBox1.Font = new Font(
fontFamily,
30,//字号
FontStyle.Regular,//普通文本
GraphicsUnit.Pixel); //
SetFont01MenuItem.Checked = false;
SetFont02MenuItem.Checked = false;
SetFont03MenuItem.Checked = false;
SetFont04MenuItem.Checked = true;
getPages();
getCurrentPage();
}
private void MainForm_KeyDown(object sender, KeyEventArgs
e)//不用此事件
{
}
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
Int32 p = Convert.ToInt32(this.textCurrentPage.Text);
if (Keys.PageDown == e.KeyCode)
{
if
(p < Convert.ToInt32(this.textPages.Text))
{
this.textCurrentPage.Text = Convert.ToString(p +1);
}
}
if (Keys.PageUp == e.KeyCode)
{
if (p > 1 )
{
this.textCurrentPage.Text = Convert.ToString(p -1);
}
}
// this.my_p.Visible = false;
// this.textCurrentPage.Refresh();
// MessageBox.Show("您所按动的键是");
}
private void button3_Click(object sender, EventArgs e)
{
textBox1.Focus();
SendKeys.Send("{PGUP}");
}
private void button4_Click(object sender, EventArgs e)
{
textBox1.Focus();
SendKeys.Send("{PGDN}");
}
private void button1_Click(object sender, EventArgs e)
{
this.textBox1.SelectionStart = 1;
this.textBox1.ScrollToCaret();
getCurrentPage();
}
private void button6_Click(object sender, EventArgs e)
{
this.textBox1.SelectionStart = textBox1.Text.Length;
this.textBox1.ScrollToCaret();
getCurrentPage();
}
private void button2_Click(object sender, EventArgs e)
{
WordIndex = WordIndex - 5 * (this.textBox1.Height /
this.textBox1.Font.Height) *(this.textBox1.Width/
this.textBox1.Font.Height);
if
(WordIndex < 1)
{
WordIndex = 1;
}
this.textBox1.SelectionStart = WordIndex;
this.textBox1.ScrollToCaret();
getCurrentPage();
}
private void button5_Click(object sender, EventArgs e)
{
WordIndex = WordIndex + 5 * (this.textBox1.Height /
this.textBox1.Font.Height) *(this.textBox1.Width/
this.textBox1.Font.Height);
if (WordIndex >
textBox1.Text.Length)
{
WordIndex
= textBox1.Text.Length;
}
this.textBox1.SelectionStart = WordIndex;
this.textBox1.ScrollToCaret();
getCurrentPage();
}
private void textBox1_MouseClick(object sender, MouseEventArgs
e)
{
getCurrentPage();
}
private void SetBackground01MenuItem_Click(object sender, EventArgs
e)
{
this.BackgroundImage =
Properties.Resources._11;
SetBackgroundMenuItemCheckedFalse();
SetBackground01MenuItem.Checked = true;
}
private void SetBackground02MenuItem_Click(object sender, EventArgs
e)
{
this.BackgroundImage =Properties.Resources._12;
SetBackgroundMenuItemCheckedFalse();
SetBackground02MenuItem.Checked = true;
}
private void SetBackground03MenuItem_Click(object sender, EventArgs
e)
{
this.BackgroundImage =Properties.Resources._13;
SetBackgroundMenuItemCheckedFalse();
SetBackground03MenuItem.Checked = true;
}
private void SetBackground04MenuItem_Click(object sender, EventArgs
e)
{
this.BackgroundImage = Properties.Resources._14;
SetBackgroundMenuItemCheckedFalse();
SetBackground04MenuItem.Checked = true;
}
private void SetBackground05MenuItem_Click(object sender, EventArgs
e)
{
this.BackgroundImage = Properties.Resources._15;
SetBackgroundMenuItemCheckedFalse();
SetBackground05MenuItem.Checked = true;
}
private void SetBackground06MenuItem_Click(object sender, EventArgs
e)
{
this.BackgroundImage = Properties.Resources._16;
SetBackgroundMenuItemCheckedFalse();
SetBackground06MenuItem.Checked = true;
}
private void SetBackground07MenuItem_Click(object sender, EventArgs
e)
{
this.BackgroundImage = Properties.Resources._17;
SetBackgroundMenuItemCheckedFalse();
SetBackground07MenuItem.Checked = true;
}
private void SetBackground08MenuItem1_Click(object sender,
EventArgs e)
{
this.BackgroundImage = Properties.Resources._18;
SetBackgroundMenuItemCheckedFalse();
SetBackground08MenuItem.Checked = true;
}
public void SetBackgroundMenuItemCheckedFalse()
{
SetBackground01MenuItem.Checked = false;
SetBackground02MenuItem.Checked = false;
SetBackground03MenuItem.Checked = false;
SetBackground04MenuItem.Checked = false;
SetBackground05MenuItem.Checked = false;
SetBackground06MenuItem.Checked = false;
SetBackground07MenuItem.Checked
= false;
SetBackground08MenuItem.Checked = false;
}
public void SetLabelTitleLeft()
{
labelTitle.Left = this.Width -
labelTitle.Text.Length*labelTitle.Font.Height-60 ;
if
(labelTitle.Left < 100)
{
labelTitle.Left = 100;
}
}
private void 抓屏ToolStripMenuItem_Click(object sender, EventArgs
e)
{
//抓屏
Image objImage = new Bitmap(this.textBox1.Width,
this.textBox1.Height);
Graphics g = Graphics.FromImage(objImage);
// g.CopyFromScreen(new Point(Cursor.Position.X
-500, Cursor.Position.Y - 300),new Point(0, 0), new Size(400,
300));
g.CopyFromScreen(new Point(this.textBox1.Left + this.Left + 3,
this.textBox1.Top + this.Top + 30 ),new Point(0, 0), new
Size(this.textBox1.Width,this.textBox1.Height));
// g.CopyFromScreen(new Point(0, 0), new Point(0, 0), new
Size(this.textBox1.Width, this.textBox1.Height);
//
this.CreateGraphics().DrawImage(objImage, 0, 0);
// this.CreateGraphics().DrawIcon
IntPtr dc1 = g.GetHdc();
g.ReleaseHdc(dc1);
objImage.Save("myJPG.jpg");
//
pictureBox1.Load("myJPG.jpg");
//
pictureBox1.Visible = true;
SaveFileDialog fileDialog = new SaveFileDialog();
//
OpenFileDialog fileDialog = new OpenFileDialog();
fileDialog.Title = "保存为";
fileDialog.Filter = "JPG files (*.jpg)|*.jpg";
fileDialog.FilterIndex = 1;
fileDialog.RestoreDirectory = true;
if (fileDialog.ShowDialog() == DialogResult.OK)
{
// this.pictureBox2.Image = objImage;
objImage.Save(fileDialog.FileName);
}
}
}
}
加载中,请稍候......