繁体   English   中英

如何在c#winforms中为标签页设置关闭按钮的颜色

[英]How to set the color of a close button for a tab page in c# winforms

我有一个标签控件说tabMain。 在我的主页选项卡的第一个选项卡中,我有一个下拉列表和一个添加按钮。 单击“添加”按钮时,将打开一个新选项卡,其中所选项目为选项卡标题。 除了标签标题,我还需要包含一个关闭按钮。 现在我有关闭按钮但我还需要为关闭按钮设置背景颜色。 这是我的一部分代码。

 TabPage newTabPage= new TabPage();   
 newTabPage.Text = cmbType.SelectedItem.ToString() +"  X";
 tabMain.TabPages.Add(newTabPage);

在我的鼠标按下事件中,我已将函数包含在中。

 private void tabMain_MouseDown(object sender, MouseEventArgs e)
    {
        try
        {                
            if (tabMain.SelectedIndex > 0)
            {
                Rectangle r = tabMain.GetTabRect(tabMain.SelectedIndex);                    
                Rectangle closeButton = new Rectangle(r.Right - 15, r.Y, 15, 18);

                if (closeButton.Contains(e.Location))
                {
                    if (MessageBox.Show("Would you like to Close this Tab?", "Confirm", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                    {
                        this.tabMain.TabPages.RemoveAt(tabMain.SelectedIndex);
                    }
                }
            } 
        }
        catch(Exception)
        {
            MessageBox.Show("Exception in closing a tab");
        }
    }

我的关闭按钮工作正常。 但我需要为关闭按钮设置一种颜色。 我也试过加一个标签。 这是代码

TabPage newTabPage = new TabPage();                
Label labelClose = new Label();
labelClose.Text = "  X";
labelClose.BackColor = System.Drawing.Color.Red;               
newTabPage.Text = cmbType.SelectedItem.ToString() + labelClose.Text;
tabMain.TabPages.Add(newTabPage); 

任何帮助,将不胜感激。 提前致谢

要为矩形添加背景颜色,您需要执行以下操作:

要绘制一个填充颜色的Rectangle,您需要一个Graphics对象和一个从Brush派生的对象,如SolidBrush或LinearGradientBrush。 Graphics对象提供FillRectangle方法,Brush对象提供颜色和填充信息。

//Create graphic object for the current form
Graphics gs = this.CreateGraphics();
//Create a rectangle object
Rectangle closeButton = new Rectangle(r.Right - 15, r.Y, 15, 18);
//Fill the rectangle with red color
gs.FillRectangle(new SolidBrush(Color.Red), closeButton);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM