一个button上两种字体 重写OnPaint方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | public class XButton : Button { public XButton() { UseVisualStyleBackColor = false ; TextImageRelation = TextImageRelation.ImageAboveText; } public override string Text { get { return "" ; } set { base .Text = value; } } public string LeftText { get ; set ; } public string RightText { get ; set ; } protected override void OnPaint(PaintEventArgs pevent) { base .OnPaint(pevent); Rectangle rect = ClientRectangle; rect.Inflate(-5, -5); using (StringFormat sf = new StringFormat() { Alignment = StringAlignment.Near, LineAlignment = StringAlignment.Far }) { using (Brush brush = new SolidBrush(ForeColor)) { Font font= new Font(Font,FontStyle.Bold); pevent.Graphics.DrawString(LeftText, font, brush, rect, sf); sf.Alignment = StringAlignment.Far; pevent.Graphics.DrawString(RightText, Font, brush, rect, sf); } } } } private void Form1_Load( object sender, EventArgs e) { XButton button = new XButton(); button.LeftText = "hello" ; button.RightText = "test" ; this .Controls.Add(button); } |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Brainfly: 用 C# 类型系统构建 Brainfuck 编译器
· 智能桌面机器人:用.NET IoT库控制舵机并多方法播放表情
· Linux glibc自带哈希表的用例及性能测试
· 深入理解 Mybatis 分库分表执行原理
· 如何打造一个高并发系统?
· DeepSeek 全面指南,95% 的人都不知道的9个技巧(建议收藏)
· Tinyfox 发生重大改版
· 对比使用DeepSeek与文新一言,了解DeepSeek的关键技术论文
· Brainfly: 用 C# 类型系统构建 Brainfuck 编译器
· DeepSeekV3+Roo Code,智能编码好助手