C#怎么让窗体中绘制的图像(如矩形)通过鼠标滚轮缩放?

 我来答
greystar_cn
2015-07-08 · 知道合伙人软件行家
greystar_cn
知道合伙人软件行家
采纳数:16407 获赞数:17260
本人主要从事.NET C#方向的技术开发工作,具有10多年的各类架构开发工作经验。

向TA提问 私信TA
展开全部
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));
}
}
}

代码测试通过
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式