继续接着上面的
自动格式设置
先看个图

相信大家都很熟悉吧,我们可以用这个面板很方面的使用预定的样式.我们可以称之为自动格式设置或者自动套用样式.
ControlDesigner类提供了AutoFormats属性,其提供了DesignerAutoFormat类的DesignerAutoFormatCollection集合.我们来看下相关的类.

DesignerAutoFormat 是一个基类,如果你想为你的控件在设计时提供格式化的功能,你可以从此类派生,你必须实现Apply方法,此方法会将相关联的控件设置样式.由于实现比较简单就不再多多了,就直接拿MSDN的例子来看吧. 注意给 IndentLabelDesigner 加上SupportsPreviewControl元数据,这样可以支持预览功能.


[Designer(typeof(IndentLabelDesigner)),
ToolboxData("<{0}:IndentLabel Runat=\"server\"></{0}:IndentLabel>")]
public class IndentLabel : Label

{
[SupportsPreviewControl(true)]
public class IndentLabelDesigner : LabelDesigner

{
private DesignerAutoFormatCollection _autoFormats = null;

public override DesignerAutoFormatCollection AutoFormats

{
get

{
if (_autoFormats == null)

{
_autoFormats = new DesignerAutoFormatCollection();
_autoFormats.Add(new IndentLabelAutoFormat("MyClassic"));
_autoFormats.Add(new IndentLabelAutoFormat("MyBright"));
_autoFormats.Add(new IndentLabelAutoFormat("Default"));
}
return _autoFormats;
}
}
}

private class IndentLabelAutoFormat : DesignerAutoFormat

{
public IndentLabelAutoFormat(string name)
: base(name)

{ }

public override void Apply(Control inLabel)

{
if (inLabel is IndentLabel)

{
IndentLabel ctl = (IndentLabel)inLabel;

if (this.Name == "MyClassic")

{
ctl.ForeColor = Color.Gray;
ctl.BackColor = Color.LightGray;
ctl.Font.Size = FontUnit.XSmall;
ctl.Font.Name = "Verdana,Geneva,Sans-Serif";
}
else if (this.Name == "MyBright")

{
this.Style.ForeColor = Color.Maroon;
this.Style.BackColor = Color.Yellow;
this.Style.Font.Size = FontUnit.Medium;
ctl.MergeStyle(this.Style);
}
else

{
ctl.ForeColor = Color.Black;
ctl.BackColor = Color.Empty;
ctl.Font.Size = FontUnit.XSmall;
}
}
}
}
}
这么着效果就实现了,这次比较懒,没好好写,还想打算写别的,就先这样吧.
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构