Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net WinForm调整矩阵?_.net_Winforms - Fatal编程技术网

.net WinForm调整矩阵?

.net WinForm调整矩阵?,.net,winforms,.net,Winforms,WinForms(.NET 2)问题: 当调整父窗体(或面板)的大小时,是否有办法使元素保持成比例的距离 我可以在此范围内使用Graphics.TransformPoints或Graphics.TransformVectors?怎样 编辑: TableLayoutPanel将不起作用,因为应接受重叠的元素 编辑2: 这是我的代码: using System; using System.Collections.Generic; using System.Drawing; using System

WinForms(.NET 2)问题:

当调整父窗体(或面板)的大小时,是否有办法使元素保持成比例的距离

我可以在此范围内使用
Graphics.TransformPoints
Graphics.TransformVectors
?怎样

编辑:
TableLayoutPanel将不起作用,因为应接受重叠的元素

编辑2:
这是我的代码:

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
using Microsoft.VisualBasic.PowerPacks;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        List<Point> points;
        List<Point> shapePoints;
        Matrix m;
        float dx, dy;

        public Form1()
        {
            InitializeComponent();

            points = new List<Point>();
            shapePoints = new List<Point>();
            foreach (Control c in this.Controls)
            {
                points.Add(c.Location);
            }

            foreach (Shape s in this.shapeContainer1.Shapes)
            {
                if (s is SimpleShape)
                {
                    shapePoints.Add((s as SimpleShape).Location);
                }
                else if (s is LineShape)
                {
                    shapePoints.Add((s as LineShape).StartPoint);
                }
            }

            m = new Matrix();
            dx = this.Width;
            dy = this.Height;

            // this code will allow(?) do not move this control
            this.shapeContainer1.Dock = DockStyle.Fill;
        }

        protected override void OnSizeChanged(EventArgs e)
        {
            dx = this.Width / dx;
            dy = this.Height / dy;
            ApplyScale(dx, dy);

            dx = this.Width;
            dy = this.Height;

            base.OnSizeChanged(e);
        }

        private void ApplyScale(float dx, float dy)
        {
            //m.Reset();
            m.Scale(dx, dy);

            Point[] locations = points.ToArray();
            m.TransformVectors(locations);

            for (int i = 0; i < this.Controls.Count; i++)
            {
                this.Controls[i].Location = locations[i];
            }

            Point[] shapeLocations = shapePoints.ToArray();
            m.TransformVectors(shapeLocations);

            for (int i = 0; i < this.shapeContainer1.Shapes.Count; i++)
            {
                SimpleShape ss = this.shapeContainer1.Shapes.get_Item(i) 
                                                             as SimpleShape;
                if (ss != null)
                {
                    ss.Location = locations[i];
                    continue;
                }

                LineShape ls = this.shapeContainer1.Shapes.get_Item(i) 
                                                             as LineShape;
                if (ls != null)
                {
                    ls.StartPoint = locations[i];
                    ls.Scale(new SizeF(dx, dy));
                }
            }
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用系统图;
使用System.Drawing.Drawing2D;
使用System.Windows.Forms;
使用Microsoft.VisualBasic.PowerPacks;
命名空间Windows窗体应用程序1
{
公共部分类Form1:Form
{
列出要点;
列出形状点;
矩阵m;
浮动dx,dy;
公共表格1()
{
初始化组件();
点=新列表();
shapePoints=新列表();
foreach(此.Controls中的控件c)
{
点。添加(c.位置);
}
foreach(此.shapeContainer1.Shapes中的形状s)
{
如果(s)是SimpleShape
{
添加((作为SimpleShape.Location);
}
否则,如果(s为线型)
{
shapePoints.Add((作为线型).StartPoint);
}
}
m=新矩阵();
dx=该宽度;
dy=这个高度;
//此代码将允许(?)不移动此控件
this.shapeContainer1.Dock=DockStyle.Fill;
}
IzeChanged上的受保护覆盖无效(EventArgs e)
{
dx=此宽度/dx;
dy=这个高度/dy;
ApplyScale(dx,dy);
dx=该宽度;
dy=这个高度;
基地.OnSizeChanged(e);
}
专用void ApplyScale(浮点dx、浮点dy)
{
//m、 重置();
m、 刻度(dx,dy);
点[]位置=点。ToArray();
m、 转换向量(位置);
for(int i=0;i
这就是我得到的:

在表单上放置固定(或锚定)的表格布局面板,并将其所有列/行设置为百分比大小

然后,您可以将控件停靠在单元格中,当调整表格布局的大小时,单元格将保持比例

编辑:

对于重叠元素,您能否增加TLP中的列/行数以适应

如果减少列/行的数量,然后将子TLP添加到需要更多定位的单元格中?

这似乎可行: 在表单加载中,我为每个要调整大小的控件存储%-left和%-top。我将其存储在Tag属性中以便于访问。 然后在form resize事件中,我只需计算每个控件的新的%-left和%-top,并将它们定位出来

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    For Each c As Control In Me.Controls
        c.Anchor = AnchorStyles.None
        c.Tag = CInt((100 / Me.Width) * c.Left).ToString & "|" & CInt((100 / Me.Height) * c.Top).ToString
    Next
End Sub

Private Sub Form1_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize
    For Each c As Control In Me.Controls
        c.Location = New Point((Me.Width / 100) * CInt(Split(c.Tag, "|")(0)), (Me.Height / 100) * CInt(Split(c.Tag, "|")(1)))
    Next
End Sub
开始:

调整表单大小后:

我这里有每个单元格的元素限制。如果我有(一半)重叠的元素呢?查看更新后的图片。如果您没有其他想法,请删除您的答案,该问题将无法回答,因此更频繁:)感谢TLP,您不能有重叠的元素。您只是无法复制第一幅图像。是否希望网格按表单大小展开,将元素的x/y位置固定到网格上,而不是元素的宽度和高度?@Stefan:是的,就像它们在图形中一样。元素大小的任何更改。在线型上,您必须同时处理起点和终点…是的。。我改为使用了缩放…(元素的大小没有改变,只是因为我是如何复制它们并将它们保存为图片的…)我想当表单包含多个控件(比如>50)时,这应该是性能泄漏。如果你说的是数百个控件,然后,我将在绘制事件中将它们绘制为图形,而不是在控件周围移动。这很容易用另一种方法实现。请a)接受正确的答案。b) 请给出原因,以便我更正或撤回我的答案。如果您有关于性能的问题,请将其放入问题中,我将根据新标准修改我的答案。是否绘制控件?嗯。首先,我有,在其他人之间,文本框,其次,即使是我的矩形(圆圈)也应该回复事件,如单击,有工具提示等。关于将此作为答案。。。也许吧,但我会等待(片刻)另一种选择,如果它不出现,我会做。