[英]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.