实例介绍
【实例简介】
指定文件夹后,使文件夹中的图自自动切换为电脑桌面,并开始播放指定的音乐文件,实现在欣赏美丽背景的同时,倾听动听的音乐,而且还可以设置桌面背景的样式,如平铺,居中或拉伸等。
【实例截图】
需要启用 Windows Media Player 功能,否则会提示【System.Runtime.InteropServices.COMException:“没有注册类】
操作步骤:控制面板>>程序>>启用或者关闭windows 功能>>选中 媒体功能>>Windows Media Player,点击确定即可
【核心代码】
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.Runtime.InteropServices;
using Microsoft.Win32;
using System.IO;
using System.Threading;
using System.Drawing.Imaging;
namespace MusicViewDesk
{
public partial class Frm_Main : Form
{
public Frm_Main()
{
InitializeComponent();
}
string path;
private void 关闭电脑ToolStripMenuItem_Click(object sender, EventArgs e)
{
baseClass.ExitWindowsEx(baseClass.EWX_SHUTDOWN, 0);
}
private void 退出系统ToolStripMenuItem_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void 设置桌面ToolStripMenuItem_Click(object sender, EventArgs e)
{
System.Diagnostics.Process.Start("desk.cpl");
}
private void 系统信息ToolStripMenuItem_Click(object sender, EventArgs e)
{
System.Diagnostics.Process.Start("MSINFO32.exe");
}
private void 隐藏任务栏ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (隐藏任务栏ToolStripMenuItem.CheckState== CheckState.Unchecked)
{
baseClass.ShowWindow(baseClass.FindWindow("Shell_TrayWnd", null), 0);
隐藏任务栏ToolStripMenuItem.Checked = true;
}
else
{
baseClass.ShowWindow(baseClass.FindWindow("Shell_TrayWnd", null), 5);
隐藏任务栏ToolStripMenuItem.Checked = false;
}
}
private void 隐藏桌面ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (隐藏桌面ToolStripMenuItem.CheckState == CheckState.Unchecked)
{
baseClass.ShowWindow(baseClass.FindWindow("progman", null), 0);
隐藏桌面ToolStripMenuItem.Checked = true;
}
else
{
baseClass.ShowWindow(baseClass.FindWindow("progman", null), 5);
隐藏桌面ToolStripMenuItem.Checked = false;
}
}
private void 壁纸居中ToolStripMenuItem_Click(object sender, EventArgs e)
{
baseClass.SetDesk(0);
}
private void 平铺ToolStripMenuItem_Click(object sender, EventArgs e)
{
baseClass.SetDesk(1);
}
private void 壁纸拉伸ToolStripMenuItem_Click(object sender, EventArgs e)
{
baseClass.SetDesk(2);
}
private void 系统设置ToolStripMenuItem_Click(object sender, EventArgs e)
{
Frm_Set frm2 = new Frm_Set();
frm2.ShowDialog();
}
string musicPath;//音乐路径
string picPath;//图片路径
string pictime;//时间间隔
string screenpath;//屏保路径
private bool getinfo()
{
bool flag = false;
musicPath = baseClass.IniReadValue("setup", "musicpath", path);
picPath = baseClass.IniReadValue("setup", "picpath", path);
pictime = baseClass.IniReadValue("setup", "pictime", path);
screenpath = baseClass.IniReadValue("setup", "screen", path);
if (musicPath != "" && pictime != "" && picPath != "" && screenpath != "")
{
DirectoryInfo di = new DirectoryInfo(musicPath);
DirectoryInfo di1 = new DirectoryInfo(picPath);
if (di.Exists||di1.Exists)
{
flag = true;
}
}
return flag;
}
private void Form1_Load(object sender, EventArgs e)
{
path = Application.StartupPath.ToString();
path = path.Substring(0, path.LastIndexOf("\\"));
path = path.Substring(0, path.LastIndexOf("\\"));
path = @"\Setup.ini";
CheckForIllegalCrossThreadCalls = false;
if (!getinfo())
{
MessageBox.Show("请先对系统进行设置!");
}
}
private void 开启音乐屏保ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (getinfo())
{
baseClass.ScreenStart();
axWindowsMediaPlayer1.URL = musicPath;
}
}
private void 启动ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (getinfo())
{
if ((int)axWindowsMediaPlayer1.playState == 2)
{
timer1.Interval = Convert.ToInt32(pictime) * 1000;
timer1.Start();
启动ToolStripMenuItem.Enabled = false;
axWindowsMediaPlayer1.Ctlcontrols.play();
}
else
{
timer1.Interval = Convert.ToInt32(pictime) * 1000;
timer1.Start();
启动ToolStripMenuItem.Enabled = false;
axWindowsMediaPlayer1.URL = musicPath;
}
}
else
{
MessageBox.Show("请先对系统进行设置!");
}
}
private void ConvertType()
{
string FPath = picp;
//获取指定图片的扩展名
string SFileType = FPath.Substring(FPath.LastIndexOf(".") 1, (FPath.Length - FPath.LastIndexOf(".") - 1));
//将扩展名转换成小写
SFileType = SFileType.ToLower();
//获取文件名
string SFileName = FPath.Substring(FPath.LastIndexOf("\\") 1, (FPath.LastIndexOf(".") - FPath.LastIndexOf("\\") - 1));
//如果图片的类型是bmp则调用API中的方法将其设置为桌面背景
if (SFileType == "bmp")
{
baseClass.SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, FPath, 1);
}
else
{
if (SFileType != "db")
{
string SystemPath = Environment.SystemDirectory;//获取系统路径
DirectoryInfo di = new DirectoryInfo(SystemPath "\\WildWolf");
if (di.Exists)
{
di.Delete(true);
Directory.CreateDirectory(SystemPath "\\WildWolf");
}
else
{
Directory.CreateDirectory(SystemPath "\\WildWolf");
}
string path = SystemPath "\\WildWolf\\" SFileName ".bmp";
FileInfo fi = new FileInfo(path);
if (fi.Exists)
{
fi.Delete();
PictureBox pb = new PictureBox();
pb.Image = Image.FromFile(FPath);
pb.Image.Save(SystemPath "\\WildWolf\\" SFileName ".bmp", ImageFormat.Bmp);
}
else
{
PictureBox pb = new PictureBox();
pb.Image = Image.FromFile(FPath);
pb.Image.Save(SystemPath "\\WildWolf\\" SFileName ".bmp", ImageFormat.Bmp);
}
baseClass.SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, path, 1);
}
}
}
int i = 0;
string[] pic;
string picp;
private const int SPI_SETDESKWALLPAPER = 20;
private void timer1_Tick(object sender, EventArgs e)
{
pic = Directory.GetFiles(picPath);
int plength = pic.Length;
if (i <plength)
{
picp = pic[i].ToString();
ConvertType();
i ;
}
else
{
i = 0;
}
}
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
}
private void 暂停ToolStripMenuItem_Click(object sender, EventArgs e)
{
timer1.Stop();
启动ToolStripMenuItem.Enabled = true;
axWindowsMediaPlayer1.Ctlcontrols.pause();
}
}
}
网友评论
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
支持(0) 盖楼(回复)