[英]Change button border and disabled appearance
我的表單中有2個按鈕。 兩者都有背景圖片,並且Flat樣式為“ Flat”。
在單擊TAB鍵時,當焦點位於按鈕上時,我看到內部是純色矩形,而不是我希望在按鈕中具有虛線邊框的實心邊框。
單擊該按鈕時,正在進行一些活動,並且該按鈕被禁用。 禁用后,按鈕文本顏色變為灰色。 我不希望它更改文本顏色。 我相信這是Windows的標准行為,但是當選擇按鈕時如何不更改文本顏色?
我找不到設置這些點的任何屬性。 我應該怎么做才能達到目標。 非常感謝您的幫助。
我還創建了一個自定義按鈕,但無法擺脫上述問題。
我的自定義按鈕的繪制方法:
public partial class MyButton : Button {
protected override void OnPaint(PaintEventArgs pevent)
{
base.OnPaint(pevent);
// Set custom fore color
base.ForeColor = Color.FromArgb(0, 255, 254, 255);
// set the same forecolor even if the button is disabled
if (base.Enabled == false)
{
base.ForeColor = Color.FromArgb(0, 255, 254, 255);
}
} //OnPaint method ends
} // class ends
(1)和(2)的更新Soluiton在onpaint()中添加了以下內容:
private void init()
{
base.ForeColor = Color.FromArgb(0, 255, 254, 255);
base.FlatAppearance.BorderColor = Color.FromArgb(0, 255, 255, 254); // Transparent border
}
protected override void OnPaint(PaintEventArgs pevent)
{
base.OnPaint(pevent);
if (base.ContainsFocus)
{
// Draw inner dotted rectangle when button is on focus
Pen pen = new Pen(Color.Gray, 3);
Point p = base.Location;
pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot;
Rectangle rectangle = new Rectangle(4, 4, Size.Width - 8, Size.Height - 8);
ControlPaint.DrawFocusRectangle(pevent.Graphics, rectangle);
}
// Draw the string to screen
SizeF sf = pevent.Graphics.MeasureString(displayText, this.Font, this.Width);
Point ThePoint = new Point();
ThePoint.X = (int)((this.Width / 2) - (sf.Width / 2));
ThePoint.Y = (int)((this.Height / 2) - (sf.Height / 2));
//pevent.Graphics.DrawString(displayText, Font, new SolidBrush(this.ForeColor), ThePoint);
//pevent.Graphics.DrawString(displayText, Font, new SolidBrush(Color.FromArgb(0, 255, 254, 255)), ThePoint);
pevent.Graphics.DrawString(displayText, Font, new SolidBrush(Color.Black), ThePoint);
//this.Text = "";
}//OnPaint ends
// Avoids the inner solid rectangle shown on focus
protected override bool ShowFocusCues
{
get
{
return false;
}
}
這成功在我的按鈕上繪制了一條虛線以指示焦點。 為了擺脫默認的實線,我重寫了ShowFocusCues()。
這幫助我徹底解決了1。 但是,對於第2點,並沒有獲得100%的結果。 我在MyButton類中創建了一個屬性“ displayText”。 在OnPaint()中,如果我將ForeColor,Color.FromARGB提供給SolidBrush,則不會顯示任何文本。 但是,如果我通過Color.Black,則它將接受它並以黑色顯示文本。 為什么不接受ForeColor或自定義顏色,而僅接受默認顏色? 對於這個簡單的任務,我仍然在哪里出錯。 我通過在表單中實現button_paint來嘗試了相同的操作,但是我看到了相同的結果。
知道我仍然在哪里出錯嗎?
private void button3_Paint(object sender, PaintEventArgs e)
{
SolidBrush br = new SolidBrush(Color.Blue);
Pen pen = new Pen(br);
pen.Width = 3;
pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot;
// to draw left border
e.Graphics.DrawLine(pen, new Point(0, 0), new Point(0, this.button3.Width));
SolidBrush drawBrush = new SolidBrush(ForeColor); //Use the ForeColor property
// Draw string to screen.
e.Graphics.DrawString("Sample", Font, drawBrush, 5f, 3f);
this.button3.Text = "";
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.