views:

517

answers:

4

Hello,

I've an app written C & C++. Now, I need to provide a GUI for this app. MFC is the best option for me. But I'm not familiar with MFC.

So can I use .NET to build GUI for this? If so, How? Please be clear.

If I can use .NET I guess I can use WPF too right?

+5  A: 

You can technically write a GUI in C++/CLI, but I would highly discourage it. C++/CLI is good for writing .NET wrappers around native C++ and exposing it to other .NET languages, but not much else.

In your case, if you're really set on using WinForms/WPF, then I would suggest using C++/CLI to create a wrapper around your C++ code and then building the actual GUI in C#.

Otherwise, a C++ library like Qt or wxWidgets would also suffice for doing a "native" C++ GUI.

Here's a quick introduction to C++/CLI. This is a quick-start guide for getting started with C++/CLI. Once you build a C++/CLI DLL, you can just add it as a reference to your C# project and It Just Works(tm).

Dean Harding
claws
MFC has nothing to do with either C# or Qt. Qt is an *alternative* to MFC. You cannot use MFC from C#, but C# has WinForms which is much, much better than MFC from a productivity standpoint.
Dean Harding
claws
Oh right, sorry... http://www.codeproject.com/KB/mcpp/quickcppcli.aspx This is a quick-start guide for getting started with C++/CLI. Once you build a C++/CLI DLL, you can just add it as a reference to your C# project and It Just Works(tm). For the Qt/wxWidgets/MFC thing, my *personal* preference would be wxWidgets over Qt, since Qt is a bit heavy on the pre-processing for my taste. But that's personal opinion. IMO, both are light-years ahead of MFC.
Dean Harding
light years is not a unit of time but unit of distance. :P
claws
Dean Harding
It's a unit of time if you can relate the speed at which you work to the speed of light :P
jeffora
@codeka : Please include your codeproject comment in the answer so that I can mark it as answer.
claws
@claws: done :)
Dean Harding
+3  A: 

Organize the C++ app as a "server", exporting functions which can be called by a GUI "client". Build this C++ code as a DLL, exporting said functions. Create your GUI app as a .NET EXE and let it call said functions in your DLL using Platform Invoke (P/Invoke).

Conrad Albrecht
claws
Creating DLLs and exporting functions from them is covered in the Visual C++ docs. There's a whole section on using P/Invoke to call native DLLs from .NET in the .NET SDK.
Conrad Albrecht
+1  A: 

Depends on if you want to learn another language? If you choose the C++/CLI or C# route you will have to get familiar with those languages before you even start with your GUI. Yeah MFC isn't the greatest but at least you can still use C++ with it. And that seems to be what Microsoft wants you to use since most of their video tutorials are MFC based: http://msdn.microsoft.com/en-us/visualc/bb693459.aspx

If you choose the WinForms C++/CLI route Visual Studio actually ships with a template for this. Even 2010 doesn't ship with a WPF project template like C# though so you should get the message that Microsoft wants you to use C# for GUI stuff.

Anyways, if it's a trivial app or program you are porting it's not that hard using WinForms. Actually, it's just a bit harder than using C# since you get to use the same GUI editor in Visual Studio but you have to write a lot more of the code by hand than using C#.

And you have to know Microsoft's C++/CLI since the template will autogenerate code in that and you need to understand what it does so you can ignore most of it.

Ivor Horton's Beginning Visual C++ 2010 and Visual C++ 2008 How to Program (2nd Edition) ~ Paul J. Deitel, Harvey M. Deitel are 2 of the only books I've seen that cover WinForm/C++/CLI programming so you might want to look at that or just go with C# as everyone recommends.

daveangel
@daveangel well could give links to those two books.
claws
http://www.wrox.com/WileyCDA/WroxTitle/productCd-0470500883.html
daveangel
Actually, if you read that first PDF sample of CH1 of the link I provided Horton does a good job of explaining the differences between C++/MFC/CLI and even has a good pic to go along with it :)
daveangel
@daveangel: excellent!! Why don't you edit your post to include the detailed link. Also please link the 2nd book you were talking about
claws
+1  A: 

This book also deserves particular mention since it covers advanced topics and using WPF with C++/CLI: C++/CLI in Action (Manning)

daveangel