gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-30 00:53:38 28 4
gpt4 key购买 nike

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

我一直在使用面板对控件进行分组(即面板包含文本框、标签、 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;
}

我想问的是:这样做正确吗?

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

最佳答案

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

相反,您应该在设计器中设置 AnchorDock 属性,这样它就会自动发生。

关于c# - 我是否使用 Paint 事件在运行时定位控件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15490757/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com