具有关闭和添加按钮的TabControl [英] TabControl with Close and Add Button
问题描述
我想使选项卡控件具有 x(关闭按钮)和 +(新选项卡按钮)。我找到了添加
但我想添加一个 +
现在的黑圆圈。我不知道如何,我尝试在最后一个标签的 Paint
事件上绘制,例如:
var p = tabs.TabPages [tabs.TabCount-1];
p.Paint + = new PaintEventHandler(tab_OnDrawPage);
private void tab_OnDrawPage(object sender,PaintEventArgs e)
{
// e.ClipRectangle。
e.Graphics.DrawString( +,
新字体( verdana,
10,
FontStyle.Bold),
Brushes.Black,
e.ClipRectangle.X + 10,
e.ClipRectangle.Y + 10);
}
但是它并没有显示平局。我想这与我传递给 DrawString()
调用的位置有关,但是我不知道要使用的位置。我用+10画出了最后一个标签。如何解决?我自己还没有做过任何自定义绘图,我正在学习它。
作为一个选项,您可以添加一个额外的标签其中显示一个添加图标并检查用户何时单击该选项卡,然后插入新的绘制关闭并添加图标, MouseDown
处理点击在关闭并添加按钮上,选择
以防止选择最后一个选项卡,而 HandleCreated
可以调整选项卡的宽度。您可以在下面查看所有实施设置和代码。
初始化
设置填充和并指定标签宽度允许的最小大小:
[DllImport( user32.dll)]
私有静态外部IntPtr SendMessage(IntPtr hWnd,int msg,IntPtr wp,IntPtr lp);
private const int TCM_SETMINTABWIDTH = 0x1300 + 49;
private void tabControl1_HandleCreated(对象发送者,EventArgs e)
{
SendMessage(this.tabControl1.Handle,TCM_SETMINTABWIDTH,IntPtr.Zero,(IntPtr)16);
}
下载
您可以在此处下载代码或克隆存储库:
I'm tring to make a tab control have a "x" (close button) and "+" (new tab button). I found a solution to add a x button
, the tab looks like this now:
But I want to add a +
where's that black circle right now. I have no idea how, I tried draw on Paint
event of the last tab, like this:
var p = tabs.TabPages[tabs.TabCount - 1];
p.Paint += new PaintEventHandler(tab_OnDrawPage);
private void tab_OnDrawPage(object sender, PaintEventArgs e)
{
// e.ClipRectangle.
e.Graphics.DrawString("+",
new Font("verdana",
10,
FontStyle.Bold),
Brushes.Black,
e.ClipRectangle.X + 10,
e.ClipRectangle.Y + 10);
}
But it didn't show anything draw. I guess it has to do with the positions I passed to DrawString()
call, but I don't know the proper ones to use. I used +10 to draw it away from last tab. How to fix that?. I haven't done any custom drawing myself, I'm learning it.
As an option you can add an extra tab which shows an add icon and check when the user clicks on that tab, then insert a new TabPage
before it.
Also you can prevent selecting that extra tab simply using Selecting
event of TabControl
. This way the last tab acts only like an add button for you, like IE and Chrome.
Implementation Details
We will use an owner draw tab to show close icons on each tab an add icon on the last tab. We use DrawItem
to draw close and add icons, MouseDown
to handle click on close and add buttons, Selecting
to prevent selecting of the last tab and HandleCreated
to adjust tab width. You can see all implementation settings and codes below.
Initialization
Set padding and DrawMode
and assign event handlers for DrawItem
, MouseDown
, Selecting
and HandleCreated
event.
this.tabControl1.Padding = new Point(12, 4);
this.tabControl1.DrawMode = TabDrawMode.OwnerDrawFixed;
this.tabControl1.DrawItem += tabControl1_DrawItem;
this.tabControl1.MouseDown += tabControl1_MouseDown;
this.tabControl1.Selecting += tabControl1_Selecting;
this.tabControl1.HandleCreated += tabControl1_HandleCreated;
Handle click on close button and add button
You can handle MouseDown
or MouseClick
event and check if the last tab rectangle contains the mouse clicked point, then insert a tab before the last tab. Otherwose check if one of close buttons contains clicked location, then close the tab which its close button was clicked:
private void tabControl1_MouseDown(object sender, MouseEventArgs e)
{
var lastIndex = this.tabControl1.TabCount - 1;
if (this.tabControl1.GetTabRect(lastIndex).Contains(e.Location))
{
this.tabControl1.TabPages.Insert(lastIndex, "New Tab");
this.tabControl1.SelectedIndex = lastIndex;
}
else
{
for (var i = 0; i < this.tabControl1.TabPages.Count; i++)
{
var tabRect = this.tabControl1.GetTabRect(i);
tabRect.Inflate(-2, -2);
var closeImage = Properties.Resources.DeleteButton_Image;
var imageRect = new Rectangle(
(tabRect.Right - closeImage.Width),
tabRect.Top + (tabRect.Height - closeImage.Height) / 2,
closeImage.Width,
closeImage.Height);
if (imageRect.Contains(e.Location))
{
this.tabControl1.TabPages.RemoveAt(i);
break;
}
}
}
}
Prevent selectin last tab
To prevent selection the last tab, you can handle Selecting
event of control and check if the selecting tab is the last tab, cancel the event:
private void tabControl1_Selecting(object sender, TabControlCancelEventArgs e)
{
if (e.TabPageIndex == this.tabControl1.TabCount - 1)
e.Cancel = true;
}
Draw Close Button and Add Button
To draw close button and add button, you can handle DrawItem
event. I used these icons for add and close buttons.
private void tabControl1_DrawItem(object sender, DrawItemEventArgs e)
{
var tabPage = this.tabControl1.TabPages[e.Index];
var tabRect = this.tabControl1.GetTabRect(e.Index);
tabRect.Inflate(-2, -2);
if (e.Index == this.tabControl1.TabCount - 1)
{
var addImage = Properties.Resources.AddButton_Image;
e.Graphics.DrawImage(addImage,
tabRect.Left + (tabRect.Width - addImage.Width) / 2,
tabRect.Top + (tabRect.Height - addImage.Height) / 2);
}
else
{
var closeImage = Properties.Resources.DeleteButton_Image;
e.Graphics.DrawImage(closeImage,
(tabRect.Right - closeImage.Width),
tabRect.Top + (tabRect.Height - closeImage.Height) / 2);
TextRenderer.DrawText(e.Graphics, tabPage.Text, tabPage.Font,
tabRect, tabPage.ForeColor, TextFormatFlags.Left);
}
}
Adjust Tab width
To adjust tab width and let the last tab have smaller width, you can hanlde HandleCreated
event and send a TCM_SETMINTABWIDTH
to the control and specify the minimum size allowed for the tab width:
[DllImport("user32.dll")]
private static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wp, IntPtr lp);
private const int TCM_SETMINTABWIDTH = 0x1300 + 49;
private void tabControl1_HandleCreated(object sender, EventArgs e)
{
SendMessage(this.tabControl1.Handle, TCM_SETMINTABWIDTH, IntPtr.Zero, (IntPtr)16);
}
Download
You can download the code or clone the repository here:
这篇关于具有关闭和添加按钮的TabControl的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!