Adjust Panels Automatically

0

Good afternoon, this day my question is a bit simple but I do not know how to solve it, I am creating a% main windows forms , in which I have two panel , and which I need to adjust automatically to the resolution of my screen without having to modify the entire size of my form and be playing with the sizes.

If anyone knows how to do it, I'll be very grateful.

    
asked by Alberto Arenas 09.11.2017 в 20:17
source

1 answer

0

It would be easier if you put an image of the form with the panels to get a better idea of what you want to do.

But well, what I understand is that, if you change the resolution, the panels look, or very large, or very small, and you want them to always look the same size, regardless of the screen resolution.

If that's what you want, you have to readjust the sizes by getting the aspect ratio, for example in this answer give the following code: link

First you save the height and width of the screen in pixels:

int i_StandardHeight = 768;// la resolución de pantalla (alto en pixeles) inicial
                int i_StandardWidth = 1024; ;// resolución de pantalla (ancho en pixeles) al iniciar la aplicación

And when you redraw the panels:

                int i_PresentHeight = Screen.PrimaryScreen.Bounds.Height;
                int i_PresentWidth = Screen.PrimaryScreen.Bounds.Width;
                float f_HeightRatio = new float();
                float f_WidthRatio = new float();
                f_HeightRatio = (float)((float)i_PresentHeight / (float)i_StandardHeight);
                f_WidthRatio = (float)((float)i_PresentWidth / (float)i_StandardWidth);
                foreach (Control c in this.Controls)
                {
                    if (c.GetType().ToString() == "System.Windows.Forms.Button")
                    {
                        Button obtn = (Button)c;
                        obtn.TextAlign = ContentAlignment.MiddleCenter;
                    }
                    if (c.HasChildren)
                    {
                        foreach (Control cChildren in c.Controls)
                        {
                            cChildren.SetBounds(Convert.ToInt32(cChildren.Bounds.X * f_WidthRatio), Convert.ToInt32(cChildren.Bounds.Y * f_WidthRatio), Convert.ToInt32(cChildren.Bounds.Width * f_WidthRatio), Convert.ToInt32(cChildren.Bounds.Height * f_HeightRatio));
                            //cChildren.Font = new Font(cChildren.Font.FontFamily, cChildren.Font.Size * f_HeightRatio, cChildren.Font.Style, cChildren.Font.Unit, ((byte)(0)));
                        }
                        c.SetBounds(Convert.ToInt32(c.Bounds.X * f_WidthRatio), Convert.ToInt32(c.Bounds.Y * f_WidthRatio), Convert.ToInt32(c.Bounds.Width * f_WidthRatio), Convert.ToInt32(c.Bounds.Height * f_HeightRatio));
                       // c.Font = new Font(c.Font.FontFamily, c.Font.Size * f_HeightRatio, c.Font.Style, c.Font.Unit, ((byte)(0)));
                    }
                    else
                    {
                        c.SetBounds(Convert.ToInt32(c.Bounds.X * f_WidthRatio), Convert.ToInt32(c.Bounds.Y * f_WidthRatio), Convert.ToInt32(c.Bounds.Width * f_WidthRatio), Convert.ToInt32(c.Bounds.Height * f_HeightRatio));
                       // c.Font = new Font(c.Font.FontFamily, c.Font.Size * f_HeightRatio, c.Font.Style, c.Font.Unit, ((byte)(0)));
                    }
                }
                this.Height = Convert.ToInt32(i_StandardHeight * f_HeightRatio);
                this.Width = Convert.ToInt32(i_StandardWidth * f_WidthRatio);

Now you only have to capture the resolution change event and then redraw the panels:

Microsoft.Win32.SystemEvents.DisplaySettingsChanged

But if you want the panels to always stay in one place and with a spacing even if you change the size of the form, you can play with the 'Anchor' property or put the panels in a TableLayoutPanel.

    
answered by 11.11.2017 в 20:54