Groups | Blog | Home
all groups > dotnet windows forms > august 2004 >

dotnet windows forms : How to embed old C++ window into .NET form?


Lei Jiang
8/6/2004 7:08:06 PM
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!

Lei Jiang
8/7/2004 12:14:45 PM

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?


"Lei Jiang" <code@263.net> дÈëÏûÏ¢
news:OQy$pW6eEHA.372@TK2MSFTNGP12.phx.gbl...
[quoted text, click to view]

Munir Husseini
8/27/2004 5:14:31 PM
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




"Lei Jiang" <code@263.net> schrieb im Newsbeitrag
news:OQy$pW6eEHA.372@TK2MSFTNGP12.phx.gbl...
[quoted text, click to view]

AddThis Social Bookmark Button