Managed C++ and Windows Forms

Introduction

One big complaint I have about Visual Studio .NET is that it does not support
a GUI designer tool for Managed C++. Its not going to be easy to design a
heavily-GUI oriented program in Managed C++. But then we can always try.
In this article I'll try and give an introduction to using Windows Forms in your
Managed C++ applications.

Lets jump straight into our first program. Use the App Wizard to generate a
basic Managed C++ application for you. I used the name sample01 for my project
and so my main file is sample01.cpp. In your case this filename will depend on
the name you chose for your project. Make the required changes to your cpp
source so that you have a file looking like the listing below.

Well, as you can see, I have made several changes to the App wizard generated
code. First I have added all those dll references and their corresponding
namespace references. I have added a new managed class called

MyForm

which is derived from System::Windows::Forms::Form.

I have replaced wmain with WinMain so that we won't have to see that ugly
console window behind our beautiful Form. I have prefixed it with __stdcall
otherwise you'll see a warning. That's how the Win 32 API has defined WinMain().
Then we have used the static function Runof the Application
class. This will start the standard application message loop.

Well, go ahead. Build and run it. You'll see your first Windows Form. Use Ctrl-F5
to run it. If you click on the play button the program will run in debug mode
which is considerably slower.

Well, now we have made several changes more. We have added a Label member to
our Form derived class and also a constructor. In the constructor we set some
properties and fields of our form. We give it a title. We fix its border and
make it non-sizable. We disable the maximize box. And also set its size.

We then create our Label object and set its text, size and location. Then we
add the label to the form by adding it to the form's controls collection. Build
and run it. You should see a more lively window now that, it has a title and some
text on it.

Well, now we have added a button, a text box and an event handler for the
button. We have written our btn_Click member function using the expected
parameters of the delegate that is associated with the click event. We use
add_Click to add our event handler to the list of functions that get notified of
the event. And as you can see, in the handler we have written code to put the
current date and time into the text box.

Build and run the program. You'll see that each time you click the button,
the text box gets updated with the current date and time. Easy, huh?

Conclusion

You can build more complex programs but the basic idea is the same. Event
handling is done via virtual functions which you can override. But I still have
a faint hope that when Microsoft finally releases the next version of VS.NET, they'll have a CodeDOM
like gadget for Managed C++.

Thank You

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

Share

About the Author

Nish Nishant is a Principal Software Architect based out of Columbus, Ohio. He has over 17 years of software industry experience in various roles including Lead Software Architect, Principal Software Engineer, and Product Manager. Nish was a Microsoft Visual C++ MVP between 2002 and 2015.

Nish is an industry acknowledged expert in the Microsoft technology stack. He authored C++/CLI in Action for Manning Publications in 2005, and had previously co-authored Extending MFC Applications with the .NET Framework for Addison Wesley in 2003. In addition, he has over 140 published technology articles on CodeProject.com and another 250+ blog articles on his WordPress blog. Nish is vastly experienced in team management, mentoring teams, and directing all stages of software development.

Contact Nish : If you are interested in hiring Nish as a consultant, you can reach him via his google email id voidnish.

"Well, the guy that's giving you a hard time is a f***in moron, and you can tell him thats straight from another Linux user." - John Simmons on Linux UsersJust do the American thing and shoot him... - Jim Crafton on Linux Users

You only need to distribute your own programs and libraries. The required .NET DLLs need not be distributed.
The end-users can download and install the .NET redistributable set which is available from the MS site.

Nish

My miniputt high is now 29
I do not think I can improve on that
My temperament won't hold