1.当用户在图片框上向上鼠标按下鼠标时,我正在尝试缩放矩形。 2.完成5个鼠标滚轮后,如果你将鼠标滚轮向下移动,矩形仍然保持向上扩展(扩展)。 3.任何解决方案吗?
GraphicsPath path=new GraphicsPath();
private float scale=1.0F;
private bool ActiveWheel=false;
public Form1()
{
path.AddRectangle(new Rectangle(10,10,50,100));
}
private void PictureBox1_Paint(object sender,PaintEventArgs e)
{
if(ActiveWheel)
{
ActiveWheel=false;
ScaleRectangle(e);
}
else
{
e.Graphics.DrawPath(Pens.Red,path);
}
}
private void PictureBox1_MouseWheel(object sender,MouseEventArgs e)
{
ActiveWheel=true;
scale=Math.Max(scale+Math.Sign(e.Delta)*0.1F,0.1F);
pictureBox1.Refresh();
}
}
private void ScaleRectangle(PaintEventArgs e)
{
var matrix=new Matrix();
matrix.Scale(scale,scale,MatrixOrder.Append);
path.Transform(matrix);
e.Graphics.DrawPath(Pens.Blue,path);
}
任何解决方案或想法如何在鼠标滚轮和鼠标滚轮之间没有延迟的情况下突然缩小或放大形状(参见2.如果想要实际看到o / p)。
只需在MouseWheel()事件中调整缩放值,然后在Paint()事件中调整ScaleTransform()GRAPHICS曲面(而不是Path本身)并绘制:
public partial class Form1 : Form
{
private GraphicsPath path = new GraphicsPath();
private float scale = 1.0F;
public Form1()
{
InitializeComponent();
path.AddRectangle(new Rectangle(10, 10, 50, 100));
pictureBox1.MouseWheel += PictureBox1_MouseWheel;
pictureBox1.Paint += pictureBox1_Paint;
}
private void PictureBox1_MouseWheel(object sender, MouseEventArgs e)
{
scale = Math.Max(scale + Math.Sign(e.Delta) * 0.1F, 0.1F);
pictureBox1.Invalidate();
}
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
e.Graphics.ScaleTransform(scale, scale);
e.Graphics.DrawPath(Pens.Red, path);
}
}
---编辑---
它完全缩放,你能告诉我如何只缩放图形路径对象和顶部,左边必须固定,意味着没有缩放顶部,左边的点?
在这种情况下,转换为矩形的左上角,缩放,然后转换回原点。现在绘制您的UNCHANGED矩形:
private void PictureBox1_Paint(object sender, PaintEventArgs e)
{
e.Graphics.TranslateTransform(10, 10);
e.Graphics.ScaleTransform(scale, scale);
e.Graphics.TranslateTransform(-10, -10);
e.Graphics.DrawPath(Pens.Red, path);
}
如果你有其他元素在不同的位置和/或比例绘制,那么你可以在之前和之后重置图形表面(每个“元素”可以做相同类型的事情来定位自己并缩放自己):
private void PictureBox1_Paint(object sender, PaintEventArgs e)
{
// possibly other drawing operations
e.Graphics.ResetTransform();
e.Graphics.TranslateTransform(10, 10);
e.Graphics.ScaleTransform(scale, scale);
e.Graphics.TranslateTransform(-10, -10);
e.Graphics.DrawPath(Pens.Red, path);
e.Graphics.ResetTransform();
// possibly other drawing operations
}
这种方法很好,因为它保留了关于矩形的原始信息;变化只是视觉上的。