问题描述
有没有办法禁用或更好地为常规按钮控件绘制自己的焦点矩形!(那条虚线看起来很像 Windows 95ish)
Is there a way to disable or better yet draw your own focus rectangle for a regular button control! (that dotted line seems so Windowss 95ish)
我注意到控件属性(FOR BUTTONS)没有 ownerdrawfixed 设置(我不知道这是否是用于解决方案的路径,尽管我已经看到它用于自定义其他控件).
I've noticed that the control properties (FOR BUTTONS) does not have a ownerdrawfixed setting (which I don't know if that's even the route to use for the solution, although i've seen it used for customizing other controls).
推荐答案
要做到这一点比听起来更棘手.毫无疑问,自定义按钮绘制不可覆盖的原因之一.这按预期工作:
Getting this right is trickier than it sounds. No doubt one of the reasons that custom button painting isn't overridable. This worked as expected:
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Windows.Forms.VisualStyles;
class MyButton : Button {
private VisualStyleRenderer renderer;
protected override void OnPaint(PaintEventArgs e) {
base.OnPaint(e);
if (this.Focused && Application.RenderWithVisualStyles && this.FlatStyle == FlatStyle.Standard) {
if (renderer == null) {
VisualStyleElement elem = VisualStyleElement.Button.PushButton.Normal;
renderer = new VisualStyleRenderer(elem.ClassName, elem.Part, (int)PushButtonState.Normal);
}
Rectangle rc = renderer.GetBackgroundContentRectangle(e.Graphics, new Rectangle(0, 0, this.Width, this.Height));
rc.Height--;
rc.Width--;
using (Pen p = new Pen(Brushes.DarkGray)) {
e.Graphics.DrawRectangle(p, rc);
}
}
}
}
这篇关于c# WinForm:删除或自定义按钮的“焦点矩形"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!