微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c#WinForm:删除或自定义按钮的“对焦矩形”

有没有办法禁用或更好地绘制自己的焦点矩形为常规的按钮控件! (虚线似乎是 Windowss 95ish)

我注意到,控件属性(FOR BUTTONS)没有ownerdrawfixed设置(我不知道这甚至是用于解决方案的路径,尽管我已经看到它用于自定义其他控件).

解决方法

得到这个权利比听起来更棘手.毫无疑问,自定义按钮绘画不可覆盖的原因之一.这样工作如预期:
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,this.Width,this.Height));
            rc.Height--;
            rc.Width--;
            using (Pen p = new Pen(Brushes.DarkGray)) {
                e.Graphics.DrawRectangle(p,rc);
            }
        }
    }
}

原文地址:https://www.jb51.cc/csharp/92704.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐