问题
Basically, I want everything (Controls etc) to scale with the WindowsForm when resizing it by dragging, so that the user can determine the size of the UI himself. The picture is not perfect, but I hope it explains it:
The easiest way to do this would be to use a (Flow/Table)LayoutPanel and the appropriate Anchor/Drop properties, but I feel like that restricts my design, which currently looks like this:
My idea was to scale all the components when Resize() is called:
foreach(Control c in Controls){
c.Scale(scaleFactor);
}
My problems are: Locations aren't set properly and the rounding needed to determine the 'scaleFactor' leads to inconsistencies.
Is there a clean way to do all this? Do I have to use LayoutPanels to get a clean way?
回答1:
Are you sure that you want to zoom those text boxes and labels, too? Text boxes have a fairly standard height, for example. What do you expect if the window is so small that the text is too large for the buttons/text boxes, etc.?
The typical resizing logic of the contents of a resizable window is a little bit different. I would rather create a borderless panel for the groupboxes and stack/dock everything like this:
If I resize this window, that will look like this:
Btw, I do not like if buttons are resized like this. I would use only Anchor = Left, Right
for them so they would preserve their height. And do not forget to set a proper MinimumSize
for the form.
回答2:
Simplest and cleanest way is use layout panles.
It's hard to code location of controls which depends of previous control's location and size (expect case when you know order of controls).
In WPF everything are on ContentControl which is layout panel. Location of control is calculated automatically.
Another problem might be scaling font or glyph icon in combobox. In WPF this simplest and supported.
来源:https://stackoverflow.com/questions/33911313/scale-windowsform