frmMain.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.Windows.Forms;
  5. namespace TaskbarGroups
  6. {
  7. /// <summary>
  8. /// 主界面
  9. /// </summary>
  10. public partial class frmMain : Form
  11. {
  12. private readonly string passedDirec;
  13. public List<ucShortcut> ControlList;
  14. public Color HoverColor;
  15. public Point mouseClick;
  16. public Panel shortcutPanel;
  17. public PictureBox shortcutPic;
  18. public Category ThisCategory;
  19. public frmMain()
  20. {
  21. InitializeComponent();
  22. }
  23. public frmMain(string passedDirectory, int cursorPosX, int cursorPosY)
  24. {
  25. InitializeComponent();
  26. ProfileOptimization.StartProfile("frmMain.Profile");
  27. mouseClick = new Point(cursorPosX, cursorPosY); // Consstruct point p based on passed x y mouse values
  28. passedDirec = passedDirectory;
  29. FormBorderStyle = FormBorderStyle.None;
  30. using (var ms = new MemoryStream(File.ReadAllBytes(MainPath.path + "\\config\\" + passedDirec + "\\GroupIcon.ico")))
  31. {
  32. Icon = new Icon(ms);
  33. }
  34. if (Directory.Exists(MainPath.path + @"\config\" + passedDirec))
  35. {
  36. ControlList = new List<ucShortcut>();
  37. SetStyle(ControlStyles.SupportsTransparentBackColor, true);
  38. ThisCategory = new Category($"config\\{passedDirec}");
  39. BackColor = ImageFunctions.FromString(ThisCategory.ColorString);
  40. Opacity = 1 - ThisCategory.Opacity / 100;
  41. HoverColor = BackColor.R * 0.2126 + BackColor.G * 0.7152 + BackColor.B * 0.0722 > 255 / 2
  42. ? Color.FromArgb(BackColor.A, BackColor.R - 50, BackColor.G - 50, BackColor.B - 50)
  43. : Color.FromArgb(BackColor.A, BackColor.R + 50, BackColor.G + 50, BackColor.B + 50);
  44. }
  45. else
  46. Application.Exit();
  47. }
  48. // Allow doubleBuffering drawing each frame to memory and then onto screen
  49. // Solves flickering issues mostly as the entire rendering of the screen is done in 1 operation after being first loaded to memory
  50. protected override CreateParams CreateParams
  51. {
  52. get
  53. {
  54. var cp = base.CreateParams;
  55. cp.ExStyle |= 0x02000000;
  56. return cp;
  57. }
  58. }
  59. private void frmMain_Load(object sender, EventArgs e)
  60. {
  61. LoadCategory();
  62. SetLocation();
  63. }
  64. // Sets location of form
  65. private void SetLocation()
  66. {
  67. var taskbarList = FindDockedTaskBars();
  68. var taskbar = new Rectangle();
  69. var screen = new Rectangle();
  70. var i = 0;
  71. int locationy;
  72. int locationx;
  73. if (taskbarList.Count != 0)
  74. {
  75. foreach (var scr in Screen.AllScreens) // Get what screen user clicked on
  76. {
  77. if (scr.Bounds.Contains(mouseClick))
  78. {
  79. screen.X = scr.Bounds.X;
  80. screen.Y = scr.Bounds.Y;
  81. screen.Width = scr.Bounds.Width;
  82. screen.Height = scr.Bounds.Height;
  83. taskbar = taskbarList[i];
  84. }
  85. i++;
  86. }
  87. if (taskbar.Contains(mouseClick)) // Click on taskbar
  88. {
  89. if (taskbar.Top == screen.Top && taskbar.Width == screen.Width)
  90. {
  91. // TOP
  92. locationy = screen.Y + taskbar.Height + 10;
  93. locationx = mouseClick.X - Width / 2;
  94. }
  95. else if (taskbar.Bottom == screen.Bottom && taskbar.Width == screen.Width)
  96. {
  97. // BOTTOM
  98. locationy = screen.Y + screen.Height - Height - taskbar.Height - 10;
  99. locationx = mouseClick.X - Width / 2;
  100. }
  101. else if (taskbar.Left == screen.Left)
  102. {
  103. // LEFT
  104. locationy = mouseClick.Y - Height / 2;
  105. locationx = screen.X + taskbar.Width + 10;
  106. }
  107. else
  108. {
  109. // RIGHT
  110. locationy = mouseClick.Y - Height / 2;
  111. locationx = screen.X + screen.Width - Width - taskbar.Width - 10;
  112. }
  113. }
  114. else // not click on taskbar
  115. {
  116. locationy = mouseClick.Y - Height - 20;
  117. locationx = mouseClick.X - Width / 2;
  118. }
  119. Location = new Point(locationx, locationy);
  120. // If form goes over screen edge
  121. if (Left < screen.Left)
  122. Left = screen.Left + 10;
  123. if (Top < screen.Top)
  124. Top = screen.Top + 10;
  125. if (Right > screen.Right)
  126. Left = screen.Right - Width - 10;
  127. // If form goes over taskbar
  128. if (taskbar.Contains(Left, Top) && taskbar.Contains(Right, Top)) // Top taskbar
  129. Top = screen.Top + 10 + taskbar.Height;
  130. if (taskbar.Contains(Left, Top)) // Left taskbar
  131. Left = screen.Left + 10 + taskbar.Width;
  132. if (taskbar.Contains(Right, Top)) // Right taskbar
  133. Left = screen.Right - Width - 10 - taskbar.Width;
  134. }
  135. else // Hidden taskbar
  136. {
  137. foreach (var scr in Screen.AllScreens) // get what screen user clicked on
  138. {
  139. if (scr.Bounds.Contains(mouseClick))
  140. {
  141. screen.X = scr.Bounds.X;
  142. screen.Y = scr.Bounds.Y;
  143. screen.Width = scr.Bounds.Width;
  144. screen.Height = scr.Bounds.Height;
  145. }
  146. i++;
  147. }
  148. if (mouseClick.Y > Screen.PrimaryScreen.Bounds.Height - 35)
  149. locationy = Screen.PrimaryScreen.Bounds.Height - Height - 45;
  150. else
  151. locationy = mouseClick.Y - Height - 20;
  152. locationx = mouseClick.X - Width / 2;
  153. Location = new Point(locationx, locationy);
  154. // If form goes over screen edge
  155. if (Left < screen.Left)
  156. Left = screen.Left + 10;
  157. if (Top < screen.Top)
  158. Top = screen.Top + 10;
  159. if (Right > screen.Right)
  160. Left = screen.Right - Width - 10;
  161. // If form goes over taskbar
  162. if (taskbar.Contains(Left, Top) && taskbar.Contains(Right, Top)) // Top taskbar
  163. Top = screen.Top + 10 + taskbar.Height;
  164. if (taskbar.Contains(Left, Top)) // Left taskbar
  165. Left = screen.Left + 10 + taskbar.Width;
  166. if (taskbar.Contains(Right, Top)) // Right taskbar
  167. Left = screen.Right - Width - 10 - taskbar.Width;
  168. }
  169. }
  170. // Search for active taskbars on screen
  171. public static List<Rectangle> FindDockedTaskBars()
  172. {
  173. var dockedRects = new List<Rectangle>();
  174. foreach (var tmpScrn in Screen.AllScreens)
  175. if (!tmpScrn.Bounds.Equals(tmpScrn.WorkingArea))
  176. {
  177. var rect = new Rectangle();
  178. var leftDockedWidth = Math.Abs(Math.Abs(tmpScrn.Bounds.Left) - Math.Abs(tmpScrn.WorkingArea.Left));
  179. var topDockedHeight = Math.Abs(Math.Abs(tmpScrn.Bounds.Top) - Math.Abs(tmpScrn.WorkingArea.Top));
  180. var rightDockedWidth = tmpScrn.Bounds.Width - leftDockedWidth - tmpScrn.WorkingArea.Width;
  181. var bottomDockedHeight = tmpScrn.Bounds.Height - topDockedHeight - tmpScrn.WorkingArea.Height;
  182. if (leftDockedWidth > 0)
  183. {
  184. rect.X = tmpScrn.Bounds.Left;
  185. rect.Y = tmpScrn.Bounds.Top;
  186. rect.Width = leftDockedWidth;
  187. rect.Height = tmpScrn.Bounds.Height;
  188. }
  189. else if (rightDockedWidth > 0)
  190. {
  191. rect.X = tmpScrn.WorkingArea.Right;
  192. rect.Y = tmpScrn.Bounds.Top;
  193. rect.Width = rightDockedWidth;
  194. rect.Height = tmpScrn.Bounds.Height;
  195. }
  196. else if (topDockedHeight > 0)
  197. {
  198. rect.X = tmpScrn.WorkingArea.Left;
  199. rect.Y = tmpScrn.Bounds.Top;
  200. rect.Width = tmpScrn.WorkingArea.Width;
  201. rect.Height = topDockedHeight;
  202. }
  203. else if (bottomDockedHeight > 0)
  204. {
  205. rect.X = tmpScrn.WorkingArea.Left;
  206. rect.Y = tmpScrn.WorkingArea.Bottom;
  207. rect.Width = tmpScrn.WorkingArea.Width;
  208. rect.Height = bottomDockedHeight;
  209. }
  210. dockedRects.Add(rect);
  211. }
  212. if (dockedRects.Count == 0)
  213. {
  214. // Taskbar is set to "Auto-Hide".
  215. }
  216. return dockedRects;
  217. }
  218. //
  219. //------------------------------------------------------------------------------------
  220. //
  221. // Loading category and building shortcuts
  222. private void LoadCategory()
  223. {
  224. //System.Diagnostics.Debugger.Launch();
  225. Width = 0;
  226. Height = 45;
  227. var x = 0;
  228. var y = 0;
  229. var width = ThisCategory.Width;
  230. var columns = 1;
  231. // Check if icon caches exist for the category being loaded
  232. // If not then rebuild the icon cache
  233. if (!Directory.Exists(MainPath.path + @"\config\" + ThisCategory.Name + @"\Icons\"))
  234. ThisCategory.CacheIcons();
  235. foreach (var psc in ThisCategory.ShortcutList)
  236. {
  237. if (columns > width) // creating new row if there are more psc than max width
  238. {
  239. x = 0;
  240. y += 45;
  241. Height += 45;
  242. columns = 1;
  243. }
  244. if (Width < width * 55)
  245. Width += 55;
  246. // OLD
  247. //BuildShortcutPanel(x, y, psc);
  248. // Building shortcut controls
  249. var pscPanel = new ucShortcut
  250. {
  251. Psc = psc,
  252. MotherForm = this,
  253. ThisCategory = ThisCategory
  254. };
  255. pscPanel.Location = new Point(x, y);
  256. Controls.Add(pscPanel);
  257. ControlList.Add(pscPanel);
  258. pscPanel.Show();
  259. pscPanel.BringToFront();
  260. // Reset values
  261. x += 55;
  262. columns++;
  263. }
  264. Width -= 2; // For some reason the width is 2 pixels larger than the shortcuts. Temporary fix
  265. }
  266. // OLD (Having some issues with the uc build, so keeping the old code below)
  267. private void BuildShortcutPanel(int x, int y, ProgramShortcut psc)
  268. {
  269. shortcutPic = new PictureBox();
  270. shortcutPic.BackColor = Color.Transparent;
  271. shortcutPic.Location = new Point(25, 15);
  272. shortcutPic.Size = new Size(25, 25);
  273. shortcutPic.BackgroundImage =
  274. ThisCategory.LoadImageCache(psc); // Use the local icon cache for the file specified as the icon image
  275. shortcutPic.BackgroundImageLayout = ImageLayout.Stretch;
  276. shortcutPic.TabStop = false;
  277. shortcutPic.Click += (sender, e) => OpenFile(psc.Arguments, psc.FilePath, psc.WorkingDirectory);
  278. shortcutPic.Cursor = Cursors.Hand;
  279. shortcutPanel.Controls.Add(shortcutPic);
  280. shortcutPic.Show();
  281. shortcutPic.BringToFront();
  282. shortcutPic.MouseEnter += (sender, e) => shortcutPanel.BackColor = Color.Black;
  283. shortcutPic.MouseLeave += (sender, e) => shortcutPanel.BackColor = Color.Transparent;
  284. }
  285. // Click handler for shortcuts
  286. public void OpenFile(string arguments, string path, string workingDirec)
  287. {
  288. // starting program from psc panel click
  289. var proc = new ProcessStartInfo
  290. {
  291. Arguments = arguments,
  292. FileName = path,
  293. WorkingDirectory = workingDirec
  294. };
  295. /*
  296. proc.EnableRaisingEvents = false;
  297. proc.StartInfo.FileName = path;
  298. */
  299. try
  300. {
  301. Process.Start(proc);
  302. }
  303. catch (Exception ex)
  304. {
  305. MessageBox.Show(ex.Message);
  306. }
  307. }
  308. // Closes application upon deactivation
  309. private void frmMain_Deactivate(object sender, EventArgs e)
  310. {
  311. // closes program if user clicks outside form
  312. Close();
  313. }
  314. // Keyboard shortcut handlers
  315. private void frmMain_KeyDown(object sender, KeyEventArgs e)
  316. {
  317. try
  318. {
  319. switch (e.KeyCode)
  320. {
  321. case Keys.D1:
  322. ControlList[0].ucShortcut_MouseEnter(sender, e);
  323. break;
  324. case Keys.D2:
  325. ControlList[1].ucShortcut_MouseEnter(sender, e);
  326. break;
  327. case Keys.D3:
  328. ControlList[2].ucShortcut_MouseEnter(sender, e);
  329. break;
  330. case Keys.D4:
  331. ControlList[3].ucShortcut_MouseEnter(sender, e);
  332. break;
  333. case Keys.D5:
  334. ControlList[4].ucShortcut_MouseEnter(sender, e);
  335. break;
  336. case Keys.D6:
  337. ControlList[5].ucShortcut_MouseEnter(sender, e);
  338. break;
  339. case Keys.D7:
  340. ControlList[6].ucShortcut_MouseEnter(sender, e);
  341. break;
  342. case Keys.D8:
  343. ControlList[7].ucShortcut_MouseEnter(sender, e);
  344. break;
  345. case Keys.D9:
  346. ControlList[8].ucShortcut_MouseEnter(sender, e);
  347. break;
  348. case Keys.D0:
  349. ControlList[9].ucShortcut_MouseEnter(sender, e);
  350. break;
  351. }
  352. }
  353. catch
  354. {
  355. }
  356. }
  357. private void frmMain_KeyUp(object sender, KeyEventArgs e)
  358. {
  359. //System.Diagnostics.Debugger.Launch();
  360. if (e.Modifiers == Keys.Control && e.KeyCode == Keys.Enter && ThisCategory.allowOpenAll)
  361. foreach (var usc in ControlList)
  362. usc.ucShortcut_Click(sender, e);
  363. try
  364. {
  365. switch (e.KeyCode)
  366. {
  367. case Keys.D1:
  368. ControlList[0].ucShortcut_MouseLeave(sender, e);
  369. ControlList[0].ucShortcut_Click(sender, e);
  370. break;
  371. case Keys.D2:
  372. ControlList[1].ucShortcut_MouseLeave(sender, e);
  373. ControlList[1].ucShortcut_Click(sender, e);
  374. break;
  375. case Keys.D3:
  376. ControlList[2].ucShortcut_MouseLeave(sender, e);
  377. ControlList[2].ucShortcut_Click(sender, e);
  378. break;
  379. case Keys.D4:
  380. ControlList[3].ucShortcut_MouseLeave(sender, e);
  381. ControlList[3].ucShortcut_Click(sender, e);
  382. break;
  383. case Keys.D5:
  384. ControlList[4].ucShortcut_MouseLeave(sender, e);
  385. ControlList[4].ucShortcut_Click(sender, e);
  386. break;
  387. case Keys.D6:
  388. ControlList[5].ucShortcut_MouseLeave(sender, e);
  389. ControlList[5].ucShortcut_Click(sender, e);
  390. break;
  391. case Keys.D7:
  392. ControlList[6].ucShortcut_MouseLeave(sender, e);
  393. ControlList[6].ucShortcut_Click(sender, e);
  394. break;
  395. case Keys.D8:
  396. ControlList[7].ucShortcut_MouseLeave(sender, e);
  397. ControlList[7].ucShortcut_Click(sender, e);
  398. break;
  399. case Keys.D9:
  400. ControlList[8].ucShortcut_MouseLeave(sender, e);
  401. ControlList[8].ucShortcut_Click(sender, e);
  402. break;
  403. case Keys.D0:
  404. ControlList[9].ucShortcut_MouseLeave(sender, e);
  405. ControlList[9].ucShortcut_Click(sender, e);
  406. break;
  407. }
  408. }
  409. catch
  410. {
  411. }
  412. }
  413. }
  414. }