动态添加多个PictureBox,并在PictureBox上面写字

由于项目需求,需要动态的添加多个菜单,左右两列,有同样的背景图片,就是文字不一样,开始考虑的是使用Button,设置背景,然后再设置Button的文字即可,但是遇到一个问题,就是透明的PNG图片无法实现透明,最后只能考虑使用PictureBox了,动态添加多个PictureBox,并在PictureBox上面写字

利用的是PictureBox的Paint事件,在其重绘事件时利用Graphics绘画文字

如果只是单个的PB,可以利用pictureBox1.CreateGraphics()创建Graphics对象,完成绘画

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
private void button1_Click(object sender, EventArgs e)
  {
      int leftY=50;
      for (int i = 0; i < 5; i++)
      {
          PictureBox pb = new PictureBox();
          pb.Image = global::WindowsFormsApplication2.Properties.Resources.leftempty;
          pb.Size = new System.Drawing.Size(186, 67);
          pb.Location = new System.Drawing.Point(30, leftY);
          pb.Paint += new System.Windows.Forms.PaintEventHandler(this.pictureBox1_Paint);
          leftY += 100;
          this.Controls.Add(pb);   
      }
  }
 
  private void pictureBox1_Paint(object sender, PaintEventArgs e)
  {
      Graphics g = e.Graphics;
      g.SmoothingMode = SmoothingMode.HighQuality;
      g.DrawString("sg.com.cn ", new Font("Arial ", 10, FontStyle.Bold), SystemBrushes.ActiveCaptionText, new PointF(20, 30));
  }
posted @ 2011-03-03 16:42  徐文峰  阅读(4958)  评论(1)    收藏  举报
编辑推荐:
· 还在手写JSON调教大模型?.NET 9有新玩法
· 复杂业务系统线上问题排查过程
· 通过抓包,深入揭秘MCP协议底层通信
· 记一次.NET MAUI项目中绑定Android库实现硬件控制的开发经历
· 糊涂啊!这个需求居然没想到用时间轮来解决
阅读排行:
· Coze Studio:字节跳动 Coze 的开源版本来了!第一时间深度解析
· 一款超级经典复古的 Windows 9x 主题风格 Avalonia UI 控件库,满满的回忆杀!
· 我给 AI 接上了一个 C# 运行器,结果它学会了自己上网、调试代码
· AI再强大,也不如人类员工用的爽?
· 我只说需求,AI 全程托管,代码自己长出来了!
点击右上角即可分享
微信分享提示