好长时间没有学习编程了,但心中一直挂念着程序,怀念过去曾经的时光。
最近工作需要,需要修改一些文件的属性,看到网上有一个,研究一番,觉得还是自己写一个,一来练练手过过瘾,二来也增加一些功能,于是花费了3个晚上写了这个文件属性修改器,(汗,用这么长时间)。
程序中用到了以前不曾使用的三项技术:窗体之间事件捕获、画图和资源管理。
下面是程序代码,资源管理文件无法上传。
//
Form1
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Resources;
using System.Reflection;
namespace FileAttrib
{
/// <summary>
/// 设置文件属性
/// </summary>
public partial class Form1 : Form
{
private Form2 form2;
// button4被单击的次数,用来判断当前应显示哪个文件的属性
int buttonClickCount = 0 ;
private ResourceManager getLocalResource = new ResourceManager( " FileAttrib.Properties.Resources " , Assembly.GetEntryAssembly());
public Form1()
{
// 设置图标
this .Icon = (Icon )getLocalResource.GetObject( " propertiesORoptions " );
InitializeComponent();
}
private void button3_Click( object sender, EventArgs e)
{
Application.Exit();
}
/// <summary>
/// 选择文件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click( object sender, EventArgs e)
{
int fileNameCount = 0 ;
string [] fileNames;
if ( this .openFileDialog1.ShowDialog() == DialogResult.OK)
{
fileNames = this .openFileDialog1.FileNames;
fileNameCount = fileNames.Length;
if (fileNameCount == 1 )
{
this .textBox1.Text = fileNames[ 0 ];
button4.Visible = false ;
// 取文件属性
loadFileAttrib(fileNames[ 0 ]);
}
else
{
// 选择文件多于一个时,打开第二个窗口,以放置文件
if (form2 == null || form2.IsDisposed)
{
form2 = new Form2();
form2.Width = base .Width;
form2.Left = base .Left;
form2.Top = base .Top - form2.Height;
// form2成为form1的附属窗体
this .AddOwnedForm(form2);
form2.Show();
}
// form2移动、修改大小的事件通知,这样两个窗口的相对位置可以保持不动
form2.moveHaveRaise += new Form2.formMoveHandler( this .Form2_Move);
// form2的ListViewItem选择的事件通知
form2.itemSelectChange += new Form2.formMoveHandler( this .itemSelect_Change);
}
// 将选择的文件放到form2中
if (form2 != null )
{
form2.addListViewItems = fileNames;
}
}
}
/// <summary>
/// 修改文件属性
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button2_Click( object sender, EventArgs e)
{
char [] ss = { ' | ' };
int fileNumber = textBox1.Text.Split(ss).Length;
if (fileNumber == 1 )
{
writeFileAttrib(textBox1.Text);
}
else
{
if (MessageBox.Show( " 你选择了 " + fileNumber.ToString() + " 个文件,这些文件的属性都将被修改,确定吗? " , " 修改确认 " ,MessageBoxButtons.YesNo) == DialogResult.Yes)
{
foreach ( string fileName in textBox1.Text.Split(ss))
{
writeFileAttrib(fileName);
}
}
}
}
/// <summary>
/// 点击该按钮,顺序显示textBox1中个文件的属性,同时form2.SelectedItems的当前项被用红色的矩形框标示出来
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button4_Click( object sender, EventArgs e)
{
char [] ss = { ' | ' };
int len, i = 1 ;
len = textBox1.Text.Split(ss).Length;
buttonClickCount ++ ;
string [] fileNames = textBox1.Text.Split(ss);
foreach ( string fileName in fileNames)
{
if (i == buttonClickCount + 1 )
{
// 显示属性
loadFileAttrib(fileName);
// 文件名传递到form2,用来查找相对应的项。
form2.flashItem = fileName;
if (buttonClickCount + 1 == len)
{
buttonClickCount =- 1 ;
}
break ;
}
i ++ ;
}
}
/// <summary>
/// 窗口移动,form2跟着移动
/// 两个form2.moveHaveRaise事件是为了防止移动事件重复出现导致图像闪烁
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Form1_Move( object sender, EventArgs e)
{
if (form2 != null )
{
form2.moveHaveRaise -= new Form2.formMoveHandler( this .Form2_Move);
form2.Left = base .Left;
form2.Top = base .Top - form2.Height;
form2.moveHaveRaise += new Form2.formMoveHandler( this .Form2_Move);
}
}
/// <summary>
/// form2窗口移动,form1跟着移动
/// 两个this.Move事件是为了防止移动事件重复出现导致图像闪烁
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Form2_Move( object sender, EventArgs e)
{
if (form2 != null )
{
this .Move -= new System.EventHandler( this .Form1_Move);
this .Left = form2.Left;
this .Top = form2.Top + form2.Height;
this .Move += new System.EventHandler( this .Form1_Move);
}
}
/// <summary>
/// form2的ListViewItem选择的事件处理程序
/// 选择ListViewItem项,相应的将文件名写到textBox1中,文件名之间用"|"分割
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void itemSelect_Change( object sender, EventArgs e)
{
textBox1.Text = "" ;
// SelectedItemArray是SelectedItems的集合数组
foreach (ListViewItem item in form2.SelectedItemArray)
{
textBox1.Text += item.SubItems[ 1 ].Text + " | " ;
}
char [] ss = { ' | ' };
textBox1.Text = textBox1.Text.TrimEnd(ss); // 去掉末尾的"|"
if (textBox1.Text.Split(ss).Length == 1 )
{
loadFileAttrib(textBox1.Text);
button4.Visible = false ;
}
else
{
button4.Visible = true ;
buttonClickCount = 0 ;
loadFileAttrib(textBox1.Text.Split(ss)[ 0 ]);
}
}
/// <summary>
/// 取文件属性
/// </summary>
/// <param name="fileName"></param>
private void loadFileAttrib( string fileName)
{
this .checkBox1.Checked = false ;
this .checkBox2.Checked = false ;
this .checkBox3.Checked = false ;
this .checkBox4.Checked = false ;
this .checkBox5.Checked = false ;
if (File.Exists(fileName))
{
this .dateTimePicker1.Text = File.GetCreationTime(fileName).ToString();
this .dateTimePicker2.Text = File.GetLastWriteTime(fileName).ToString();
this .dateTimePicker3.Text = File.GetLastAccessTime(fileName).ToString();
string text1 = File.GetAttributes(fileName).ToString();
if (text1.LastIndexOf( " ReadOnly " ) != - 1 )
{
this .checkBox1.Checked = true ;
}
if (text1.LastIndexOf( " System " ) != - 1 )
{
this .checkBox2.Checked = true ;
}
if (text1.LastIndexOf( " Hidden " ) != - 1 )
{
this .checkBox3.Checked = true ;
}
if (text1.LastIndexOf( " Archive " ) != - 1 )
{
this .checkBox4.Checked = true ;
}
if (text1.LastIndexOf( " Temporary " ) != - 1 )
{
this .checkBox5.Checked = true ;
}
FileInfo fi = new FileInfo(fileName);
label6.Text = fi.Length.ToString( " #,# " ) + " 字节 " ;
fi = null ;
}
}
/// <summary>
/// 写文件属性
/// </summary>
/// <param name="fileName"></param>
private void writeFileAttrib( string fileName)
{
if (File.Exists(fileName))
{
File.SetAttributes(fileName, FileAttributes.Normal);
FileAttributes attributes1 = File.GetAttributes(fileName);
if ( this .checkBox1.Checked)
{
File.SetAttributes(fileName, FileAttributes.ReadOnly);
}
if ( this .checkBox2.Checked)
{
File.SetAttributes(fileName, attributes1 | FileAttributes.System);
}
if ( this .checkBox3.Checked)
{
File.SetAttributes(fileName, attributes1 | FileAttributes.Hidden);
}
if ( this .checkBox4.Checked)
{
File.SetAttributes(fileName, attributes1 | FileAttributes.Archive);
}
if ( this .checkBox1.Checked)
{
File.SetAttributes(fileName, attributes1 | FileAttributes.Temporary);
}
File.SetCreationTime(fileName, this .dateTimePicker1.Value);
File.SetLastWriteTime(fileName, this .dateTimePicker2.Value);
File.SetLastAccessTime(fileName, this .dateTimePicker3.Value);
char [] ss = { ' | ' };
int fileNumber = textBox1.Text.Split(ss).Length;
if (fileNumber == 1 || fileName == textBox1.Text.Split(ss)[fileNumber - 1 ])
{
MessageBox.Show(getLocalResource.GetString( " setAttribSuccess " ), getLocalResource.GetString( " infoTip " ), MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
}
}
else
{
string fileNotExistMessage = getLocalResource.GetString( " fileNotExist " );
MessageBox.Show(fileNotExistMessage + " : " + fileName);
}
}
private void Form1_MouseDoubleClick( object sender, MouseEventArgs e)
{
System.Diagnostics.Process.Start( " mailto:kfqcharge@126.com " );
}
}
}
// Form2
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace FileAttrib
{
public partial class Form2 : Form
{
// 定义事件委托
public delegate void formMoveHandler( object sender, EventArgs e);
// 定义事件,1、窗体移动或大小改变时间;2、ListView项选择事件
public event formMoveHandler moveHaveRaise,itemSelectChange;
string [] addFileNames;
int fileNumber = 0 ;
public Form2()
{
InitializeComponent();
}
/// <summary>
/// 属性:增加文件,调用addItems
/// </summary>
public string [] addListViewItems
{
set
{
addFileNames = value;
addItems(addFileNames);
}
}
/// <summary>
/// 增加文件,调用addItem
/// </summary>
private void addItems( string [] fileNames)
{
foreach ( string fileName in fileNames)
{
addItem(fileName);
}
}
/// <summary>
/// 增加文件
/// </summary>
/// <param name="fileName"></param>
private void addItem( string fileName)
{
fileNumber ++ ;
ListViewItem additem = new ListViewItem(fileNumber.ToString());
additem.SubItems.Add(fileName);
listView1.Items.Add(additem);
listView1.Columns[ 1 ].AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
// 是否显示button1
buttonVisible();
}
/// <summary>
/// 清除所有项
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void clearAll_Click( object sender, EventArgs e)
{
listView1.Items.Clear();
fileNumber = 0 ;
buttonVisible();
}
/// <summary>
/// 清除选择项
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void deleteSelected_Click( object sender, EventArgs e)
{
foreach (ListViewItem item in listView1.SelectedItems)
{
item.Remove();
}
buttonVisible();
}
/// <summary>
/// 窗口移动,触发moveHaveRaise事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Form2_Move( object sender, EventArgs e)
{
if (moveHaveRaise != null )
{
moveHaveRaise( this , e);
}
}
/// <summary>
/// 窗口大小改变,触发moveHaveRaise事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Form2_ResizeEnd( object sender, EventArgs e)
{
if (moveHaveRaise != null )
{
moveHaveRaise( this , e);
}
buttonVisible();
}
/// <summary>
/// 选择想改变,触发itemSelectChange事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void listView1_ItemSelectionChanged( object sender, ListViewItemSelectionChangedEventArgs e)
{
if (itemSelectChange != null )
{
itemSelectChange( this ,e);
}
}
/// <summary>
/// 属性:返回当前所有选择项
/// </summary>
public ListViewItem[] SelectedItemArray
{
get
{
ListViewItem[] selectedItemNames = new ListViewItem[listView1.SelectedItems.Count];
listView1.SelectedItems.CopyTo(selectedItemNames, 0 );
return selectedItemNames;
}
}
/// <summary>
/// 属性:用GDI+为选择项画一个红色矩形,调用下面的Flash
/// </summary>
public string flashItem
{
set
{
string flashFile = value;
Flash(flashFile);
}
}
/// <summary>
/// 用GDI+为选择项画一个红色矩形,调用下面的drawItem
/// </summary>
/// <param name="flashFile"></param>
private void Flash( string flashFile)
{
listView1.Refresh();
ListViewItem foundItem = listView1.FindItemWithText(flashFile, true , listView1.SelectedIndices[ 0 ]);
drawItem(foundItem);
}
/// <summary>
/// 用GDI+为选择项画一个红色矩形
/// </summary>
/// <param name="foundItem"></param>
private void drawItem(ListViewItem currentItem)
{
if (currentItem != null )
{
// 创建画笔颜色
Pen redPen = new Pen(Color.Red);
// 创建绘画图面
Graphics itemGraphics = listView1.CreateGraphics();
// 创建矩形
Rectangle rect = currentItem.Bounds;
currentItem.Bounds.Intersect(rect);
// 在屏幕上画矩形
itemGraphics.DrawRectangle(redPen, rect);
// 释放资源
redPen.Dispose();
itemGraphics.Dispose();
}
}
/// <summary>
/// 窗口关闭前,将主窗口textBox1清空
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Form2_FormClosing( object sender, FormClosingEventArgs e)
{
listView1.Items.Clear();
if (itemSelectChange != null )
{
itemSelectChange( this , e);
}
}
/// <summary>
/// 将窗口宽度在listView1实际宽度和主窗口宽度之间切换
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click( object sender, EventArgs e)
{
int width1 = listView1.Columns[ 0 ].Width + listView1.Columns[ 1 ].Width;
if (width1 > this .Width)
{
this .Width = width1 + 30 ;
}
else
{
this .Width = 348 ;
}
button1.Left = this .Width - 27 ;
listView1.Select();
}
/// <summary>
/// 根据窗体宽度和listView1实际宽度,设置button1是否可见
/// </summary>
private void buttonVisible()
{
int width1 = listView1.Columns[ 0 ].Width + listView1.Columns[ 1 ].Width + 30 ;
if (width1 > this .Width)
{
button1.Visible = true ;
}
else
{
button1.Visible = false ;
}
button1.Left = this .Width - 27 ;
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Resources;
using System.Reflection;
namespace FileAttrib
{
/// <summary>
/// 设置文件属性
/// </summary>
public partial class Form1 : Form
{
private Form2 form2;
// button4被单击的次数,用来判断当前应显示哪个文件的属性
int buttonClickCount = 0 ;
private ResourceManager getLocalResource = new ResourceManager( " FileAttrib.Properties.Resources " , Assembly.GetEntryAssembly());
public Form1()
{
// 设置图标
this .Icon = (Icon )getLocalResource.GetObject( " propertiesORoptions " );
InitializeComponent();
}
private void button3_Click( object sender, EventArgs e)
{
Application.Exit();
}
/// <summary>
/// 选择文件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click( object sender, EventArgs e)
{
int fileNameCount = 0 ;
string [] fileNames;
if ( this .openFileDialog1.ShowDialog() == DialogResult.OK)
{
fileNames = this .openFileDialog1.FileNames;
fileNameCount = fileNames.Length;
if (fileNameCount == 1 )
{
this .textBox1.Text = fileNames[ 0 ];
button4.Visible = false ;
// 取文件属性
loadFileAttrib(fileNames[ 0 ]);
}
else
{
// 选择文件多于一个时,打开第二个窗口,以放置文件
if (form2 == null || form2.IsDisposed)
{
form2 = new Form2();
form2.Width = base .Width;
form2.Left = base .Left;
form2.Top = base .Top - form2.Height;
// form2成为form1的附属窗体
this .AddOwnedForm(form2);
form2.Show();
}
// form2移动、修改大小的事件通知,这样两个窗口的相对位置可以保持不动
form2.moveHaveRaise += new Form2.formMoveHandler( this .Form2_Move);
// form2的ListViewItem选择的事件通知
form2.itemSelectChange += new Form2.formMoveHandler( this .itemSelect_Change);
}
// 将选择的文件放到form2中
if (form2 != null )
{
form2.addListViewItems = fileNames;
}
}
}
/// <summary>
/// 修改文件属性
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button2_Click( object sender, EventArgs e)
{
char [] ss = { ' | ' };
int fileNumber = textBox1.Text.Split(ss).Length;
if (fileNumber == 1 )
{
writeFileAttrib(textBox1.Text);
}
else
{
if (MessageBox.Show( " 你选择了 " + fileNumber.ToString() + " 个文件,这些文件的属性都将被修改,确定吗? " , " 修改确认 " ,MessageBoxButtons.YesNo) == DialogResult.Yes)
{
foreach ( string fileName in textBox1.Text.Split(ss))
{
writeFileAttrib(fileName);
}
}
}
}
/// <summary>
/// 点击该按钮,顺序显示textBox1中个文件的属性,同时form2.SelectedItems的当前项被用红色的矩形框标示出来
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button4_Click( object sender, EventArgs e)
{
char [] ss = { ' | ' };
int len, i = 1 ;
len = textBox1.Text.Split(ss).Length;
buttonClickCount ++ ;
string [] fileNames = textBox1.Text.Split(ss);
foreach ( string fileName in fileNames)
{
if (i == buttonClickCount + 1 )
{
// 显示属性
loadFileAttrib(fileName);
// 文件名传递到form2,用来查找相对应的项。
form2.flashItem = fileName;
if (buttonClickCount + 1 == len)
{
buttonClickCount =- 1 ;
}
break ;
}
i ++ ;
}
}
/// <summary>
/// 窗口移动,form2跟着移动
/// 两个form2.moveHaveRaise事件是为了防止移动事件重复出现导致图像闪烁
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Form1_Move( object sender, EventArgs e)
{
if (form2 != null )
{
form2.moveHaveRaise -= new Form2.formMoveHandler( this .Form2_Move);
form2.Left = base .Left;
form2.Top = base .Top - form2.Height;
form2.moveHaveRaise += new Form2.formMoveHandler( this .Form2_Move);
}
}
/// <summary>
/// form2窗口移动,form1跟着移动
/// 两个this.Move事件是为了防止移动事件重复出现导致图像闪烁
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Form2_Move( object sender, EventArgs e)
{
if (form2 != null )
{
this .Move -= new System.EventHandler( this .Form1_Move);
this .Left = form2.Left;
this .Top = form2.Top + form2.Height;
this .Move += new System.EventHandler( this .Form1_Move);
}
}
/// <summary>
/// form2的ListViewItem选择的事件处理程序
/// 选择ListViewItem项,相应的将文件名写到textBox1中,文件名之间用"|"分割
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void itemSelect_Change( object sender, EventArgs e)
{
textBox1.Text = "" ;
// SelectedItemArray是SelectedItems的集合数组
foreach (ListViewItem item in form2.SelectedItemArray)
{
textBox1.Text += item.SubItems[ 1 ].Text + " | " ;
}
char [] ss = { ' | ' };
textBox1.Text = textBox1.Text.TrimEnd(ss); // 去掉末尾的"|"
if (textBox1.Text.Split(ss).Length == 1 )
{
loadFileAttrib(textBox1.Text);
button4.Visible = false ;
}
else
{
button4.Visible = true ;
buttonClickCount = 0 ;
loadFileAttrib(textBox1.Text.Split(ss)[ 0 ]);
}
}
/// <summary>
/// 取文件属性
/// </summary>
/// <param name="fileName"></param>
private void loadFileAttrib( string fileName)
{
this .checkBox1.Checked = false ;
this .checkBox2.Checked = false ;
this .checkBox3.Checked = false ;
this .checkBox4.Checked = false ;
this .checkBox5.Checked = false ;
if (File.Exists(fileName))
{
this .dateTimePicker1.Text = File.GetCreationTime(fileName).ToString();
this .dateTimePicker2.Text = File.GetLastWriteTime(fileName).ToString();
this .dateTimePicker3.Text = File.GetLastAccessTime(fileName).ToString();
string text1 = File.GetAttributes(fileName).ToString();
if (text1.LastIndexOf( " ReadOnly " ) != - 1 )
{
this .checkBox1.Checked = true ;
}
if (text1.LastIndexOf( " System " ) != - 1 )
{
this .checkBox2.Checked = true ;
}
if (text1.LastIndexOf( " Hidden " ) != - 1 )
{
this .checkBox3.Checked = true ;
}
if (text1.LastIndexOf( " Archive " ) != - 1 )
{
this .checkBox4.Checked = true ;
}
if (text1.LastIndexOf( " Temporary " ) != - 1 )
{
this .checkBox5.Checked = true ;
}
FileInfo fi = new FileInfo(fileName);
label6.Text = fi.Length.ToString( " #,# " ) + " 字节 " ;
fi = null ;
}
}
/// <summary>
/// 写文件属性
/// </summary>
/// <param name="fileName"></param>
private void writeFileAttrib( string fileName)
{
if (File.Exists(fileName))
{
File.SetAttributes(fileName, FileAttributes.Normal);
FileAttributes attributes1 = File.GetAttributes(fileName);
if ( this .checkBox1.Checked)
{
File.SetAttributes(fileName, FileAttributes.ReadOnly);
}
if ( this .checkBox2.Checked)
{
File.SetAttributes(fileName, attributes1 | FileAttributes.System);
}
if ( this .checkBox3.Checked)
{
File.SetAttributes(fileName, attributes1 | FileAttributes.Hidden);
}
if ( this .checkBox4.Checked)
{
File.SetAttributes(fileName, attributes1 | FileAttributes.Archive);
}
if ( this .checkBox1.Checked)
{
File.SetAttributes(fileName, attributes1 | FileAttributes.Temporary);
}
File.SetCreationTime(fileName, this .dateTimePicker1.Value);
File.SetLastWriteTime(fileName, this .dateTimePicker2.Value);
File.SetLastAccessTime(fileName, this .dateTimePicker3.Value);
char [] ss = { ' | ' };
int fileNumber = textBox1.Text.Split(ss).Length;
if (fileNumber == 1 || fileName == textBox1.Text.Split(ss)[fileNumber - 1 ])
{
MessageBox.Show(getLocalResource.GetString( " setAttribSuccess " ), getLocalResource.GetString( " infoTip " ), MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
}
}
else
{
string fileNotExistMessage = getLocalResource.GetString( " fileNotExist " );
MessageBox.Show(fileNotExistMessage + " : " + fileName);
}
}
private void Form1_MouseDoubleClick( object sender, MouseEventArgs e)
{
System.Diagnostics.Process.Start( " mailto:kfqcharge@126.com " );
}
}
}
// Form2
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace FileAttrib
{
public partial class Form2 : Form
{
// 定义事件委托
public delegate void formMoveHandler( object sender, EventArgs e);
// 定义事件,1、窗体移动或大小改变时间;2、ListView项选择事件
public event formMoveHandler moveHaveRaise,itemSelectChange;
string [] addFileNames;
int fileNumber = 0 ;
public Form2()
{
InitializeComponent();
}
/// <summary>
/// 属性:增加文件,调用addItems
/// </summary>
public string [] addListViewItems
{
set
{
addFileNames = value;
addItems(addFileNames);
}
}
/// <summary>
/// 增加文件,调用addItem
/// </summary>
private void addItems( string [] fileNames)
{
foreach ( string fileName in fileNames)
{
addItem(fileName);
}
}
/// <summary>
/// 增加文件
/// </summary>
/// <param name="fileName"></param>
private void addItem( string fileName)
{
fileNumber ++ ;
ListViewItem additem = new ListViewItem(fileNumber.ToString());
additem.SubItems.Add(fileName);
listView1.Items.Add(additem);
listView1.Columns[ 1 ].AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
// 是否显示button1
buttonVisible();
}
/// <summary>
/// 清除所有项
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void clearAll_Click( object sender, EventArgs e)
{
listView1.Items.Clear();
fileNumber = 0 ;
buttonVisible();
}
/// <summary>
/// 清除选择项
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void deleteSelected_Click( object sender, EventArgs e)
{
foreach (ListViewItem item in listView1.SelectedItems)
{
item.Remove();
}
buttonVisible();
}
/// <summary>
/// 窗口移动,触发moveHaveRaise事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Form2_Move( object sender, EventArgs e)
{
if (moveHaveRaise != null )
{
moveHaveRaise( this , e);
}
}
/// <summary>
/// 窗口大小改变,触发moveHaveRaise事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Form2_ResizeEnd( object sender, EventArgs e)
{
if (moveHaveRaise != null )
{
moveHaveRaise( this , e);
}
buttonVisible();
}
/// <summary>
/// 选择想改变,触发itemSelectChange事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void listView1_ItemSelectionChanged( object sender, ListViewItemSelectionChangedEventArgs e)
{
if (itemSelectChange != null )
{
itemSelectChange( this ,e);
}
}
/// <summary>
/// 属性:返回当前所有选择项
/// </summary>
public ListViewItem[] SelectedItemArray
{
get
{
ListViewItem[] selectedItemNames = new ListViewItem[listView1.SelectedItems.Count];
listView1.SelectedItems.CopyTo(selectedItemNames, 0 );
return selectedItemNames;
}
}
/// <summary>
/// 属性:用GDI+为选择项画一个红色矩形,调用下面的Flash
/// </summary>
public string flashItem
{
set
{
string flashFile = value;
Flash(flashFile);
}
}
/// <summary>
/// 用GDI+为选择项画一个红色矩形,调用下面的drawItem
/// </summary>
/// <param name="flashFile"></param>
private void Flash( string flashFile)
{
listView1.Refresh();
ListViewItem foundItem = listView1.FindItemWithText(flashFile, true , listView1.SelectedIndices[ 0 ]);
drawItem(foundItem);
}
/// <summary>
/// 用GDI+为选择项画一个红色矩形
/// </summary>
/// <param name="foundItem"></param>
private void drawItem(ListViewItem currentItem)
{
if (currentItem != null )
{
// 创建画笔颜色
Pen redPen = new Pen(Color.Red);
// 创建绘画图面
Graphics itemGraphics = listView1.CreateGraphics();
// 创建矩形
Rectangle rect = currentItem.Bounds;
currentItem.Bounds.Intersect(rect);
// 在屏幕上画矩形
itemGraphics.DrawRectangle(redPen, rect);
// 释放资源
redPen.Dispose();
itemGraphics.Dispose();
}
}
/// <summary>
/// 窗口关闭前,将主窗口textBox1清空
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Form2_FormClosing( object sender, FormClosingEventArgs e)
{
listView1.Items.Clear();
if (itemSelectChange != null )
{
itemSelectChange( this , e);
}
}
/// <summary>
/// 将窗口宽度在listView1实际宽度和主窗口宽度之间切换
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click( object sender, EventArgs e)
{
int width1 = listView1.Columns[ 0 ].Width + listView1.Columns[ 1 ].Width;
if (width1 > this .Width)
{
this .Width = width1 + 30 ;
}
else
{
this .Width = 348 ;
}
button1.Left = this .Width - 27 ;
listView1.Select();
}
/// <summary>
/// 根据窗体宽度和listView1实际宽度,设置button1是否可见
/// </summary>
private void buttonVisible()
{
int width1 = listView1.Columns[ 0 ].Width + listView1.Columns[ 1 ].Width + 30 ;
if (width1 > this .Width)
{
button1.Visible = true ;
}
else
{
button1.Visible = false ;
}
button1.Left = this .Width - 27 ;
}
}
}