12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364 |
- using System;
- using System.Collections.Generic;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- namespace GUI.GUIUtils
- {
- public class GUIParagraph : ISizeChangedListener
- {
- // 不要长期持有WinForm控件的引用(不要长期持有拥有非托管资源和/或拥有大量托管资源的对象,为了防止环状引用,在其销毁时立即释放(一代GC))
- WeakReference<FlowLayoutPanel> reference;
- public TargetXY Target { get; set; } = TargetXY.X;
- public int SplitEdge { get; set; } = 10;
- public enum TargetXY { X, Y }
- public Action<Control, FlowLayoutPanel> FixedPaneReferenceFunc;
- public GUIParagraph(FlowLayoutPanel weak_ref)
- {
- reference = new WeakReference<FlowLayoutPanel>(weak_ref);
- }
- public void OnSizeChanged()
- {
- if (reference.TryGetTarget(out FlowLayoutPanel pane))
- {
- var selectVal = Target switch
- {
- TargetXY.X => pane.Width,
- TargetXY.Y => pane.Height,
- _ => throw new ArgumentOutOfRangeException(nameof(Target)),
- };
- int selectSize = selectVal - SplitEdge * 2;
- selectSize /= pane.Controls.Count;
- for (int i = 0; i < pane.Controls.Count; i++)
- {
- var control = pane.Controls[i];
- switch (Target)
- {
- case TargetXY.X:
- {
- control.Size = new Size(selectSize, control.Size.Height);
- control.Location = new Point(SplitEdge + i * (selectSize), control.Location.Y);
- }
- break;
- case TargetXY.Y:
- {
- control.Size = new Size(control.Size.Width, selectSize);
- control.Location = new Point(control.Location.X, SplitEdge + i * selectSize);
- }
- break;
- }
- FixedPaneReferenceFunc?.Invoke(control, pane);
- }
- }
- }
- }
- }
|