解决 Winform 上调整控件大小时的舍入错误
我需要在调整表单大小时调整表单上所有控件的大小。 不幸的是,由于某些问题,我无法使用任何容器控件或使用锚点/停靠属性。我需要使用代码来完成所有事情。 我可以根据屏幕大小调整的比例来调整控件的大小。在执行此调整大小时,我需要计算所有控件的新位置以及新大小。问题是在计算这些新点时,我得到了十进制的新值,如果我对它们进行四舍五入,那么它会导致问题,并在每次调整表单大小时计算错误的大小和位置。 我看到一些第三方控件执行相同的任务,但它们都准确计算位置。
请帮我解决这个问题。 提前致谢, 阿什什·夏尔马
I have a requirement to resize all the controls on a form when my form resizes.
Unfortunately, due to some issues I can not use any container control or make use of anchor/dock property. I need to do everything using code.
I am able to resize the controls as per the ratio the screen is resized. While performing this resizing, I need to calculate the new position of all the controls along with there new size. The issue is while calculating these new points I am getting the new values in decimal and if I round them then it is causing the problems and calculating wrong size and position each time I resize my form.
I saw some third party controls for the same task but they all calculate the position exactly.
Please help me resolve this issue.
Thanks in advance,
Ashish Sharma
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是 C#/Windows 窗体吗?
手动缩放是一件令人头疼的事情。实际上,在表单的长期生命周期中完全防止任何重大舍入错误的唯一方法是存储您重新定位/调整大小的每个控件的原始位置和大小,并使所有重新定位/调整大小代码相对于原始位置与目前的立场相反。
你最终会得到一些像这样的混乱:
...显然你必须自己实现
UpdatePosition
和UpdateSize
方法 - 我假设你已经有某种实施。老实说,这真是太糟糕了。我强烈建议您不要尝试执行任何此操作,而是重新审视阻止您使用
Anchor
和Dock的任何要求/设计限制
属性。 在我这些年里,我认为我从未听说过不使用这些或布局控件TableLayoutPanel
/的合理理由。 FlowLayoutPanel
或两者兼而有之。这些是在 WinForms 中处理布局的方式;其他任何事情都是笨拙的黑客。Is this C#/Windows Forms?
Manual scaling is a pain in the neck. Realistically the only way to completely prevent any major rounding errors over the long-term lifetime of a form is to store the original locations and sizes of every control that you reposition/resize, and make all your reposition/resize code relative to the original position as opposed to the current position.
You'll end up with some fugliness like this:
...Where obviously you would have to implement the
UpdatePosition
andUpdateSize
methods on your own - I'm assuming you already have some sort of implementation.Honestly, it's pretty god-awful. I would strongly recommend that instead of trying to do any of this, you revisit whatever requirements/design constraints are preventing you from using the
Anchor
andDock
properties. In all my years, I don't think I've ever heard of a legitimate reason for not using either these or the layout controlsTableLayoutPanel
/FlowLayoutPanel
or both. Those are the way to deal with layouts in WinForms; anything else is a clumsy hack.