c#中如何实现在picturebox控件中画一个矩形并填充颜色点击时判断是不是该矩形框并改变填充颜色?
1个回答
展开全部
//比如在 picturebox 有一个矩形 rectA(X=0; Y=0; Width=100; Height=200;)
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
//绘制矩形
e.Graphics.DrawRectangle(new Pen(Brushes.Blue, 2f), rect);
}
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
//判断按下的是鼠标右键还是左键,这里随你选择
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
//获取鼠标点下的位置
Point p = new Point(e.X, e.Y);
//判断鼠标点下的位置是否包含在矩形里面,以此判断是否选中某个矩形
if (rect.Contains(p))
{
Graphics g = pictureBox1.CreateGraphics();
g.FillRectangle(Brushes.Red, rect);//填充颜色
g.Dispose();
}
}
}
Rectangle rect = new Rectangle(0, 0, 100, 200);//矩形
private void Form1_Load(object sender, EventArgs e)
{
}
}
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
//绘制矩形
e.Graphics.DrawRectangle(new Pen(Brushes.Blue, 2f), rect);
}
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
//判断按下的是鼠标右键还是左键,这里随你选择
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
//获取鼠标点下的位置
Point p = new Point(e.X, e.Y);
//判断鼠标点下的位置是否包含在矩形里面,以此判断是否选中某个矩形
if (rect.Contains(p))
{
Graphics g = pictureBox1.CreateGraphics();
g.FillRectangle(Brushes.Red, rect);//填充颜色
g.Dispose();
}
}
}
Rectangle rect = new Rectangle(0, 0, 100, 200);//矩形
private void Form1_Load(object sender, EventArgs e)
{
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询