Advertisement
CrisMod

Iconos Pestañas

Mar 13th, 2025 (edited)
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.79 KB | Source Code | 0 0
  1. public Editor()
  2. {
  3.     InitializeComponent();
  4.  
  5.     tabControl1.TabPages[tabControl1.TabCount - 1].Text = "";
  6.     tabControl1.Padding = new Point(12, 4);
  7.     tabControl1.DrawMode = System.Windows.Forms.TabDrawMode.OwnerDrawFixed;
  8.  
  9.     tabControl1.DrawItem += tabControl1_DrawItem;
  10.     tabControl1.MouseDown += tabControl1_MouseDown;
  11.     tabControl1.Selecting += tabControl1_Selecting;
  12.     tabControl1.HandleCreated += tabControl1_HandleCreated;
  13. }
  14.  
  15. [DllImport("user32.dll")]
  16. private static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wp, IntPtr lp);
  17. private const int TCM_SETMINTABWIDTH = 0x1300 + 49;
  18.  
  19. private void tabControl1_HandleCreated(object sender, EventArgs e)
  20. {
  21.     SendMessage(tabControl1.Handle, TCM_SETMINTABWIDTH, IntPtr.Zero, (IntPtr)16);
  22. }
  23. private void tabControl1_Selecting(object sender, TabControlCancelEventArgs e)
  24. {
  25.     if (e.TabPageIndex == tabControl1.TabCount - 1)
  26.         e.Cancel = true;
  27. }
  28. private void tabControl1_MouseDown(object sender, MouseEventArgs e)
  29. {
  30.     var lastIndex = tabControl1.TabCount - 1;
  31.     if (tabControl1.GetTabRect(lastIndex).Contains(e.Location))
  32.     {
  33.         tabControl1.TabPages.Insert(lastIndex, "Sin título");
  34.         tabControl1.SelectedIndex = lastIndex;
  35.         tabControl1.TabPages[lastIndex].UseVisualStyleBackColor = true;
  36.     }
  37.     else
  38.     {
  39.         for (var i = 0; i < tabControl1.TabPages.Count; i++)
  40.         {
  41.             var tabRect = tabControl1.GetTabRect(i);
  42.             tabRect.Inflate(-2, -2);
  43.             var closeImage = Properties.Resources.Close;
  44.             var imageRect = new Rectangle(
  45.                 (tabRect.Right - closeImage.Width),
  46.                 tabRect.Top + (tabRect.Height - closeImage.Height) / 2,
  47.                 closeImage.Width,
  48.                 closeImage.Height);
  49.             if (imageRect.Contains(e.Location))
  50.             {
  51.                 tabControl1.TabPages.RemoveAt(i);
  52.                 break;
  53.             }
  54.         }
  55.     }
  56. }
  57.  
  58. private void tabControl1_DrawItem(object sender, DrawItemEventArgs e)
  59. {
  60.     var tabPage = tabControl1.TabPages[e.Index];
  61.     var tabRect = tabControl1.GetTabRect(e.Index);
  62.     tabRect.Inflate(-2, -2);
  63.     if (e.Index == tabControl1.TabCount - 1)
  64.     {
  65.         var addImage = Properties.Resources.Add;
  66.         e.Graphics.DrawImage(addImage,
  67.             tabRect.Left + (tabRect.Width - addImage.Width) / 2,
  68.             tabRect.Top + (tabRect.Height - addImage.Height) / 2);
  69.     }
  70.     else
  71.     {
  72.         var closeImage = Properties.Resources.Close;
  73.         e.Graphics.DrawImage(closeImage,
  74.             (tabRect.Right - closeImage.Width),
  75.             tabRect.Top + (tabRect.Height - closeImage.Height) / 2);
  76.         TextRenderer.DrawText(e.Graphics, tabPage.Text, tabPage.Font,
  77.             tabRect, tabPage.ForeColor, TextFormatFlags.Left);
  78.     }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement