1 c# 添加MouseEnter事件
当鼠标进入按钮的可见部分的时候,给按钮一个新的坐标
private void btnUnLove_MouseEnter(object sender, EventArgs e)
{
//给按钮一个新的坐标
//这个按钮活动的最大宽度就是 窗体的宽度减去按钮的宽度
int x = this.ClientSize.Width - btnUnLove.Width;
int y = this.ClientSize.Height - btnUnLove.Height;
Random r = new Random();
//要给button一个随机的坐标
btnUnLove.Location = new Point(r.Next(0, x + 1), r.Next(0, y + 1));
}
2 Directory类
创建文件夹
Directory.CreateDirectory(@"C:\a");
删除文件夹:
Directory.Delete(@"C:\a",true);
剪切文件夹:
Directory.Move(@"c:\a", @"C:\Users\SpringRain\Desktop\new");
获得指定文件夹下所有png文件的全路径
string[] path = Directory.GetFiles(@"D:\queena.pan\My Documents\My Pictures", "*.png");
for (int i = 0; i < path.Length; i++)
{
Console.WriteLine(path[i]);
}
Console.ReadKey();
输出结果:
D:\queena.pan\My Documents\My Pictures\4M.png
D:\queena.pan\My Documents\My Pictures\8M.png
判断文件夹是否存在:
if (Directory.Exists(@"C:\a\b"))
获得指定目录下所有文件夹的全路径
string[] path = Directory.GetDirectories(@"C:\Users\SpringRain\Desktop\new");
for (int i = 0; i < path.Length; i++)
{
Console.WriteLine(path[i]);
}
Console.ReadKey();
3 ListBox实现双击更换图片/播放音乐
public Form1()
{
InitializeComponent();
}
//用来存储图片文件的全路径
List<string> list = new List<string>();
private void Form1_Load(object sender, EventArgs e)
{
//得到文件全路径
string[] path = Directory.GetFiles(@"D:\queena.pan\桌面\picture", "*.jpg");
for (int i = 0; i < path.Length; i++)
{
//得到文件名
string fileName = Path.GetFileName(path[i]);
//将文件名显示到ListBox中
listBox1.Items.Add(fileName);
//将图片的全路径添加到List泛型集合中
list.Add(path[i]);
}
}
/// 双击播放图片
private void listBox1_DoubleClick(object sender, EventArgs e)
{
pictureBox1.Image = Image.FromFile(list[listBox1.SelectedIndex]);
}
}
效果:
播放音乐:
private void listBox1_DoubleClick(object sender, EventArgs e)
{
SoundPlayer sp = new SoundPlayer();
sp.SoundLocation=listSongs[listBox1.SelectedIndex];
sp.Play();
}