【问题标题】:How to change BorderColor of a selectable Panel?如何更改可选面板的边框颜色?
【发布时间】:2021-04-02 18:31:38
【问题描述】:

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

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

它显示了这个错误:

严重性代码描述项目文件行抑制状态 错误 CS1061“EventArgs”不包含“Graphics”的定义,并且找不到接受“EventArgs”类型的第一个参数的可访问扩展方法“Graphics”(您是否缺少 using 指令或程序集引用?)

【问题讨论】:

  • 这是按钮的点击事件吗? EventArgs 不会有 Graphics 对象。您必须改为在每个 Panel 的 Paint 事件中进行绘画。
  • 哦,你能告诉我正确的吗?
  • 我已经这样做了。
  • @LarsTech 还有其他方法吗?因为我有 100 多个面板,必须有更简单的方法来做到这一点。

标签: c# winforms graphics custom-controls gdi+


【解决方案1】:

作为建议,如果您不需要任何预定义的行为(即基本控件功能除外),请使用从 Panel 派生的自定义控件 - 如果您需要它的功能 - 或从 Control 派生一个轻量级控件.

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

  • 分配给边框的颜色
  • 边框的大小
  • 选择控件时的边框颜色
  • 意思是让Panel可选择:输入Control时会改变Border Color,你可以设置TabStop = true和Tab到highlight等。李>

为了使控件可选择,在设置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();
    }
}

【讨论】:

  • 它总是给出这个错误:(linkpicture.com/q/rip_7.jpg
  • 我的项目是64位
  • OMFGGG 成功了!我将 64 位更改为任何 cpu 并重建 yeyyy thx man thx thx thx THX ...我真的要画每个面板:D 你是救生员❤????????
  • 那么我应该如何添加“类库项目”?并将其更改为 x64? 抱歉,我问了这么多问题。我只是不想做错任何事。
  • 我可以在“CustomControls”项目中创建一个类。但是当我粘贴相同的东西时,它向我展示了很多错误,例如 'GreenPanel' does not contain a definition for 'Invalidate' and no accessible extension method 'Invalidate' accepting a first argument of type 'GreenPanel' could be found Errors
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-26
  • 2017-03-06
  • 1970-01-01
  • 1970-01-01
  • 2020-05-14
  • 1970-01-01
相关资源
最近更新 更多