SecondsWindow.xaml.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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. //IntPtr taskBarWnd = WindowUtil.FindWindow("Shell_TrayWnd", null);
  110. //IntPtr tray = WindowUtil.FindWindowEx(taskBarWnd, IntPtr.Zero, "TrayNotifyWnd", null);
  111. ////IntPtr trayclock = WindowUtil.FindWindowEx(tray, IntPtr.Zero, "TrayClockWClass", null);
  112. //IntPtr trayclock = WindowUtil.GetForegroundWindow();
  113. //StringBuilder title = new StringBuilder(256);
  114. //WindowUtil.GetWindowText(trayclock, title, title.Capacity);//得到窗口的标题
  115. ////Console.WriteLine(title.ToString());
  116. //if (title.Equals("通知中心"))
  117. //{
  118. //}
  119. if (e.Button == System.Windows.Forms.MouseButtons.Left)
  120. {
  121. if (ScreenUtil.IsPrimaryFullScreen()) return;
  122. App.Current.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Render, new Action(() =>
  123. {
  124. try
  125. {
  126. int x = e.X;
  127. int y = e.Y;
  128. double w = 1920;
  129. double h = 1080;
  130. double width = SystemParameters.PrimaryScreenWidth;
  131. double height = SystemParameters.PrimaryScreenHeight;
  132. if (x > 1843 / w * width
  133. && x < 1907 / w * width
  134. && y > 1037 / h * height
  135. && y < 1074 / h * height)
  136. {
  137. System.Drawing.Color c;
  138. int count = sleepTime;
  139. do
  140. {
  141. c = GetBottomBeforeColor();
  142. if (c.A != beforeColor.A
  143. || c.R != beforeColor.R
  144. || c.G != beforeColor.G
  145. || c.B != beforeColor.B)
  146. {
  147. break;
  148. }
  149. Thread.Sleep(50);
  150. count -= 50;
  151. } while (count > 0);
  152. if (c.A != beforeColor.A
  153. || c.R != beforeColor.R
  154. || c.G != beforeColor.G
  155. || c.B != beforeColor.B)
  156. {
  157. //判断是否展开时钟
  158. System.Drawing.Color ct = GetTopBeforeColor();
  159. if (ct.A != topBeforeColor.A
  160. || ct.R != topBeforeColor.R
  161. || ct.G != topBeforeColor.G
  162. || ct.B != topBeforeColor.B)
  163. {
  164. expandClock = true;
  165. }
  166. else
  167. {
  168. expandClock = false;
  169. }
  170. if (!BGBorder.IsVisible)
  171. {
  172. System.Drawing.Color theamColor = GetColor(1919, 1079);
  173. if (CalculateLight(theamColor) > 255 / 2)
  174. {
  175. //light
  176. BGBorder.Background = new SolidColorBrush(System.Windows.Media.Color.FromArgb(theamColor.A, theamColor.R, theamColor.G, theamColor.B));
  177. SecondsText.Foreground = Constants.lightFont;
  178. }
  179. else
  180. {
  181. // dark
  182. //BGBorder.Background = new SolidColorBrush(System.Windows.Media.Color.FromArgb(theamColor.A, theamColor.R, theamColor.G, theamColor.B));
  183. BGBorder.Background = Constants.darkBG;
  184. SecondsText.Foreground = Constants.darkFont;
  185. }
  186. SecondsDataContext dc = this.DataContext as SecondsDataContext;
  187. dc.Seconds = (DateTime.Now.Hour).ToString() + ":" +
  188. FormatMS(DateTime.Now.Minute) + ":" +
  189. FormatMS(DateTime.Now.Second);
  190. int sx = (int)(SystemParameters.PrimaryScreenWidth * lProportion);
  191. int sMarginBottom = (int)(SystemParameters.WorkArea.Height * tProportion);
  192. Left = sx - Width;
  193. Top = SystemParameters.WorkArea.Height - Height;
  194. BGBorder.Visibility = Visibility.Visible;
  195. timer.Start();
  196. }
  197. else
  198. {
  199. BGBorder.Visibility = Visibility.Collapsed;
  200. timer.Stop();
  201. }
  202. }
  203. }
  204. else if (true)
  205. {
  206. if ((expandClock && (x < 1574 / w * width
  207. || x > 1906 / w * width
  208. || y < 598 / h * height
  209. || y > 1020 / h * height)
  210. )
  211. || !expandClock && (x < 1574 / w * width
  212. || x > 1906 / w * width
  213. || y < 950 / h * height
  214. || y > 1020 / h * height)
  215. )
  216. {
  217. BGBorder.Visibility = Visibility.Collapsed;
  218. timer.Stop();
  219. }
  220. }
  221. }
  222. catch (Exception e1) { }
  223. }));
  224. }
  225. }
  226. private static System.Windows.Window window = null;
  227. public static void ShowWindow()
  228. {
  229. try
  230. {
  231. if (window == null || !window.Activate())
  232. {
  233. window = new SecondsWindow();
  234. }
  235. } catch (Exception e)
  236. {
  237. LogUtil.WriteErrorLog(e, "打开显秒窗口异常!");
  238. }
  239. }
  240. public static void CloseWindow()
  241. {
  242. try
  243. {
  244. window.Close();
  245. } catch (Exception e)
  246. {
  247. LogUtil.WriteErrorLog(e, "关闭显秒窗口异常!");
  248. }
  249. }
  250. private void SecondsBakColorFun(object sender, MouseEventExtArgs e)
  251. {
  252. if (e.Button == System.Windows.Forms.MouseButtons.Left)
  253. {
  254. beforeColor = GetBottomBeforeColor();
  255. topBeforeColor = GetTopBeforeColor();
  256. }
  257. }
  258. private static System.Drawing.Color GetBottomBeforeColor()
  259. {
  260. return GetColor(1760, 985);
  261. }
  262. private static System.Drawing.Color GetTopBeforeColor()
  263. {
  264. return GetColor(1751, 693);
  265. }
  266. private static System.Drawing.Color GetColor(int w2, int h2)
  267. {
  268. double w = 1920;
  269. double h = 1080;
  270. double width = SystemParameters.PrimaryScreenWidth;
  271. double height = SystemParameters.PrimaryScreenHeight;
  272. System.Drawing.Point p = new System.Drawing.Point((int)(w2 / w * width), (int)(h2 / h * height));
  273. return ScreenUtil.GetColorAt(p);
  274. }
  275. private static int CalculateLight(System.Drawing.Color color)
  276. {
  277. int[] colorArr = new int[] { color.R, color.G, color.B };
  278. int max = 0;
  279. int min = 255;
  280. foreach (int i in colorArr)
  281. {
  282. max = Math.Max(max, i);
  283. min = Math.Min(min, i);
  284. }
  285. int avg = (max + min) / 2;
  286. return avg;
  287. }
  288. }
  289. }