我正在尝试创建一个用于绘制不同对象的画布。我已经使用graphics.scaleTransform和graphics.translateTransform创建了缩放和平移功能,但我不想将画布背景(网格)全部填满整个窗口,但它没有,使用以下代码:
编辑:我尝试使用转换后的图形对象中的坐标,但似乎不接受负数??
编辑:这张照片解释了我的问题:
public partial class Form1 : Form
{
PointF mouseDown;
float newX;
float newY;
float zoomFactor = 1F;
Region _rgn;
Graphics _dc;
PointF zoomPoint = new PointF(150, 150);
public Form1()
{
InitializeComponent();
mouseDown = new PointF(0F, 0F);
this.panel1.Paint += new PaintEventHandler(panel1_Paint);
this.panel1.MouseDown += new System.Windows.Forms.MouseEventHandler(panel1_MouseDown);
this.panel1.MouseMove += new System.Windows.Forms.MouseEventHandler(panel1_MouseMove);
}
private void panel1_Paint(object sender, PaintEventArgs e)
{
base.OnPaint(e);
//Graphics bg =
Graphics dc = e.Graphics;
_dc = dc;
dc.SmoothingMode = SmoothingMode.AntiAlias;
Color gridColor = Color.FromArgb(230, 230, 230);
Pen gridPen = new Pen(gridColor, 1);
for (float i = 0; i < this.Height * (zoomFactor); i = i + 30*zoomFactor)
{
dc.DrawLine(gridPen, 0, i, this.Width * (zoomFactor), i);
}
for (float i = 0; i < this.Width * (zoomFactor); i = i + 30*zoomFactor)
{
dc.DrawLine(gridPen, i, 0, i, this.Height * (zoomFactor));
}
dc.TranslateTransform(newX, newY);
dc.ScaleTransform(zoomFactor, zoomFactor, MatrixOrder.Prepend);
float XPosition = 10;
float YPosition = 10;
float CornerRadius = 5;
float Width = 50;
float Height = 50;
Color BoxColor = Color.FromArgb(0, 0, 0);
Pen BoxPen = new Pen(BoxColor, 2);
GraphicsPath Path = new GraphicsPath();
Path.AddLine(XPosition + CornerRadius, YPosition, XPosition + Width - (CornerRadius * 2), YPosition);
Path.AddArc(XPosition + Width - (CornerRadius * 2), YPosition, CornerRadius * 2, CornerRadius * 2, 270, 90);
Path.AddLine(XPosition + Width, YPosition + CornerRadius, XPosition + Width, YPosition + Height - (CornerRadius * 2));
Path.AddArc(XPosition + Width - (CornerRadius * 2), YPosition + Height - (CornerRadius * 2), CornerRadius * 2, CornerRadius * 2, 0, 90);
Path.AddLine(XPosition + Width - (CornerRadius * 2), YPosition + Height, XPosition + CornerRadius, YPosition + Height);
Path.AddArc(XPosition, YPosition + Height - (CornerRadius * 2), CornerRadius * 2, CornerRadius * 2, 90, 90);
Path.AddLine(XPosition, YPosition + Height - (CornerRadius * 2), XPosition, YPosition + CornerRadius);
Path.AddArc(XPosition, YPosition, CornerRadius * 2, CornerRadius * 2, 180, 90);
Path.CloseFigure();
LinearGradientBrush lgb = new LinearGradientBrush(new PointF(XPosition+(Width/2),YPosition), new PointF(XPosition+(Width/2),YPosition + Height), Color.RosyBrown, Color.Red);
dc.FillPath(lgb, Path);
dc.DrawPath(BoxPen, Path);
Matrix transformMatrix = new Matrix();
transformMatrix.Translate(newX, newY);
transformMatrix.Scale(zoomFactor, zoomFactor);
_rgn = new Region(Path);
_rgn.Transform(transformMatrix);
}
private void panel1_MouseDown(object sender, EventArgs e)
{
MouseEventArgs mouse = e as MouseEventArgs;
if (mouse.Button == MouseButtons.Right)
{
mouseDown = mouse.Location;
mouseDown.X = mouseDown.X - newX;
mouseDown.Y = mouseDown.Y - newY;
}
else if (mouse.Button == MouseButtons.Left)
{
if (_rgn.IsVisible(mouse.Location, _dc))
{
MessageBox.Show("tada");
}
}
}
private void panel1_MouseMove(object sender, EventArgs e)
{
MouseEventArgs mouse = e as MouseEventArgs;
if (mouse.Button == MouseButtons.Right)
{
PointF mousePosNow = mouse.Location;
float deltaX = mousePosNow.X - mouseDown.X;
float deltaY = mousePosNow.Y - mouseDown.Y;
newX = deltaX;
newY = deltaY;
panel1.Invalidate();
}
}
protected override void OnMouseWheel(MouseEventArgs e)
{
MouseEventArgs mouse = e as MouseEventArgs;
PointF mP = mouse.Location;
if (e.Delta > 0)
{
if (zoomFactor >= 1 && zoomFactor <= 10)
{
zoomFactor += 1F;
newX = newX - ((mP.X - newX) / (zoomFactor - 1));
newY = newY - ((mP.Y - newY) / (zoomFactor - 1));
}
else if (zoomFactor == 0.5)
{
zoomFactor = zoomFactor * 2;
newX = 2 * newX - mP.X ;
newY = 2 * newY - mP.Y ;
}
else if (zoomFactor < 0.5)
{
zoomFactor = zoomFactor * 2;
newX = 2 * newX - mP.X;
newY = 2 * newY - mP.Y;
}
}
else if (e.Delta < 0)
{
if (zoomFactor >2)
{
zoomFactor -= 1F;
newX = newX + (((mP.X - newX)) / (zoomFactor+1 ));
newY = newY + (((mP.Y - newY)) / (zoomFactor+1));
}
else if (zoomFactor == 2) {
zoomFactor -= 1F;
newX = newX + ((mP.X - newX)/2);
newY = newY + ((mP.Y - newY)/2);
}else if(zoomFactor <= 1 && zoomFactor > 0.2)
{
zoomFactor = zoomFactor / 2;
newX = newX + ((mP.X - newX) / 2);
newY = newY + ((mP.Y - newY) / 2);
}
}
panel1.Invalidate();
}
}
答案 0 :(得分:2)
在变换坐标系之前绘制GRID。
如果你不能,请使用GetTransform()和ResetTransform(),然后绘制网格,然后返回SetTransform(第一步得到的那个)。
恕我直言:如果你“手动”缩放图像而不是使用坐标转换会更好。你可以通过这种方式获得更大的控制力,并且不会出现任何问题。
编辑:
另外:你的网格绘图程序中有一个错误:
for (float i = 0; i < this.Height * (zoomFactor); i = i + 30*zoomFactor)
尝试替换
for (float i = 0; i < this.Height; i = i + 30*zoomFactor)