调整表单大小时保持图片框的相对位置

发布于 2024-10-04 02:41:55 字数 535 浏览 5 评论 0原文

在窗口窗体中,我放置了一个图片框作为背景图像。然后,我做到了,如果我调整窗口大小,图像会随着我调整大小而拉伸并变大,并且整个图片始终填充整个表单。为此,我使用了以下代码:

     image.Dock = DockStyle.Fill;
     image.Anchor = AnchorStyles.Top | AnchorStyles.Left;
     image.SizeMode = PictureBoxSizeMode.StretchImage;

我对此没有任何问题,只是因为图像填充了整个窗口。

然而,我现在想做的是放置另一个(较小的)图片框,它将位于窗体中心上方 20 px 处,并且当我调整窗口大小时也会适当拉伸,并且始终保持在稍高于窗体中心的位置窗体的中心,无论我如何调整它的大小。

问题是我无法设置 pictureBox 的 Location 属性,因为这样位置是绝对的,并且当我调整窗口大小时图像不会拉伸并保持在中心,而是根据 x 和 y 保持在原来的位置窗口的坐标。我想要的是图片根据我拉伸和移动窗口的方式来拉伸和移动,就像我设法处理背景中的图像一样。

In Window Forms, I've put a pictureBox as a background image. Then, I've made it so that if I resize the window, the image stretches and gets bigger as I resize it, and that the entire picture is always filling the entire form. For this, I've used the following code:

     image.Dock = DockStyle.Fill;
     image.Anchor = AnchorStyles.Top | AnchorStyles.Left;
     image.SizeMode = PictureBoxSizeMode.StretchImage;

I had no problems with that whatsoever simply because the image is filling in the entire window.

What I want to do now, however, is to put on another (smaller) pictureBox, that's going to be exactly 20 px above the center of the Form, and will also stretch appropriately as I resize the window, and will always stay slightly above the center of the Form, no matter how much I resize it.

The thing is that I cannot set the Location property of the pictureBox, because then the position is absolute and the image will not stretch and stay in the center as I resize the window, but will remain where it is, according to the x and y coordinates of the window. What I want is for the picture to stretch and move according to how I stretch and move the window, just like I've managed to do with the image I have in background.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不醒的梦 2024-10-11 02:41:55

将图像框放置在表单上所需的位置,然后将锚点属性设置为顶部、底部、右侧和左侧。通过将其固定到所有四个侧面,它会随着表单大小的调整而拉伸和收缩。

将它们与MinimumSize 和MaximumSize 属性结合使用可以防止图像变得太小或太大。

Position your imagebox on the form where you want it, then set the Anchor proprety to top, bottom, right, and left. By pinning it to all four sides, it will stretch and shrink as the form resizes.

Use these in conjunction with the MinimumSize and MaximumSize properties to keep the image from getting too small or too large.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文