在 win32 应用程序中使用 c++/clr 创建 c#(WinFrom) 控件

问题描述 投票:0回答:1

我有两个项目,一个是win32程序,另一个是c++/cli动态库。

在c++/cli动态库中,导出这个函数:

using namespace System::Windows::Forms;
using namespace System::Diagnostics;

LIB_API void Create(HWND parent){
    auto c = gcnew TextBox();
    c->Location = System::Drawing::Point(0, 0);
    c->Name = "textBox1";
    c->Size = System::Drawing::Size(100, 21);
    c->Parent = Control::FromHandle(IntPtr(parent));
}

当win32程序调用该函数时,win创建一个

TextBox
控件。 但这不起作用。

调试日志显示

TextBox
已创建,但使用spy++找不到它。

c# c++-cli
1个回答
0
投票

我自己解决了这个问题。

using namespace System::Windows::Forms;
using namespace System::Diagnostics;

LIB_API void Create(HWND parent){
    auto c = gcnew TextBox();
    //c->Location = System::Drawing::Point(0, 0);
    c->Name = "textBox1";
    //c->Size = System::Drawing::Size(100, 21);
    //c->Parent = Control::FromHandle(IntPtr(parent));
    ::SetParent((HWND)c->Handle.ToPointer(), parent);
    ::MoveWindow((HWND)c->Handle.ToPointer(), 0, 0, 200, 200, true);
}
``
© www.soinside.com 2019 - 2024. All rights reserved.