如何将鼠标输入事件添加到动态创建的图形矩形到tabcontrol
我正在使用tabcontrol,我可以通过代码添加一个tabpages。并且我正在向标签页添加一张图片(这是一个关闭按钮)。
即时通讯使用以下代码给标签页标题关闭按钮。
我的尝试:
Hi, i am having an tabcontrol in which i add a tabpages through code. and in that i am adding a picture (which is a close button) to the tab header.
im using the following code to give a close button to the tabpage header.
What I have tried:
<pre> private void TabControl1_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e)
{
Image img = new Bitmap(Properties.Resources.CCloseBlack16x16,new Size(12,12));
try
{
Font fnt;
Brush backBrush;
Brush foreBrush;
if (e.Index == this.tabControl1.SelectedIndex)
{
fnt = new Font(e.Font, FontStyle.Bold | FontStyle.Bold);
fnt = new Font(e.Font, FontStyle.Bold);
backBrush = new System.Drawing.SolidBrush(Color.FromArgb(16, 128, 128));
// this.tabControl1.SelectedTab.BackColor = Color.FromArgb(16, 128, 128);
foreBrush = new SolidBrush(Color.White);
}
else
{
fnt = e.Font;
backBrush = new SolidBrush(Color.White);
foreBrush = new SolidBrush(Color.Black);
}
string tabName = this.tabControl1.TabPages[e.Index].Text;
StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Center;
e.Graphics.FillRectangle(backBrush, e.Bounds);
Rectangle r = e.Bounds;
r = new Rectangle(r.X, r.Y + 3, r.Width + 10, r.Height - 0);
e.Graphics.DrawString(tabName, fnt, foreBrush, r, sf);
e.Graphics.DrawImage(img, new Point(r.X + (this.tabControl1.GetTabRect(e.Index).Width - _imageLocation.X - 5), _imageLocation.Y ));
this.tabControl1.SelectedTab.BackColor = Color.FromArgb(16, 128, 128);
sf.Dispose();
if (e.Index == this.tabControl1.SelectedIndex)
{
fnt.Dispose();
backBrush.Dispose();
}
else
{
backBrush.Dispose();
foreBrush.Dispose();
}
}
catch (Exception) { }
}
但我需要一个鼠标输入和鼠标离开(我应该给另一个图像)事件这个特定的图像我添加到tabpage标题。
请帮我解决这个问题。
but i need an mouse enter and mouse leave (in which i should give another image) event for this particular image i added to the tabpage header.
please help me to solve this.
对于标准 TabControl 你可以这样做,不是很优雅,但它的工作原理:
For a standard TabControl you can do something like this, not very elegant but it works:
private void tabControl1_MouseEnter(object sender, EventArgs e)
{
TabControl tab = sender as TabControl;
Point tabpos = tab.PointToClient(Cursor.Position);
Debug.Print("MouseEnter " + tab.SelectedTab.Name);
Debug.Print("MouseEnter " + tabpos);
if (tabpos.X < tabControl1.ItemSize.Width)
{
Debug.Print("Tab 1");
}
else
{
Debug.Print("Tab 2");
}
}
对于像这样的自定义控件:绘制自己的标签 - 第二版 [ ^ ]
你可以修改 CustomTabControl的代码像这样:
For a custom control like this one: Painting Your Own Tabs - Second Edition[^]
You could modify the code of CustomTabControl like this:
protected override void OnMouseEnter(EventArgs e)
{
Debug.Print("OnMouseEnter");
base.OnMouseEnter(e);
}
这一个:具有阴影效果的斜面板 - 现在带有标签 [ ^ ]
您可以这样修改:
And this one: Beveled Panel with Shadow Effect - Now With Tabs[^]
You can modify like this:
public class AdvancedTab : Panel
...
public AdvancedTab()
{
this.Size = new Size(440, 40);
this.Paint += this.AdvancedTab_Paint;
this.MouseEnter += this.TabMouseEnter;
...
}
private void TabMouseEnter(object sender, System.EventArgs e)
{
Debug.Print("TabMouseEnter");
}