Search code examples
c#c++-cli

create c#(WinFrom) control in win32 app use c++/clr


I have two projects, one is a win32 program and other is a c++/cli dynamic library.

in c++/cli dynamic library,it export this function:

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));
}

when the win32 program call this function, will create a TextBox control. but it's not work.

The debug log shows that the TextBox was created,but use spy++ can't find it.


Solution

  • I solved the problem myself.

    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);
    }