You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

咨询.NET Form.MinimumSize导致焦点变更问题的可行替代方案(Tradestation 10)

解决方案:TradeStation 10 EasyLanguage 设置 Form.MinimumSize 焦点丢失问题

已知这是.NET WinForms的原生bug,以下是替代方案:

方案1:手动在Resize事件中限制窗体最小尺寸

不直接设置Form.MinimumSize,而是通过监听窗体Resize事件,手动拦截尺寸过小的情况,强制恢复到目标最小尺寸:

method void Form1_Resize(sender: System.Object; e: System.EventArgs)
begin
    // 定义最小尺寸(宽300,高200)
    var minWidth = 300;
    var minHeight = 200;

    if (this.Width < minWidth) then
        this.Width = minWidth;
    if (this.Height < minHeight) then
        this.Height = minHeight;
end;

注意:需要在窗体初始化时绑定Resize事件,比如在Form_Load中添加:

this.Resize += new System.EventHandler(this.Form1_Resize);

方案2:延迟设置MinimumSize

利用异步调用延迟设置MinimumSize,避开窗体初始化阶段的焦点抢夺:

method void Form1_Load(sender: System.Object; e: System.EventArgs)
begin
    // 使用BeginInvoke延迟执行,让窗体先完成加载并获取焦点
    this.BeginInvoke(new System.Action(
        delegate()
        begin
            this.MinimumSize = new System.Drawing.Size(300, 200);
        end
    ));
end;

方案3:通过Windows API直接设置最小尺寸

绕过.NET的Form封装,调用原生Windows API来设置窗口最小尺寸,完全避开该bug:
首先需要导入API函数并处理窗口消息:

// 导入API函数
[DllImport("user32.dll")]
static method intptr SetWindowLongPtr(intptr hWnd, int nIndex, intptr dwNewLong);

[DllImport("user32.dll")]
static method intptr GetWindowLongPtr(intptr hWnd, int nIndex);

// 常量定义
const int WM_GETMINMAXINFO = 0x0024;

// 重写WndProc处理最小尺寸消息
method void WndProc(var m: System.Windows.Forms.Message)
begin
    if (m.Msg = WM_GETMINMAXINFO) then
    begin
        var minMaxInfo = cast(System.Windows.Forms.MINMAXINFO*)m.LParam;
        // 设置最小尺寸为宽300、高200
        minMaxInfo->ptMinTrackSize.x = 300;
        minMaxInfo->ptMinTrackSize.y = 200;
        m.Result = intptr.Zero;
        return;
    end;
    base.WndProc(m);
end;

注意:需确保在窗体类中正确定义MINMAXINFO结构体以适配EasyLanguage的类型要求。

内容的提问来源于stack exchange,提问作者Atom

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.14 21:17:37