How do i draw anywhere in a graphics object after it is transformed
I'm trying to create a canvas for drawing different objects. I've created zoom and pan functions using graphics.scaleTransform and graphics.translateTransform, but i wan't the canvas background (a grid) to allways fill out the entire window, however it does not, using the following code:
EDIT: I've tried using the coordinates in the transformed graphics object, but it seems it won't accept negative numbers?!?
EDIT: This picture explains my problem:
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;
开发者_如何学C 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();
}
}
Draw your GRID BEFORE transforming the coordinate system.
If you can't, use GetTransform() and ResetTransform(), then draw grid, then SetTransform back (the one got in the first step).
IMHO: It would be much better if you scale you image 'manually' instead of using coordinate transformation. You'll have much greater control this way, and won't hit the wall with anything.
EDIT:
Also: you have a bug in your grid drawing routine:
for (float i = 0; i < this.Height * (zoomFactor); i = i + 30*zoomFactor)
try replacing with
for (float i = 0; i < this.Height; i = i + 30*zoomFactor)
精彩评论