c# - 如何更改可选面板的边框颜色?

标签 c# winforms graphics custom-controls gdi+

我想将所有面板的 BorderColor 更改为 Color.Lime:

foreach (Control G in GetAllControls (this))
{
    Panel p = sender as Panel;
    ControlPaint.DrawBorder(e.Graphics, p.DisplayRectangle, Color.Lime, ButtonBorderStyle.Inset);
}

它向我显示了这个错误:

Severity Code Description Project File Line Suppression State Error CS1061 'EventArgs' does not contain a definition for 'Graphics' and no accessible extension method 'Graphics' accepting a first argument of type 'EventArgs' could be found (are you missing a using directive or an assembly reference?)

最佳答案

作为建议,使用从面板派生的自定义控件 - 如果您需要其功能 - 或使用从控件派生的自定义控件,以拥有轻量级控件 - 如果您不需要任何预定义的行为(即基本控件功能除外) .

例如,对 Panel 类进行简单调整,添加一些允许定义的属性:

  • 分配给边框的颜色
  • 边框的大小
  • 选择控件时的边框颜色
  • 表示使Panel可选择:进入Control时会改变边框颜色,可以设置TabStop = true和Tab高亮 等等

为了使控件可选择,设置 Selectable 属性时会修改一些样式。
控制中心调用 SetStyle()并将 ControlStyles.SelectableControlStyles.UserMouseControlStyles.StandardClick 设置为 truefalse,然后 UpdateStyles() ,强制使用新样式(控件从 CreateParams 属性检索样式),然后 Invalidate()本身(这会调用 OnPaint 方法,该方法又会引发 Paint 事件)以在新状态下绘制边框。


将名为 PanelEx 的类添加到项目中,粘贴此处的内容(当然,保留命名空间),构建项目,在工具箱中找到新的控件,然后将其放在窗体上。
如果您想用此控件替换所有标准面板控件,请使用 Visual Studio 的搜索/替换功能 (CTRL+H) 并替换现有的标准 Panel 类型对象(那些需要新行为)与 PanelEx 类型。

注意 - x64 项目:
如果主项目需要以 x64 为目标,您可以向解决方案添加一个以 AnyCPU 为目标的新类库项目。将此自定义控件类(当然,或您构建的任何其他自定义控件)添加到此库中。还要添加对 System.Windows.FormsSystem.Drawing 程序集的引用。
重新生成解决方案,Visual Studio 会将库中找到的所有控件添加到工具箱。
当您从工具箱中将控件放到表单上时,没有人会提示。

using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

[DesignerCategory("code")]
public class PanelEx : Panel
{
    private Color m_BorderColorSel = Color.Transparent;
    private Color m_BorderColor = Color.Transparent;
    private bool m_Selectable = false;
    private bool m_Selected = false;
    private int m_BorderSize = 1;

    public PanelEx() { }

    public Color BorderColor { get => m_BorderColor;
        set {
            if (value == m_BorderColor) return;
            m_BorderColor = value;
            Invalidate();
        }
    }

    public int BorderSize {
        get => m_BorderSize;
        set {
            if (value == m_BorderSize) return;
            m_BorderSize = value;
            Invalidate();
        }
    }

    public bool Selectable {
        get => m_Selectable;
        set {
            if (value == m_Selectable) return;
            m_Selectable = value;
            SetStyle(ControlStyles.Selectable | ControlStyles.UserMouse | ControlStyles.StandardClick, value);
            UpdateStyles();
            Invalidate();
        }
    }

    public Color BorderColorSelected {
        get => m_BorderColorSel;
        set {
            m_BorderColorSel = value;
            if (!Selectable || value == m_BorderColorSel) return;
            Invalidate();
        }
    }


    protected override void OnPaint(PaintEventArgs e)
    {
        Color penColor = m_Selectable && m_Selected ? m_BorderColorSel : m_BorderColor;
        int rectOffset = BorderSize / 2;
        using (Pen pen = new Pen(penColor, BorderSize)) {
            var rect = new Rectangle(rectOffset, rectOffset, ClientSize.Width - BorderSize, ClientSize.Height - BorderSize);
            e.Graphics.DrawRectangle(pen, rect);
        }
        base.OnPaint(e);
    }

    protected override void OnMouseEnter(EventArgs e)
    {
        base.OnMouseEnter(e);
        OnEnter(e);
    }
    protected override void OnMouseLeave(EventArgs e)
    {
        base.OnMouseLeave(e);
        OnLeave(e);
    }

    protected override void OnEnter(EventArgs e)
    {
        base.OnEnter(e);
        m_Selected = true;
        Invalidate();
    }
    protected override void OnLeave(EventArgs e)
    {
        base.OnLeave(e);
        m_Selected = false;
        Invalidate();
    }
}

关于c# - 如何更改可选面板的边框颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66923713/

相关文章:

java - 为什么我的背景在重绘后会出现故障?

Android粒子系统收敛单点

c# - 窗体在 C# 中失去焦点

c# - 像 VLOOKUP 的东西

c# - 如何从路径中获取文件名?

winforms - 启动默认的网络浏览器,但如果 URL 已经打开则不启动

c# - 在 Elasticsearch 中按日期范围列表搜索

c# - 字符串未被识别为阿拉伯语的有效日期时间

c# - 如何在 gridview 中创建密码类型的列?

java - 如何从一组形成直线的点中获取曲线的控制点?