Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C++/CLI

Windows Forms Using Managed C++

1.46/5 (7 votes)
18 Oct 2006CPOL1 min read 1   282  
How to use Windows Forms in a Win32 application using Managed C++

Introduction

Now that Microsoft has decided to drop the well known MFC/Win32 SDK in favour of .NET Windows Forms, many MFC/Win32 developers are attempting to move over to .NET but think they must use Managed C++ or C#.

I present here a very easy way to start with Windows Forms using Win32 applications!

Start Microsoft Visual Studio .NET and start a new Win32 application. Make the project empty. I called mine "Win32_Project". Once you have your empty Win32 application, add a new item. Choose UI -> Windows Form, and call it whatever you want; I call mine "AddedForm".

Now add a new .cpp file with a defined entry point; just for argument's sake, use the standard C (int main ()) declaration. Make sure to add the header of the Windows Form (in my case, "AddedForm.h").

Now add the following code, substituting my namespace and form name with yours:

C++
// Enabling Windows XP visual effects before any controls are created
Application::EnableVisualStyles();
Application::SetCompatibleTextRenderingDefault(false); 

// Create the main window and run it
Application::Run(gcnew Win32_Project::AddedForm());

Make sure you have set your build configuration to use the Common Language Runtime. Run your application! It should work fine!

You may have noticed that a DOS box pops up first and then your Form on top of the window; if not, run your application. This is due to the program's entry call. Change the program entry point to the standard Win32 entry point (int APIENTRY _tWinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPTSTR lpCmdLine,int nCmdShow)). Make sure to add windows.h and tchar.h to your main file.

Run the program again. No DOS box! There you have it, Windows Forms in Win32!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)