一个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);
        }

  

posted @   JackJu  阅读(457)  评论(0编辑  收藏  举报
编辑推荐:
· 智能桌面机器人:用.NET IoT库控制舵机并多方法播放表情
· Linux glibc自带哈希表的用例及性能测试
· 深入理解 Mybatis 分库分表执行原理
· 如何打造一个高并发系统?
· .NET Core GC压缩(compact_phase)底层原理浅谈
阅读排行:
· 新年开篇:在本地部署DeepSeek大模型实现联网增强的AI应用
· DeepSeek火爆全网,官网宕机?本地部署一个随便玩「LLM探索」
· Janus Pro:DeepSeek 开源革新,多模态 AI 的未来
· 上周热点回顾(1.20-1.26)
· 【译】.NET 升级助手现在支持升级到集中式包管理
点击右上角即可分享
微信分享提示