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

Delphi 10.3.3拖拽程序在4K触摸屏触发浮点运算错误求助

解决方案:Delphi 10.3.3触摸拖拽触发浮点运算错误

问题根源

Delphi 10.3.3的VCL框架在Per Monitor v2 DPI感知模式下,触摸操作触发的拖拽流程中,内部坐标转换或ItemAtPos的计算逻辑存在浮点运算异常,尤其在4K高DPI触摸屏场景下容易触发。

修复步骤

1. 替换原生BeginDrag为系统API DragDetect

原生BeginDrag在触摸场景下兼容性不足,改用Windows系统API的DragDetect启动拖拽,能更好适配触摸输入逻辑:

修改ListBoxMouseDown事件代码:

procedure TForm1.ListBoxMouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
var
  ListBox: TListBox;
  ItemIdx: Integer;
begin
  if Button = mbLeft then
  begin
    ListBox := Sender as TListBox;
    ItemIdx := ListBox.ItemAtPos(Point(X, Y), True);
    if ItemIdx >= 0 then
    begin
      ListBox.ItemIndex := ItemIdx; // 确保选中项状态正确
      // 用系统DragDetect替代BeginDrag,兼容触摸输入
      if DragDetect(ListBox.Handle, Point(X, Y)) then
        ListBox.BeginDrag(False);
    end;
  end;
end;

2. 全局禁用浮点异常(应急方案)

如果上述方法无效,可在程序启动时全局禁用浮点运算异常,阻止错误弹窗触发:

在项目主文件(Project1.dpr)中添加:

uses
  System.SysUtils,
  Vcl.Forms,
  Unit1 in 'Unit1.pas' {Form1};

{$R *.res}

begin
  // 禁用浮点运算异常
  Set8087CW($133F);
  Application.Initialize;
  Application.MainFormOnTaskbar := True;
  Application.CreateForm(TForm1, Form1);
  Application.Run;
end.

3. 优化DPI感知清单配置

补充明确的DPI感知配置,确保VCL的缩放逻辑正确:

<application xmlns="urn:schemas-microsoft-com:asm.v3">
  <windowsSettings>
    <dpiAware xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">True/PM</dpiAware>
    <dpiAwareness xmlns="http://schemas.microsoft.com/SMI/2016/WindowsSettings">PerMonitorV2, PerMonitor</dpiAwareness>
  </windowsSettings>
</application>

额外检查项

  • 确认所有TListBox的DragMode属性设置为dmManual(与手动触发拖拽的逻辑匹配)
  • 测试时关闭系统触摸手势、笔输入等可能干扰触摸操作的设置

内容的提问来源于stack exchange,提问作者Michiel van Vaardegem

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.25 05:25:12