Skip to content

Instantly share code, notes, and snippets.

@hmetgundogdu
Last active May 14, 2025 07:09
Show Gist options
  • Select an option

  • Save hmetgundogdu/ca51c8bcf337cdf14a4eac9b3b3b1e87 to your computer and use it in GitHub Desktop.

Select an option

Save hmetgundogdu/ca51c8bcf337cdf14a4eac9b3b3b1e87 to your computer and use it in GitHub Desktop.

Revisions

  1. hmetgundogdu revised this gist May 13, 2025. 1 changed file with 14 additions and 14 deletions.
    28 changes: 14 additions & 14 deletions NoBorderTabControl.cs
    Original file line number Diff line number Diff line change
    @@ -3,14 +3,23 @@ public partial class NoBorderTabControl : TabControl
    /// <summary>
    /// Fine-tune adjustments to hide the border more precisely.
    /// </summary>
    public RECT BorderAdjustments { get; set; } = new()
    public Padding BorderAdjustments { get; set; } = new()
    {
    Left = 6,
    Right = 6,
    Top = 2,
    Bottom = 6,
    };

    [StructLayout(LayoutKind.Sequential)]
    public struct RECT
    {
    public int Left;
    public int Top;
    public int Right;
    public int Bottom;
    }

    private const string TAB_CONTROL_NAME = "TabControlAsNoBorder";

    private const int TCM_FIRST = 0x1300;
    @@ -23,26 +32,17 @@ protected override void WndProc(ref Message m)
    RECT rc = Marshal.PtrToStructure<RECT>(m.LParam);

    // Adjust these values depending on your needs
    rc.Left -= OffsetPixels.Left;
    rc.Right += OffsetPixels.Right;
    rc.Top -= OffsetPixels.Top;
    rc.Bottom += OffsetPixels.Bottom;
    rc.Left -= BorderAdjustments.Left;
    rc.Right += BorderAdjustments.Right;
    rc.Top -= BorderAdjustments.Top;
    rc.Bottom += BorderAdjustments.Bottom;

    Marshal.StructureToPtr(rc, m.LParam, true);
    }

    base.WndProc(ref m);
    }

    [StructLayout(LayoutKind.Sequential)]
    public struct RECT
    {
    public int Left;
    public int Top;
    public int Right;
    public int Bottom;
    }

    public static TabControl ReplaceTabControl(Control originalParent, TabControl originalTabControl)
    {
    if (!originalParent.Controls.ContainsKey(TAB_CONTROL_NAME))
  2. hmetgundogdu revised this gist May 13, 2025. 1 changed file with 4 additions and 1 deletion.
    5 changes: 4 additions & 1 deletion NoBorderTabControl.cs
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,9 @@
    public partial class NoBorderTabControl : TabControl
    {
    public RECT OffsetPixels { get; set; } = new()
    /// <summary>
    /// Fine-tune adjustments to hide the border more precisely.
    /// </summary>
    public RECT BorderAdjustments { get; set; } = new()
    {
    Left = 6,
    Right = 6,
  3. hmetgundogdu created this gist May 13, 2025.
    97 changes: 97 additions & 0 deletions NoBorderTabControl.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,97 @@
    public partial class NoBorderTabControl : TabControl
    {
    public RECT OffsetPixels { get; set; } = new()
    {
    Left = 6,
    Right = 6,
    Top = 2,
    Bottom = 6,
    };

    private const string TAB_CONTROL_NAME = "TabControlAsNoBorder";

    private const int TCM_FIRST = 0x1300;
    private const int TCM_ADJUSTRECT = TCM_FIRST + 40;

    protected override void WndProc(ref Message m)
    {
    if (m.Msg == TCM_ADJUSTRECT)
    {
    RECT rc = Marshal.PtrToStructure<RECT>(m.LParam);

    // Adjust these values depending on your needs
    rc.Left -= OffsetPixels.Left;
    rc.Right += OffsetPixels.Right;
    rc.Top -= OffsetPixels.Top;
    rc.Bottom += OffsetPixels.Bottom;

    Marshal.StructureToPtr(rc, m.LParam, true);
    }

    base.WndProc(ref m);
    }

    [StructLayout(LayoutKind.Sequential)]
    public struct RECT
    {
    public int Left;
    public int Top;
    public int Right;
    public int Bottom;
    }

    public static TabControl ReplaceTabControl(Control originalParent, TabControl originalTabControl)
    {
    if (!originalParent.Controls.ContainsKey(TAB_CONTROL_NAME))
    {
    var newTabControl = new CustomTabControl
    {
    Name = TAB_CONTROL_NAME,
    Size = originalTabControl.Size,
    Width = originalTabControl.Width,
    Location = originalTabControl.Location,
    Dock = originalTabControl.Dock
    };

    foreach (TabPage tabpage in originalTabControl.TabPages)
    {
    newTabControl.TabPages.Add(tabpage);
    }

    originalTabControl.Hide();

    if (originalParent is TableLayoutPanel parentPanel)
    {
    int row = parentPanel.GetRow(originalTabControl);
    int column = parentPanel.GetColumn(originalTabControl);
    parentPanel.SetColumnSpan(newTabControl, parentPanel.GetColumnSpan(originalTabControl));
    parentPanel.Controls.Add(newTabControl, column, row);
    }
    else
    {
    if (originalTabControl.Parent is null)
    {
    throw new InvalidOperationException("There is no parent of tab control here");
    }

    originalTabControl.Parent.Controls.Add(newTabControl);
    }

    return newTabControl;
    }
    else
    {
    var controlNoBorder = originalParent.Controls[TAB_CONTROL_NAME];

    if (controlNoBorder is TabControl)
    {
    return (TabControl)controlNoBorder;
    }
    else
    {
    throw new InvalidOperationException("There is no tab control in side of tab control parent children");
    }

    }
    }
    }