|
邀请回答
马上注册,享受更多特权
您需要 登录 才可以下载或查看,没有帐号?立即注册 ![](source/plugin/zhanmishu_wechat/template/static/img/wechat_login.png)
x
实现方式:
会将from装进Panel里面对控件进行自动计算。
在项目中添加AutoWindowsSize.cs类,内容如下:
(优势:不需要在Form1中添加任何布局,直接将类复制到项目中,在form1代码中调用即可,页面大小会自动计算,修改方便,速度快。)
缺点:页面如果是背景图片,页面特别快的时候,页面会有闪烁情况。
缺点:
(如果Form1中有背景图片,会直接显示成纯图的背景,原因是在AutoAdaptWindowsSize类中会创建一个panel,panel背景颜色会自动设置,而pannel的纯色背景会覆盖掉Form1的背景,本质上是panel的背景。所以可以给Panel设置背景图片或将panel背景色修改为Transparent,如:
Win_Panel1.BackColor = Color.Transparent;
这样在展示中可以正常的显示背景图片。)
其次:因为修改窗口需要对界面重新绘制,在拖动窗口时会出现比较严重的闪屏现象,需要在Form1的设计器中添加任意位置添加如下代码:
// 引入Forms。
using System.Windows.Forms;protected override CreateParams CreateParams{ get { CreateParams cp = base.CreateParams; cp.ExStyle |= 0x02000000; return cp; }}
AutoWindowsSize.cs类(复制即可)
<article data-content="[{"type":"block","id":"wPgG-1669300232392","name":"code","data":{"wrap":false,"language":"","theme":"default"},"nodes":[{"type":"block","id":"YS8l-1669300232391","name":"code-line","data":{},"nodes":[{"type":"text","id":"6u7q-1669300217457","leaves":[{"text":"using System;\r","marks":[]}]}]},{"type":"block","id":"TAN8-1669300246206","name":"code-line","data":{},"nodes":[{"type":"text","id":"XG9G-1669300246205","leaves":[{"text":"using System.Collections.Generic;\r","marks":[]}]}]},{"type":"block","id":"0ICs-1669300246208","name":"code-line","data":{},"nodes":[{"type":"text","id":"Xqj2-1669300246207","leaves":[{"text":"using System.ComponentModel;\r","marks":[]}]}]},{"type":"block","id":"RXvr-1669300246210","name":"code-line","data":{},"nodes":[{"type":"text","id":"0ctW-1669300246209","leaves":[{"text":"using System.Data;\r","marks":[]}]}]},{"type":"block","id":"sJgi-1669300246212","name":"code-line","data":{},"nodes":[{"type":"text","id":"Dxwy-1669300246211","leaves":[{"text":"using System.Drawing;\r","marks":[]}]}]},{"type":"block","id":"G9Xe-1669300246214","name":"code-line","data":{},"nodes":[{"type":"text","id":"1BUL-1669300246213","leaves":[{"text":"//using System.Linq;\r","marks":[]}]}]},{"type":"block","id":"qiu2-1669300246216","name":"code-line","data":{},"nodes":[{"type":"text","id":"NmCS-1669300246215","leaves":[{"text":"using System.Text;\r","marks":[]}]}]},{"type":"block","id":"rBmq-1669300246218","name":"code-line","data":{},"nodes":[{"type":"text","id":"aY7q-1669300246217","leaves":[{"text":"using System.Windows.Forms;\r","marks":[]}]}]},{"type":"block","id":"kqwY-1669300246220","name":"code-line","data":{},"nodes":[{"type":"text","id":"r3z8-1669300246219","leaves":[{"text":"\r","marks":[]}]}]},{"type":"block","id":"2gmB-1669300246222","name":"code-line","data":{},"nodes":[{"type":"text","id":"drtV-1669300246221","leaves":[{"text":"using System.Drawing.Drawing2D;\r","marks":[]}]}]},{"type":"block","id":"6uTQ-1669300246225","name":"code-line","data":{},"nodes":[{"type":"text","id":"WerU-1669300246223","leaves":[{"text":"//using System.Threading.Tasks;\r","marks":[]}]}]},{"type":"block","id":"HW9e-1669300246227","name":"code-line","data":{},"nodes":[{"type":"text","id":"AEzv-1669300246226","leaves":[{"text":"\r","marks":[]}]}]},{"type":"block","id":"XXjM-1669300246229","name":"code-line","data":{},"nodes":[{"type":"text","id":"qi8n-1669300246228","leaves":[{"text":"\r","marks":[]}]}]},{"type":"block","id":"WlWq-1669300246231","name":"code-line","data":{},"nodes":[{"type":"text","id":"wQbt-1669300246230","leaves":[{"text":"namespace AutoWindowsSize\r","marks":[]}]}]},{"type":"block","id":"Nn3v-1669300246233","name":"code-line","data":{},"nodes":[{"type":"text","id":"itv3-1669300246232","leaves":[{"text":"{\r","marks":[]}]}]},{"type":"block","id":"xxO9-1669300246235","name":"code-line","data":{},"nodes":[{"type":"text","id":"y7Fw-1669300246234","leaves":[{"text":" class AutoAdaptWindowsSize //窗口缩放对象\r","marks":[]}]}]},{"type":"block","id":"oyKI-1669300246237","name":"code-line","data":{},"nodes":[{"type":"text","id":"OnQX-1669300246236","leaves":[{"text":" {\r","marks":[]}]}]},{"type":"block","id":"pHaZ-1669300246239","name":"code-line","data":{},"nodes":[{"type":"text","id":"YoPh-1669300246238","leaves":[{"text":" double formOriginalWidth;//窗体高度原始宽度\r","marks":[]}]}]},{"type":"block","id":"g0hA-1669300246241","name":"code-line","data":{},"nodes":[{"type":"text","id":"Wbos-1669300246240","leaves":[{"text":" double formOriginalHeight;//窗体原始\r","marks":[]}]}]},{"type":"block","id":"D3Ee-1669300246243","name":"code-line","data":{},"nodes":[{"type":"text","id":" HLn-1669300246242","leaves":[{"text":" double scaleX;//水平缩放比例\r","marks":[]}]}]},{"type":"block","id":"uHTw-1669300246245","name":"code-line","data":{},"nodes":[{"type":"text","id":"gagz-1669300246244","leaves":[{"text":" double scaleY;//垂直缩放比例\r","marks":[]}]}]},{"type":"block","id":"ZLth-1669300246249","name":"code-line","data":{},"nodes":[{"type":"text","id":"5vwj-1669300246248","leaves":[{"text":" Dictionary ControlsInfo = new Dictionary();//控件中心Left,Top,控件Width,控件Height,控件字体Size\r","marks":[]}]}]},{"type":"block","id":"kRFz-1669300246251","name":"code-line","data":{},"nodes":[{"type":"text","id":"Wg3J-1669300246250","leaves":[{"text":"\r","marks":[]}]}]},{"type":"block","id":"47JG-1669300246253","name":"code-line","data":{},"nodes":[{"type":"text","id":"V63d-1669300246252","leaves":[{"text":" private Form _form;\r","marks":[]}]}]},{"type":"block","id":"iNjC-1669300246255","name":"code-line","data":{},"nodes":[{"type":"text","id":"xO4Q-1669300246254","leaves":[{"text":" Panel Win_Panel1 = new Panel();\r","marks":[]}]}]},{"type":"block","id":"iUvK-1669300246259","name":"code-line","data":{},"nodes":[{"type":"text","id":"poQH-1669300246258","leaves":[{"text":" public AutoAdaptWindowsSize(Form form)\r","marks":[]}]}]},{"type":"block","id":"wYXG-1669300246261","name":"code-line","data":{},"nodes":[{"type":"text","id":"0QkD-1669300246260","leaves":[{"text":" {\r","marks":[]}]}]},{"type":"block","id":"eKmQ-1669300246263","name":"code-line","data":{},"nodes":[{"type":"text","id":"CmLH-1669300246262","leaves":[{"text":" _form = form;\r","marks":[]}]}]},{"type":"block","id":"Ly2d-1669300246265","name":"code-line","data":{},"nodes":[{"type":"text","id":"0OVh-1669300246264","leaves":[{"text":"\r","marks":[]}]}]},{"type":"block","id":"lpsu-1669300246267","name":"code-line","data":{},"nodes":[{"type":"text","id":"7S5E-1669300246266","leaves":[{"text":" //代码生成一个容器panel1,添加至窗体\r","marks":[]}]}]},{"type":"block","id":"5Fsc-1669300246269","name":"code-line","data":{},"nodes":[{"type":"text","id":"6LHt-1669300246268","leaves":[{"text":" _form.Controls.Add(Win_Panel1);\r","marks":[]}]}]},{"type":"block","id":"ABsB-1669300246271","name":"code-line","data":{},"nodes":[{"type":"text","id":"RcLE-1669300246270","leaves":[{"text":" Win_Panel1.BorderStyle = BorderStyle.None; //容器border样式\r","marks":[]}]}]},{"type":"block","id":"nGbT-1669300246273","name":"code-line","data":{},"nodes":[{"type":"text","id":"A3m5-1669300246272","leaves":[{"text":" Win_Panel1.Dock = DockStyle.Fill; //设置填充,下面添加控件至容器完成后,容器会填充窗口","marks":[]}]}]},{"type":"block","id":"zYas-1669360114552","name":"code-line","data":{},"nodes":[{"type":"text","id":"lsEO-1669360114550","leaves":[{"text":" Win_Panel1.BackColor = Color.TransParents; // 设置为透明背景\r","marks":[]}]}]},{"type":"block","id":"4kSh-1669300246279","name":"code-line","data":{},"nodes":[{"type":"text","id":"5o2f-1669300246278","leaves":[{"text":" //将窗体所有控件添加至panel1\r","marks":[]}]}]},{"type":"block","id":"FeAN-1669300246281","name":"code-line","data":{},"nodes":[{"type":"text","id":"vWVw-1669300246280","leaves":[{"text":" while (_form.Controls[0].Name.Trim() != \"\")\r","marks":[]}]}]},{"type":"block","id":"fOYK-1669300246283","name":"code-line","data":{},"nodes":[{"type":"text","id":"dPDs-1669300246282","leaves":[{"text":" {\r","marks":[]}]}]},{"type":"block","id":"GBaG-1669300246285","name":"code-line","data":{},"nodes":[{"type":"text","id":"VCSq-1669300246284","leaves":[{"text":" foreach (Control item in _form.Controls)\r","marks":[]}]}]},{"type":"block","id":"hysF-1669300246287","name":"code-line","data":{},"nodes":[{"type":"text","id":"4VCm-1669300246286","leaves":[{"text":" {\r","marks":[]}]}]},{"type":"block","id":"FbAv-1669300246289","name":"code-line","data":{},"nodes":[{"type":"text","id":"ac7W-1669300246288","leaves":[{"text":"\r","marks":[]}]}]},{"type":"block","id":"HPao-1669300246291","name":"code-line","data":{},"nodes":[{"type":"text","id":"t3HE-1669300246290","leaves":[{"text":" if (item.Name.Trim() != \"\" && item.Name.Trim() != Win_Panel1.Name.Trim())\r","marks":[]}]}]},{"type":"block","id":"yt3K-1669300246293","name":"code-line","data":{},"nodes":[{"type":"text","id":"Sve6-1669300246292","leaves":[{"text":" {\r","marks":[]}]}]},{"type":"block","id":"Ot9n-1669300246295","name":"code-line","data":{},"nodes":[{"type":"text","id":"vyyg-1669300246294","leaves":[{"text":" Win_Panel1.Controls.Add(item);\r","marks":[]}]}]},{"type":"block","id":"UKkA-1669300246297","name":"code-line","data":{},"nodes":[{"type":"text","id":"WCMG-1669300246296","leaves":[{"text":" }\r","marks":[]}]}]},{"type":"block","id":"uvAr-1669300246299","name":"code-line","data":{},"nodes":[{"type":"text","id":"a8Vy-1669300246298","leaves":[{"text":" }\r","marks":[]}]}]},{"type":"block","id":"oDHa-1669300246301","name":"code-line","data":{},"nodes":[{"type":"text","id":"Dz5f-1669300246300","leaves":[{"text":" }\r","marks":[]}]}]},{"type":"block","id":"l6i1-1669300246303","name":"code-line","data":{},"nodes":[{"type":"text","id":"xc41-1669300246302","leaves":[{"text":"\r","marks":[]}]}]},{"type":"block","id":"jSwL-1669300246305","name":"code-line","data":{},"nodes":[{"type":"text","id":"BWgB-1669300246304","leaves":[{"text":" //保存窗体和控件初始大小\r","marks":[]}]}]},{"type":"block","id":"tNPc-1669300246307","name":"code-line","data":{},"nodes":[{"type":"text","id":"gZWq-1669300246306","leaves":[{"text":" InitControlsInfo(Win_Panel1);\r","marks":[]}]}]},{"type":"block","id":"Zm7T-1669300246309","name":"code-line","data":{},"nodes":[{"type":"text","id":"iPqc-1669300246308","leaves":[{"text":" }\r","marks":[]}]}]},{"type":"block","id":"GCCg-1669300246311","name":"code-line","data":{},"nodes":[{"type":"text","id":"R3M3-1669300246310","leaves":[{"text":"\r","marks":[]}]}]},{"type":"block","id":"s18w-1669300246319","name":"code-line","data":{},"nodes":[{"type":"text","id":"HCwu-1669300246318","leaves":[{"text":" public void InitControlsInfo(Control ctrlContainer)\r","marks":[]}]}]},{"type":"block","id":"Lr1q-1669300246321","name":"code-line","data":{},"nodes":[{"type":"text","id":"OJEg-1669300246320","leaves":[{"text":" {\r","marks":[]}]}]},{"type":"block","id":"6sds-1669300246323","name":"code-line","data":{},"nodes":[{"type":"text","id":"bXkn-1669300246322","leaves":[{"text":" if (ctrlContainer.Parent == _form)//获取窗体的高度和宽度\r","marks":[]}]}]},{"type":"block","id":"9oih-1669300246325","name":"code-line","data":{},"nodes":[{"type":"text","id":"4GKN-1669300246324","leaves":[{"text":" {\r","marks":[]}]}]},{"type":"block","id":"3uqf-1669300246327","name":"code-line","data":{},"nodes":[{"type":"text","id":"LSAq-1669300246326","leaves":[{"text":" formOriginalWidth = Convert.ToDouble(ctrlContainer.Width);\r","marks":[]}]}]},{"type":"block","id":"WLcY-1669300246329","name":"code-line","data":{},"nodes":[{"type":"text","id":"kaPc-1669300246328","leaves":[{"text":" formOriginalHeight = Convert.ToDouble(ctrlContainer.Height);\r","marks":[]}]}]},{"type":"block","id":"lmgG-1669300246331","name":"code-line","data":{},"nodes":[{"type":"text","id":"It9Z-1669300246330","leaves":[{"text":" }\r","marks":[]}]}]},{"type":"block","id":" yfH-1669300246333","name":"code-line","data":{},"nodes":[{"type":"text","id":"nSA5-1669300246332","leaves":[{"text":" foreach (Control item in ctrlContainer.Controls)\r","marks":[]}]}]},{"type":"block","id":"C6ln-1669300246335","name":"code-line","data":{},"nodes":[{"type":"text","id":"uvla-1669300246334","leaves":[{"text":" {\r","marks":[]}]}]},{"type":"block","id":"eQrj-1669300246337","name":"code-line","data":{},"nodes":[{"type":"text","id":"cpbn-1669300246336","leaves":[{"text":" if (item.Name.Trim() != \"\")\r","marks":[]}]}]},{"type":"block","id":"w8Yq-1669300246339","name":"code-line","data":{},"nodes":[{"type":"text","id":"iKlg-1669300246338","leaves":[{"text":" {\r","marks":[]}]}]},{"type":"block","id":"sRCk-1669300246341","name":"code-line","data":{},"nodes":[{"type":"text","id":"9T94-1669300246340","leaves":[{"text":" //添加信息:键值:控件名,内容:据左边距离,距顶部距离,控件宽度,控件高度,控件字体。\r","marks":[]}]}]},{"type":"block","id":"ZCfz-1669300246343","name":"code-line","data":{},"nodes":[{"type":"text","id":"OR1V-1669300246342","leaves":[{"text":" ControlsInfo.Add(item.Name, (item.Left + item.Width / 2) + \",\" + (item.Top + item.Height / 2) + \",\" + item.Width + \",\" + item.Height + \",\" + item.Font.Size);\r","marks":[]}]}]},{"type":"block","id":"EOFF-1669300246345","name":"code-line","data":{},"nodes":[{"type":"text","id":"dIPP-1669300246344","leaves":[{"text":" }\r","marks":[]}]}]},{"type":"block","id":"fdS4-1669300246347","name":"code-line","data":{},"nodes":[{"type":"text","id":"Lh03-1669300246346","leaves":[{"text":" if ((item as UserControl) == null && item.Controls.Count > 0)\r","marks":[]}]}]},{"type":"block","id":"uSEm-1669300246349","name":"code-line","data":{},"nodes":[{"type":"text","id":"UESX-1669300246348","leaves":[{"text":" {\r","marks":[]}]}]},{"type":"block","id":"DFBS-1669300246352","name":"code-line","data":{},"nodes":[{"type":"text","id":"6vos-1669300246351","leaves":[{"text":" InitControlsInfo(item);\r","marks":[]}]}]},{"type":"block","id":"z3bj-1669300246354","name":"code-line","data":{},"nodes":[{"type":"text","id":"uwDJ-1669300246353","leaves":[{"text":" }\r","marks":[]}]}]},{"type":"block","id":"S21t-1669300246356","name":"code-line","data":{},"nodes":[{"type":"text","id":"ijMZ-1669300246355","leaves":[{"text":" }\r","marks":[]}]}]},{"type":"block","id":" gcL-1669300246358","name":"code-line","data":{},"nodes":[{"type":"text","id":"3qNO-1669300246357","leaves":[{"text":"\r","marks":[]}]}]},{"type":"block","id":"GCPW-1669300246360","name":"code-line","data":{},"nodes":[{"type":"text","id":"BUmM-1669300246359","leaves":[{"text":" }\r","marks":[]}]}]},{"type":"block","id":"JjX7-1669300246366","name":"code-line","data":{},"nodes":[{"type":"text","id":"YLsB-1669300246365","leaves":[{"text":" public void FormSizeChanged()\r","marks":[]}]}]},{"type":"block","id":"vwph-1669300246368","name":"code-line","data":{},"nodes":[{"type":"text","id":"6JVo-1669300246367","leaves":[{"text":" {\r","marks":[]}]}]},{"type":"block","id":"ktI2-1669300246370","name":"code-line","data":{},"nodes":[{"type":"text","id":"2Cti-1669300246369","leaves":[{"text":" try\r","marks":[]}]}]},{"type":"block","id":"rVwO-1669300246372","name":"code-line","data":{},"nodes":[{"type":"text","id":"9Js4-1669300246371","leaves":[{"text":" {\r","marks":[]}]}]},{"type":"block","id":"UDPA-1669300246374","name":"code-line","data":{},"nodes":[{"type":"text","id":"rnKF-1669300246373","leaves":[{"text":" if (ControlsInfo.Count > 0)//如果字典中有数据,即窗体改变\r","marks":[]}]}]},{"type":"block","id":"SQQ3-1669300246376","name":"code-line","data":{},"nodes":[{"type":"text","id":"k5V0-1669300246375","leaves":[{"text":" {\r","marks":[]}]}]},{"type":"block","id":"j4Xu-1669300246378","name":"code-line","data":{},"nodes":[{"type":"text","id":"jmqP-1669300246377","leaves":[{"text":" ControlsZoomScale(Win_Panel1);//表示pannel控件\r","marks":[]}]}]},{"type":"block","id":"XdBX-1669300246380","name":"code-line","data":{},"nodes":[{"type":"text","id":"bOMC-1669300246379","leaves":[{"text":" ControlsChange(Win_Panel1);\r","marks":[]}]}]},{"type":"block","id":"84ie-1669300246382","name":"code-line","data":{},"nodes":[{"type":"text","id":"xAsy-1669300246381","leaves":[{"text":" }\r","marks":[]}]}]},{"type":"block","id":"6dtW-1669300246384","name":"code-line","data":{},"nodes":[{"type":"text","id":"LY6F-1669300246383","leaves":[{"text":" }\r","marks":[]}]}]},{"type":"block","id":"fXpY-1669300246386","name":"code-line","data":{},"nodes":[{"type":"text","id":"SIDi-1669300246385","leaves":[{"text":" catch { }\r","marks":[]}]}]},{"type":"block","id":"4uss-1669300246388","name":"code-line","data":{},"nodes":[{"type":"text","id":"M8KF-1669300246387","leaves":[{"text":" }\r","marks":[]}]}]},{"type":"block","id":"tBsr-1669300246390","name":"code-line","data":{},"nodes":[{"type":"text","id":"2TWu-1669300246389","leaves":[{"text":" private void ControlsZoomScale(Control ctrlContainer)\r","marks":[]}]}]},{"type":"block","id":"kKzK-1669300246392","name":"code-line","data":{},"nodes":[{"type":"text","id":"NmPO-1669300246391","leaves":[{"text":" {\r","marks":[]}]}]},{"type":"block","id":"U52r-1669300246394","name":"code-line","data":{},"nodes":[{"type":"text","id":"n3Nq-1669300246393","leaves":[{"text":" scaleX = (Convert.ToDouble(ctrlContainer.Width) / formOriginalWidth);\r","marks":[]}]}]},{"type":"block","id":"fYZM-1669300246396","name":"code-line","data":{},"nodes":[{"type":"text","id":"5Dn2-1669300246395","leaves":[{"text":" scaleY = (Convert.ToDouble(ctrlContainer.Height) / formOriginalHeight);\r","marks":[]}]}]},{"type":"block","id":"UNx9-1669300246398","name":"code-line","data":{},"nodes":[{"type":"text","id":"kMnk-1669300246397","leaves":[{"text":" }\r","marks":[]}]}]},{"type":"block","id":"E8d9-1669300246402","name":"code-line","data":{},"nodes":[{"type":"text","id":"8F2v-1669300246401","leaves":[{"text":"\r","marks":[]}]}]},{"type":"block","id":"CtpI-1669300246404","name":"code-line","data":{},"nodes":[{"type":"text","id":"9IXg-1669300246403","leaves":[{"text":" // 改变控件大小\r","marks":[]}]}]},{"type":"block","id":"fpaO-1669300246406","name":"code-line","data":{},"nodes":[{"type":"text","id":"siUQ-1669300246405","leaves":[{"text":"\r","marks":[]}]}]},{"type":"block","id":"udZJ-1669300246410","name":"code-line","data":{},"nodes":[{"type":"text","id":"d7nR-1669300246409","leaves":[{"text":" private void ControlsChange(Control ctrlContainer)\r","marks":[]}]}]},{"type":"block","id":"KjFb-1669300246412","name":"code-line","data":{},"nodes":[{"type":"text","id":"RZKR-1669300246411","leaves":[{"text":" {\r","marks":[]}]}]},{"type":"block","id":"3XUa-1669300246414","name":"code-line","data":{},"nodes":[{"type":"text","id":"SwUw-1669300246413","leaves":[{"text":" double[] pos = new double[5];//pos数组保存当前控件中心Left,Top,控件Width,控件Height,控件字体Size\r","marks":[]}]}]},{"type":"block","id":"7y8G-1669300246416","name":"code-line","data":{},"nodes":[{"type":"text","id":"Rglk-1669300246415","leaves":[{"text":" foreach (Control item in ctrlContainer.Controls)//遍历控件\r","marks":[]}]}]},{"type":"block","id":"x4eF-1669300246418","name":"code-line","data":{},"nodes":[{"type":"text","id":"fryZ-1669300246417","leaves":[{"text":" {\r","marks":[]}]}]},{"type":"block","id":"MyBG-1669300246420","name":"code-line","data":{},"nodes":[{"type":"text","id":"TTJF-1669300246419","leaves":[{"text":" if (item.Name.Trim() != \"\")//如果控件名不是空,则执行\r","marks":[]}]}]},{"type":"block","id":"ITjI-1669300246422","name":"code-line","data":{},"nodes":[{"type":"text","id":"ERtd-1669300246421","leaves":[{"text":" {\r","marks":[]}]}]},{"type":"block","id":"aHWX-1669300246424","name":"code-line","data":{},"nodes":[{"type":"text","id":"nEMJ-1669300246423","leaves":[{"text":" if ((item as UserControl) == null && item.Controls.Count > 0)//如果不是自定义控件\r","marks":[]}]}]},{"type":"block","id":"6ba2-1669300246426","name":"code-line","data":{},"nodes":[{"type":"text","id":"jHNx-1669300246425","leaves":[{"text":" {\r","marks":[]}]}]},{"type":"block","id":"rdmN-1669300246428","name":"code-line","data":{},"nodes":[{"type":"text","id":"3I9j-1669300246427","leaves":[{"text":" ControlsChange(item);//循环执行\r","marks":[]}]}]},{"type":"block","id":"8JEh-1669300246430","name":"code-line","data":{},"nodes":[{"type":"text","id":"vzQz-1669300246429","leaves":[{"text":" }\r","marks":[]}]}]},{"type":"block","id":"tBDl-1669300246432","name":"code-line","data":{},"nodes":[{"type":"text","id":"FpB8-1669300246431","leaves":[{"text":" string[] strs = ControlsInfo[item.Name].Split(',');//从字典中查出的数据,以‘,’分割成字符串组\r","marks":[]}]}]},{"type":"block","id":"pffY-1669300246434","name":"code-line","data":{},"nodes":[{"type":"text","id":"oB3c-1669300246433","leaves":[{"text":"\r","marks":[]}]}]},{"type":"block","id":"5teH-1669300246436","name":"code-line","data":{},"nodes":[{"type":"text","id":"cyBV-1669300246435","leaves":[{"text":" for (int i = 0; i using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;//using System.Linq;using System.Text;using System.Windows.Forms;using System.Drawing.Drawing2D;//using System.Threading.Tasks;namespace AutoWindowsSize{ class AutoAdaptWindowsSize //窗口缩放对象 { double formOriginalWidth;//窗体高度原始宽度 double formOriginalHeight;//窗体原始 double scaleX;//水平缩放比例 double scaleY;//垂直缩放比例 Dictionary<string, string> ControlsInfo = new Dictionary<string, string>();//控件中心Left,Top,控件Width,控件Height,控件字体Size private Form _form; Panel Win_Panel1 = new Panel(); public AutoAdaptWindowsSize(Form form) { _form = form; //代码生成一个容器panel1,添加至窗体 _form.Controls.Add(Win_Panel1); Win_Panel1.BorderStyle = BorderStyle.None; //容器border样式 Win_Panel1.Dock = DockStyle.Fill; //设置填充,下面添加控件至容器完成后,容器会填充窗口 Win_Panel1.BackColor = Color.TransParents; // 设置为透明背景 //将窗体所有控件添加至panel1 while (_form.Controls[0].Name.Trim() != "") { foreach (Control item in _form.Controls) { if (item.Name.Trim() != "" && item.Name.Trim() != Win_Panel1.Name.Trim()) { Win_Panel1.Controls.Add(item); } } } //保存窗体和控件初始大小 InitControlsInfo(Win_Panel1); } public void InitControlsInfo(Control ctrlContainer) { if (ctrlContainer.Parent == _form)//获取窗体的高度和宽度 { formOriginalWidth = Convert.ToDouble(ctrlContainer.Width); formOriginalHeight = Convert.ToDouble(ctrlContainer.Height); } foreach (Control item in ctrlContainer.Controls) { if (item.Name.Trim() != "") { //添加信息:键值:控件名,内容:据左边距离,距顶部距离,控件宽度,控件高度,控件字体。 ControlsInfo.Add(item.Name, (item.Left + item.Width / 2) + "," + (item.Top + item.Height / 2) + "," + item.Width + "," + item.Height + "," + item.Font.Size); } if ((item as UserControl) == null && item.Controls.Count > 0) { InitControlsInfo(item); } } } public void FormSizeChanged() { try { if (ControlsInfo.Count > 0)//如果字典中有数据,即窗体改变 { ControlsZoomScale(Win_Panel1);//表示pannel控件 ControlsChange(Win_Panel1); } } catch { } } private void ControlsZoomScale(Control ctrlContainer) { scaleX = (Convert.ToDouble(ctrlContainer.Width) / formOriginalWidth); scaleY = (Convert.ToDouble(ctrlContainer.Height) / formOriginalHeight); } // 改变控件大小 private void ControlsChange(Control ctrlContainer) { double[] pos = new double[5];//pos数组保存当前控件中心Left,Top,控件Width,控件Height,控件字体Size foreach (Control item in ctrlContainer.Controls)//遍历控件 { if (item.Name.Trim() != "")//如果控件名不是空,则执行 { if ((item as UserControl) == null && item.Controls.Count > 0)//如果不是自定义控件 { ControlsChange(item);//循环执行 } string[] strs = ControlsInfo[item.Name].Split(',');//从字典中查出的数据,以‘,’分割成字符串组 for (int i = 0; i < 5; i++) { pos[i] = Convert.ToDouble(strs[i]);//添加到临时数组 } double itemWidth = pos[2] * scaleX; //计算控件宽度,double类型 double itemHeight = pos[3] * scaleY; //计算控件高度 item.Left = Convert.ToInt32(pos[0] * scaleX - itemWidth / 2);//计算控件距离左边距离 item.Top = Convert.ToInt32(pos[1] * scaleY - itemHeight / 2);//计算控件距离顶部距离 item.Width = Convert.ToInt32(itemWidth);//控件宽度,int类型 item.Height = Convert.ToInt32(itemHeight);//控件高度 if (float.Parse((pos[4] * Math.Min(scaleX, scaleY)).ToString()) != 0) //缩放字体大小不能为0 { item.Font = new Font(item.Font.Name, float.Parse((pos[4] * Math.Min(scaleX, scaleY)).ToString())); } //字体 } } } }}
创建结束以后,在Form1类中做如下操作:
1.引入using AutoWindowsSize;
using AutoWindowsSize;
2.在Form1类中设置全局变量。
AutoAdaptWindowsSize AutoSize;
3.在Form1_Load添加如下代码。
private void Form1_Load(object sender, EventArgs e){
AutoSize = new AutoAdaptWindowsSize(this);}
4.创建Form1_SizeChanged事件函数,如下:
private void Form1_SizeChanged(object sender, EventArgs e){ if (AutoSize != null) { AutoSize.FormSizeChanged(); }}
|
上一篇: Learning hard C#学习笔记 [李志著]下一篇: C#基础资料学习
|