优化代码

This commit is contained in:
BookerLiu
2022-09-01 15:23:44 +08:00
parent e82af431b5
commit d5e0dc98db
14 changed files with 576 additions and 83 deletions

View File

@@ -0,0 +1,39 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ShowSeconds.Common
{
public class Constants
{
public readonly static string MY_UUID = "8400A17AEEF7C029";
//dark theam
public readonly static System.Windows.Media.SolidColorBrush darkBG
= new System.Windows.Media.SolidColorBrush
{
Color = System.Windows.Media.Color.FromRgb(46, 50, 54),
Opacity = 0.8
};
public readonly static System.Windows.Media.SolidColorBrush darkFont
= new System.Windows.Media.SolidColorBrush
{
Color = System.Windows.Media.Color.FromRgb(255, 255, 255)
};
//light theam
public readonly static System.Windows.Media.SolidColorBrush lightBG
= new System.Windows.Media.SolidColorBrush
{
Color = System.Windows.Media.Color.FromRgb(236, 244, 251),
Opacity = 1
};
public readonly static System.Windows.Media.SolidColorBrush lightFont
= new System.Windows.Media.SolidColorBrush
{
Color = System.Windows.Media.Color.FromRgb(65, 63, 61),
};
}
}

View File

@@ -0,0 +1,32 @@
<Window x:Class="ShowSeconds.SecondsWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:hc="https://handyorg.github.io/handycontrol"
xmlns:xf="clr-namespace:XamlFlair;assembly=XamlFlair.WPF"
Height="90"
Width="160"
WindowStyle="None"
Title=""
AllowsTransparency="True"
Background="Transparent" ShowInTaskbar="False"
Focusable="True"
Loaded="Window_Loaded"
>
<Grid Margin="20">
<Grid.Effect>
<DropShadowEffect BlurRadius="20" Direction="-90" Color="Gray"
RenderingBias="Quality" ShadowDepth="2"/>
</Grid.Effect>
<Border x:Name="BGBorder" CornerRadius="8" xf:Animations.Primary="{xf:Animate BasedOn={StaticResource FadeInAndSlideFromRight}, Event=Visibility, Delay=0}">
<TextBlock x:Name="SecondsText" HorizontalAlignment="Center"
VerticalAlignment="Center"
FontSize="25"
FontWeight="Bold"
Foreground="White"
Text="{Binding Seconds}"/>
</Border>
</Grid>
</Window>

View File

@@ -0,0 +1,312 @@
using GeekDesk.Util;
using Gma.System.MouseKeyHook;
using ShowSeconds.Common;
using GeekDesk.Util;
using ShowSeconds.ViewModel;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Timers;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
using GeekDesk.MyThread;
using GeekDesk;
using System.Collections;
namespace ShowSeconds
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class SecondsWindow : Window
{
private System.Drawing.Color beforeColor;
private System.Drawing.Color topBeforeColor;
private bool expandClock = true; //是否展开时钟
private System.Windows.Forms.Timer timer;
private static double lProportion = 0.82;
private static double tProportion = 0.03;
private static int sleepTime = 1000;
public SecondsWindow()
{
SecondsDataContext dc = new SecondsDataContext
{
Seconds = (DateTime.Now.Hour).ToString() + ":" +
FormatMS(DateTime.Now.Minute) + ":" +
FormatMS(DateTime.Now.Second)
};
InitializeComponent();
SolidColorBrush scb = new SolidColorBrush(System.Windows.Media.Color.FromArgb(255, 47, 52, 44))
{
Opacity = 0.8
};
try
{
Hashtable settings = (Hashtable)ConfigurationManager.GetSection("ShowSecondsSettings");
lProportion = Convert.ToDouble(settings["LProportion"]);
tProportion = Convert.ToDouble(settings["TProportion"]);
sleepTime = Convert.ToInt32(settings["DelayTime"]);
}
catch (Exception ex)
{
lProportion = 0.82;
tProportion = 0.03;
sleepTime = 1000;
}
BGBorder.Background = scb;
this.DataContext = dc;
this.Topmost = true;
BGBorder.Visibility = Visibility.Collapsed;
this.Show();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
timer = new System.Windows.Forms.Timer();
timer.Interval = 1000;
timer.Tick += Timer_Tick;
Dispatcher secondsDP = DispatcherBuild.Build();
IKeyboardMouseEvents secondsHook = Hook.GlobalEvents();
secondsDP.Invoke((Action)(() =>
{
secondsHook.MouseDownExt += SecondsBakColorFun;
secondsHook.MouseUpExt += SecondsHookSetFuc;
}));
HideWindowUtil.HideAltTab(this);
}
private void Timer_Tick(object sender, EventArgs e)
{
string str = (DateTime.Now.Hour).ToString() + ":" +
FormatMS(DateTime.Now.Minute) + ":" +
FormatMS(DateTime.Now.Second);
SecondsDataContext dc = this.DataContext as SecondsDataContext;
dc.Seconds = str;
}
private static string FormatMS(int ms)
{
if (ms < 10)
{
return "0" + ms;
}
else
{
return ms.ToString();
}
}
private void SecondsHookSetFuc(object sender, MouseEventExtArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
if (ScreenUtil.IsPrimaryFullScreen()) return;
App.Current.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Render, new Action(() =>
{
int x = e.X;
int y = e.Y;
double w = 1920;
double h = 1080;
double width = SystemParameters.PrimaryScreenWidth;
double height = SystemParameters.PrimaryScreenHeight;
if (x > 1843 / w * width
&& x < 1907 / w * width
&& y > 1037 / h * height
&& y < 1074 / h * height)
{
System.Drawing.Color c;
int count = sleepTime;
do
{
c = GetBottomBeforeColor();
if (c.A != beforeColor.A
|| c.R != beforeColor.R
|| c.G != beforeColor.G
|| c.B != beforeColor.B)
{
break;
}
Thread.Sleep(50);
count -= 50;
} while (count > 0);
if (c.A != beforeColor.A
|| c.R != beforeColor.R
|| c.G != beforeColor.G
|| c.B != beforeColor.B)
{
//判断是否展开时钟
System.Drawing.Color ct = GetTopBeforeColor();
if (ct.A != topBeforeColor.A
|| ct.R != topBeforeColor.R
|| ct.G != topBeforeColor.G
|| ct.B != topBeforeColor.B)
{
expandClock = true;
}
else
{
expandClock = false;
}
if (!BGBorder.IsVisible)
{
System.Drawing.Color theamColor = GetColor(1919, 1079);
if (CalculateLight(theamColor) > 255 / 2)
{
//light
BGBorder.Background = new SolidColorBrush(System.Windows.Media.Color.FromArgb(theamColor.A, theamColor.R, theamColor.G, theamColor.B));
SecondsText.Foreground = Constants.lightFont;
}
else
{
// dark
//BGBorder.Background = new SolidColorBrush(System.Windows.Media.Color.FromArgb(theamColor.A, theamColor.R, theamColor.G, theamColor.B));
BGBorder.Background = Constants.darkBG;
SecondsText.Foreground = Constants.darkFont;
}
SecondsDataContext dc = this.DataContext as SecondsDataContext;
dc.Seconds = (DateTime.Now.Hour).ToString() + ":" +
FormatMS(DateTime.Now.Minute) + ":" +
FormatMS(DateTime.Now.Second);
int sx = (int)(SystemParameters.PrimaryScreenWidth * lProportion);
int sMarginBottom = (int)(SystemParameters.WorkArea.Height * tProportion);
Left = sx - Width;
Top = SystemParameters.WorkArea.Height - Height;
BGBorder.Visibility = Visibility.Visible;
timer.Start();
}
else
{
BGBorder.Visibility= Visibility.Collapsed;
timer.Stop();
}
}
}
else if (true)
{
if ((expandClock && (x < 1574 / w * width
|| x > 1906 / w * width
|| y < 598 / h * height
|| y > 1020 / h * height)
)
|| !expandClock && (x < 1574 / w * width
|| x > 1906 / w * width
|| y < 950 / h * height
|| y > 1020 / h * height)
)
{
BGBorder.Visibility = Visibility.Collapsed;
timer.Stop();
}
}
}));
}
}
private static System.Windows.Window window = null;
public static void ShowWindow()
{
try
{
if (window == null || !window.Activate())
{
window = new SecondsWindow();
}
} catch (Exception e)
{
LogUtil.WriteErrorLog(e, "打开显秒窗口异常!");
}
}
public static void CloseWindow()
{
try
{
window.Close();
} catch (Exception e)
{
LogUtil.WriteErrorLog(e, "关闭显秒窗口异常!");
}
}
private void SecondsBakColorFun(object sender, MouseEventExtArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
beforeColor = GetBottomBeforeColor();
topBeforeColor = GetTopBeforeColor();
}
}
private static System.Drawing.Color GetBottomBeforeColor()
{
return GetColor(1760, 985);
}
private static System.Drawing.Color GetTopBeforeColor()
{
return GetColor(1751, 693);
}
private static System.Drawing.Color GetColor(int w2, int h2)
{
double w = 1920;
double h = 1080;
double width = SystemParameters.PrimaryScreenWidth;
double height = SystemParameters.PrimaryScreenHeight;
System.Drawing.Point p = new System.Drawing.Point((int)(w2 / w * width), (int)(h2 / h * height));
return ScreenUtil.GetColorAt(p);
}
private static int CalculateLight(System.Drawing.Color color)
{
int[] colorArr = new int[] { color.R, color.G, color.B };
int max = 0;
int min = 255;
foreach (int i in colorArr)
{
max = Math.Max(max, i);
min = Math.Min(min, i);
}
int avg = (max + min) / 2;
return avg;
}
}
}

View File

@@ -0,0 +1,25 @@
using System.ComponentModel;
namespace ShowSeconds.ViewModel
{
public class SecondsDataContext : INotifyPropertyChanged
{
private string seconds;
public string Seconds
{
set
{
seconds = value;
OnPropertyChanged("Seconds");
}
get { return seconds; }
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
}