如何将鼠标输入事件添加到动态创建的图形矩形到tabcontrol [英] How do I add an mouse enter event to a dynamically created graphic rectangle to a tabcontrol

查看:74
本文介绍了如何将鼠标输入事件添加到动态创建的图形矩形到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");
	}


这篇关于如何将鼠标输入事件添加到动态创建的图形矩形到tabcontrol的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆