我正在尝试调整我动态创建的面板的大小。调整大小时,面板的边框会捕捉网格。但它的宽度/高度并没有像理想情况下那样改变。只要鼠标指针在锚点上,宽度/高度就会不断变化。这是我的代码。我应该如何解决这个问题?
Tile.cs(这是一个用户控件。)
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(这里的画布是使用网格点创建的,用户可以在其中绘制平铺并调整其大小)
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();
}
}
}
这是绘制瓷砖后表单的外观。目前我只在顶部、底部、右侧和左侧锚点上工作。