C# WinForms - 未选择表单时自定义按钮不需要的边框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9399215/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 07:12:55  来源:igfitidea点击:

C# WinForms - custom button unwanted border when form unselected

c#winformsbuttonborder

提问by Jpin

I'm having a problem with a custom button I have created in c# win forms.. The button appears fine when the form is selected but as soon as I click away from the form a border appears on the button. A good example of when this happens is when the desktop is clicked but the form is still maximised so you can see its contents. An image of the problem can be seen below:

我在C#Win表单中创建了一个自定义按钮的问题。在选择表单时,按钮似乎很好,但只要我从“远离”按钮上出现边框,就会出现在按钮上的边框。发生这种情况的一个很好的例子是单击桌面但表单仍然最大化以便您可以看到其内容。可以在下面看到问题的图像:

Button border problem

按钮边框问题

This does not happen on all buttons, only when the button has been clicked prior (only appears on one button at a time). This lead me to believe that it was something to do with the button focus cues but these are set to false. The border is set to 0 and I also have tabstop set to false.

这不会发生在所有按钮上,只有在先前单击该按钮时才会发生(一次只出现在一个按钮上)。这让我相信这与按钮焦点提示有关,但这些提示设置为 false。边框设置为 0,我也将制表位设置为 false。

Any suggestions?

有什么建议?

采纳答案by Omar

When you're dealing with a custom button you should set:

当您处理自定义按钮时,您应该设置:

button.TabStop = false;
button.FlatStyle = FlatStyle.Flat;
button.FlatAppearance.BorderSize = 0;

Then since ButtonBasedoesn't support the border color on Color.Transparent, you can overcome the issue by setting an Argb color:

然后由于ButtonBase不支持边框颜色Color.Transparent,您可以通过设置 Argb 颜色来解决该问题:

button.FlatAppearance.BorderColor = Color.FromArgb(0, 255, 255, 255); //transparent

回答by FrankX

fuex's answer can remove border in theory, but there is a bug that sometimes button will still have focus cue after you change the button enable status.

fuex 的回答理论上可以去除边框,但是有一个 bug,有时按钮在更改按钮启用状态后仍然有焦点提示。

(I ran into this bug in .Net 4.0 and I don't know the bug is fixed or not in later versions).

(我在 .Net 4.0 中遇到了这个错误,我不知道该错误在以后的版本中是否已修复)。

To work around this bug, you should disable the ShowFocusCuesproperty:

要解决此错误,您应该禁用该ShowFocusCues属性:

protected override bool ShowFocusCues => false; // return base.ShowFocusCues;