当前位置:网站首页>Implementation of form control adaptation (adaptive form) layout in WinForm_ By C code
Implementation of form control adaptation (adaptive form) layout in WinForm_ By C code
2022-07-21 06:07:00 【Milk coffee 13】
One 、 Realization effect
1.1、 The implementation effect of control scaling is not enabled
namely : When the control scaling effect code is not enabled , Control content is fixed in the specified position of the form interface , Will not follow the stretching of the form , Zoom in and adapt , As shown in the figure below :
1.2、 Enable the implementation effect of control scaling
namely : When the control scaling effect code is enabled , The content of the control will follow the stretching of the form , Zoom in and adapt , As shown in the figure below :
Two 、 Realize the control to scale and fit with the form
2.1、 Control scales and fits with the form
The way of realization is :
① Get the width and height of the form first when initializing the form , then ;
② Traverse the form control to initialize and set the zoom position ;
③ Get the width and height of the current form when the form changes , Then traverse the form control to set .
2.2、 The core code that controls adapt to the form
#region The size of the control is scaled in proportion to the size of the form
private readonly float x; // Defines the width of the current form
private readonly float y; // Defines the height of the current form
private void setTag(Control cons)
{
foreach (Control con in cons.Controls)
{
con.Tag = con.Width + ";" + con.Height + ";" + con.Left + ";" + con.Top + ";" + con.Font.Size;
if (con.Controls.Count > 0) setTag(con);
}
}
private void setControls(float newx, float newy, Control cons)
{
// Traverse the controls in the form , Reset the value of the control
foreach (Control con in cons.Controls)
// Gets the Tag Property value , And store string array after segmentation
if (con.Tag != null)
{
var mytag = con.Tag.ToString().Split(';');
// Determine the value of the control according to the scale of the form
con.Width = Convert.ToInt32(Convert.ToSingle(mytag[0]) * newx); // Width
con.Height = Convert.ToInt32(Convert.ToSingle(mytag[1]) * newy); // Height
con.Left = Convert.ToInt32(Convert.ToSingle(mytag[2]) * newx); // The left margin
con.Top = Convert.ToInt32(Convert.ToSingle(mytag[3]) * newy); // Top margin
var currentSize = Convert.ToSingle(mytag[4]) * newy; // font size
if (currentSize > 0) con.Font = new Font(con.Font.Name, currentSize, con.Font.Style, con.Font.Unit);
con.Focus();
if (con.Controls.Count > 0) setControls(newx, newy, con);
}
}
/// <summary>
/// Reset form layout
/// </summary>
private void ReWinformLayout()
{
var newx = Width / x;
var newy = Height / y;
setControls(newx, newy, this);
}
#endregion
3、 ... and 、 Usage method
3.1、 Initialize the adapter in the form constructor
// Be careful :UIComponetForm Is the name of the form you need to adapt
public UIComponetForm()
{
InitializeComponent();
#region Initialize control scaling
x = Width;
y = Height;
setTag(this);
#endregion
}
3.2、 Reset zoom layout
private void UIComponetForm_Resize(object sender, EventArgs e)
{
// Reset window layout
ReWinformLayout();
}
By using C# Code implementation form control adaptation ( Adaptive forms ) The layout is finished , Run the project to see the effect .
边栏推荐
- 重金属行业B2B电商交易平台:打破数据孤立状态,提升重金属行业效益
- Learning records [email protected] Moveexecutionstosingleactivityid parallel gateway or inclusive gateway fallback
- 请问 PostgreSQL CDC 只能连接主库是吗?连接从库报错logical decodin
- 【黑马早报】马斯克:我已将大脑上传到云端;字节跳动确认自研芯片;趣店罗敏称被东方甄选董宇辉拉黑;世纪佳缘高管被刑拘...
- Kettle【实践 03】水经微图kml类型文件分类解析入库难点细节说明(完整流程实例云资源分享:包含sql+kjb+ktr+测试文件)
- 边云协同场景下基于强化学习的精英分层任务卸载策略研究
- C DataGridView data export excel file
- 和外星人如何交流
- Maybe it's the next generation data specification of WebGIS - OGC API series
- 短视频直播系统源码
猜你喜欢
Redis-Set类型
What is the role of GIS equipment in land and resources management?
重金属行业B2B电商交易平台:打破数据孤立状态,提升重金属行业效益
[postgraduate entrance examination vocabulary training camp] day 8 - complete, traditional, extra, aford, professional, required
Cookie和Session
Redis-数据结构&&通用命令
仍旧是同步问题
9 Redis压力测试redis-benchmark
配置静态路由和 RIP 路由协议
web安全入门-telent测试与防御
随机推荐
语雀—好用的文档编写、知识沉淀的工具
Query the space occupied by a table and the amount of data in the table
在 Business Application Studio 里使用 SAP UI5 应用消费 OData 的 Create 和 Delete 操作
Maybe it's the next generation data specification of WebGIS - OGC API series
智能隐形眼镜的研究进展与挑战
Redis-SortSet类型
利用神器Typora+PicGo,实现有道云不用会员就可在Markdown自动上传图片
Role of subnet mask
煤炭行业供应链集采系统:数字化推进煤炭产业转型升级
9 redis stress test redis benchmark
Some summary of QT | QWidget
C DataGridView data export excel file
Knowledge points of MySQL (11)
大数处理--使用案例
基于CLAR架构打造的宝马i3,并非是简单的“油改电”产品
STM32+DHT11读取温湿度数据显示
npm 和yarn的常用指令
一个诡异的 JedisConnectionException: Connection refused 问题
NepCTF
pycharm 写 sqlite 语句没有代码提示