110,571
社区成员
发帖
与我相关
我的任务
分享
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication3
{
public partial class UserControl1 : UserControl
{
private int count = 4;
public int Count
{
get { return count; }
set
{
count = value;
this.Invalidate();
}
}
public int index = 0;
public string name = "";
public Color color;
public UserControl1()
{
InitializeComponent();
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
Graphics g = e.Graphics;
#region 定义颜色
if (index <= count)
{
if (index == 1)
{
color = Color.Red ;
}
if (index == 2)
{
color = Color.Black;
}
if (index == 3)
{
color = Color.Blue;
}
if (index == 4)
{
color = Color.SkyBlue;
}
}
else
{
count = index;
if (index == 5)
{
color = Color.Coral;
}
if (index == 6)
{
color = Color.CornflowerBlue;
}
}
#endregion
#region 分割图形
Pen pn1 = new Pen(Color.Black, 2);
for (int i = 0; i < count; i++)
{
if (i < count)
{
Point pot1 = new Point((this.Width * i) / count, 0);
Point pot2 = new Point((this.Width * i) / count, this.Height);
g.DrawLine(pn1, pot1, pot2);
}
}
#endregion
#region 写字填充
Font verdanaFont = new Font("Verdana", 10, FontStyle.Bold);
Rectangle rect = new Rectangle(this.Width * (index - 1) / count + 1, 0, this.Width / count - 1, this.Height);
StringFormat strFormat1 = new StringFormat();
strFormat1.Alignment = StringAlignment.Center;
strFormat1.LineAlignment = StringAlignment.Center;
strFormat1.Trimming = StringTrimming.EllipsisCharacter;
g.FillRectangle(new SolidBrush(color), rect);
g.DrawString(name, verdanaFont, new SolidBrush(Color.Black), rect, strFormat1);
#endregion
#region 边框绘制
Pen pn = new Pen(Color.Black);
g.DrawRectangle(pn, 0, 0, this.Width - 1, this.Height - 1);
#endregion
strFormat1.Dispose();
pn1.Dispose();
pn.Dispose();
verdanaFont.Dispose();
}
#region 公有方法
public void Init(int count)
{
this.count = count;
}
public void SetColor(int index, string name)
{
this.index = index;
this.name = name;
this.Invalidate();
}
//public void save()
//{
// Dictionary<int, string> dic = new Dictionary<int, string>();
// for (int i = 0; i < 6; i++)
// {
// if (i == index)
// {
// dic.Add(index, name);
// }
// }
//}
#endregion
}
}
public partial class UserControl1 : UserControl
{
private int count = 4;
public int Count
{
get { return count; }
set
{
count = value;
this.Invalidate();
}
}
private List<string> showText;
public List<string> ShowText
{
get { return showText; }
set { showText = value; }
}
private int index;
public int Index
{
get { return index; }
set { index = value; }
}
public UserControl1()
{
InitializeComponent();
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
Graphics g = e.Graphics;
Color[] colors = { Color.Red, Color.Black, Color.Blue, Color.SkyBlue };
#region 分割图形
Pen pn1 = new Pen(Color.Black, 2);
for (int i = 0; i < count; i++)
{
if (i < count)
{
Point pot1 = new Point((this.Width * i) / count, 0);
Point pot2 = new Point((this.Width * i) / count, this.Height);
g.DrawLine(pn1, pot1, pot2);
}
}
#endregion
#region 背景颜色和写字填充
for (int i = 0; i < Index; i++)
{
Font verdanaFont = new Font("Verdana", 10, FontStyle.Bold);
Rectangle rect = new Rectangle(this.Width * i / count + 1, 0, this.Width / count - 1, this.Height);
StringFormat strFormat1 = new StringFormat();
strFormat1.Alignment = StringAlignment.Center;
strFormat1.LineAlignment = StringAlignment.Center;
strFormat1.Trimming = StringTrimming.EllipsisCharacter;
g.FillRectangle(new SolidBrush(colors[i]), rect);
g.DrawString(ShowText[i], verdanaFont, new SolidBrush(Color.Black), rect, strFormat1);
}
#endregion
#region 边框绘制
Pen pn = new Pen(Color.Black);
g.DrawRectangle(pn, 0, 0, this.Width - 1, this.Height - 1);
#endregion
pn1.Dispose();
pn.Dispose();
}
#region 公有方法
public void Init(int count)
{
this.count = count;
}
public void SetColor(int index, List<string> name)
{
this.Index = index;
this.ShowText = name;
this.Invalidate();
}
#endregion
} 使用 List<string> names = new List<string>();
private void button1_Click(object sender, EventArgs e)
{
int number = Convert.ToInt32(textBox3.Text);
names.Add(textBox1.Text);
userControl11.SetColor(number, names);
}
我只是粗略的帮你写了一下,你还需要做一些优化和判断哦!!!!!切记!!! protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
Graphics g = e.Graphics;
Color[] colors = { Color.Red, Color.Black, Color.Blue, Color.SkyBlue };
#region 分割图形
Pen pn1 = new Pen(Color.Black, 2);
for (int i = 0; i < count; i++)
{
if (i < count)
{
Point pot1 = new Point((this.Width * i) / count, 0);
Point pot2 = new Point((this.Width * i) / count, this.Height);
g.DrawLine(pn1, pot1, pot2);
}
}
#endregion
#region 背景颜色和写字填充
for (int i = 0; i < index && index <= colors.Length; i++)
{
Font verdanaFont = new Font("Verdana", 10, FontStyle.Bold);
Rectangle rect = new Rectangle(this.Width * i / count + 1, 0, this.Width / count - 1, this.Height);
StringFormat strFormat1 = new StringFormat();
strFormat1.Alignment = StringAlignment.Center;
strFormat1.LineAlignment = StringAlignment.Center;
strFormat1.Trimming = StringTrimming.EllipsisCharacter;
g.FillRectangle(new SolidBrush(colors[i]), rect);
g.DrawString(name, verdanaFont, new SolidBrush(Color.Black), rect, strFormat1);
}
#endregion
#region 边框绘制
Pen pn = new Pen(Color.Black);
g.DrawRectangle(pn, 0, 0, this.Width - 1, this.Height - 1);
#endregion
pn1.Dispose();
pn.Dispose();
}