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

Citrix环境下VSTO Excel插件WPF窗体跨显示器拖动后空白的解决方法

解决Citrix环境下VSTO Excel加载项WPF窗体切换屏幕后空白的问题

以下是针对该问题的几种有效解决方案:

  • 强制WPF控件适配当前DPI
    Citrix环境切换屏幕时,显示设备的DPI可能发生变化,导致WPF控件渲染异常。可在WpfContainerForm的构造函数中添加DPI适配逻辑:

    // 在初始化objPrepareView后添加
    var presentationSource = PresentationSource.FromVisual(objPrepareView);
    if (presentationSource != null)
    {
        var dpiScaleX = presentationSource.CompositionTarget.TransformToDevice.M11;
        var dpiScaleY = presentationSource.CompositionTarget.TransformToDevice.M22;
        objPrepareView.LayoutTransform = new ScaleTransform(dpiScaleX, dpiScaleY);
    }
    

    也可在显示窗体前先触发一次布局更新:

    prepareViewForm.Show();
    objPrepareView.UpdateLayout();
    prepareViewForm.Hide();
    var result = prepareViewForm.ShowDialog();
    
  • 基于Excel窗口所在屏幕手动定位窗体
    避免CenterParent在跨屏幕场景下的定位偏差,改为获取Excel窗口所在屏幕并计算居中位置:

    var excelWindowHandle = Globals.ThisAddIn.Application.MainWindowHandle;
    var targetScreen = Screen.FromHandle(excelWindowHandle);
    
    prepareViewForm.StartPosition = System.Windows.Forms.FormStartPosition.Manual;
    int posX = targetScreen.Bounds.Left + (targetScreen.Bounds.Width - prepareViewForm.Width) / 2;
    int posY = targetScreen.Bounds.Top + (targetScreen.Bounds.Height - prepareViewForm.Height) / 2;
    prepareViewForm.Location = new System.Drawing.Point(posX, posY);
    
  • 禁用WPF硬件加速
    Citrix虚拟显示环境常与WPF硬件加速存在兼容问题,可全局或针对单个窗体禁用:

    // 全局禁用(建议在WPF控件初始化前添加)
    System.Windows.Media.RenderOptions.ProcessRenderMode = System.Windows.Media.RenderMode.SoftwareOnly;
    
    // 或针对单个WPF窗体禁用
    var windowInteropHelper = new System.Windows.Interop.WindowInteropHelper(objPrepareView);
    var hwndSource = System.Windows.Interop.HwndSource.FromHwnd(windowInteropHelper.Handle);
    if (hwndSource != null)
    {
        hwndSource.CompositionTarget.RenderMode = System.Windows.Media.RenderMode.SoftwareOnly;
    }
    
  • 监听容器Resize事件触发重绘
    屏幕切换时WinForms容器会触发Resize事件,此时手动强制WPF控件重绘:

    prepareViewForm.Resize += (sender, e) =>
    {
        objPrepareView.InvalidateVisual();
        objPrepareView.UpdateLayout();
    };
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.23 23:05:14