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

使用COM组件在WPF中托管Delphi控件:按钮等控件不显示求助

Delphi控件WPF托管部分控件不显示问题排查

我通过ActiveX Library创建了Delphi控件,并借助COM类型库将其托管至WPF应用中。目前仅能正常渲染Delphi端的Label文本与背景模板颜色,Button、DatePicker、Checkbox等控件均无法显示。我采用HwndHost实现托管,相关代码如下:

示例Delphi UI

示例Delphi UI

托管至WPF后的效果

托管至WPF后的效果

WPF托管代码(HwndHost实现)

public class DelphiControlHost : HwndHost
{

    [DllImport("user32.dll")]
    static extern IntPtr CreateWindowEx(int dwExStyle, string lpszClassName, string lpszWindowName, int style, int x, int y, int width, int height, IntPtr hWndParent, IntPtr hMenu, IntPtr hInstance, IntPtr lpParam);

    [DllImport("user32.dll")]
    static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);

    [DllImport("user32.dll")]
    static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

    [DllImport("user32.dll")]
    static extern bool DestroyWindow(IntPtr hWnd);

    int height;
    int width;
    IntPtr child;

    public DelphiControlHost(double initialWidth, double initialHeight, IntPtr hostedControl)
    {
        width = (int)initialWidth;
        height = (int)initialHeight;
        child = hostedControl;
    }

    protected override HandleRef BuildWindowCore(HandleRef hwndParent)
    {
        
        var host = CreateWindowEx(0, "static", null, 0x40000000 | 0x10000000, 0, 0, height, width, 
            hwndParent.Handle, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero);
      
        SetParent(child, host);
        ShowWindow(child, 5);
        return new HandleRef(this, host);
    }

    protected override void DestroyWindowCore(HandleRef hwnd)
    {
        DestroyWindow(hwnd.Handle);
    }
}

MainWindow.xaml.cs代码

public partial class MainWindow : Window
{
    [DllImport("msvcr110.dll", CallingConvention = CallingConvention.Cdecl)]
    public static extern int _fpreset();

    private ProFileUXCOMImpl service;

    public MainWindow()
    {
        _fpreset();
        InitializeComponent();
        MainWindow_OnActivated(null,null);
    }

    private void MainWindow_OnActivated(object sender, EventArgs e)
    {
        if (service != null)
            return;
        service = new ProFileUXCOMImpl();
        // var control = service.GetMainFrame(DelphiHostElement.ActualWidth, DelphiHostElement.ActualHeight);
        var control = service.GetMainFrame(600, 300);
        DelphiHostElement.Child = new DelphiControlHost(DelphiHostElement.ActualWidth, DelphiHostElement.ActualHeight, control);
    }

    private void MainCanvas_OnSizeChanged(object sender, SizeChangedEventArgs e)
    {
        service?.WindowResized();
    }
}

Delphi端代码片段

unit ProFileUXCOMU;

interface

uses
  SysUtils, ComObj, ComServ, ProFileUXCOM_TLB, Winapi.ActiveX, StdVcl,Vcl.Forms, Winapi.Windows,
  MainFormU, MainFrameU;

type

  ProFileUXCOMImpl = class(TComObject, IProFileUXCOM)
  private
    MainFrame: TMainFrame;
    MainHandle: Cardinal;
    MainPointer: Pointer;
    MainForm: TMainForm;
  protected
    function GetMainFrame(width: Double; height: Double): Pointer; stdcall;
    procedure WindowResized(width: Double; height: Double); safecall;  procedure IProFileUXCOM.WindowResized = IProFileUXCOM_WindowResized;

    procedure IProFileUXCOM_WindowResized; safecall;
  end;

implementation

function ProFileUXCOMImpl.GetMainFrame(width: Double; height: Double): Pointer; stdcall;
begin
  MainForm := TMainForm.Create(Application);
  MainForm.ClientWidth := Trunc(width);
  MainForm.ClientHeight := Trunc(height);
  MainHandle := MainForm.Frame.Handle;
  MainPointer := System.Pointer(MainHandle);
  Result := MainPointer;
end;

procedure ProFileUXCOMImpl.WindowResized(width: Double; height: Double); safecall;
begin
  MainForm.ClientWidth := Trunc(width);
  MainForm.ClientHeight := Trunc(height);
end;

procedure ProFileUXCOMImpl.IProFileUXCOM_WindowResized;
begin

end;

initialization

  TComObjectFactory.Create(ComServer, ProFileUXCOMImpl, CLASS_ProFileUXCOMImpl, 'ProFileUXCOMImpl', '', ciMultiInstance, tmApartment);

end.

恳请各位帮忙排查是否存在遗漏配置或代码问题?

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.19 00:01:39