我是否使用Paint事件在运行时定位控件?

时间:2013-03-19 02:59:21

标签: c# .net winforms resize controls

我对来自VB6的C#编程相当陌生,所以请保持温和:P

我一直在使用Panel来对控件进行分组(即Panel包含Textbox,Labels,Listview等),然后让面板在运行时对齐,以便控件以不同的分辨率对齐。但是,我是通过Panel的Paint例程(?)

来完成的

即:

private void pnlTop_Paint(object sender, PaintEventArgs e)
    {
        btnExit.Location                = new Point(this.Width - (this.Left + lblTitleMain.Left + btnExit.Width), 10);
        btnMinimize.Location            = new Point(this.Width - (this.Left + lblTitleMain.Left + (btnExit.Width * 2)), 10);
        btnSettings.Location            = new Point(this.Width - (this.Left + lblTitleMain.Left + (btnExit.Width * 2 + btnExit.Width)), 10);

        lblTitleMain.Left               = (((this.ClientSize.Width - lblTitleMain.Width) / 2) / 2) / 2;
        lblTitleMain.Top                = btnExit.Top + lblTitleMain.Height;

        int intMenuY                    = lblTitleMain.Bottom + 5;
        lnkMenuSystem.Location          = new Point(lblTitleMain.Left + 3, intMenuY);
        lnkMenuDeployment.Location      = new Point(lnkMenuSystem.Right + 50, intMenuY);
        lnkMenuTables.Location          = new Point(lnkMenuDeployment.Right + 50, intMenuY);
        lnkMenuTCP.Location             = new Point(lnkMenuTables.Right + 50, intMenuY);
        lnkMenuDCM.Location             = new Point(lnkMenuTCP.Right + 50, intMenuY);
        lnkMenuProcessData.Location     = new Point(lnkMenuDCM.Right + 50, intMenuY);
        lnkMenuGenerateReports.Location = new Point(lnkMenuProcessData.Right + 50, intMenuY);

        lineMenuButtom.StartPoint       = new Point((((this.ClientSize.Width - lblTitleMain.Width) / 2) / 2) / 2, lnkMenuSystem.Top + lnkMenuSystem.Height + 10);
        lineMenuButtom.EndPoint         = new Point(this.Width - (this.Left + lblTitleMain.Left), lnkMenuSystem.Top + lnkMenuSystem.Height + 10);

        lnkMenuErrorMessage.Location    = new Point(lnkMenuSystem.Left, lineMenuButtom.Y1+5);
        lnkMessageWelcome.Location      = new Point(lineMenuButtom.X2 - lnkMessageWelcome.Width, lineMenuButtom.Y2 + 5);
        GlobalVariables.intGeneralLeft  = lineMenuButtom.StartPoint.X;
        GlobalVariables.intGeneralWidth = lineMenuButtom.X2;
    }

我想问的是:这是正确的方法吗?

原因是因为我不确定一旦应用程序在旧系统上运行,这是否会影响性能(假设它将在XP中使用2g RAM Pentium 4 HT或同等系统)。

1 个答案:

答案 0 :(得分:6)

没有;这当然是错误的做法。
Paint可以经常发射;你应该做尽可能少的工作。 (你当然不应该修改布局)

相反,您应该在设计器中设置AnchorDock属性,以便它们自动完成。

相关问题