I'm trying to resize the panels which I've created dynamically. While resizing, the panels' borders snap the grid. But it's width/height doesn't change as it should ideally. As long as the mouse pointer is on the anchor, the width/height keeps changing.Here is my code. How should I solve this issue?
Tile.cs (This is a User Control.)
namespace WindowsFormsApp6
{
public partial class Tile : UserControl
{
bool mouseClicked = false;
int pixels = 16;
public Tile()
{
InitializeComponent();
ShowHideAnchorPoints(true);
}
private void ShowHideAnchorPoints(bool flag)
{
rightAnchor.Visible =
leftAnchor.Visible =
topAnchor.Visible =
bottomAnchor.Visible = flag;
}
private void Anchor_MouseDown(object sender, MouseEventArgs e)
{
mouseClicked = true;
ShowHideAnchorPoints(false);
}
private void Anchor_MouseUp(object sender, MouseEventArgs e)
{
mouseClicked = false;
ShowHideAnchorPoints(true);
}
private void topAnchor_MouseMove(object sender, MouseEventArgs e)
{
if (mouseClicked)
{
Location = GridSnapY(Location, e.Y);
Height -= e.Y;
}
}
private void rightAnchor_MouseMove(object sender, MouseEventArgs e)
{
if (mouseClicked)
{
int width = (sender as Panel).Right + e.X;
Width = SnapWidth(width);
}
}
private void bottomAnchor_MouseMove(object sender, MouseEventArgs e)
{
if (mouseClicked)
{
int height = (sender as Panel).Bottom + e.Y;
Height = height;
Height = SnapHeight(height);
}
}
private void leftAnchor_MouseMove(object sender, MouseEventArgs e)
{
if (mouseClicked)
{
Location = GridSnapX(Location, e.X);
Width -= e.X;
}
}
private Point GridSnapX(Point location, int x)
{
Point p = new Point(location.X + x, location.Y);
if (p.X % pixels > pixels/2)
{
p.X += pixels - (p.X % pixels);
}
else
{
p.X -= (p.X % pixels);
}
return p;
}
private Point GridSnapY(Point location, int y)
{
Point p = new Point(location.X, location.Y + y);
if (p.Y % pixels > pixels/2)
{
p.Y += pixels - (p.Y % pixels);
}
else
{
p.Y -= (p.Y % pixels);
}
return p;
}
private int SnapHeight(int height)
{
if (height % pixels > pixels/2)
{
height += pixels - (height % pixels);
}
else
{
height -= (height % pixels);
}
return height;
}
private int SnapWidth(int width)
{
if (width % pixels > pixels/2)
{
width += pixels - (width % pixels);
}
else
{
width -= (width % pixels);
}
return width;
}
Form1.cs (Here canvas is created with grid points where user can draw tile and resize it)
namespace WindowsFormsApp6
{
public partial class Form1 : Form
{
Tile t;
Point locationXY;
Point locationX1Y1;
bool isMouseDown;
public Form1()
{
InitializeComponent();
canvas.BackgroundImage = CreatePixelGrid();
}
private Image CreatePixelGrid()
{
int width = canvas.Width;
int height = canvas.Height;
int pixels = 16;
Bitmap image = new Bitmap(width, height);
for (int y = 0; y < height; y += pixels)
{
for (int x = 0; x < width; x += pixels)
{
image.SetPixel(x, y, Color.Black);
}
}
return image;
}
private void DrawTile()
{
t = new Tile();
locationXY.X = Math.Min(locationXY.X, locationX1Y1.X);
locationXY.Y = Math.Min(locationXY.Y, locationX1Y1.Y);
t.Location = locationXY;
t.Height = Math.Abs(locationX1Y1.Y - locationXY.Y);
t.Width = Math.Abs(locationX1Y1.X - locationXY.X);
t.BorderStyle = BorderStyle.None;
canvas.Controls.Add(t);
t.BringToFront();
}
private void canvas_MouseDown(object sender, MouseEventArgs e)
{
isMouseDown = true;
locationXY = e.Location;
}
private void canvas_MouseMove(object sender, MouseEventArgs e)
{
if (isMouseDown)
{
locationX1Y1 = e.Location;
Refresh();
}
}
private void canvas_MouseUp(object sender, MouseEventArgs e)
{
isMouseDown = false;
DrawTile();
}
}
}
Here is how the form looks after drawing the tile. Currently I'm working on top, bottom, right and left anchors only.