C#如何用鼠标画矩形
请问C#如何用鼠标在一张图片上画出多个矩形,望高手指点,麻烦给一段代码示例,谢谢谢谢二楼的高手,我再补充一下问题,就是画出的矩形每一个都必须显示在图片上,现在的代码是画出...
请问C#如何用鼠标在一张图片上画出多个矩形,望高手指点,麻烦给一段代码示例,谢谢
谢谢二楼的高手,我再补充一下问题,就是画出的矩形每一个都必须显示在图片上,现在的代码是画出一个上一个就消失了?怎么能让他们全部显示在图片上?麻烦再帮我实现一下,谢谢 展开
谢谢二楼的高手,我再补充一下问题,就是画出的矩形每一个都必须显示在图片上,现在的代码是画出一个上一个就消失了?怎么能让他们全部显示在图片上?麻烦再帮我实现一下,谢谢 展开
展开全部
给你写了个比较全面的
1.有处理重绘区域的,这样闪烁的不是很明显。在你画矩形的过程中,你会发现,你的鼠标经过的距离越大,闪烁越明显。楼上的兄台每次都是更新整个界面,闪烁比较严重。
2.在画矩形的方向上,你可以任意方向绘画,因为我上次的和楼上那位兄台的都只能从左上往右下拉动鼠标绘制矩形。
3.请耐性看代码
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Collections.Generic;
namespace ReverseArray
{
public partial class Form1 : Form
{
private bool mouseStatus = false;//鼠标状态,false为松开
private List<Rectangle> rectList = new List<Rectangle>();//存储所有画过的矩形
private Point startPoint;//鼠标按下的点
private Point endPoint;//
private Rectangle currRect;//当前正在绘制的举行
private int minStartX, minStartY, maxEndX, maxEndY;//最大重绘矩形的上下左右的坐标,这样重绘的效率更高。
public Form1()
{
InitializeComponent();
}
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
mouseStatus = true;
startPoint.X = e.X;
startPoint.Y = e.Y;
//重新一个矩形,重置最大重绘矩形的上下左右的坐标
minStartX = e.X;
minStartY = e.Y;
maxEndX = e.X;
maxEndY = e.Y;
}
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if (mouseStatus)
{
endPoint.X = e.X; endPoint.Y = e.Y;
//这一段是获取要绘制矩形的上下左右的坐标,如果不这样处理的话,只有从左上开始往右下角才能画出矩形。
//这样处理的话,可以任意方向,当然中途可以更换方向。
int realStartX = Math.Min(startPoint.X, endPoint.X);
int realStartY = Math.Min(startPoint.Y, endPoint.Y);
int realEndX = Math.Max(startPoint.X, endPoint.X);
int realEndY = Math.Max(startPoint.Y, endPoint.Y);
minStartX = Math.Min(minStartX, realStartX);
minStartY = Math.Min(minStartY, realStartY);
maxEndX = Math.Max(maxEndX, realEndX);
maxEndY = Math.Max(maxEndY, realEndY);
currRect = new Rectangle(realStartX, realStartY, realEndX - realStartX, realEndY - realStartY);
//一下是为了获取最大重绘矩形。
Rectangle refeshRect = new Rectangle(minStartX, minStartY, maxEndX - minStartX, maxEndY - minStartY);
refeshRect.Inflate(1, 1);//重绘矩形的大小扩展1个单位
this.Invalidate(refeshRect);//失效一个区域,并使其重绘。
}
}
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
mouseStatus = false;
endPoint.X = e.X; endPoint.Y = e.Y;
int realStartX = Math.Min(startPoint.X, endPoint.X);
int realStartY = Math.Min(startPoint.Y, endPoint.Y);
int realEndX = Math.Max(startPoint.X, endPoint.X);
int realEndY = Math.Max(startPoint.Y, endPoint.Y);
currRect = new Rectangle(realStartX, realStartY, realEndX - realStartX, realEndY - realStartY);
rectList.Add(currRect);//当前矩形算是被认可了,所以存起来
this.Invalidate();//重绘整个界面
}
private void Form1_Paint(object sender, PaintEventArgs e)//处理重绘情况
{
Graphics g = e.Graphics;
g.Clear(this.BackColor);
g.DrawRectangle(new Pen(Color.Red, 1), currRect);
foreach (Rectangle rect in rectList)
g.DrawRectangle(new Pen(Color.Red, 1), rect);
}
}
}
1.有处理重绘区域的,这样闪烁的不是很明显。在你画矩形的过程中,你会发现,你的鼠标经过的距离越大,闪烁越明显。楼上的兄台每次都是更新整个界面,闪烁比较严重。
2.在画矩形的方向上,你可以任意方向绘画,因为我上次的和楼上那位兄台的都只能从左上往右下拉动鼠标绘制矩形。
3.请耐性看代码
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Collections.Generic;
namespace ReverseArray
{
public partial class Form1 : Form
{
private bool mouseStatus = false;//鼠标状态,false为松开
private List<Rectangle> rectList = new List<Rectangle>();//存储所有画过的矩形
private Point startPoint;//鼠标按下的点
private Point endPoint;//
private Rectangle currRect;//当前正在绘制的举行
private int minStartX, minStartY, maxEndX, maxEndY;//最大重绘矩形的上下左右的坐标,这样重绘的效率更高。
public Form1()
{
InitializeComponent();
}
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
mouseStatus = true;
startPoint.X = e.X;
startPoint.Y = e.Y;
//重新一个矩形,重置最大重绘矩形的上下左右的坐标
minStartX = e.X;
minStartY = e.Y;
maxEndX = e.X;
maxEndY = e.Y;
}
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if (mouseStatus)
{
endPoint.X = e.X; endPoint.Y = e.Y;
//这一段是获取要绘制矩形的上下左右的坐标,如果不这样处理的话,只有从左上开始往右下角才能画出矩形。
//这样处理的话,可以任意方向,当然中途可以更换方向。
int realStartX = Math.Min(startPoint.X, endPoint.X);
int realStartY = Math.Min(startPoint.Y, endPoint.Y);
int realEndX = Math.Max(startPoint.X, endPoint.X);
int realEndY = Math.Max(startPoint.Y, endPoint.Y);
minStartX = Math.Min(minStartX, realStartX);
minStartY = Math.Min(minStartY, realStartY);
maxEndX = Math.Max(maxEndX, realEndX);
maxEndY = Math.Max(maxEndY, realEndY);
currRect = new Rectangle(realStartX, realStartY, realEndX - realStartX, realEndY - realStartY);
//一下是为了获取最大重绘矩形。
Rectangle refeshRect = new Rectangle(minStartX, minStartY, maxEndX - minStartX, maxEndY - minStartY);
refeshRect.Inflate(1, 1);//重绘矩形的大小扩展1个单位
this.Invalidate(refeshRect);//失效一个区域,并使其重绘。
}
}
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
mouseStatus = false;
endPoint.X = e.X; endPoint.Y = e.Y;
int realStartX = Math.Min(startPoint.X, endPoint.X);
int realStartY = Math.Min(startPoint.Y, endPoint.Y);
int realEndX = Math.Max(startPoint.X, endPoint.X);
int realEndY = Math.Max(startPoint.Y, endPoint.Y);
currRect = new Rectangle(realStartX, realStartY, realEndX - realStartX, realEndY - realStartY);
rectList.Add(currRect);//当前矩形算是被认可了,所以存起来
this.Invalidate();//重绘整个界面
}
private void Form1_Paint(object sender, PaintEventArgs e)//处理重绘情况
{
Graphics g = e.Graphics;
g.Clear(this.BackColor);
g.DrawRectangle(new Pen(Color.Red, 1), currRect);
foreach (Rectangle rect in rectList)
g.DrawRectangle(new Pen(Color.Red, 1), rect);
}
}
}
展开全部
//鼠标是否按下
private bool MouseDownFlg = false;
//记录矩形
private List<Rectangle> lst = new List<Rectangle>();
//当前绘制矩形
private Rectangle rect = Rectangle.Empty;
private void Form1_Paint(object sender, PaintEventArgs e)
{
foreach (Rectangle itm in lst)
{
e.Graphics.DrawRectangle(Pens.Black, itm);
}
if (rect != Rectangle.Empty)
{
e.Graphics.DrawRectangle(Pens.Red, rect);
}
}
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
MouseDownFlg = true;
rect = new Rectangle(e.X, e.Y, 0, 0);
}
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
if (MouseDownFlg == true)
{
rect.Width = e.X - rect.X;
rect.Height = e.Y - rect.Y;
lst.Add(rect);
this.Refresh();
MouseDownFlg = false;
}
}
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if (MouseDownFlg == true)
{
rect.Width = e.X - rect.X;
rect.Height = e.Y - rect.Y;
this.Refresh();
}
}
private bool MouseDownFlg = false;
//记录矩形
private List<Rectangle> lst = new List<Rectangle>();
//当前绘制矩形
private Rectangle rect = Rectangle.Empty;
private void Form1_Paint(object sender, PaintEventArgs e)
{
foreach (Rectangle itm in lst)
{
e.Graphics.DrawRectangle(Pens.Black, itm);
}
if (rect != Rectangle.Empty)
{
e.Graphics.DrawRectangle(Pens.Red, rect);
}
}
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
MouseDownFlg = true;
rect = new Rectangle(e.X, e.Y, 0, 0);
}
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
if (MouseDownFlg == true)
{
rect.Width = e.X - rect.X;
rect.Height = e.Y - rect.Y;
lst.Add(rect);
this.Refresh();
MouseDownFlg = false;
}
}
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if (MouseDownFlg == true)
{
rect.Width = e.X - rect.X;
rect.Height = e.Y - rect.Y;
this.Refresh();
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询