Skip to content
This repository has been archived by the owner on May 13, 2024. It is now read-only.

Commit

Permalink
judge powerstatus and screen
Browse files Browse the repository at this point in the history
  • Loading branch information
KoakiMiku committed Jul 25, 2018
1 parent 7e7ef1c commit 2ba2f98
Show file tree
Hide file tree
Showing 15 changed files with 85 additions and 137 deletions.
10 changes: 4 additions & 6 deletions wallpaper/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions wallpaper/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
7 changes: 0 additions & 7 deletions wallpaper/RegistryGet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
}
2 changes: 1 addition & 1 deletion wallpaper/Resources/en-US.txt
Original file line number Diff line number Diff line change
@@ -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.
2 changes: 1 addition & 1 deletion wallpaper/Resources/zh-CN.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Message
error=错误
mpvMessage=找不到Mpv应用程序。
mpvMessage=找不到 Mpv 应用程序。
fileMessage=找不到视频文件。
68 changes: 21 additions & 47 deletions wallpaper/Wallpaper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,16 @@ namespace wallpaper
class Wallpaper
{
static Timer timer = new Timer();
static bool isEnable = false;
static List<string> exclude = new List<string>();
static string video = string.Empty;
static List<string> list = new List<string>();

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;
Expand All @@ -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<string> list = new List<string>();
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;
}
}
}
2 changes: 1 addition & 1 deletion wallpapersetting/Language.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
4 changes: 2 additions & 2 deletions wallpapersetting/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
20 changes: 0 additions & 20 deletions wallpapersetting/RegistryEdit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions wallpapersetting/Resources/en-US.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ settingCancel=Cancel
# Video
videoLabel=Video
videoBrowser=Browser
videoFilter=Video File|*.mp4

# Wallpaper
wallpaperLabel=General
Expand All @@ -21,6 +22,7 @@ exitClear=Cleanup
excludeLabel=Exclude
excludeAdd=Add
excludeDelete=Delete
excludeFilter=Application|*.exe

# Message
information=Information
Expand Down
2 changes: 2 additions & 0 deletions wallpapersetting/Resources/zh-CN.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ settingCancel=取消
# Video
videoLabel=视频
videoBrowser=浏览
videoFilter=视频文件|*.mp4

# Wallpaper
wallpaperLabel=通用
Expand All @@ -21,6 +22,7 @@ exitClear=清理
excludeLabel=排除项
excludeAdd=添加
excludeDelete=删除
excludeFilter=应用程序|*.exe

# Message
information=通知
Expand Down
53 changes: 20 additions & 33 deletions wallpapersetting/Setting.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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()
Expand All @@ -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()
Expand All @@ -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('|'));
}
}

Expand All @@ -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)
Expand Down Expand Up @@ -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();
}
}
Expand Down Expand Up @@ -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<string>());
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();
}

Expand All @@ -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;
Expand Down
8 changes: 0 additions & 8 deletions wallpapersetting/Setting.designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 2ba2f98

Please sign in to comment.