private float scale = 1.0F;
private void Form1_Load(object sender, EventArgs e)
{
pictureBox1.MouseWheel += PictureBox1_MouseWheel;
pictureBox1.Paint += PictureBox1_Paint;
}
private void PictureBox1_MouseWheel(object sender, MouseEventArgs e)
{
// Scale in 0.1 increments down to a minimum of 0.1.
scale = Math.Max(scale + Math.Sign(e.Delta) * 0.1F, 0.1F);
pictureBox1.Refresh();
}
private void PictureBox1_Paint(object sender, PaintEventArgs e)
{
var matrix = new Matrix();
matrix.Scale(scale, scale, MatrixOrder.Append);
e.Graphics.Transform = matrix;
e.Graphics.DrawRectangle(Pens.Black, 10, 10, 50, 100);
}
兄弟,可以缩放而不移动它吗?这是您想要的东西:
在X和Y方向上使用相同的比例值,因此保留了宽高比。使用者将轮毂向上拨动每个刻度,比例会增加0.1。类似地,每降低一个陷波,它就会降低0.1,降低到最小值0.1。C#:private float scale = 1.0F; private void Form1_Load(object sender, EventArgs e) { pictureBox1.MouseWheel += PictureBox1_MouseWheel; pictureBox1.Paint += PictureBox1_Paint; } private void PictureBox1_MouseWheel(object sender, MouseEventArgs e) { // Scale in 0.1 increments down to a minimum of 0.1. scale = Math.Max(scale + Math.Sign(e.Delta) * 0.1F, 0.1F); pictureBox1.Refresh(); } private void PictureBox1_Paint(object sender, PaintEventArgs e) { var matrix = new Matrix(); matrix.Scale(scale, scale, MatrixOrder.Append); e.Graphics.Transform = matrix; e.Graphics.DrawRectangle(Pens.Black, 10, 10, 50, 100); }
private float scale = 1.0F;
private void Form1_Load(object sender, EventArgs e)
{
pictureBox1.MouseWheel += PictureBox1_MouseWheel;
pictureBox1.Paint += PictureBox1_Paint;
}
private void PictureBox1_MouseWheel(object sender, MouseEventArgs e)
{
// Scale in 0.1 increments down to a minimum of 0.1.
scale = Math.Max(scale + Math.Sign(e.Delta) * 0.1F, 0.1F);
pictureBox1.Refresh();
}
private void PictureBox1_Paint(object sender, PaintEventArgs e)
{
var matrix = new Matrix();
matrix.Scale(scale, scale, MatrixOrder.Append);
matrix.Translate(10.0F, 10.0F, MatrixOrder.Append);
e.Graphics.Transform = matrix;
e.Graphics.DrawRectangle(Pens.Black, 0, 0, 50, 100);
}
private float scale = 1.0F;
private void Form1_Load(object sender, EventArgs e)
{
pictureBox1.MouseWheel += PictureBox1_MouseWheel;
pictureBox1.Paint += PictureBox1_Paint;
}
private void PictureBox1_MouseWheel(object sender, MouseEventArgs e)
{
// Scale in 0.1 increments down to a minimum of 0.1.
scale = Math.Max(scale + Math.Sign(e.Delta) * 0.1F, 0.1F);
pictureBox1.Refresh();
}
private void PictureBox1_Paint(object sender, PaintEventArgs e)
{
var matrix = new Matrix();
matrix.Scale(scale, scale, MatrixOrder.Append);
matrix.Translate(10.0F, 10.0F, MatrixOrder.Append);
e.Graphics.Transform = matrix;
e.Graphics.DrawRectangle(Pens.Black, 0, 0, 50, 100);
matrix.Translate(200.0F, 0.0F, MatrixOrder.Append);
e.Graphics.Transform = matrix;
e.Graphics.DrawRectangle(Pens.Black, 0, 0, 50, 100);
}
MouseWheel
event handler, then create a new Matrix
with the appropriate translation and scale before drawing each shape.If you wanted to be able to scale multiple shapes separately then you'd have to store separate scale values for each shape and only modify the right one in theMouseWheel
event handler, then create a newMatrix
with the appropriate translation and scale before drawing each shape.
GraphicsPath path=new GraphicsPath();
private float scale=1.0F;
private bool ActiveWheel=false;
public Form1()
{
path.AddRectangle(new Rectangle(0,0,50,100));
}
private void PictureBox1_Paint(object sender,PaintEventArgs e)
{
if(ActiveWheel)
{
ActiveWheel=false;
ScaleRectangle(e);
pictureBox1.Invalidate();
}
else
{
e.Graphics.DrawPath(Pens.Red,path);
}
}
private void ScaleRectangle(PaintEventArgs e)
{
var matrix=new Matrix();
matrix.Scale(scale,scale,MatrixOrder.Append);
path.Transform(matrix);
e.Graphics.DrawPath(Pens.Blue,path);
}
Invalidate
in the Paint
event handler. That's the method you call if you want to cause that event to be raised. If you cause the event to be raised each time it's raised, it will never stop being raised. Look at the code I provided. Do I ever do that? I call Refresh
in the MouseWheel
event handler, so that's where you would call Invalidate
. Refresh
calls Invalidate
and then also calls Update
, which causes the Paint
event to be raised immediately rather than waiting until other queued events have been processed.我将代码从pictureBox1.Invalidate()更改为pictureBox1.Refresh(),但问题仍然存在。Don't callInvalidate
in thePaint
event handler. That's the method you call if you want to cause that event to be raised. If you cause the event to be raised each time it's raised, it will never stop being raised. Look at the code I provided. Do I ever do that? I callRefresh
in theMouseWheel
event handler, so that's where you would callInvalidate
.Refresh
callsInvalidate
and then also callsUpdate
, which causes thePaint
event to be raised immediately rather than waiting until other queued events have been processed.
GraphicsPath path=new GraphicsPath();
private float scale=1.0F;
private bool ActiveWheel=false;
public Form1()
{
path.AddRectangle(new Rectangle(0,0,50,100));
}
private void PictureBox1_Paint(object sender,PaintEventArgs e)
{
if(ActiveWheel)
{
ActiveWheel=false;
ScaleRectangle(e);
//I removed the pictureBox1.Invalidate() from here also.
}
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);
}