MoonTabs.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. using System;
  2. using System.ComponentModel;
  3. using System.Drawing;
  4. using System.Drawing.Drawing2D;
  5. using System.Runtime.InteropServices;
  6. using System.Security.Permissions;
  7. using System.Windows.Forms;
  8. namespace Optimizer
  9. {
  10. [
  11. ComVisible(true), ClassInterface(ClassInterfaceType.AutoDispatch),
  12. DefaultProperty("TabPages"), DefaultEvent("SelectedIndexChanged")
  13. ]
  14. public class MoonTabs : TabControl
  15. {
  16. [Category("Custom"), Description("Indicates whether or not you the components Tabpages Headers have border edges."), DefaultValue(false)]
  17. public bool BorderEdges { get; set; } = false;
  18. private int _BorderSize = 0;
  19. [Category("Custom"), Description("The size of the components border."), DefaultValue(0)]
  20. public int BorderSize
  21. {
  22. get => _BorderSize;
  23. set => _BorderSize = 0;
  24. }
  25. [Category("Custom"), Description("The size of the components Indicator."), DefaultValue(0)]
  26. public int IndicatorSize { get; set; } = 0;
  27. private int _DividerSize = 0;
  28. [Category("Custom"), Description("The size of the components Divider."), DefaultValue(0)]
  29. public int DividerSize
  30. {
  31. get => _DividerSize;
  32. set => _DividerSize = value.LimitToRange(0, 0);
  33. }
  34. [Category("Custom"), Description("Indicates whether or not you can rearrange the components Tabpages. CLICK on the Tabpages HEADER with the LEFT mousebutton and HOLD DOWN the <SHIFT> KEY, to drag it LEFT or RIGHT."), DefaultValue(false)]
  35. public bool CanDrag { get; set; }
  36. StringFormat format = new StringFormat { Alignment = StringAlignment.Center };
  37. private Bitmap bitDrag = default(Bitmap);
  38. private bool bDrag, bMouseDown, bShiftKey;
  39. private Point ptPreviousLocation, ptMaxDrag;
  40. private int DraggedIndex = -1;
  41. protected override CreateParams CreateParams
  42. {
  43. [SecurityPermission(SecurityAction.Demand, Flags = SecurityPermissionFlag.UnmanagedCode)]
  44. get
  45. {
  46. CreateParams cp = base.CreateParams;
  47. cp.ExStyle |= 0x02000000;
  48. return cp;
  49. }
  50. }
  51. // OVERRIDE TAB HEADER WIDTH
  52. protected override void OnHandleCreated(EventArgs e)
  53. {
  54. base.OnHandleCreated(e);
  55. // Send TCM_SETMINTABWIDTH
  56. SendMessage(this.Handle, 0x1300 + 49, IntPtr.Zero, (IntPtr)(Program.DPI_PREFERENCE / 3));
  57. }
  58. [System.Runtime.InteropServices.DllImport("user32.dll")]
  59. private static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wp, IntPtr lp);
  60. public MoonTabs()
  61. {
  62. SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer |
  63. ControlStyles.UserPaint | ControlStyles.ResizeRedraw | ControlStyles.CacheText, true);
  64. Alignment = TabAlignment.Top;
  65. Margin = new Padding(0);
  66. Padding = new Point(0, 0);
  67. this.SizeMode = TabSizeMode.Normal;
  68. }
  69. private void SetDragState() => bDrag = (CanDrag && bMouseDown && bShiftKey);
  70. protected override void OnMouseDown(MouseEventArgs e)
  71. {
  72. bMouseDown = true;
  73. SetDragState();
  74. Rectangle rectDrag = GetTabRect(SelectedIndex);
  75. ptPreviousLocation = new Point(rectDrag.X, rectDrag.Y);
  76. rectDrag.Width += 1; rectDrag.Height += 1;
  77. Bitmap src = new Bitmap(Width, Height);
  78. DrawToBitmap(src, ClientRectangle);
  79. using (Graphics g = Graphics.FromImage(bitDrag = new Bitmap(rectDrag.Width, rectDrag.Height)))
  80. {
  81. g.DrawImage(src, new Rectangle(0, 0, bitDrag.Width, bitDrag.Height), rectDrag, GraphicsUnit.Pixel);
  82. }
  83. }
  84. protected override void OnMouseMove(MouseEventArgs e)
  85. {
  86. if (bDrag)
  87. {
  88. if (Alignment == TabAlignment.Top || Alignment == TabAlignment.Bottom)
  89. {
  90. ptPreviousLocation = new Point(((e.X < 0) ? 0 : (e.X > ptMaxDrag.X) ? ptMaxDrag.X : e.X), (Alignment == TabAlignment.Top ? BorderSize : ptMaxDrag.Y));
  91. }
  92. if (Alignment == TabAlignment.Right || Alignment == TabAlignment.Left)
  93. {
  94. ptPreviousLocation = new Point(ptMaxDrag.X, ((e.Y < 0) ? 0 : (e.Y > ptMaxDrag.Y) ? ptMaxDrag.Y : e.Y));
  95. }
  96. for (int i = 0; i < TabCount; i++)
  97. {
  98. if (GetTabRect(i).Contains(PointToClient(Cursor.Position))) { DraggedIndex = i; break; }
  99. }
  100. Invalidate();
  101. }
  102. }
  103. protected override void OnMouseUp(MouseEventArgs e)
  104. {
  105. void SwapTabPages(TabPage inDestTab)
  106. {
  107. int SourceIndex = TabPages.IndexOf(SelectedTab);
  108. int DestinationIndex = TabPages.IndexOf(inDestTab);
  109. TabPages[DestinationIndex] = SelectedTab;
  110. TabPages[SourceIndex] = inDestTab;
  111. if (SelectedIndex == SourceIndex) { SelectedIndex = DestinationIndex; }
  112. else if (SelectedIndex == DestinationIndex) { SelectedIndex = SourceIndex; }
  113. }
  114. bDrag = bMouseDown = false;
  115. if (DraggedIndex > -1)
  116. {
  117. SwapTabPages(TabPages[DraggedIndex]);
  118. DraggedIndex = -1;
  119. }
  120. SetDragState();
  121. Invalidate();
  122. }
  123. protected override void OnKeyDown(KeyEventArgs ke) { bShiftKey = ke.Shift; SetDragState(); }
  124. protected override void OnKeyUp(KeyEventArgs e) { bDrag = bShiftKey = false; SetDragState(); }
  125. protected override void OnPaint(PaintEventArgs e)
  126. {
  127. if (DesignMode) return;
  128. e.Graphics.Clear(Color.FromArgb(40, 40, 40));
  129. Rectangle container = new Rectangle(0, 0, Width - (BorderSize % 2), Height - (BorderSize % 2));
  130. Rectangle containerHead = default(Rectangle);
  131. if (TabCount > 0)
  132. {
  133. using (SolidBrush brushBackgroundTab = new SolidBrush(Color.FromArgb(40, 40, 40)))
  134. using (SolidBrush brushDivider = new SolidBrush(Options.ForegroundColor))
  135. {
  136. {
  137. e.Graphics.FillRectangle(brushBackgroundTab, DisplayRectangle);
  138. }
  139. {
  140. Rectangle rectDivider = GetTabRect(SelectedIndex);
  141. if (Alignment == TabAlignment.Top || Alignment == TabAlignment.Bottom)
  142. {
  143. e.Graphics.FillRectangle(brushDivider,
  144. 0,
  145. (((Alignment == TabAlignment.Top) ? (TabPages[SelectedIndex].Top - DividerSize - (DividerSize % 2)) :
  146. (TabPages[SelectedIndex].Bottom + (DividerSize % 2)))),
  147. (Width - BorderSize), DividerSize
  148. );
  149. }
  150. if (Alignment == TabAlignment.Right || Alignment == TabAlignment.Left)
  151. {
  152. e.Graphics.FillRectangle(brushDivider,
  153. ((Alignment == TabAlignment.Right) ? (TabPages[SelectedIndex].Right + (DividerSize % 2)) : TabPages[SelectedIndex].Left - DividerSize - (DividerSize % 2)),
  154. BorderSize,
  155. DividerSize,
  156. (Height - (BorderSize * 2))
  157. );
  158. }
  159. }
  160. }
  161. }
  162. SolidBrush brushActiveText;
  163. using (Pen penActive = new Pen(Options.ForegroundColor))
  164. using (Pen penBorder = new Pen(Color.FromArgb(40, 40, 40), 0))
  165. using (SolidBrush brushActive = new SolidBrush(Options.ForegroundColor))
  166. using (SolidBrush brushInActive = new SolidBrush(Color.FromArgb(40, 40, 40)))
  167. using (SolidBrush brushAlternative = new SolidBrush(Options.ForegroundColor))
  168. using (SolidBrush brushActiveIndicator = new SolidBrush(ControlPaint.Light(Options.ForegroundColor)))
  169. using (SolidBrush brushInActiveIndicator = new SolidBrush(Options.ForegroundColor))
  170. using (brushActiveText = new SolidBrush(Options.TextColor))
  171. using (SolidBrush brushInActiveText = new SolidBrush(Color.White))
  172. using (SolidBrush brushDrag = new SolidBrush(ControlPaint.Dark(Options.ForegroundColor)))
  173. {
  174. //if (MoonManager.THEME_PREFERENCE == THEME.LIGHT) brushActiveText = new SolidBrush(Color.White);
  175. penBorder.Alignment = penActive.Alignment = PenAlignment.Inset;
  176. e.Graphics.DrawRectangle(penBorder, container);
  177. if (TabCount > 0)
  178. {
  179. ptMaxDrag = new Point(0, 0);
  180. for (int i = 0; i < TabCount; i++)
  181. {
  182. containerHead = GetTabRect(i);
  183. e.Graphics.FillRectangle((SelectedIndex == i) ? (bDrag ? brushDrag : brushActive) : brushInActive, containerHead);
  184. if (BorderEdges && (i == SelectedIndex))
  185. {
  186. Point ptA = new Point(0, 0); Point ptB = new Point(0, 0);
  187. Point ptC = new Point(0, 0); Point ptD = new Point(0, 0);
  188. ptA.X = ptB.X = ptD.X = containerHead.X;
  189. ptA.Y = ptB.Y = ptC.Y = containerHead.Y;
  190. ptA.Y = ptC.Y = ptD.Y = containerHead.Y + containerHead.Height - 1;
  191. if (Alignment == TabAlignment.Top || Alignment == TabAlignment.Bottom)
  192. {
  193. ptD.X = ptC.X = containerHead.X + containerHead.Width;
  194. ptC.Y = containerHead.Y;
  195. if (Alignment == TabAlignment.Bottom)
  196. {
  197. MoonTabHelper.Swap(ref ptA, ref ptB); MoonTabHelper.Swap(ref ptC, ref ptD);
  198. }
  199. }
  200. if (Alignment == TabAlignment.Right || Alignment == TabAlignment.Left)
  201. {
  202. ptA.Y = containerHead.Y;
  203. ptB.X = ptC.X = containerHead.X + containerHead.Width - 1;
  204. if (Alignment == TabAlignment.Left)
  205. {
  206. MoonTabHelper.Swap(ref ptA, ref ptC); MoonTabHelper.Swap(ref ptB, ref ptD);
  207. }
  208. }
  209. e.Graphics.DrawLine(new Pen(ControlPaint.Light(brushActive.Color)), ptA, ptB);
  210. e.Graphics.DrawLine(new Pen(ControlPaint.Light(brushActive.Color)), ptB, ptC);
  211. e.Graphics.DrawLine(new Pen(ControlPaint.Dark(brushActive.Color)), ptC, ptD);
  212. }
  213. {
  214. Rectangle rectDivider = default(Rectangle);
  215. if (Alignment == TabAlignment.Top || Alignment == TabAlignment.Bottom)
  216. {
  217. rectDivider = new Rectangle(containerHead.X + 300, containerHead.Y + ((Alignment == TabAlignment.Top) ? containerHead.Height : -DividerSize), containerHead.Width, DividerSize);
  218. }
  219. if (Alignment == TabAlignment.Right || Alignment == TabAlignment.Left)
  220. {
  221. rectDivider = new Rectangle(containerHead.X - ((Alignment == TabAlignment.Right) ? DividerSize : -containerHead.Width), containerHead.Y, DividerSize, containerHead.Height);
  222. }
  223. e.Graphics.FillRectangle(((MoonTabHelper.TagToInt(TabPages[i]) == 1) ? brushAlternative : ((i == SelectedIndex) ? brushActiveIndicator : brushInActiveIndicator)), rectDivider);
  224. }
  225. if (!(bDrag && i == SelectedIndex))
  226. {
  227. int angle = 0;
  228. {
  229. if (Alignment == TabAlignment.Right) angle = 90;
  230. if (Alignment == TabAlignment.Left) angle = 270;
  231. }
  232. float w, h;
  233. w = h = 0f;
  234. if (Alignment == TabAlignment.Top || Alignment == TabAlignment.Bottom) { w = containerHead.X + (containerHead.Width / 2); }
  235. if (Alignment == TabAlignment.Right || Alignment == TabAlignment.Left) { w = containerHead.X; h = containerHead.Y + (containerHead.Height / 2); }
  236. if (Alignment == TabAlignment.Top || Alignment == TabAlignment.Bottom) { h = containerHead.Y + ((Alignment == TabAlignment.Top) ? IndicatorSize : 0) + ((containerHead.Height - IndicatorSize) / 2); }
  237. if (Alignment == TabAlignment.Right || Alignment == TabAlignment.Left) { w += (((Alignment == TabAlignment.Right) ? 0 : IndicatorSize) + ((containerHead.Width - IndicatorSize) / 2)); }
  238. e.Graphics.TranslateTransform(w, h);
  239. {
  240. Size textSize = e.Graphics.MeasureString(TabPages[i].Text, Font).ToSize();
  241. e.Graphics.RotateTransform(angle);
  242. e.Graphics.DrawString(TabPages[i].Text, Font, ((SelectedIndex == i) ? brushActiveText : brushInActiveText), new PointF((-textSize.Width / 2f), (-textSize.Height / 2f)));
  243. }
  244. e.Graphics.ResetTransform();
  245. }
  246. if (bMouseDown)
  247. {
  248. if (Alignment == TabAlignment.Top || Alignment == TabAlignment.Bottom) { if (i > 0) { ptMaxDrag.X += GetTabRect(i).Width; } }
  249. if (Alignment == TabAlignment.Top) { ptMaxDrag.Y = BorderSize; }
  250. if (Alignment == TabAlignment.Bottom) { ptMaxDrag.Y = containerHead.Y; };
  251. if (Alignment == TabAlignment.Right || Alignment == TabAlignment.Left) { ptMaxDrag.X = containerHead.X; if (i > 0) { ptMaxDrag.Y += containerHead.Height; } }
  252. }
  253. if (bDrag && (bitDrag != null)) { e.Graphics.DrawImage(bitDrag, new Point(ptPreviousLocation.X, ptPreviousLocation.Y)); }
  254. }
  255. }
  256. }
  257. }
  258. }
  259. public static class MoonTabHelper
  260. {
  261. public static int LimitToRange(this int value, int inclusiveMinimum, int inclusiveMaximum)
  262. {
  263. if (value < inclusiveMinimum) { return inclusiveMinimum; }
  264. if (value > inclusiveMaximum) { return inclusiveMaximum; }
  265. return value;
  266. }
  267. public static void Swap<T>(ref T a, ref T b)
  268. {
  269. T temp = a;
  270. a = b; b = temp;
  271. }
  272. public static int TagToInt(object inObject) => Convert.ToInt32((inObject as Control).Tag);
  273. }
  274. }