阅读跟上次没有差别,这次增加了书架功能,下方的导入书籍可以在书架导入新书籍,在书籍上点击鼠标右键可以打开书籍或者从书架中移除书籍(只是移除书架中的书籍,不会删除文本).
1. 本来想用数据库来存储导入书籍的信息,但是存储的数据比较少而且数据库安装太大,于是选择使用txt文本存储数据。下面是生成书架的代码。界面初始化时读取txt文本中已经添加的书籍。
private void BookShelf()
{
//文件路径
string filePath = @"../../TXT/bookshelf.txt";
try
{
if (File.Exists(filePath))
{
string[] strContent = File.ReadAllLines(filePath, Encoding.UTF8);
for (int i = 0; i < strContent.Length; i++)
{
ShowBook(strContent);
}
}
else
{
MessageBox.Show("缺少系统文件");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
2. 这里生成图书的封面,在选项卡中动态生成PictureBox和label标签组成书架上的图书
private void ShowBook(string[] strContent)
{
int ShowColumnNumber = 5;// (6 * pictureBox2.Width) / 784;//每一行显示的列数
int controlWidth = 125;//每个控件占位宽度
int controlHeight = 144;//每个控件占位高度
// DisposeControls(pictureBox1);//清除
Image myBitmap;
myBitmap = Image.FromFile(Application.StartupPath + @"\Icon\001.jpg");
int num = 0;//防止空行影响坐标
for (int i = 0; i < strContent.Length; i++)
{
if (strContent[i].Trim()!="")
{
string[] strArray = strContent[i].Split('|');
int RowsCount = num / ShowColumnNumber;
/********图片***********/
PictureBox pic = new PictureBox
{
Size = new Size(76, 100),
Name = strArray[1],
Parent = tabPage2,
Location = new Point(controlWidth * (num - ShowColumnNumber * RowsCount) + 35, RowsCount * controlHeight + 25)//设置坐标
};
//绑定右键菜单
pic.ContextMenuStrip = contextMenuStrip1;
//pic.Click += B_Click;
pic.SizeMode = PictureBoxSizeMode.StretchImage;
pic.Image = myBitmap;
this.tabPage2.Controls.Add(pic);
/*********标签**********/
int LocationW = controlWidth * (num - ShowColumnNumber * RowsCount) + 35;//坐标X点
int LocationH = RowsCount * controlHeight + 136;//坐标Y点
Label lab = new Label
{
Size = new Size(120, 30),//设置大小
Location = new Point(LocationW, LocationH),//设置坐标
Text = strArray[0]
};//初始化一个控件
this.tabPage2.Controls.Add(lab);
num++;//不为空的行才进行绘画
}
}
}
3.添加右键菜单的功能代码,打开功能为将读取到的小说目录和内容放到主页在上一次的代码中有,删除操作操作TXT文本有点麻烦,由于File中没有直接修改文本的方法,只能先读取原来的所有文本,在使用list盛放时把需要删除的小说排除后,清空原来的txt再写入新的书籍信息。用一个全局变量来存放书籍的地址,在contextMenuStrip1_Opening事件中将选中PictureBox所代表图书的路径放进去。否则打开和删除的事件无法找到当前书籍的信息。
string PicPath = "";
//左键菜单
private void 打开ToolStripMenuItem_Click(object sender, EventArgs e)
{
string path = PicPath;
//MessageBox.Show(path);
if (File.Exists(path))
{
this.tabControl1.SelectedIndex = 0;
ImportBooks(path);
}
else
{
MessageBox.Show("此书已经不在!");
BookShelf();
//this.tabControl1.SelectedIndex = 1;
}
}
private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
{
string filePath = @"../../TXT/bookshelf.txt";
try
{
if (File.Exists(filePath))
{
//将bookshelf.txt所有行导出
string[] strContentOld = File.ReadAllLines(filePath, Encoding.UTF8);
//创建一个新的容器
List<string> strContentNew = new List<string>();
//遍历bookshelf.txt 将要删除的行排除后的所有行添加到新容器
for (int i = 0; i < strContentOld.Length; i++)
{
if (strContentOld[i].Trim() == "")
{
continue;
}
string[] strArray = strContentOld[i].Split('|');
if (strArray[1] != PicPath)
{
//strContentNew[i] = strContentOld[i];
strContentNew.Add(strContentOld[i]);
}
}
//清空bookshelf.txt
File.WriteAllText(filePath, string.Empty);
//将排除要删除行的新数组写进bookshelf.txt中
for (int i = 0; i < strContentNew.Count; i++)
{
byte[] mybyte = Encoding.UTF8.GetBytes(strContentNew[i] + "\n");
string mystr1 = Encoding.UTF8.GetString(mybyte);
File.AppendAllText(filePath, mystr1);//添加至文件
}
}
else
{
MessageBox.Show("缺少系统文件");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
DisposeControls(tabPage2);
BookShelf();
}
private void contextMenuStrip1_Opening(object sender, CancelEventArgs e)
{
PicPath = (sender as ContextMenuStrip).SourceControl.Name;
//MessageBox.Show(PicPath);
}