在GDI中用C#绘图的时候怎么通过comboBox控件(如图)改变画笔的颜色
1个回答
展开全部
一个简单例子
using System;
using System.Windows.Forms;
using System.Drawing;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
// 添加颜色
comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;
comboBox1.Items.Add(new ColorName() { Color = Color.Red, Name = "红色" });
comboBox1.Items.Add(new ColorName() { Color = Color.Green, Name = "绿色" });
comboBox1.Items.Add(new ColorName() { Color = Color.Blue, Name = "蓝色" });
comboBox1.SelectedIndex = 0;
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
// 用指定颜色做直线
ColorName colorName =(ColorName)comboBox1.SelectedItem;
Brush brush = new SolidBrush(colorName.Color);
Pen pen = new Pen(brush, 3);
g.DrawLine(pen, new Point(10, 60), new Point(200, 60));
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
this.Refresh();
}
}
//颜色--名称类,用于ComboBox表示颜色
class ColorName
{
public Color Color;
public string Name;
public override string ToString()
{
return Name;
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询