win7自动壁纸切换小工具AutoDesk一:初始化托盘
win7的自带主题自动切换壁纸的功能挺不错,美中不足的是图片只能放本地,我一千多张背景图片占了不少磁盘空间,遂萌生写一个类似的自动切换软件,图片则从网上下载。
计划工具分两部分,一是本地切换部分,一是网上下载部分。先说本地切换部分。
本地切换部分,要实现的功能包括:初始化托盘,手动切换,定时切换,开机自启动等功能。
一,初始化托盘
这个功能博客园有很多文章,大家可以参考。基本上实现的功能包括,初始化时最小化到托盘,实现托盘右键菜单功能,双击显示主界面。
托盘的菜单如下图:
主界面就是“下一个”按钮。
拖动notifyicon控件到主窗体,拖动菜单到主窗体,设置notifyicon的菜单项为床窗体菜单。
窗体实现部分如下:
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Linq; 7 using System.Text; 8 using System.Windows.Forms; 9 10 using System.Runtime.InteropServices; 11 using Microsoft.Win32; 12 using System.IO; 13 14 namespace AutoDesk 15 { 16 public partial class AutoDesk : Form 17 { 18 // 存放壁纸的文件位置 19 private string m_deskpick_path = "E:\\deskPic"; 20 private string m_pick_name = "3.jpg"; 21 // 记录快捷键的键码 22 private int Hotkey1; 23 // 记录是否关闭窗体。 24 private DialogResult m_dialogResult = DialogResult.No; 25 26 27 public AutoDesk() 28 { 29 InitializeComponent(); 30 31 } 32 33 /// <summary> 34 /// 从m_deskpick_path文件夹中随即选取一个文件 35 /// </summary> 36 /// <returns></returns> 37 private string get_random_pick_name() 38 { 39 Random random = new Random(Environment.TickCount); 40 string pick_name = "3.jpg"; 41 DirectoryInfo dInfo = new DirectoryInfo(m_deskpick_path); 42 FileInfo[] files = dInfo.GetFiles();//"*.jpg|*.gif|*.png|*.bmp"); 43 if (files != null && files.Length > 0) 44 { 45 int index = random.Next(0, files.Length); 46 pick_name = files[index].Name; 47 } 48 return pick_name; 49 } 50 51 /// <summary> 52 /// 通过IActiveDesktop接口切换桌面, 53 /// IActiveDesktop接口切换桌面具有win7的渐变效果(fade effect或者称为slideshow) 54 /// 而通过SystemParametersInfo函数也能实现切换壁纸,但是没有渐变效果。 55 /// 56 /// </summary> 57 private void switch_to_next_waller() 58 { 59 string strSavePath = Path.Combine(m_deskpick_path, get_random_pick_name()); 60 //WinAPI.SystemParametersInfo(20, 1, strSavePath, 1); 61 IActiveDesktop iad = shlobj.GetActiveDesktop(); 62 iad.SetWallpaper(strSavePath, 0); 63 iad.ApplyChanges(AD_Apply.ALL | AD_Apply.FORCE | AD_Apply.BUFFERED_REFRESH); 64 System.Runtime.InteropServices.Marshal.ReleaseComObject(iad); 65 } 66 67 /// <summary> 68 /// 主界面切换下一桌面的实现。 69 /// </summary> 70 /// <param name="sender"></param> 71 /// <param name="e"></param> 72 private void btn_center_Click(object sender, EventArgs e) 73 { 74 try 75 { 76 switch_to_next_waller(); 77 } 78 catch (Exception ex) 79 { 80 MessageBox.Show(ex.Message); 81 } 82 } 83 84 /// <summary> 85 /// 最小化到托盘,要实现初始化到托盘,需要设置窗体的初始状态为最小化。 86 /// </summary> 87 /// <param name="sender"></param> 88 /// <param name="e"></param> 89 private void AutoDesk_SizeChanged(object sender, EventArgs e) 90 { 91 try 92 { 93 if (this.WindowState == FormWindowState.Minimized) 94 { 95 this.Hide(); 96 this.notifyIcon1.Visible = true; 97 } 98 } 99 catch (Exception ex) 100 { 101 MessageBox.Show(ex.Message); 102 } 103 } 104 105 /// <summary> 106 /// 107 /// </summary> 108 /// <param name="sender"></param> 109 /// <param name="e"></param> 110 private void notifyIcon1_Click(object sender, EventArgs e) 111 { 112 try 113 { 114 //this.Visible = true; 115 //this.WindowState = FormWindowState.Normal; 116 //this.notifyIcon1.Visible = false; 117 } 118 catch (Exception ex) 119 { 120 MessageBox.Show(ex.Message); 121 } 122 } 123 124 /// <summary> 125 /// 菜单项实现切换到下一桌面 126 /// </summary> 127 /// <param name="sender"></param> 128 /// <param name="e"></param> 129 private void NextToolStripMenuItem_Click(object sender, EventArgs e) 130 { 131 try 132 { 133 switch_to_next_waller(); 134 } 135 catch (Exception ex) 136 { 137 MessageBox.Show(ex.Message); 138 } 139 140 } 141 142 /// <summary> 143 /// 菜单项退出应用程序 144 /// </summary> 145 /// <param name="sender"></param> 146 /// <param name="e"></param> 147 private void exitToolStripMenuItem_Click(object sender, EventArgs e) 148 { 149 try 150 { 151 m_dialogResult = System.Windows.Forms.DialogResult.Yes; 152 this.Close(); 153 } 154 catch (Exception ex) 155 { 156 MessageBox.Show(ex.Message); 157 } 158 } 159 160 /// <summary> 161 /// 菜单项显示主界面 162 /// </summary> 163 /// <param name="sender"></param> 164 /// <param name="e"></param> 165 private void showMainFrmToolStripMenuItem_Click(object sender, EventArgs e) 166 { 167 try 168 { 169 this.Visible = true; 170 this.WindowState = FormWindowState.Normal; 171 172 } 173 catch (Exception ex) 174 { 175 MessageBox.Show(ex.Message); 176 } 177 } 178 179 /// <summary> 180 /// 窗体关闭相应事件。如果是点击窗体的关闭按钮,则只是隐藏。 181 /// </summary> 182 /// <param name="sender"></param> 183 /// <param name="e"></param> 184 private void AutoDesk_FormClosing(object sender, FormClosingEventArgs e) 185 { 186 try 187 { 188 if (m_dialogResult == System.Windows.Forms.DialogResult.Yes) 189 { 190 e.Cancel = false; 191 Application.Exit(); 192 } 193 else 194 { 195 e.Cancel = true; 196 this.Hide(); 197 } 198 } 199 catch (Exception ex) 200 { 201 MessageBox.Show(ex.Message); 202 } 203 } 204 205 /// <summary> 206 /// 窗体启动时挂载全局快捷键Ctrl+F1. 207 /// </summary> 208 /// <param name="sender"></param> 209 /// <param name="e"></param> 210 private void AutoDesk_Load(object sender, EventArgs e) 211 { 212 try 213 { 214 215 SystemHotKey.Hotkey hotkey; 216 hotkey = new SystemHotKey.Hotkey(this.Handle); 217 218 //定义快键(Ctrl + F1) 219 Hotkey1 = hotkey.RegisterHotkey(System.Windows.Forms.Keys.F1, SystemHotKey.Hotkey.KeyFlags.MOD_CONTROL); 220 hotkey.OnHotkey += new SystemHotKey.HotkeyEventHandler(OnHotkey); 221 222 } 223 catch (Exception ex) 224 { 225 MessageBox.Show(ex.Message); 226 } 227 } 228 229 /// <summary> 230 /// 快捷键按下后切换到下一个随即壁纸。 231 /// </summary> 232 /// <param name="HotkeyID"></param> 233 public void OnHotkey(int HotkeyID) 234 { 235 try 236 { 237 if (HotkeyID == Hotkey1) 238 { 239 switch_to_next_waller(); 240 } 241 else 242 { 243 //this.Visible = false; 244 } 245 } 246 catch (Exception ex) 247 { 248 MessageBox.Show(ex.Message); 249 } 250 } 251 252 /// <summary> 253 /// 托盘双击显示主界面 254 /// </summary> 255 /// <param name="sender"></param> 256 /// <param name="e"></param> 257 private void notifyIcon1_DoubleClick(object sender, EventArgs e) 258 { 259 try 260 { 261 this.Visible = true; 262 this.WindowState = FormWindowState.Normal; 263 } 264 catch (Exception ex) 265 { 266 MessageBox.Show(ex.Message); 267 } 268 } 269 } 270 }