|
|
其实winform并不适合做自适应这玩意的,虽然,能做是能做,
就像菜刀一样能切水果,但是,不是那么合适,毕竟有水果刀
wpf做自适应拉伸窗体,会更好一些。
代码:
- #region 自适应
- private Size m_szInit;//初始窗体大小
- private Dictionary<Control, Rectangle> m_dicSize
- = new Dictionary<Control, Rectangle>();
- protected override void OnLoad(EventArgs e)
- {
- m_szInit = this.Size;//获取初始大小
- this.GetInitSize(this);
- base.OnLoad(e);
- }
- private void GetInitSize(Control ctrl)
- {
- foreach (Control c in ctrl.Controls)
- {
- m_dicSize.Add(c, new Rectangle(c.locatio{过滤}n, c.Size));
- this.GetInitSize(c);
- }
- }
- protected override void OnResize(EventArgs e)
- {
- //计算当前大小和初始大小的比例
- float fx = (float)this.Width / m_szInit.Width;
- float fy = (float)this.Height / m_szInit.Height;
- foreach (var v in m_dicSize)
- {
- v.Key.Left = (int)(v.Value.Left * fx);
- v.Key.Top = (int)(v.Value.Top * fy);
- v.Key.Width = (int)(v.Value.Width * fx);
- v.Key.Height = (int)(v.Value.Height * fy);
- }
- base.OnResize(e);
- }
- #endregion
复制代码
|
上一篇:屏蔽右键、F12,避免查看源代码下一篇:C#实现数字字符串左补齐0的两种方法
|