调整面板大小时,如何在面板上保留相对于面板的图片框(在面板中找到)? [英] How to keep a picture box (found within a panel) relative to the panel when the panel is resized?

查看:86
本文介绍了调整面板大小时,如何在面板上保留相对于面板的图片框(在面板中找到)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Windows窗体应用程序,中间有一个矩形面板。此面板具有背景图像,这对于图片框(带有背景图像)很重要,然后将其小心地放置在面板的顶部。我已将面板设置为Anchor Top,Bottom,Left和Right以及Zoom的背景图像布局。这允许在调整窗口大小时调整面板图像的大小。太棒了!



我遇到的问题是我需要将相框保持在相对于其后面板背景图像的相同位置。目前,当调整窗体窗口的大小时,顶部的图像相对于其后面的面板移出位置,正在正确调整大小。他们正在移动,好像他们有一个右,底部锚,但他们的锚被设置为无。



换句话说,如果我将图像定位为30%在整个面板的方式上,即使面板调整大小,我也需要在整个面板上达到30%。



下图显示了我的目标:



http://i261.photobucket。 com / albums / ii61 / scottj15_2008 / PanelResize_zpsfea30619.png



我尝试了几个Anchor和Dock设置,但没有太多运气。请问有谁可以点亮这个吗?



非常感谢



实际解决方案代码(感谢BillWoodruff):



此解决方案基于存储每个Picturebox相对于面板的比率,然后在Form_SizeChanged方法中使用该比率重新调整大小&重新设置每个PictureBox。



重要提示:此解决方案基于面板已经通过使用所有4个锚点自动调整自身大小。



首先我们需要字典来保存每个图片框的位置和大小比例:

I have a windows forms application with a rectangular panel in the centre. This panel has a background image which is important for Picture boxes (with background images) which are then carefully positioned over the top of the panel. I have set the panel to Anchor Top, Bottom, Left and Right with Background image layout of Zoom. This allows the panel image to be resized when the window is resized. Great!

The problem I'm having is I need the picture boxes to stay in the same position relative to the panel background image behind it. Currently when the forms window is resized the images on top move out of position relative to the panel behind it which is resizing correctly. They are moving as if they have a Right, Bottom anchor but their anchor is set to None.

In other words, if I've positioned an image 30% of the way across the panel, I need it to be 30% across the panel even when the panel has resized.

The image below shows exactly what I am after:

http://i261.photobucket.com/albums/ii61/scottj15_2008/PanelResize_zpsfea30619.png

I have tried several Anchor and Dock Settings but not having much luck. Can anyone shine some light on this please?

Many Thanks

Actual Code for Solution (Thanks to BillWoodruff):

This solution is based upon storing the ratio for each Picturebox relative to the panel and then using that ratio within the Form_SizeChanged Method to Re-Size & Re-Poistion each PictureBox.

IMPORTANT: This Solution is based upon the panel already automatically resizing itself through use of all 4 Anchors.

First we need Dictionaries to hold position and size ratios for each picture box:

//Location Ratios
private Dictionary<Control, double> XRatio = new Dictionary<Control, double>();
private Dictionary<Control, double> YRatio = new Dictionary<Control, double>();
//Size Ratios
private Dictionary<Control, double> XSizeRatio = new Dictionary<Control, double>();
private Dictionary<Control, double> YSizeRatio = new Dictionary<Control, double>();





In我们需要存储每个图片框的比率的加载事件:



In the Load event we need to store the ratio for each picturebox:

foreach (Control thisControl in panel.Controls)
{
    if (thisControl is PictureBox)
    {
        //Location Ratios
        XRatio.Add(theControl, Convert.ToDouble(thisControl.Left) / panel.Width);
        YRatio.Add(theControl, Convert.ToDouble(thisControl.Top) / panel.Height);
        //Size Ratios
        XSizeRatio.Add(theControl, Convert.ToDouble(thisControl.Width) / panel.Width);
        YSizeRatio.Add(theControl, Convert.ToDouble(thisControl.Height) / panel.Height);
    }
}





我们需要使用此比率来重新调整/重新定位每个PictureBox每当窗体窗口调整大小时:



We then need to use this ratio to Re-Size/Re-Position each PictureBox whenever the form window is resized:

private void Form_SizeChanged(object sender, EventArgs e)
{
    foreach (Control thisControl in panel.Controls)
    {
        if (thisControl is PictureBox)
        {
            //*********************Re-Position the PictureBox********************
            double localXRatio = 0;
            double localYRatio = 0;
            //Grab the Location Ratios for this particular PictureBox
            localXRatio = XRatio.FirstOrDefault(ratio => ratio.Key == thisControl).Value;
            localYRatio = YRatio.FirstOrDefault(ratio => ratio.Key == thisControl).Value;
            //Calculate the new Location by using the ratios
            newX = Convert.ToInt32(panel.Width * localXRatio);
            newY = Convert.ToInt32(panel.Height * localYRatio);
            //Set the new Location
            thisControl.Location = new Point(newX, newY);
            //*********************Re-Size the PictureBox********************
            double localXSizeRatio = 0;
            double localYSizeRatio = 0;
            //Grab the Size Ratios for this particular PictureBox
            localXSizeRatio = XSizeRatio.FirstOrDefault(ratio => ratio.Key == thisControl).Value;
            localYSizeRatio = YSizeRatio.FirstOrDefault(ratio => ratio.Key == thisControl).Value;
            //Calculate the new sizes by using the set ratio
            newWidth = Convert.ToInt32(panel.Width * localXSizeRatio);
            newHeight = Convert.ToInt32(panel.Height * localYSizeRatio);
            //Set the new Size
            thisControl.Width = newWidth;
            thisControl.Height = newHeight;
        }
    }
}

推荐答案

以下是如何实现相对定位和重新定位的概述控件窗体/容器控件更改大小时控件的大小[ ^ ]。



我还没有获得许可,但是,要在CP上发布整个代码,但是,我希望。



我建议你阅读帖子上的各种评论以及其他回复。我还建议你不要试图在带有文本的控件中扩展FontSize:这通常会导致WinForms中的印刷混乱。
Here's an overview of how to achieve relative positioning and re-sizing of Controls when the Controls Form/ContainerControl changes size [^].

I've not been able to get permission, yet, to publish the entire code on CP, but, I hope to.

I suggest you read the various comments on the post, and the other responses. I also suggest you avoid trying to scale FontSize in Controls with Text: that's usually leads to a typographic mess in WinForms.


我想你自己已经提到了解决方案,



如果我理解正确的问题,你需要使用你所说的比率。



使用这个比例来调整这些图片的大小框使用宽度,高度并使用代码中的左侧,顶部属性重新定位
I think you have mentioned the solution yourself,

If I understood the problme right, you need to use a ratio like you said.

use this ratio to resize those picture boxes using width, height and relocate them using left, top properties in Code


这篇关于调整面板大小时,如何在面板上保留相对于面板的图片框(在面板中找到)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆