From 2ba2f989bea55a4a4afc27fed1720e9a846af680 Mon Sep 17 00:00:00 2001 From: KoakiMiku <2234975857@qq.com> Date: Wed, 25 Jul 2018 17:52:02 +0800 Subject: [PATCH] judge powerstatus and screen --- wallpaper/Program.cs | 10 ++- wallpaper/Properties/AssemblyInfo.cs | 4 +- wallpaper/RegistryGet.cs | 7 --- wallpaper/Resources/en-US.txt | 2 +- wallpaper/Resources/zh-CN.txt | 2 +- wallpaper/Wallpaper.cs | 68 +++++++-------------- wallpapersetting/Language.cs | 2 +- wallpapersetting/Properties/AssemblyInfo.cs | 4 +- wallpapersetting/RegistryEdit.cs | 20 ------ wallpapersetting/Resources/en-US.txt | 2 + wallpapersetting/Resources/zh-CN.txt | 2 + wallpapersetting/Setting.cs | 53 ++++++---------- wallpapersetting/Setting.designer.cs | 8 --- wallpapersetting/Setting.resx | 21 +++++++ wallpapersetting/Wallpaper.cs | 17 +++--- 15 files changed, 85 insertions(+), 137 deletions(-) diff --git a/wallpaper/Program.cs b/wallpaper/Program.cs index f19b107..fb4b611 100644 --- a/wallpaper/Program.cs +++ b/wallpaper/Program.cs @@ -21,9 +21,8 @@ static void Main(string[] args) Language.Initialize(); if (!File.Exists("mpv.exe")) { - string mpvMessage = Language.GetString("mpvMessage"); - string error = Language.GetString("error"); - MessageBox.Show(mpvMessage, error, MessageBoxButtons.OK, MessageBoxIcon.Error); + MessageBox.Show(Language.GetString("mpvMessage"), Language.GetString("error"), + MessageBoxButtons.OK, MessageBoxIcon.Error); } else { @@ -36,9 +35,8 @@ static void Main(string[] args) } else { - string fileMessage = Language.GetString("fileMessage"); - string error = Language.GetString("error"); - MessageBox.Show(fileMessage, error, MessageBoxButtons.OK, MessageBoxIcon.Error); + MessageBox.Show(Language.GetString("fileMessage"), Language.GetString("error"), + MessageBoxButtons.OK, MessageBoxIcon.Error); } } catch (Exception) diff --git a/wallpaper/Properties/AssemblyInfo.cs b/wallpaper/Properties/AssemblyInfo.cs index f9640da..e4bbb49 100644 --- a/wallpaper/Properties/AssemblyInfo.cs +++ b/wallpaper/Properties/AssemblyInfo.cs @@ -31,5 +31,5 @@ // 可以指定所有值,也可以使用以下所示的 "*" 预置版本号和修订号 // 方法是按如下所示使用“*”: : // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("2.2.2.0")] -[assembly: AssemblyFileVersion("2.2.2.0")] +[assembly: AssemblyVersion("2.3.0.0")] +[assembly: AssemblyFileVersion("2.3.0.0")] diff --git a/wallpaper/RegistryGet.cs b/wallpaper/RegistryGet.cs index 93bd413..abd847a 100644 --- a/wallpaper/RegistryGet.cs +++ b/wallpaper/RegistryGet.cs @@ -5,18 +5,11 @@ namespace wallpaper class RegistryGet { static readonly string settingPath = @"Software\Wallpaper"; - static readonly string excludeListPath = @"Software\Wallpaper\ExcludeList"; public static string GetSetting(string name) { RegistryKey setting = Registry.CurrentUser.OpenSubKey(settingPath); return setting.GetValue(name).ToString(); } - - public static string GetExcludeList(string name) - { - RegistryKey excludeList = Registry.CurrentUser.OpenSubKey(excludeListPath); - return excludeList.GetValue(name).ToString(); - } } } diff --git a/wallpaper/Resources/en-US.txt b/wallpaper/Resources/en-US.txt index 7895dfc..4aa3093 100644 --- a/wallpaper/Resources/en-US.txt +++ b/wallpaper/Resources/en-US.txt @@ -1,4 +1,4 @@ # Message error=Error -mpvMessage=Can't find "Mpv" application. +mpvMessage=Can't find Mpv application. fileMessage=Can't find video file. diff --git a/wallpaper/Resources/zh-CN.txt b/wallpaper/Resources/zh-CN.txt index 0479882..38085c4 100644 --- a/wallpaper/Resources/zh-CN.txt +++ b/wallpaper/Resources/zh-CN.txt @@ -1,4 +1,4 @@ # Message error=错误 -mpvMessage=找不到“Mpv”应用程序。 +mpvMessage=找不到 Mpv 应用程序。 fileMessage=找不到视频文件。 diff --git a/wallpaper/Wallpaper.cs b/wallpaper/Wallpaper.cs index 2d0e17b..09cbf55 100644 --- a/wallpaper/Wallpaper.cs +++ b/wallpaper/Wallpaper.cs @@ -9,25 +9,16 @@ namespace wallpaper class Wallpaper { static Timer timer = new Timer(); - static bool isEnable = false; - static List exclude = new List(); + static string video = string.Empty; + static List list = new List(); public static void Run() { - if (Convert.ToBoolean(RegistryGet.GetSetting("wallpaperExclude"))) + video = RegistryGet.GetSetting("videoLocation"); + if (Convert.ToBoolean(RegistryGet.GetSetting("wallpaperExclude")) && + !string.IsNullOrWhiteSpace(RegistryGet.GetSetting("excludeList"))) { - for (int i = 0; i < int.MaxValue; i++) - { - try - { - string item = RegistryGet.GetExcludeList(i.ToString()); - exclude.Add(item); - } - catch (Exception) - { - break; - } - } + list = RegistryGet.GetSetting("excludeList").Split('|').ToList(); } timer.Interval = 1000; timer.Tick += TimerTick; @@ -36,49 +27,32 @@ public static void Run() private static void TimerTick(object sender, EventArgs e) { - Process[] explorer = Process.GetProcessesByName("explorer"); - Process[] mpv = Process.GetProcessesByName("mpv"); - Process[] process = Process.GetProcesses(); - List list = new List(); - foreach (var item in process) + timer.Enabled = false; + bool power = SystemInformation.PowerStatus.PowerLineStatus == PowerLineStatus.Online; + bool screen = Screen.AllScreens.Count() == 1; + int explorer = Process.GetProcessesByName("explorer").Length; + int mpv = Process.GetProcessesByName("mpv").Length; + int exclude = Process.GetProcesses().Select(item => item.ProcessName).Intersect(list).Count(); + if (power && screen && explorer != 0 && mpv == 0 && exclude == 0) { - list.Add(item.ProcessName); - } - int excludeNum = list.Intersect(exclude).Count(); - if (explorer.Length > 0 && mpv.Length < 1 && excludeNum < 1) - { - timer.Enabled = false; System.Threading.Thread.Sleep(1000); IntPtr hwndShell = WindowsApi.GetShellWindow(); WindowsApi.SendMessageTimeout(hwndShell, 0x52c, IntPtr.Zero, IntPtr.Zero, 0, 1000, IntPtr.Zero); + System.Threading.Thread.Sleep(1000); IntPtr hwndWorkerW = WindowsApi.GetWindow(hwndShell, 3); - string file = "\"" + RegistryGet.GetSetting("videoLocation") + "\""; - string args = " --hwdec=auto --ao=null --loop-file=yes"; Process newMpv = new Process(); newMpv.StartInfo.FileName = "mpv.exe"; - newMpv.StartInfo.Arguments = file + args + " --wid=" + hwndWorkerW; + newMpv.StartInfo.Arguments = $"\"{video}\" --wid={hwndWorkerW} --loop-file=yes --hwdec=auto --ao=null"; newMpv.Start(); - isEnable = true; - timer.Enabled = true; } - else if (excludeNum > 0) + else if ((!power || !screen || exclude > 0) && mpv != 0) { - timer.Enabled = false; - if (isEnable) - { - Stop(); - isEnable = false; - } - timer.Enabled = true; + IntPtr hwndShell = WindowsApi.GetShellWindow(); + IntPtr hwndWorkerW = WindowsApi.GetWindow(hwndShell, 3); + IntPtr hwndMpv = WindowsApi.GetWindow(hwndWorkerW, 5); + WindowsApi.SendMessage(hwndMpv, 0x0010, 0, 0); } - } - - public static void Stop() - { - IntPtr hwndShell = WindowsApi.GetShellWindow(); - IntPtr hwndWorkerW = WindowsApi.GetWindow(hwndShell, 3); - IntPtr hwndMpv = WindowsApi.GetWindow(hwndWorkerW, 5); - WindowsApi.SendMessage(hwndMpv, 0x0010, 0, 0); + timer.Enabled = true; } } } diff --git a/wallpapersetting/Language.cs b/wallpapersetting/Language.cs index f6592be..04714c0 100644 --- a/wallpapersetting/Language.cs +++ b/wallpapersetting/Language.cs @@ -54,7 +54,7 @@ public static string GetString(string value) } } - public static void GetText(System.Windows.Forms.Control control) + public static void GetText(Control control) { try { diff --git a/wallpapersetting/Properties/AssemblyInfo.cs b/wallpapersetting/Properties/AssemblyInfo.cs index 94b811f..680fde2 100644 --- a/wallpapersetting/Properties/AssemblyInfo.cs +++ b/wallpapersetting/Properties/AssemblyInfo.cs @@ -32,5 +32,5 @@ // 可以指定所有值,也可以使用以下所示的 "*" 预置版本号和修订号 // 方法是按如下所示使用“*”: : // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("1.0.0.0")] -[assembly: AssemblyFileVersion("1.0.0.0")] +[assembly: AssemblyVersion("1.1.0.0")] +[assembly: AssemblyFileVersion("1.1.0.0")] diff --git a/wallpapersetting/RegistryEdit.cs b/wallpapersetting/RegistryEdit.cs index ba945a6..f99925d 100644 --- a/wallpapersetting/RegistryEdit.cs +++ b/wallpapersetting/RegistryEdit.cs @@ -6,7 +6,6 @@ namespace wallpapersetting class RegistryEdit { static readonly string settingPath = @"Software\Wallpaper"; - static readonly string excludeListPath = @"Software\Wallpaper\ExcludeList"; static readonly string autorunPath = @"Software\Microsoft\Windows\CurrentVersion\Run"; static readonly string desktopPath = @"DesktopBackground\Shell\Wallpaper"; static readonly string shellPath = @"DesktopBackground\Shell\Wallpaper\Shell"; @@ -47,25 +46,6 @@ public static void RemoveSetting() setting.Close(); } - public static string GetExcludeList(string name) - { - RegistryKey excludeList = Registry.CurrentUser.OpenSubKey(excludeListPath); - return excludeList.GetValue(name).ToString(); - } - - public static void SetExcludeList(string name, string value) - { - RegistryKey excludeList = Registry.CurrentUser.CreateSubKey(excludeListPath); - excludeList.SetValue(name, value); - } - - public static void RemoveExcludeList() - { - RegistryKey excludeList = Registry.CurrentUser; - excludeList.DeleteSubKeyTree(excludeListPath, false); - excludeList.Close(); - } - public static void SetAutorun() { RegistryKey autorun = Registry.CurrentUser.OpenSubKey(autorunPath, true); diff --git a/wallpapersetting/Resources/en-US.txt b/wallpapersetting/Resources/en-US.txt index 363bebd..9f20531 100644 --- a/wallpapersetting/Resources/en-US.txt +++ b/wallpapersetting/Resources/en-US.txt @@ -6,6 +6,7 @@ settingCancel=Cancel # Video videoLabel=Video videoBrowser=Browser +videoFilter=Video File|*.mp4 # Wallpaper wallpaperLabel=General @@ -21,6 +22,7 @@ exitClear=Cleanup excludeLabel=Exclude excludeAdd=Add excludeDelete=Delete +excludeFilter=Application|*.exe # Message information=Information diff --git a/wallpapersetting/Resources/zh-CN.txt b/wallpapersetting/Resources/zh-CN.txt index e1e839c..81bbc40 100644 --- a/wallpapersetting/Resources/zh-CN.txt +++ b/wallpapersetting/Resources/zh-CN.txt @@ -6,6 +6,7 @@ settingCancel=取消 # Video videoLabel=视频 videoBrowser=浏览 +videoFilter=视频文件|*.mp4 # Wallpaper wallpaperLabel=通用 @@ -21,6 +22,7 @@ exitClear=清理 excludeLabel=排除项 excludeAdd=添加 excludeDelete=删除 +excludeFilter=应用程序|*.exe # Message information=通知 diff --git a/wallpapersetting/Setting.cs b/wallpapersetting/Setting.cs index a97d11a..4fb1235 100644 --- a/wallpapersetting/Setting.cs +++ b/wallpapersetting/Setting.cs @@ -2,13 +2,12 @@ using System.ComponentModel; using System.IO; using System.Windows.Forms; +using System.Linq; namespace wallpapersetting { public partial class Setting : Form { - bool isChanged = false; - public Setting() { InitializeComponent(); @@ -31,6 +30,7 @@ private void Initialize() RegistryEdit.SetSetting(wallpaperAutorun.Name, false.ToString()); RegistryEdit.SetSetting(wallpaperMenu.Name, false.ToString()); RegistryEdit.SetSetting(wallpaperExclude.Name, false.ToString()); + RegistryEdit.SetSetting(excludeList.Name, string.Empty); } private void LoadText() @@ -49,6 +49,8 @@ private void LoadText() Language.GetText(excludeDelete); Language.GetText(settingOk); Language.GetText(settingCancel); + videoDialog.Filter = Language.GetString("videoFilter"); + excludeDialog.Filter = Language.GetString("excludeFilter"); } private void LoadSetting() @@ -57,17 +59,9 @@ private void LoadSetting() wallpaperAutorun.Checked = Convert.ToBoolean(RegistryEdit.GetSetting(wallpaperAutorun.Name)); wallpaperMenu.Checked = Convert.ToBoolean(RegistryEdit.GetSetting(wallpaperMenu.Name)); wallpaperExclude.Checked = Convert.ToBoolean(RegistryEdit.GetSetting(wallpaperExclude.Name)); - for (int i = 0; i < int.MaxValue; i++) + if (!string.IsNullOrWhiteSpace(RegistryEdit.GetSetting(excludeList.Name))) { - try - { - string item = RegistryEdit.GetExcludeList(i.ToString()); - excludeList.Items.Add(item); - } - catch (Exception) - { - break; - } + excludeList.Items.AddRange(RegistryEdit.GetSetting(excludeList.Name).Split('|')); } } @@ -82,7 +76,6 @@ private void LoadCheck() private void Changed(object sender, EventArgs e) { settingOk.Enabled = true; - isChanged = true; } private void videoBrowser_Click(object sender, EventArgs e) @@ -123,25 +116,23 @@ private void excludeDelete_Click(object sender, EventArgs e) private void stopClose_Click(object sender, EventArgs e) { Wallpaper.Stop(); - isChanged = false; + settingOk.Enabled = false; Close(); } private void stopClear_Click(object sender, EventArgs e) { - string warning = Language.GetString("warning"); - string exitMessage = Language.GetString("exitMessage"); - DialogResult dialogResult = MessageBox.Show(exitMessage, warning, MessageBoxButtons.OKCancel, MessageBoxIcon.Warning); + DialogResult dialogResult = MessageBox.Show(Language.GetString("exitMessage"), Language.GetString("warning"), + MessageBoxButtons.OKCancel, MessageBoxIcon.Warning); if (dialogResult == DialogResult.OK) { RegistryEdit.RemoveAutorun(); RegistryEdit.RemoveDesktopMenu(); RegistryEdit.RemoveSetting(); Wallpaper.Stop(); - string information = Language.GetString("information"); - string exitDone = Language.GetString("exitDone"); - MessageBox.Show(exitDone, information, MessageBoxButtons.OK, MessageBoxIcon.Information); - isChanged = false; + MessageBox.Show(Language.GetString("exitDone"), Language.GetString("information"), + MessageBoxButtons.OK, MessageBoxIcon.Information); + settingOk.Enabled = false; Close(); } } @@ -170,25 +161,22 @@ private void settingOk_Click(object sender, EventArgs e) if (excludeList.Items.Count == 0) { wallpaperExclude.Checked = false; - RegistryEdit.RemoveExcludeList(); + RegistryEdit.SetSetting(excludeList.Name, string.Empty); } else { - RegistryEdit.RemoveExcludeList(); - for (int i = 0; i < excludeList.Items.Count; i++) - { - RegistryEdit.SetExcludeList(i.ToString(), excludeList.Items[i].ToString()); - } + string exclude = string.Join("|", excludeList.Items.Cast()); + RegistryEdit.SetSetting(excludeList.Name, exclude); } RegistryEdit.SetSetting(wallpaperExclude.Name, wallpaperExclude.Checked.ToString()); Wallpaper.Restart(); - isChanged = false; + settingOk.Enabled = false; Close(); } private void settingCancel_Click(object sender, EventArgs e) { - isChanged = false; + settingOk.Enabled = false; Close(); } @@ -200,11 +188,10 @@ private void Setting_HelpButtonClicked(object sender, CancelEventArgs e) private void Setting_FormClosing(object sender, FormClosingEventArgs e) { - if (isChanged) + if (settingOk.Enabled) { - string information = Language.GetString("information"); - string closeMessage = Language.GetString("closeMessage"); - DialogResult dialogResult = MessageBox.Show(closeMessage, information, MessageBoxButtons.OKCancel, MessageBoxIcon.Information); + DialogResult dialogResult = MessageBox.Show(Language.GetString("closeMessage"), Language.GetString("information"), + MessageBoxButtons.OKCancel, MessageBoxIcon.Information); if (dialogResult != DialogResult.OK) { e.Cancel = true; diff --git a/wallpapersetting/Setting.designer.cs b/wallpapersetting/Setting.designer.cs index 126192a..21a05cb 100644 --- a/wallpapersetting/Setting.designer.cs +++ b/wallpapersetting/Setting.designer.cs @@ -266,14 +266,6 @@ private void InitializeComponent() this.wallpaperExclude.UseVisualStyleBackColor = true; this.wallpaperExclude.CheckedChanged += new System.EventHandler(this.wallpaperExclude_CheckedChanged); // - // videoDialog - // - this.videoDialog.Filter = "mp4|*.mp4"; - // - // excludeDialog - // - this.excludeDialog.Filter = "exe|*.exe"; - // // Setting // this.AutoScaleDimensions = new System.Drawing.SizeF(96F, 96F); diff --git a/wallpapersetting/Setting.resx b/wallpapersetting/Setting.resx index ba1dddd..48ba9bb 100644 --- a/wallpapersetting/Setting.resx +++ b/wallpapersetting/Setting.resx @@ -159,6 +159,18 @@ True + + True + + + True + + + True + + + True + True @@ -168,12 +180,21 @@ True + + True + + + True + True True + + True + 23, 24 diff --git a/wallpapersetting/Wallpaper.cs b/wallpapersetting/Wallpaper.cs index 85bdbb2..896e177 100644 --- a/wallpapersetting/Wallpaper.cs +++ b/wallpapersetting/Wallpaper.cs @@ -18,19 +18,18 @@ public static void Stop() IntPtr hwndShell = WindowsApi.GetShellWindow(); IntPtr hwndWorkerW = WindowsApi.GetWindow(hwndShell, 3); IntPtr hwndMpv = WindowsApi.GetWindow(hwndWorkerW, 5); - WindowsApi.SendMessage(hwndMpv, 0x0010, 0, 0); + if (hwndMpv != IntPtr.Zero) + { + WindowsApi.SendMessage(hwndMpv, 0x0010, 0, 0); + } } public static void Restart() { - Process[] process = Process.GetProcessesByName("wallpaper"); - if (process.Length > 0) - { - Stop(); - Process wallpaper = new Process(); - wallpaper.StartInfo.FileName = "wallpaper.exe"; - wallpaper.Start(); - } + Stop(); + Process wallpaper = new Process(); + wallpaper.StartInfo.FileName = "wallpaper.exe"; + wallpaper.Start(); } } }