关于winforms:如何在C#中的Button上设置/更改/删除焦点样式?

您所在的位置:网站首页 焦点颜色怎么操作 关于winforms:如何在C#中的Button上设置/更改/删除焦点样式?

关于winforms:如何在C#中的Button上设置/更改/删除焦点样式?

2024-07-13 07:20| 来源: 网络整理| 查看: 265

我有几个按钮,我修改了它们的外观。 我将它们设置为带有背景和自定义边框的平面按钮,因此它们看起来都非常漂亮,不再像普通按钮一样(实际上,它们现在看起来像Office 2003按钮;-)。 这些按钮的边框为一个像素。

但是,当按钮被选中时(通过单击或通过按下Tab键的键盘操作获得焦点),按钮会突然出现,并在其周围带有相同颜色的额外边框,因此使其成为两个像素的边框。 而且,当我禁用一个像素边框时,按钮不会获得一个像素边框的焦点。

在网上这个问题被问得很像"如何禁用按钮上的焦点",但这不是我想要的:焦点应该仍然存在,只是不能以现在的方式显示。

有什么建议么? :-)

这是您想要的效果吗?

12345678910public class NoFocusCueButton : Button {     protected override bool ShowFocusCues     {         get         {             return false;         }     } }

您可以像使用常规按钮一样使用此自定义按钮类,但是不会为您提供额外的矩形焦点。

相关讨论 感谢您的好建议! 这摆脱了内部的灰色焦点矩形,但没有添加一个像素边框(这使其总共有两个像素边框)。 @Chris Jester-Young:检查Josh Stribling的答案以隐藏其他一个像素边框。 谢谢Michael Perry,这个解决方案完美地解决了我的问题。

我对恼人的双边框有同样的问题,偶然发现了这个线程寻找答案...

我解决此问题的方法是将BorderSize设置为0,然后在OnPaint中绘制自己的边框

*注意:不是整个按钮,只有边框

一个简单的例子是:

1234567891011121314151617181920public 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);     } }

就我而言,这就是我制作一个模仿ToolStripButton的按钮的方式,该按钮仅在将鼠标悬停在按钮上时才显示边框:

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960public 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; } } }

制作一个自定义按钮:

123456789public partial class CustomButton: Button {     public ButtonPageButton()     {         InitializeComponent();         this.SetStyle(ControlStyles.Selectable, false);     } }

那将摆脱那个烦人的边界! ;-)

相关讨论 不幸的是,这使按钮无法聚焦。 覆盖ShowFocusCues似乎更好。

另一个选择(虽然有点怪异)是将事件处理程序附加到按钮的GotFocus事件。在该事件处理程序中,将False的值传递给按钮的NotifyDefault()方法。因此,例如:

1234void myButton_GotFocus(object sender, EventArgs e) {   myButton.NotifyDefault(false); }

我以为这每次都会起作用,但是我还没有对其进行广泛的测试。目前它对我有用,所以我对此感到满意。

还有另一种方法适合平面样式的按钮。不要使用按钮,而是使用标签。当您完全替换按钮的UI时,无论使用按钮控件还是标签都无所谓。只需以相同方式处理点击。

这对我有用,尽管不是很好的实践,但这是一个不错的技巧,只要您明显地命名按钮(并注释源代码),其他编码人员就会采纳这个想法。

瑞安

添加的第二个边框是Windows标准的"默认按钮"边框。您可能已经注意到,如果您在大多数对话框中使用多个按钮(例如任何"控制面板"属性窗口)进行制表,则原始的"双边框"按钮将变为"正常",而焦点对准的按钮将变为"双边框"。"

这不一定专注于工作,而是通过按Enter键直观地指示所采取的操作。

在我看来,听起来好像您并不真正在意内部工作。您希望显示器没有两个边界-完全可以理解。内部工作是为了解释为什么您会看到此行为。现在...尝试修复它。

我要尝试的第一件事-记住,我尚未验证-是黑客。当按钮获得焦点时(从而获得双边框),请关闭单个边框。您可能会得到想要的效果,而且非常简单。 (进入Focus事件。更好的是,子类为Button并重写OnFocus,然后将该子类用于将来的按钮。)

但是,这可能会带来新的,尴尬的视觉副作用。出于这种考虑-而且因为黑客攻击很少是最好的答案-我必须"正式"推荐其他人所说的话:"自定义绘制按钮"。尽管此处的代码可能有些过分,但CodeProject上的此链接讨论了如何执行此操作(VB链接;您需要翻译)。您应该在完全自定义模式下完全摆脱第二个边界。

您还可以创建一个不可见的按钮,并在每次按下另一个按钮时将其激活。

如果您有一个文本框和一个按钮 然后在文本框的textchange事件 写button1.focus();

会的。

在您的样式中将FocusVisualStyle依赖项属性设置为null,虚线边框将消失。

来自MSDN:样式集中于控件和FocusVisualStyle

Windows Presentation Foundation (WPF) provides two parallel mechanisms for changing the visual appearance of a control when it receives keyboard focus. The first mechanism is to use property setters for properties such as IsKeyboardFocused within the style or template that is applied to the control. The second mechanism is to provide a separate style as the value of the FocusVisualStyle property; the "focus visual style" creates a separate visual tree for an adorner that draws on top of the control, rather than changing the visual tree of the control or other UI element by replacing it. This topic discusses the scenarios where each of these mechanisms is appropriate.

您看到的额外边框是由FocusVisualStyle定义的,而不是在控件模板中定义的,因此您需要删除或覆盖样式以删除边框。

相关讨论 这是用于WPF,而不是Winforms。

考虑为按钮实现自己的绘图代码。这样,您就可以完全控制。过去,我实现了自己的Control派生,该控件自定义绘制按钮并实现所有按钮特性以达到我的目的,但是您应该能够覆盖按钮的绘制并自己完成,从而控制按钮在每种状态下的绘制方式,包括重点关注时。

当然,您可以自己绘制按钮。状态标志之一被聚焦。

因此,在draw事件中,如果标志着重,请继续按照您的喜好绘制按钮,否则将其传递给base方法。

相关讨论 由于没有Draw事件,您能否详细说明一下? 但是,有一个Paint事件,但没有状态标志。 =)我理解并喜欢这个想法,但是在实际的实现上却有所遗漏。

我很幸运,仅将按钮的Focusable属性设置为false:

1234

相关讨论 WPF与WinForms不同。



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3