Files
IOT_SIM/runtime/Sim.WinForms/Forms/Containers.cs
T
xuyueandClaude Opus 5.5 d60868d912 停靠布局、背景图片、无边框窗体;运行区按比例缩小、可放大
- Dock:和 WinForms 一样按 Controls 倒序依次停靠,扣掉 Panel 边框、GroupBox 标题占的地方;
  改大小、显示隐藏、加入移出控件时重新布局;SuspendLayout / ResumeLayout / PerformLayout
- 设计器里每次修改后按同样的规则排好停靠的控件(designer/layout.ts);属性窗口加"停靠"
- BackgroundImage + BackgroundImageLayout(平铺、居中、拉伸、缩放):窗体、面板、图片框,
  设计器里从项目资源选;无边框窗体没有标题栏,大小不含边框
- 背景色沿用父控件:没设背景色的标签、面板、分组框等画父控件的背景色(不是背景图片),
  和 WinForms 一样;分组框标题处边框断开、透出背景;文本框的 BackColor 生效
- 图片框可以放子控件,把鼠标按下、松开发给程序
- 运行区:窗体比运行区大时整体按比例缩小并显示比例;"放大"占满整个网页(窗体在左、实训台在右),Esc 还原
- Paint 事件:VS 双击面板生成的空 panel1_Paint 能编译(平台由浏览器绘制,不触发)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-23 15:14:34 +08:00

86 lines
2.3 KiB
C#

using System.Drawing;
using System.Text.Json.Nodes;
namespace System.Windows.Forms;
public class ScrollableControl : Control
{
public bool AutoScroll { get; set; }
}
public class ContainerControl : ScrollableControl
{
public SizeF AutoScaleDimensions { get; set; }
public AutoScaleMode AutoScaleMode { get; set; } = AutoScaleMode.Inherit;
public Control? ActiveControl { get; set; }
}
public class UserControl : ContainerControl
{
protected override Size DefaultSize => new(150, 150);
public event EventHandler? Load;
protected virtual void OnLoad(EventArgs e) => Load?.Invoke(this, e);
private bool _loaded;
/// <summary>UserControl 的 Load 在第一次随窗体显示时触发</summary>
internal void RaiseLoadOnce()
{
if (_loaded) return;
_loaded = true;
OnLoad(EventArgs.Empty);
}
}
public class Panel : ScrollableControl
{
private BorderStyle _borderStyle;
protected override Size DefaultSize => new(200, 100);
public BorderStyle BorderStyle
{
get => _borderStyle;
set
{
if (SetProp(ref _borderStyle, value, nameof(BorderStyle))) PerformLayout();
}
}
/// <summary>边框占掉的宽度:单线 1 像素,立体 2 像素</summary>
public override Rectangle DisplayRectangle
{
get
{
var inset = _borderStyle switch { BorderStyle.FixedSingle => 1, BorderStyle.Fixed3D => 2, _ => 0 };
var rect = base.DisplayRectangle;
return new Rectangle(rect.X, rect.Y, Math.Max(0, rect.Width - 2 * inset), Math.Max(0, rect.Height - 2 * inset));
}
}
protected override void WriteProps(JsonObject props)
{
base.WriteProps(props);
props["BorderStyle"] = _borderStyle.ToString();
}
}
public class GroupBox : Control
{
protected override Size DefaultSize => new(200, 100);
public FlatStyle FlatStyle { get; set; } = FlatStyle.Standard;
/// <summary>分组框的边框和标题占掉的地方(宋体 9pt 时和 WinForms 一致)</summary>
public override Rectangle DisplayRectangle
{
get
{
var rect = base.DisplayRectangle;
return new Rectangle(rect.X + 3, rect.Y + 17, Math.Max(0, rect.Width - 6), Math.Max(0, rect.Height - 20));
}
}
}