4

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

我一直在使用面板对控件进行分组(即面板包含文本框、标签、列表视图等),然后让面板在运行时自行对齐,以便控件以不同的分辨率对齐。但是,我是从面板的 Paint 例程中执行此操作的(?)

IE:

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 或同等系统)。

4

1 回答 1

6

不; 这当然是错误的做法。
Paint可以经常开火;你应该尽可能少地做这件事。(你当然不应该修改布局)

相反,您应该在设计器中设置AnchorDock属性,以便一切都自动发生。

于 2013-03-19T03:00:41.000 回答