调整UserControl及其子控件的大小 [英] Resize UserControl And It's Child Controls

查看:683
本文介绍了调整UserControl及其子控件的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

美好的一天所有!!!



我有一个用以下层次结构创建的UserControl:



UserControl

- PictureBox

- PictureBox

- PictureBox

- PictureBox

- PictureBox

- PictureBox

- PictureBox



我在UserControl中有一个背景图片(SizeMode) =缩放,以便背景图像增大/缩小),并且我将我的PictureBoxes(SizeMode = Zoom,使图像增大/缩小)与UserControl中背景图像上的某些点对齐。如果将我的UserControl添加到表单,它会以我想要的方式显示,PictureBoxes在正确的位置。

但是,当我将UserControl的'Dock'属性设置为'Fill'时,背景图像会正确调整大小,但PictureBoxes不会调整大小或重新定位。



如何调整PictureBoxes的大小/重新定位/缩放,以便在UserControl的背景图像上具有正确的大小及其位置。

Good day all!!!

I have a UserControl that I created with the following hierarchy:

UserControl
- PictureBox
- PictureBox
- PictureBox
- PictureBox
- PictureBox
- PictureBox
- PictureBox

I have a background image in the UserControl(SizeMode = Zoom, so that background image grow/shrinks according), and I'm aligning my PictureBoxes (SizeMode = Zoom, so that image grow/shrinks according) to certain points on the background image in the UserControl. If add my UserControl to a Form it appears just the way I want it, with the PictureBoxes in right places.
However, when I set my UserControl's 'Dock' property to 'Fill', the background image is resize correctly, but the PictureBoxes are not resize or repositioned.

How do I go about resizing/repositioning/scaling the PictureBoxes so that the have the right sizes and their locations on the UserControl's background image.

推荐答案

我找到了几年前我写的代码,它处理了在ContainerControl中重新调整大小和重新定位控件的情况,因为ContainerControl的大小按比例重新调整,因为它的形式是重新调整大小,或者窗口WindowState已更改。



但是,此代码是为客户端完成的,未经他们的许可,我无法在此处重新发布。我发了一条消息询问他们的许可。



以下是如何执行此操作的概要:



1.当应用程序启动时......在主表单加载事件中...记录主表单的大小和面板的大小。



2.计算面板宽度与表格宽度的比率,以及面板高度与表格高度的比率。



3.创建类型字典< Control,double>将PictureBox Control Left和Top Properties的比例保持在Panel的左侧和顶部属性。



4.通过Panel的控件,对于每个Panel,它是一个PictureBox,计算比率,并将PictureBox(作为Control)和比率添加到词典。



5.在Form SizeChanged EventHandler:



a。如果当前WindowState被最小化则退出



b。更新表格的大小,计算并存储表格宽度与表格高度的新比率。



c。调用代码来重新调整Panel的大小。



d。根据需要更新表单大小和边界变量



6.在重新调整面板大小的代码中:



a。暂停面板布局



b。循环遍历面板中的所有控件,对于每一个PictureBox:



1.使用字典中的比率重新定位

2.使用面板尺寸与表格尺寸的相对比例重新调整尺寸



c。恢复面板的布局



d。根据需要更新变量



以下是准备动态重新调整大小的初始代码的示例:
I have located the code I wrote a few years ago that handles the case of both re-sizing and re-positioning Controls within a ContainerControl, as the ContainerControl is re-sized proportionally as the Form it is in is re-sized, or the Form WindowState is changed.

However, this code was done for a client, and I can't re-post it here without their permission. I've sent a message asking their permission.

Here's a general outline of how to do this:

1. When the Application starts ... in the Main Form Load Event ... record the size of the Main Form and the size of the Panel.

2. Calculate ratios of the Panel Width to Form Width, and the Panel Height to Form Height.

3. Create Dictionaries of Types <Control, double> to hold the ratios of PictureBox Control Left and Top Properties to the Panel's Left and Top Properties.

4. go through the Panel's Controls, and, for each Panel that's a PictureBox, calculate the ratios, and add the PictureBox (as Control) and the ratio to the Dictionaries.

5. In the Form SizeChanged EventHandler:

a. exit if the current WindowState is Minimized

b. update the size of the Form, calculate and store the new ratios of Form Width to Form Height.

c. call the code to re-size the Panel.

d. update Form size and bounds variables as necessary

6. In the code to re-size the Panel:

a. suspend the layout of the Panel

b. loop through all the Controls in the Panel and, for each one that's a PictureBox:

1. re-position it using the ratios in the Dictionaries
2. re-size it using the relative ratio of Panel size to Form size

c. resume the layout of the Panel

d. update variables as necessary

Here's a sample of what your initial code that prepares for dynamic re-sizing could look like:
// Dictionaries to hold the ratios of PictureBox
// Left and Top to their Panel Container
// Width, and Height
private Dictionary<Control, double> XRatio = new Dictionary<Control, double>();
private Dictionary<Control, double> YRatio = new Dictionary<Control, double>();

private RectangleF currentFormDisplayRectangle;
private RectangleF currentPanelDisplayRectangle;

private void Form1_Load(object sender, EventArgs e)
{
    currentFormDisplayRectangle = DisplayRectangle;
    currentPanelDisplayRectangle = pnlForPictureBoxes.DisplayRectangle;

    foreach (Control theControl in pnlForPictureBoxes.Controls)
    {
        if (theControl is PictureBox)
        {
            // to avoid an integer result from the division
            // at least one of two values must be converted
            // to Type Double
            XRatio.Add(theControl, Convert.ToDouble(theControl.Left) / pnlForPictureBoxes.Width);
            YRatio.Add(theControl, Convert.ToDouble(theControl.Top) / pnlForPictureBoxes.Height);
        }
    }
}

我希望这在某种程度上有用。如果我可以获得许可,我将发布完整的代码。

I hope this is useful in some way. If I can get permission, I'll post complete code.


我认为没有任何自动魔法可以满足您的需求。我觉得这就是AnchorMode.None应该做的事情,但事实并非如此(我还没有找到该值有用的场景)。



你将需要向用户控件添加调整大小行为(覆盖OnResize ...确保调用基本实现,否则真的很奇怪的事情开始发生),手动调整PictureBox的位置和大小。
I don't think there's any automatic magic that does what you want. I feel like this is what AnchorMode.None should do, but it isn't (I've not yet found the scenario where that value is useful).

You'll need to add resize behaviour to the user control (override OnResize ... make sure you call the base implementation otherwise really weird things start happening) that adjusts the position and size of the PictureBoxes manually.


这篇关于调整UserControl及其子控件的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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