我想将所有面板的 BorderColor 更改为 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 的自定义控件 - 如果您需要其功能 - 或来自 Control 的轻量级控件 - 如果您不需要任何预定义行为(即基本控件功能除外)。
例如,对 Panel 类进行简单调整,添加一些允许定义的属性:
TabStop = true
和 Tab 突出显示等 为了使控件可选,在
Selectable
时修改了一些样式。属性设置。控制调用 SetStyle()和套
ControlStyles.Selectable
, ControlStyles.UserMouse
和 ControlStyles.StandardClick
至 true
或 false
,然后 UpdateStyles() , 强制使用新样式(控件从 CreateParams 属性检索样式),然后是 Invalidate()本身(这会调用 OnPaint
方法,该方法又会引发 Paint 事件)以在新状态下绘制边框。添加名为
PanelEx
的类到项目,粘贴这里的内容(当然,保留命名空间),构建项目,在工具箱中找到新控件并将其放在表单上。如果您想用这个替换所有标准面板控件,请使用 Visual Studio (CTRL+H) 的搜索/替换功能并替换现有标准
Panel
使用 PanelEx
键入对象(需要新行为的对象)类型。注意 -
x64
项目:如果主项目需要定位
x64
,您可以添加一个新的类库项目,目标是 AnyCPU
到解决方案。将此自定义控件类(或您构建的任何其他自定义控件,当然)添加到此库中。还要添加对 System.Windows.Forms
的引用和 System.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);
this.UpdateStyles();
this.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);
this.OnEnter(e);
}
protected override void OnMouseLeave(EventArgs e)
{
base.OnMouseLeave(e);
this.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/