我有几个按钮,我修改了它们的外观.我已将它们设置为带有背景和自定义边框的平面按钮,因此它们看起来很漂亮而且不再像普通按钮那样(实际上,它们现在看起来像Office 2003按钮;-).按钮的边框为一个像素.
然而,当按钮被选中时(通过点击或键盘操作获得焦点,如按Tab键),按钮会突然获得相同颜色的额外边框,因此使其成为两个像素的边框.此外,当我禁用一个像素边框时,按钮不会在焦点上获得一个像素边框.
在网上这个问题被问了很多,比如'如何禁用对按钮的关注',但这不是我想要的:焦点应该仍然存在,只是不显示是以现在的方式.
有什么建议?:-)
Mic*_*rry 38
这是你想要的效果吗?
public class NoFocusCueButton : Button
{
protected override bool ShowFocusCues
{
get
{
return false;
}
}
}
Run Code Online (Sandbox Code Playgroud)
您可以像使用常规按钮一样使用此自定义按钮类,但它不会在焦点上为您提供额外的矩形.
Jos*_*ing 21
我对烦人的双边框有同样的问题,偶然发现这个线程寻找答案......
我解决这个问题的方法是将BorderSize设置为0,然后在OnPaint中绘制我自己的边框
*注意:不是整个按钮,只是边框
一个简单的例子是:
public class CustomButton : Button
{
public CustomButton()
: base()
{
// Prevent the button from drawing its own border
FlatAppearance.BorderSize = 0;
FlatStyle = System.Windows.Forms.FlatStyle.Flat;
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
// Draw Border using color specified in Flat Appearance
Pen pen = new Pen(FlatAppearance.BorderColor, 1);
Rectangle rectangle = new Rectangle(0, 0, Size.Width - 1, Size.Height - 1);
e.Graphics.DrawRectangle(pen, rectangle);
}
}
Run Code Online (Sandbox Code Playgroud)
在我的例子中,这是我创建一个模仿ToolStripButton的按钮的方式,只有当您将鼠标悬停在按钮上时,边框才会显示:
public class ToolButton : Button
{
private bool ShowBorder { get; set; }
public ToolButton()
: base()
{
// Prevent the button from drawing its own border
FlatAppearance.BorderSize = 0;
// Set up a blue border and back colors for the button
FlatAppearance.BorderColor = Color.FromArgb(51, 153, 255);
FlatAppearance.CheckedBackColor = Color.FromArgb(153, 204, 255);
FlatAppearance.MouseDownBackColor = Color.FromArgb(153, 204, 255);
FlatAppearance.MouseOverBackColor = Color.FromArgb(194, 224, 255);
FlatStyle = System.Windows.Forms.FlatStyle.Flat;
// Set the size for the button to be the same as a ToolStripButton
Size = new System.Drawing.Size(23, 22);
}
protected override void OnMouseEnter(EventArgs e)
{
base.OnMouseEnter(e);
// Show the border when you hover over the button
ShowBorder = true;
}
protected override void OnMouseLeave(EventArgs e)
{
base.OnMouseLeave(e);
// Hide the border when you leave the button
ShowBorder = false;
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
// The DesignMode check here causes the border to always draw in the Designer
// This makes it easier to place your button
if (DesignMode || ShowBorder)
{
Pen pen = new Pen(FlatAppearance.BorderColor, 1);
Rectangle rectangle = new Rectangle(0, 0, Size.Width - 1, Size.Height - 1);
e.Graphics.DrawRectangle(pen, rectangle);
}
}
// Prevent Text from being set on the button (since it will be an icon)
[Browsable(false)]
public override string Text { get { return ""; } set { base.Text = ""; } }
[Browsable(false)]
public override ContentAlignment TextAlign { get { return base.TextAlign; } set { base.TextAlign = value; } }
}
Run Code Online (Sandbox Code Playgroud)
小智 15
制作自定义按钮:
public partial class CustomButton: Button
{
public ButtonPageButton()
{
InitializeComponent();
this.SetStyle(ControlStyles.Selectable, false);
}
}
Run Code Online (Sandbox Code Playgroud)
那将摆脱那令人讨厌的边界!;-)
小智 6
另一个选项(虽然有点hacktastic)是将事件处理程序附加到按钮的GotFocus事件.在该事件处理程序中,将值False传递给按钮的NotifyDefault()方法.所以,例如:
void myButton_GotFocus(object sender, EventArgs e)
{
myButton.NotifyDefault(false);
}
Run Code Online (Sandbox Code Playgroud)
我假设每次都会这样,但我没有对它进行过广泛的测试.它现在对我有用,所以我很满意.