C#怎么让窗体中绘制的图像(如矩形)通过鼠标滚轮缩放?
1个回答
2015-07-08 · 知道合伙人软件行家
关注
展开全部
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Image img = null;
public Point firstPoint = new Point(0, 0); //鼠标第一点
public Point secondPoint = new Point(0, 0); //鼠标第二点
public bool begin = false; //是否开始画矩形
Graphics g;
bool isDraw = false;
Rectangle rt;
private void Form1_Load(object sender, EventArgs e)
{
img = new Bitmap(Width, Height);
g = Graphics.FromImage(img);
this.MouseWheel += Form1_MouseWheel;
}
void Form1_MouseWheel(object sender, MouseEventArgs e)
{
int Mo = 0;
if (e.Delta > 0)
{
Mo = 8;
}
else
{
Mo = -8;
}
g.Clear(this.BackColor);
//开始缩放图片
rt.Inflate(Mo, Mo);
g.DrawRectangle(new Pen(Color.Red), rt);
this.Invalidate();
}
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
begin = true;
firstPoint = new Point(e.X, e.Y);
}
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
begin = false;
isDraw = true;
}
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if (isDraw)
return;
if (begin)
{
g.Clear(this.BackColor);
//获取新的右下角坐标
secondPoint = new Point(e.X, e.Y);
int minX = Math.Min(firstPoint.X, secondPoint.X);
int minY = Math.Min(firstPoint.Y, secondPoint.Y);
int maxX = Math.Max(firstPoint.X, secondPoint.X);
int maxY = Math.Max(firstPoint.Y, secondPoint.Y);
rt = new Rectangle(minX, minY, maxX - minX, maxY - minY);
//画框
g.DrawRectangle(new Pen(Color.Red), rt);
this.Invalidate();
}
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
e.Graphics.DrawImage(img, new Point(0, 0));
}
}
}
代码测试通过
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Image img = null;
public Point firstPoint = new Point(0, 0); //鼠标第一点
public Point secondPoint = new Point(0, 0); //鼠标第二点
public bool begin = false; //是否开始画矩形
Graphics g;
bool isDraw = false;
Rectangle rt;
private void Form1_Load(object sender, EventArgs e)
{
img = new Bitmap(Width, Height);
g = Graphics.FromImage(img);
this.MouseWheel += Form1_MouseWheel;
}
void Form1_MouseWheel(object sender, MouseEventArgs e)
{
int Mo = 0;
if (e.Delta > 0)
{
Mo = 8;
}
else
{
Mo = -8;
}
g.Clear(this.BackColor);
//开始缩放图片
rt.Inflate(Mo, Mo);
g.DrawRectangle(new Pen(Color.Red), rt);
this.Invalidate();
}
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
begin = true;
firstPoint = new Point(e.X, e.Y);
}
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
begin = false;
isDraw = true;
}
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if (isDraw)
return;
if (begin)
{
g.Clear(this.BackColor);
//获取新的右下角坐标
secondPoint = new Point(e.X, e.Y);
int minX = Math.Min(firstPoint.X, secondPoint.X);
int minY = Math.Min(firstPoint.Y, secondPoint.Y);
int maxX = Math.Max(firstPoint.X, secondPoint.X);
int maxY = Math.Max(firstPoint.Y, secondPoint.Y);
rt = new Rectangle(minX, minY, maxX - minX, maxY - minY);
//画框
g.DrawRectangle(new Pen(Color.Red), rt);
this.Invalidate();
}
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
e.Graphics.DrawImage(img, new Point(0, 0));
}
}
}
代码测试通过
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询