WPF 开机一键启动程序V1.0【原创】

每天上班第一件事就是开电脑,然后开一堆软件...

QQ..Opear..VS..虚拟机..RTX..等等。。很费时间.有了这个软件就可以在开始的时候点击一键启动,之后你可以去泡杯茶了。

最近也正在回顾WPF一些知识,就用 WPF做了一款开机一键启动程序的软件。

高手莫笑。

软件截图:

1

功能说明:

1,支持鼠标拖动图标到界面,并保存程序图标。

2,支持保存配置.

3,支持开机自动运行.

4,支持系统托盘右键菜单。

纯绿色,无污染。。哈哈。

目前还存在一些不满意的地方就是 图标获取的样式不对,很郁闷.

下面贴出一些关键代码。供大家学习

开机启动设置代码

public class Automatic { //判断是否已经存在此键值,此处可以在Form_Load中来使用。 //如果存在,菜单[开机自动运行]前面可以打上对钩 //如果不存在,则不操作 public bool IsExistKey(string keyName) { bool _exist = false; RegistryKey hklm = Registry.LocalMachine; RegistryKey runs = hklm.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run", true); if (runs == null) { RegistryKey key2 = hklm.CreateSubKey("SOFTWARE"); RegistryKey key3 = key2.CreateSubKey("Microsoft"); RegistryKey key4 = key3.CreateSubKey("Windows"); RegistryKey key5 = key4.CreateSubKey("CurrentVersion"); RegistryKey key6 = key5.CreateSubKey("Run"); runs = key6; } //注意此处用的是GetValueNames() string[] runsName = runs.GetValueNames(); foreach (string strName in runsName) { if (strName.ToUpper() == keyName.ToUpper()) { _exist = true; return _exist; } } return _exist; } public bool WriteKey(string keyName, string keyValue) { RegistryKey hklm = Registry.LocalMachine; //定义hklm指向注册表的LocalMachine,其中Software/Microsoft/Windows/CurrentVersion/Run就是关系到系统中随系统启动而启动的程序,通称启动项 RegistryKey run = hklm.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run",true); try { //将我们的程序加进去 run.SetValue(keyName, keyValue); //注意,一定要关闭,注册表应用。 hklm.Close(); return true; } catch //这是捕获异常的 { return false; } } //删除键值 public void DeleteKey(string keyName) { RegistryKey hklm = Registry.LocalMachine; RegistryKey runs = hklm.OpenSubKey(@"Software/Microsoft/Windows/CurrentVersion/Run", true); try { //注意此处用的是GetValueNames() string[] runsName = runs.GetValueNames(); foreach (string strName in runsName) { if (strName.ToUpper() == keyName.ToUpper()) runs.DeleteValue(strName, false); } } catch { } } }

拖动程序,保存图标方法
private void textBox1_Drop(object sender, DragEventArgs e) { if (e.Data.GetDataPresent(DataFormats.FileDrop, false)) { Icon[] myicon = new Icon[1000]; int currentIndex = 0; String[] files = (String[])e.Data.GetData(DataFormats.FileDrop); foreach (string s in files) { if (System.IO.File.Exists(s)) { string filename = ""; Statis statis = null; if (s.EndsWith(".lnk")) { WshShell shell = new WshShell(); IWshShortcut link = shell.CreateShortcut(s) as IWshShortcut; filename = link.TargetPath; statis = new Statis(); statis.ApplicationPath = filename; } else { statis = new Statis(); statis.ApplicationPath = s; } IntPtr Large, Small; int i, nIcons; Large = (IntPtr)0; Small = (IntPtr)0; nIcons = ExtractIconEx(filename, -1, ref Large, ref Small, 1); Graphics g = Graphics.FromHwnd(new System.Windows.Interop.WindowInteropHelper(new Window1() as Window).Handle);//this.CreateGraphics(); for (i = 0; i < nIcons; i++) { ExtractIconEx(filename, i, ref Large, ref Small, 1); myicon[currentIndex] = System.Drawing.Icon.FromHandle(Large); // g.DrawIcon(myicon[currentIndex], (i / 3) * 40, (i % 3) * 40); g.DrawIcon(myicon[currentIndex], -40, -40); currentIndex++; break; } string name = filename.Substring(filename.LastIndexOf("\\") + 1); string path = ""; FileStream fs; String appStartupPath = System.IO.Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName); for (int j = 0; j < currentIndex; j++) { path = appStartupPath + "/Images/" + name.Remove(name.LastIndexOf(".")) + ".ico"; if (!System.IO.File.Exists(path)) { fs = new FileStream(path, FileMode.Create, FileAccess.ReadWrite); myicon[j].Save(fs); fs.Close(); fs.Dispose(); } } statis.ImagePath = path; this.listBox1.Items.Add(statis); this.listBox1.Items.Refresh(); } } } }

软件截图:

右键菜单

3

 

2

免安装 下载地址:https://files.cnblogs.com/xiaogangqq123/一键启动程序.rar

运行环境:.netFramWork4.0

想看源码的,做程序的人你懂得.表跟我要..

 

本篇文章同步发表在我的独立博客http://xiaogangblog.com/archives/311

欢迎访问.

posted on 2011-08-29 16:25  小刚qq  阅读(4728)  评论(25编辑  收藏  举报