【发布时间】:2017-06-21 04:50:59
【问题描述】:
不幸的是,调整大小时图片框跳到了下角。 我读到我需要用 Docker 和 Anchor 设置一些东西,但我不知道怎么做。
【问题讨论】:
标签: c# .net windows-forms-designer
不幸的是,调整大小时图片框跳到了下角。 我读到我需要用 Docker 和 Anchor 设置一些东西,但我不知道怎么做。
【问题讨论】:
标签: c# .net windows-forms-designer
您的Location (x, y) 指的是控件的左上角。调整 PictureBox 的大小时,只有右下角会移动,除非您也更改了位置。
您要更改代码的大小吗?如果是这样,您可以使用辅助方法来执行此操作。图片变大需要左移宽度变化的一半,变小需要右移宽度变化的一半(同样的逻辑适用于高度):
private void ChangePictureBoxSize(int newWidth, int newHeight)
{
// these will be negative if picturebox is getting bigger
int changeInWidth = pictureBox1.Width - newWidth;
int changeInHeight = pictureBox1.Height - newHeight;
// will shift left and up if picturebox is getting bigger
int newX = pictureBox1.Location.X + (changeInWidth / 2);
int newY = pictureBox1.Location.Y + (changeInHeight / 2);
pictureBox1.Location = new Point(newX, newY);
pictureBox1.Size = new Size(newWidth, newHeight);
}
【讨论】: