//MainForm.cs
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 BlogBook
{
public partial class MainForm : Form
{
public String pathRoot = ""; //文件的根目录
public String CurrentTxtPath = ""; //当前打开文档的完整路径
public String CurrentDirPath = ""; //当前打开文档的目录路径
public string[] CurrentTxtAry; //当前文档分组
public int CurrentMousPost = 0; //当前贯标在的位置
/*********************************************************************************
初始化加载
*********************************************************************************/
public MainForm()
{
InitializeComponent();
}
private void MainForm_Load(object sender, EventArgs e)
{
LoadrootPath();
treeView.LabelEdit = true; //支持右键改变名称
TextBox.WordWrap = false; //文本框的下滚动条
//TextBox.Font = new Font("宋体", 16);
// treeViewTest();
//pictureBox.SizeMode = StretchImage; //自动调整图片显示大小
//splitContainer1.Anchor = AnchorStyles.Left; 设置与父窗体一样大
}
/*********************************************************************************
图片的操作
*********************************************************************************/
int Picw = 0;
int Pich = 0;
String CurrentPicPatch = "";
Image IMG;
private void ReadPic(String picPatch)
{
try
{
IMG = Image.FromFile(picPatch);
pictureBox.Image = IMG;
pictureBox.Show();
CurrentPicPatch = picPatch;
labelpicPatch.Text = picPatch;
Picw = IMG.Width; //图片大小
Pich = IMG.Height;
setPictureBoxSize();
}
catch (System.Exception ex)
{
ex.GetType();
pictureBox.Image = pictureBox.ErrorImage;
picPatch = "";
}
}
//双击图片
private void pictureBox_DoubleClick(object sender, EventArgs e)
{
if (CurrentPicPatch == "")
{
return;
}
System.Diagnostics.Process.Start(CurrentPicPatch);
}
private void pictureBox_Click(object sender, EventArgs e)
{
}
//设置图片显示区的大小,以便合适放置图片
void setPictureBoxSize()
{
int w, h;
if (Picw != 0 && Pich != 0)
{
w = groupBox2.Width - 10; //得到最大的相框尺寸
h = groupBox2.Height - 45;
float k1 = 1;
float k2 = 1;
k1 = (float)Picw / w;
k2 = (float)Pich / h;
if (k1 > k2)
{
pictureBox.Width = w;
pictureBox.Height = (Pich * 10) / (int)(k1 * 10);
}
else
{
pictureBox.Width = (Picw * 10) / (int)(k2 * 10);
pictureBox.Height = h;
}
}
}
/*********************************************************************************
text的操作
*********************************************************************************/
private void TextBox_TextChanged(object sender, EventArgs e)
{
}
private void TextBox_TabIndexChanged(object sender, EventArgs e)
{
}
private void TextBox_AcceptsTabChanged(object sender, EventArgs e)
{
}
private void TextBox_CursorChanged(object sender, EventArgs e)
{
}
private void TextBox_StyleChanged(object sender, EventArgs e)
{
}
private void TextBox_ParentChanged(object sender, EventArgs e)
{
}
private void TextBox_FontChanged(object sender, EventArgs e)
{
}
//【按钮】 保存文档
private void but_save_Click(object sender, EventArgs e)
{
if (CurrentTxtPath == "")
{
but_lssave();
return;
}
File.WriteAllText(CurrentTxtPath, textTypeDeal(TextBox.Text));
}
//另存为
private void but_lssave()
{
Stream myStream;
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.Filter = "txt(*.txt)|*.txt|Java(*.java)|*.java|头文件(*.h)|*.h|C文件(*.c)|*.c|C#文件(*.cs)|*.cs";
saveFileDialog1.FilterIndex = 2;
saveFileDialog1.RestoreDirectory = true;
saveFileDialog1.InitialDirectory = CurrentDirPath;
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
if ((myStream = saveFileDialog1.OpenFile()) != null)
{
using (StreamWriter sw = new StreamWriter(myStream))
{
sw.Write(TextBox.Text);
}
myStream.Close();
MessageBox.Show("Saved");
}
}
}
//【按钮】 重新加载
private void but_reopen_Click(object sender, EventArgs e)
{
if (CurrentTxtPath == "")
{
return;
}
readFileToText(CurrentTxtPath);
}
//[键盘按下]
private void TextBox_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
getTextMessage();
}
//【鼠标单击】
private void TextBox_MouseClick(object sender, MouseEventArgs e)
{
getTextMessage();
}
//获取并显示文本当前信息
void getTextMessage()
{
int post = 0;
//找出光标所在的行
int Count = TextBox.SelectionStart; //当前光标前字数
int i = 0;
int x = 0;
foreach (char ch in TextBox.Text) //foreach 比 for要快很多很多
{
if (Count <= i)
{
break;
}
x++;
if (ch == '\n')
{
post++;
x = 1;
}
i++;
}
//对文档分组
CurrentTxtAry = TextBox.Text.Substring(0, TextBox.TextLength).Split('\n');
CurrentMousPost = post;
label_CurLine.Text = "当前位置:" + (post+1).ToString() + "," + x.ToString();
label_CurAllLine.Text = "总行数:" + CurrentTxtAry.Count().ToString();
label_charnum.Text = "到光标的字数:" + i.ToString();
//提取所在行的行内容,再从内容中提取图片名字
string picname = getPicNameFormTextLine(CurrentTxtAry[post]);
if (picname != "")
{ //显示图片
ReadPic(CurrentDirPath + picname);
}
}
//按格式获取图片名称
string getPicNameFormTextLine(string textline)
{
//图片格式[[图]xxx.xxx]
string ret = "";
int start_post = textline.IndexOf("[[P]");
if (start_post < 0)
{
return "";
}
int end_post = textline.IndexOf(']',start_post+4);
if (end_post < 0)
{
return "";
}
for (start_post += 4; start_post < end_post; start_post++)
{
ret += textline[start_post];
}
return ret;
}
/*********************************************************************************
treeView的操作
*********************************************************************************/
private void treeViewTest()
{
//treeView 使用示例
treeView.Nodes.Add("根节点");
for (int i = 0; i < 5; i++)
{
treeView.Nodes[0].Nodes.Add("级目录" + i.ToString());
}
for (int i = 0; i < treeView.Nodes[0].Nodes.Count; i++)
{
for (int j = 0; j < 5; j++)
{
treeView.Nodes[0].Nodes[i].Nodes.Add("二级目录" + j.ToString());
}
}
}
//【按钮】某节点上双击
private void treeView_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
{
string nodeText ="";
string nodeText1 = "";
try
{
//选中的路径
nodeText = this.treeView.SelectedNode.Text.ToString();
//获取全路径
nodeText1 = this.treeView.SelectedNode.FullPath.ToString();
}
catch (System.Exception ex)
{
return;
}
CurrentDirPath = nodeText1.Remove(nodeText1.Length - nodeText.Length);
label_Fpath.Text = CurrentDirPath;
//读取文件 当双击文件夹时
if (Directory.Exists(nodeText1))
{
System.Diagnostics.Process.Start(nodeText1); //是文件夹,则双击打开
}else if (File.Exists(nodeText1))
{
if (nodeText1.IndexOf(".txt") >=0 //是文本
|| nodeText1.IndexOf(".h") >= 0
|| nodeText1.IndexOf(".java") >= 0
|| nodeText1.IndexOf(".c") >= 0
|| nodeText1.IndexOf(".cpp") >= 0)
{
readFileToText(nodeText1);
}else
if (nodeText1.IndexOf(".bmp") >= 0 //是图片
|| nodeText1.IndexOf(".jpg") >= 0
|| nodeText1.IndexOf(".png") >= 0)
{
ReadPic(nodeText1);
}
}
}
//读取文件到Text
private void readFileToText(string Path)
{
try
{
TextBox.Text = textTypeDeal(File.ReadAllText(Path, Encoding.Default));
CurrentTxtPath = Path;
}
catch (System.Exception ex)
{
ex.GetType();
//MessageBox.Show("读取文件失败");
TextBox.Text = "";
CurrentTxtPath = "";
CurrentDirPath = "";
}
}
//文本格式处理,以便于显示和保存
//更改 换行/回车 -> \n\r
private string textTypeDeal(string srcstr)
{
int len = srcstr.Length;
char[] SrcChar = srcstr.ToCharArray();
char[] DestChar = new char[len * 2];
int j = 0;
for (int i = 0; i < len; i++)
{
//将\t 换成4个空格
if (SrcChar[i]=='\t')
{
DestChar[j++] = ' ';
DestChar[j++] = ' ';
DestChar[j++] = ' ';
DestChar[j++] = ' ';
continue;
}
DestChar[j++] = SrcChar[i];
}
string ret = new string(DestChar, 0, j);
return ret;
}
//更名后 更改节点名称事件->更改文件名/文件夹名
String oldname = "";
private void treeView_AfterLabelEdit(object sender, NodeLabelEditEventArgs e)
{
//相同就不更改了
if (oldname == e.Label || e.Label == null)
{
return;
}
//新名字
string newname = this.treeView.SelectedNode.FullPath.ToString();
int len = newname.Length;
newname = newname.Remove(newname.Length - oldname.Length);
try
{
File.Move(newname + oldname, newname + e.Label);
}
catch (System.Exception ex)
{
ex.GetType();
MessageBox.Show("改名失败!请重新更改");
}
}
//更名前
private void treeView_BeforeLabelEdit(object sender, NodeLabelEditEventArgs e)
{
oldname = this.treeView.SelectedNode.Text.ToString();
}
/*********************************************************************************
菜单
*********************************************************************************/
private void LoadrootPath()
{
if (!File.Exists("Config.ini"))
{
File.WriteAllText("Config.ini","");
}
string pathRoot = File.ReadAllText("Config.ini");
if (!(pathRoot == ""))
{
if (!Directory.Exists(pathRoot))
{
pathRoot = "";
}
else
{
//解析该路径下的所有文件
treeView.Nodes.Clear();
treeView.Nodes.Add(pathRoot);
foreachPath(pathRoot, treeView.Nodes[0]);
}
}
}
//【菜单 File -> open】
private void Menu_Open_Click(object sender, EventArgs e)
{
//文件路径
FolderBrowserDialog fbd = new FolderBrowserDialog();
//显示原来的路径
if (pathRoot != "")
{
fbd.SelectedPath = pathRoot;
}
//新选择路径
if (DialogResult.OK != fbd.ShowDialog())
{
return;
}
//获取到新路径
pathRoot = fbd.SelectedPath;
//解析该路径下的所有文件
treeView.Nodes.Clear();
treeView.Nodes.Add(pathRoot);
foreachPath(pathRoot, treeView.Nodes[0]);
//保存路径
File.WriteAllText("Config.ini", pathRoot); //保存路径到文件
}
//【菜单 字体】
private void Menu_Font_Click(object sender, EventArgs e)
{
FontDialog FD = new FontDialog();
if (DialogResult.OK == FD.ShowDialog())
{
TextBox.Font = TextBox.Font = new Font(FD.Font.Name, FD.Font.Size);
}
}
//【菜单 help】
private void Menu_help_Click(object sender, EventArgs e)
{
System.Diagnostics.Process.Start("Help.txt");
}
//遍历目录下的所有文件及文件夹,并添加到treeView
int rootdeep = 0;
private void foreachPath(String sPath, TreeNode tv)
{
int nodecnt = 0;
DirectoryInfo dir = new DirectoryInfo(sPath);
FileInfo[] allFile;
//遍历文件
try
{
allFile = dir.GetFiles();
}
catch
{
return;
}
foreach (FileInfo fi in allFile)
{
//文档过滤
if (fi.Name.IndexOf(".txt") >= 0
|| fi.Name.IndexOf(".png") >= 0
|| fi.Name.IndexOf(".jpg") >=0
|| fi.Name.IndexOf(".h") >=0
|| fi.Name.IndexOf(".c") >= 0
|| fi.Name.IndexOf(".java") >= 0
|| fi.Name.IndexOf(".rar") >= 0
|| fi.Name.IndexOf(".bmp") >= 0
)
{
tv.Nodes.Add(fi.Name);
nodecnt++;
}
}
//遍历文件夹
DirectoryInfo[] allDir = dir.GetDirectories();
if (allDir.Count() > 0)
{
foreach (DirectoryInfo d in allDir)
{
//if (rootdeep > 2) //深度控制
//{
// break;
//}
//rootdeep++;
tv.Nodes.Add(d.Name);
foreachPath(sPath + "\\" + d.Name, tv.Nodes[nodecnt]);
nodecnt++;
//rootdeep--;
}
}
}
/*********************************************************************************
宽度调整
**********************************************************************************/
private void splitContainer1_RightToLeftChanged(object sender, EventArgs e)
{
}
//第一个split改变
private void splitContainer1_SplitterMoved(object sender, SplitterEventArgs e)
{
treeView.Width = splitContainer1.SplitterDistance - 5; //调整treeView的宽度
treeView.Height = splitContainer1.Height - 8;
}
//第二个split改变
private void splitContainer2_SplitterMoved(object sender, SplitterEventArgs e)
{
//
TextBox.Width = splitContainer2.SplitterDistance - 5; //调整TextBox的宽度
TextBox.Height = splitContainer2.Height - 8;
//
groupBox1.Width = splitContainer2.Width - splitContainer2.SplitterDistance - 20;
groupBox2.Width = groupBox1.Width;
groupBox2.Height = splitContainer2.Height - groupBox1.Height - 30;
setPictureBoxSize();
}
//窗体大小改变
private void MainForm_SizeChanged(object sender, EventArgs e)
{
}
private void labelpicPatch_Click(object sender, EventArgs e)
{
}
}
}