using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Reflection;
using System.Text.Json.Nodes;
using Sim.Host;
namespace System.Windows.Forms;
///
/// 仿真控件的基类。属性保存在本进程里;控件所在窗体显示后,属性变化以 patch 消息发给浏览器渲染。
/// Font、BackColor、ForeColor 是"环境属性":没有显式设置时沿用父控件的值,只把显式设置的值发给浏览器。
///
public class Control : Component, IWin32Window
{
private static readonly Font DefaultFontValue = new("宋体", 9F);
private string _name = "";
private string _text = "";
private Point _location;
private Size _size;
private bool _visible = true;
private bool _enabled = true;
private Color _backColor;
private Color _foreColor;
private Font? _font;
private bool _autoSize;
private DockStyle _dock;
private AnchorStyles _anchor = AnchorStyles.Top | AnchorStyles.Left;
private Control? _parent;
public Control()
{
SimId = SimHost.NewControlId();
SimHost.Register(this);
_size = DefaultSize;
Controls = CreateControlsInstance();
}
public Control(string text) : this() => _text = text ?? "";
/// 平台内部使用:控件在浏览器端的编号
[EditorBrowsable(EditorBrowsableState.Never)]
public string SimId { get; }
/// 为 true 时,从非界面线程修改已显示控件的属性会抛出异常(和在 VS 里按 F5 调试时一致)
public static bool CheckForIllegalCrossThreadCalls { get; set; } = true;
public static Color DefaultBackColor => Color.FromArgb(240, 240, 240);
public static Color DefaultForeColor => Color.Black;
public static Font DefaultFont => DefaultFontValue;
protected virtual Size DefaultSize => Size.Empty;
// ---------- 层级 ----------
public ControlCollection Controls { get; }
protected virtual ControlCollection CreateControlsInstance() => new(this);
public Control? Parent
{
get => _parent;
set
{
if (value == _parent) return;
_parent?.Controls.Remove(this);
value?.Controls.Add(this);
}
}
internal void SetParentInternal(Control? parent) => _parent = parent;
public Form? FindForm()
{
for (var c = _parent; c != null; c = c._parent)
if (c is Form form) return form;
return null;
}
/// 控件已经显示在浏览器里(所在窗体已打开)时,属性变化才需要发送
internal virtual bool IsAttached
{
get
{
var root = this;
while (root._parent != null) root = root._parent;
return root is Form { IsOpenInBrowser: true };
}
}
// ---------- 常用属性 ----------
public string Name
{
get => _name;
set => _name = value ?? "";
}
public virtual string Text
{
get => _text;
set
{
LegacyThreads.CheckPoint();
value ??= "";
if (_text == value) return;
EnsureThreadAccess();
_text = value;
NotifyChanged(nameof(Text), value);
OnTextChanged(EventArgs.Empty);
}
}
/// 来自浏览器输入的文字:只修改值并触发事件,不再回传给浏览器(避免打字时光标跳动)
internal void SetTextFromUser(string value)
{
if (_text == value) return;
_text = value;
OnTextChanged(EventArgs.Empty);
}
public Point Location
{
get => _location;
set => SetProp(ref _location, value, nameof(Location));
}
public Size Size
{
get => _size;
set => SetProp(ref _size, value, nameof(Size));
}
public int Left { get => _location.X; set => Location = new Point(value, _location.Y); }
public int Top { get => _location.Y; set => Location = new Point(_location.X, value); }
public int Width { get => _size.Width; set => Size = new Size(value, _size.Height); }
public int Height { get => _size.Height; set => Size = new Size(_size.Width, value); }
public int Right => Left + Width;
public int Bottom => Top + Height;
public Rectangle Bounds
{
get => new(_location, _size);
set
{
Location = value.Location;
Size = value.Size;
}
}
public virtual Size ClientSize
{
get => Size;
set => Size = value;
}
public bool Visible
{
get => _visible;
set
{
if (SetProp(ref _visible, value, nameof(Visible))) OnVisibleChanged(EventArgs.Empty);
}
}
public bool Enabled
{
get => _enabled;
set
{
if (SetProp(ref _enabled, value, nameof(Enabled))) OnEnabledChanged(EventArgs.Empty);
}
}
public virtual Color BackColor
{
get => !_backColor.IsEmpty ? _backColor : _parent?.BackColor ?? DefaultBackColor;
set => SetProp(ref _backColor, value, nameof(BackColor));
}
public virtual Color ForeColor
{
get => !_foreColor.IsEmpty ? _foreColor : _parent?.ForeColor ?? DefaultForeColor;
set => SetProp(ref _foreColor, value, nameof(ForeColor));
}
public virtual Font Font
{
get => _font ?? _parent?.Font ?? DefaultFont;
set => SetProp(ref _font, value, nameof(Font));
}
public virtual bool AutoSize
{
get => _autoSize;
set => SetProp(ref _autoSize, value, nameof(AutoSize));
}
public DockStyle Dock
{
get => _dock;
set => SetProp(ref _dock, value, nameof(Dock));
}
public AnchorStyles Anchor
{
get => _anchor;
set => SetProp(ref _anchor, value, nameof(Anchor));
}
// 以下属性只保存值,暂不影响浏览器显示
public int TabIndex { get; set; }
public bool TabStop { get; set; } = true;
public object? Tag { get; set; }
public Padding Margin { get; set; } = new(3);
public Padding Padding { get; set; }
public ImeMode ImeMode { get; set; } = ImeMode.NoControl;
public virtual RightToLeft RightToLeft { get; set; } = RightToLeft.No;
public virtual bool AllowDrop { get; set; }
public bool CausesValidation { get; set; } = true;
public bool Focused { get; private set; }
[EditorBrowsable(EditorBrowsableState.Never)]
public IntPtr Handle => new(int.Parse(SimId));
public bool IsHandleCreated => IsAttached;
public bool IsDisposed { get; private set; }
// ---------- 事件 ----------
public event EventHandler? Click;
public event EventHandler? DoubleClick;
public event MouseEventHandler? MouseClick;
public event MouseEventHandler? MouseDown;
public event MouseEventHandler? MouseUp;
public event EventHandler? MouseEnter;
public event EventHandler? MouseLeave;
public event EventHandler? TextChanged;
public event EventHandler? VisibleChanged;
public event EventHandler? EnabledChanged;
public event EventHandler? GotFocus;
public event EventHandler? LostFocus;
public event EventHandler? Enter;
public event EventHandler? Leave;
protected virtual void OnClick(EventArgs e) => Click?.Invoke(this, e);
protected virtual void OnDoubleClick(EventArgs e) => DoubleClick?.Invoke(this, e);
protected virtual void OnMouseClick(MouseEventArgs e) => MouseClick?.Invoke(this, e);
protected virtual void OnMouseDown(MouseEventArgs e) => MouseDown?.Invoke(this, e);
protected virtual void OnMouseUp(MouseEventArgs e) => MouseUp?.Invoke(this, e);
protected virtual void OnMouseEnter(EventArgs e) => MouseEnter?.Invoke(this, e);
protected virtual void OnMouseLeave(EventArgs e) => MouseLeave?.Invoke(this, e);
protected virtual void OnTextChanged(EventArgs e) => TextChanged?.Invoke(this, e);
protected virtual void OnVisibleChanged(EventArgs e) => VisibleChanged?.Invoke(this, e);
protected virtual void OnEnabledChanged(EventArgs e) => EnabledChanged?.Invoke(this, e);
protected virtual void OnGotFocus(EventArgs e) => GotFocus?.Invoke(this, e);
protected virtual void OnLostFocus(EventArgs e) => LostFocus?.Invoke(this, e);
protected virtual void OnEnter(EventArgs e) => Enter?.Invoke(this, e);
protected virtual void OnLeave(EventArgs e) => Leave?.Invoke(this, e);
/// 平台内部使用:在界面线程上处理浏览器上报的用户操作
[EditorBrowsable(EditorBrowsableState.Never)]
public void PerformUserAction(UserAction action)
{
if (!Enabled || !Visible) return;
OnUserAction(action);
}
/// 按真实 WinForms 的顺序,把一个用户操作翻译成事件
protected virtual void OnUserAction(UserAction action)
{
switch (action.Action)
{
case "click":
var args = new MouseEventArgs(MouseButtons.Left, 1, action.X, action.Y, 0);
OnClick(args);
OnMouseClick(args);
break;
case "doubleClick":
OnDoubleClick(EventArgs.Empty);
break;
case "mouseDown":
OnMouseDown(new MouseEventArgs(ToMouseButtons(action.Button), 1, action.X, action.Y, 0));
break;
case "mouseUp":
OnMouseUp(new MouseEventArgs(ToMouseButtons(action.Button), 1, action.X, action.Y, 0));
break;
}
}
private static MouseButtons ToMouseButtons(string button) => button == "right" ? MouseButtons.Right : MouseButtons.Left;
// ---------- 方法 ----------
public void Show() => Visible = true;
public void Hide() => Visible = false;
public bool Focus() => true;
public void Select() { }
public void BringToFront() { }
public void SendToBack() { }
public void Refresh() { }
public void Invalidate() { }
public void Update() { }
public void SuspendLayout() { }
public void ResumeLayout() { }
public void ResumeLayout(bool performLayout) { }
public void PerformLayout() { }
public bool InvokeRequired => !SimHost.Ui.IsUiThread;
public object? Invoke(Delegate method) => Invoke(method, Array.Empty