How to embed old C++ window into .NET form?

L

Lei Jiang

I have a drawing application written in C++, and want to use it in a
framework written in C#. Since the amount of C++ code is very large, it's
unpractical to translate them to C#. So I want to use the window in C++ as a
control in C#. It seems there are a lot of method to do this :

1. Rebuild the C++ code in C++/CLI, and try to call it in managed world.
2. Pass the HWND of a C# form to the C++ code, and attach it to a C++ Window
and draw on it.
3. Pass the HWND of a C++ window to C# code, and attach it to a C# form and
draw on it.
4. Pass the HDC of the C# form to the C++ code, and draw on that DC.
5. ...

I think this is a common problem in porting old C++ code to C#, so there
must be someone has already solved this problem. Could anyone give me some
advice?

Thanks!
 
L

Lei Jiang

A problem is : how to bind a HWND to a control?

For example : if I create a window in C++, could I take the window handle
and bind it to a control?
 
M

Munir Husseini

Hi,

generally speaking I'd say that, depending on how often you will use your
C++ code, it's a good idea to rewrite the C++ code to managed C++.
Unfortunately I can't tell you how big the effort is.

Points 2 and 4 ("Pass the HWND of a C# form to the C++ code, and attach it
to a C++ Window
and draw on it", "Pass the HDC of the C# form to the C++ code, and draw on
that DC") sound reasonable, too, and would be my favorite if you need a
quick and dirty solution. Note that you don't have to attach anything to the
HWND because you can get it's DC (how can you attach a window to an existing
HWND, anyway?). I'm not sure if there's a security or authentication issue
there becuase you'll have to use the InteropServices namespace though.

To point 3 ("Pass the HWND of a C++ window to C# code, and attach it to a C#
form and draw on it."): Again, how can you attach an exsiting HWND to a
window?

Hope this helps. Good luck.

Munir Husseini
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top