1
0

SecondsWindow.xaml.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. using GeekDesk.Util;
  2. using Gma.System.MouseKeyHook;
  3. using ShowSeconds.Common;
  4. using GeekDesk.Util;
  5. using ShowSeconds.ViewModel;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Configuration;
  9. using System.Drawing;
  10. using System.Linq;
  11. using System.Text;
  12. using System.Threading;
  13. using System.Threading.Tasks;
  14. using System.Timers;
  15. using System.Windows;
  16. using System.Windows.Controls;
  17. using System.Windows.Data;
  18. using System.Windows.Documents;
  19. using System.Windows.Input;
  20. using System.Windows.Media;
  21. using System.Windows.Media.Imaging;
  22. using System.Windows.Navigation;
  23. using System.Windows.Shapes;
  24. using System.Windows.Threading;
  25. using GeekDesk.MyThread;
  26. using GeekDesk;
  27. using System.Collections;
  28. namespace ShowSeconds
  29. {
  30. /// <summary>
  31. /// Interaction logic for MainWindow.xaml
  32. /// </summary>
  33. public partial class SecondsWindow : Window
  34. {
  35. private System.Drawing.Color beforeColor;
  36. private System.Drawing.Color topBeforeColor;
  37. private bool expandClock = true; //是否展开时钟
  38. private System.Windows.Forms.Timer timer;
  39. private static double lProportion = 0.82;
  40. private static double tProportion = 0.03;
  41. private static int sleepTime = 1000;
  42. public SecondsWindow()
  43. {
  44. SecondsDataContext dc = new SecondsDataContext
  45. {
  46. Seconds = (DateTime.Now.Hour).ToString() + ":" +
  47. FormatMS(DateTime.Now.Minute) + ":" +
  48. FormatMS(DateTime.Now.Second)
  49. };
  50. InitializeComponent();
  51. SolidColorBrush scb = new SolidColorBrush(System.Windows.Media.Color.FromArgb(255, 47, 52, 44))
  52. {
  53. Opacity = 0.8
  54. };
  55. try
  56. {
  57. Hashtable settings = (Hashtable)ConfigurationManager.GetSection("ShowSecondsSettings");
  58. lProportion = Convert.ToDouble(settings["LProportion"]);
  59. tProportion = Convert.ToDouble(settings["TProportion"]);
  60. sleepTime = Convert.ToInt32(settings["DelayTime"]);
  61. }
  62. catch (Exception ex)
  63. {
  64. lProportion = 0.82;
  65. tProportion = 0.03;
  66. sleepTime = 1000;
  67. }
  68. BGBorder.Background = scb;
  69. this.DataContext = dc;
  70. this.Topmost = true;
  71. BGBorder.Visibility = Visibility.Collapsed;
  72. this.Show();
  73. }
  74. private void Window_Loaded(object sender, RoutedEventArgs e)
  75. {
  76. timer = new System.Windows.Forms.Timer();
  77. timer.Interval = 1000;
  78. timer.Tick += Timer_Tick;
  79. Dispatcher secondsDP = DispatcherBuild.Build();
  80. IKeyboardMouseEvents secondsHook = Hook.GlobalEvents();
  81. secondsDP.Invoke((Action)(() =>
  82. {
  83. secondsHook.MouseDownExt += SecondsBakColorFun;
  84. secondsHook.MouseUpExt += SecondsHookSetFuc;
  85. }));
  86. HideWindowUtil.HideAltTab(this);
  87. }
  88. private void Timer_Tick(object sender, EventArgs e)
  89. {
  90. string str = (DateTime.Now.Hour).ToString() + ":" +
  91. FormatMS(DateTime.Now.Minute) + ":" +
  92. FormatMS(DateTime.Now.Second);
  93. SecondsDataContext dc = this.DataContext as SecondsDataContext;
  94. dc.Seconds = str;
  95. }
  96. private static string FormatMS(int ms)
  97. {
  98. if (ms < 10)
  99. {
  100. return "0" + ms;
  101. }
  102. else
  103. {
  104. return ms.ToString();
  105. }
  106. }
  107. private void SecondsHookSetFuc(object sender, MouseEventExtArgs e)
  108. {
  109. if (e.Button == System.Windows.Forms.MouseButtons.Left)
  110. {
  111. if (ScreenUtil.IsPrimaryFullScreen()) return;
  112. App.Current.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Render, new Action(() =>
  113. {
  114. int x = e.X;
  115. int y = e.Y;
  116. double w = 1920;
  117. double h = 1080;
  118. double width = SystemParameters.PrimaryScreenWidth;
  119. double height = SystemParameters.PrimaryScreenHeight;
  120. if (x > 1843 / w * width
  121. && x < 1907 / w * width
  122. && y > 1037 / h * height
  123. && y < 1074 / h * height)
  124. {
  125. System.Drawing.Color c;
  126. int count = sleepTime;
  127. do
  128. {
  129. c = GetBottomBeforeColor();
  130. if (c.A != beforeColor.A
  131. || c.R != beforeColor.R
  132. || c.G != beforeColor.G
  133. || c.B != beforeColor.B)
  134. {
  135. break;
  136. }
  137. Thread.Sleep(50);
  138. count -= 50;
  139. } while (count > 0);
  140. if (c.A != beforeColor.A
  141. || c.R != beforeColor.R
  142. || c.G != beforeColor.G
  143. || c.B != beforeColor.B)
  144. {
  145. //判断是否展开时钟
  146. System.Drawing.Color ct = GetTopBeforeColor();
  147. if (ct.A != topBeforeColor.A
  148. || ct.R != topBeforeColor.R
  149. || ct.G != topBeforeColor.G
  150. || ct.B != topBeforeColor.B)
  151. {
  152. expandClock = true;
  153. }
  154. else
  155. {
  156. expandClock = false;
  157. }
  158. if (!BGBorder.IsVisible)
  159. {
  160. System.Drawing.Color theamColor = GetColor(1919, 1079);
  161. if (CalculateLight(theamColor) > 255 / 2)
  162. {
  163. //light
  164. BGBorder.Background = new SolidColorBrush(System.Windows.Media.Color.FromArgb(theamColor.A, theamColor.R, theamColor.G, theamColor.B));
  165. SecondsText.Foreground = Constants.lightFont;
  166. }
  167. else
  168. {
  169. // dark
  170. //BGBorder.Background = new SolidColorBrush(System.Windows.Media.Color.FromArgb(theamColor.A, theamColor.R, theamColor.G, theamColor.B));
  171. BGBorder.Background = Constants.darkBG;
  172. SecondsText.Foreground = Constants.darkFont;
  173. }
  174. SecondsDataContext dc = this.DataContext as SecondsDataContext;
  175. dc.Seconds = (DateTime.Now.Hour).ToString() + ":" +
  176. FormatMS(DateTime.Now.Minute) + ":" +
  177. FormatMS(DateTime.Now.Second);
  178. int sx = (int)(SystemParameters.PrimaryScreenWidth * lProportion);
  179. int sMarginBottom = (int)(SystemParameters.WorkArea.Height * tProportion);
  180. Left = sx - Width;
  181. Top = SystemParameters.WorkArea.Height - Height;
  182. BGBorder.Visibility = Visibility.Visible;
  183. timer.Start();
  184. }
  185. else
  186. {
  187. BGBorder.Visibility= Visibility.Collapsed;
  188. timer.Stop();
  189. }
  190. }
  191. }
  192. else if (true)
  193. {
  194. if ((expandClock && (x < 1574 / w * width
  195. || x > 1906 / w * width
  196. || y < 598 / h * height
  197. || y > 1020 / h * height)
  198. )
  199. || !expandClock && (x < 1574 / w * width
  200. || x > 1906 / w * width
  201. || y < 950 / h * height
  202. || y > 1020 / h * height)
  203. )
  204. {
  205. BGBorder.Visibility = Visibility.Collapsed;
  206. timer.Stop();
  207. }
  208. }
  209. }));
  210. }
  211. }
  212. private static System.Windows.Window window = null;
  213. public static void ShowWindow()
  214. {
  215. try
  216. {
  217. if (window == null || !window.Activate())
  218. {
  219. window = new SecondsWindow();
  220. }
  221. } catch (Exception e)
  222. {
  223. LogUtil.WriteErrorLog(e, "打开显秒窗口异常!");
  224. }
  225. }
  226. public static void CloseWindow()
  227. {
  228. try
  229. {
  230. window.Close();
  231. } catch (Exception e)
  232. {
  233. LogUtil.WriteErrorLog(e, "关闭显秒窗口异常!");
  234. }
  235. }
  236. private void SecondsBakColorFun(object sender, MouseEventExtArgs e)
  237. {
  238. if (e.Button == System.Windows.Forms.MouseButtons.Left)
  239. {
  240. beforeColor = GetBottomBeforeColor();
  241. topBeforeColor = GetTopBeforeColor();
  242. }
  243. }
  244. private static System.Drawing.Color GetBottomBeforeColor()
  245. {
  246. return GetColor(1760, 985);
  247. }
  248. private static System.Drawing.Color GetTopBeforeColor()
  249. {
  250. return GetColor(1751, 693);
  251. }
  252. private static System.Drawing.Color GetColor(int w2, int h2)
  253. {
  254. double w = 1920;
  255. double h = 1080;
  256. double width = SystemParameters.PrimaryScreenWidth;
  257. double height = SystemParameters.PrimaryScreenHeight;
  258. System.Drawing.Point p = new System.Drawing.Point((int)(w2 / w * width), (int)(h2 / h * height));
  259. return ScreenUtil.GetColorAt(p);
  260. }
  261. private static int CalculateLight(System.Drawing.Color color)
  262. {
  263. int[] colorArr = new int[] { color.R, color.G, color.B };
  264. int max = 0;
  265. int min = 255;
  266. foreach (int i in colorArr)
  267. {
  268. max = Math.Max(max, i);
  269. min = Math.Min(min, i);
  270. }
  271. int avg = (max + min) / 2;
  272. return avg;
  273. }
  274. }
  275. }