当屏幕分辨率更改 WinForm 内的面板及其子控件显示时 [英] When Screen Resolution Changes the Panel and its Child Control display inside the WinForm

查看:23
本文介绍了当屏幕分辨率更改 WinForm 内的面板及其子控件显示时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试开发一个具有三个面板的窗体应用程序

I am trying to develop an windows form application having Three panels

Panel 1(FileBrowsePanel):- 限制 TextBox 、浏览按钮

Panel 1(FileBrowsePanel):- contrains TextBox , Browse Button

Panel 2(SearchCriteriaPanel) :- Lable From Date , ToDate DateTimePicker 控件 From Date 和 To Date CheckBox 每个 DateTimePicker 控件还有两个 TextBoxes 和 ListBox 两个按钮搜索和重置

Panel 2(SearchCriteriaPanel ) :- Lable From Date , ToDate DateTimePicker control From Date and To Date CheckBox for each DateTimePicker control also two TextBoxes and ListBox Two Buttons Search and Reset

Panel 3(DisplayDataPanel):- ListView 显示来自浏览文件路径的数据

Panel 3(DisplayDataPanel):- ListView to Display Data from from browse file path

面板 4(DeleteCriteriaPanel):- 使用 DateTimePicker 控件从列表视图和带有删除按钮的文件中删除数据

Panel 4(DeleteCriteriaPanel):- Having DateTimePicker control to delete data from listview and file with Delete Button

实际问题是,当我更改屏幕分辨率时,该面板上的所有面板和控件都超出表单宽度和高度代码

actual problem is that When i Change the resolution of screen all the panels and control on that panel goes out of the form width and Height code is

void LoadWindowsSetting()
        {
            this.SuspendLayout();
            int i_StandardHeight = 768;//Developer Desktop Height Where the Form is Designed
            int i_StandardWidth = 1024; ;//Developer Desktop Width Where the Form is Designed
            int i_PresentHeight = Screen.PrimaryScreen.WorkingArea.Height- SystemInformation.CaptionHeight;
            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.Panel")
                {
                    // 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));
                }
                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));
                    }
                    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));
                }
                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));
                }
            }
            btnBrowse.Height = txtBrowseFilePath.Height + 1;
            btnSubmit.Height = btnBrowse.Height;
            btnReset.Height = btnBrowse.Height;
            panelSearch.Height += listBoxLogType.Height;

            int leftSpacePanelBrowse = (this.Width - panelBrowse.Width) / 2;
            int leftSpacePanelSearch = (this.Width - panelSearch.Width) / 2;
            int leftSpacePanelLogList = (this.Width - panelLogList.Width) / 2;
            int leftSpacePanelDeleteLog = (this.Width - panelDeleteLog.Width) / 2;

            panelBrowse.Location = new System.Drawing.Point(leftSpacePanelBrowse - 8, 25);
            panelSearch.Location = new System.Drawing.Point(leftSpacePanelSearch - 8, panelBrowse.Height + 40);
            panelLogList.Location = new System.Drawing.Point(leftSpacePanelLogList - 8, panelSearch.Height + 140);
            progressBarFileLoad.Location = new System.Drawing.Point((this.Width / 2) - 100, panelLogList.Bounds.Y + panelLogList.Height + 5);
            panelDeleteLog.Location = new System.Drawing.Point(leftSpacePanelDeleteLog - 8, panelLogList.Bounds.Y + panelLogList.Height + progressBarFileLoad.Height + 20);

            this.ResumeLayout(false);
            Invalidate();
            Focus();
        }

所以我可以在屏幕分辨率改变时阻止控制

so I can prevent the control when the screen resolution is changes

推荐答案

表单Load事件调用ResizeForm()函数这样的方式

Form the form Load event call the ResizeForm() function in such a way

Resolution objFormResizer = new Resolution();
            objFormResizer.ResizeForm(this, 768, 1024); 



 public class Resolution
        {
            float heightRatio = new float();
            float widthRatio = new float();
            int standardHeight, standardWidth;
            public void ResizeForm(Form objForm, int DesignerHeight, int DesignerWidth)
            {           
                standardHeight = DesignerHeight;           
                standardWidth = DesignerWidth;
                int presentHeight = Screen.PrimaryScreen.WorkingArea.Height;//.Bounds.Height;
                int presentWidth = Screen.PrimaryScreen.Bounds.Width;
                heightRatio = (float)((float)presentHeight / (float)standardHeight);
                widthRatio = (float)((float)presentWidth / (float)standardWidth);
                objForm.AutoScaleMode = AutoScaleMode.None;
                objForm.Scale(new SizeF(widthRatio, heightRatio));
                foreach (Control c in objForm.Controls)
                {
                    if (c.HasChildren)
                    {
                        ResizeControlStore(c);                    
                    }
                    else
                    {
                        c.Font = new Font(c.Font.FontFamily, c.Font.Size * heightRatio, c.Font.Style, c.Font.Unit, ((byte)(0)));
                    }
                }
                objForm.Font = new Font(objForm.Font.FontFamily, objForm.Font.Size * heightRatio, objForm.Font.Style, objForm.Font.Unit, ((byte)(0)));           
            }

            private void ResizeControlStore(Control objCtl)
            {
                if (objCtl.HasChildren)
                {
                    foreach (Control cChildren in objCtl.Controls)
                    {
                        if (cChildren.HasChildren)
                        {
                            ResizeControlStore(cChildren);

                        }
                        else
                        {
                            cChildren.Font = new Font(cChildren.Font.FontFamily, cChildren.Font.Size * heightRatio, cChildren.Font.Style, cChildren.Font.Unit, ((byte)(0)));
                        }
                    }
                    objCtl.Font = new Font(objCtl.Font.FontFamily, objCtl.Font.Size * heightRatio, objCtl.Font.Style, objCtl.Font.Unit, ((byte)(0)));
                }
                else
                {
                    objCtl.Font = new Font(objCtl.Font.FontFamily, objCtl.Font.Size * heightRatio, objCtl.Font.Style, objCtl.Font.Unit, ((byte)(0)));
                }
            }
        }

这门课解决了我的问题.......希望你也喜欢快乐编码

this class slove my issue.......hope so you also enjoy Happy Coding

这篇关于当屏幕分辨率更改 WinForm 内的面板及其子控件显示时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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