How do I create a C++/CLI Winforms app in VS2012?

2018-12-31 20:35发布

问题:

I just installed Visual Studio 2012 express for Desktop. I can\'t see any place to create a GUI application with C++ ! Where is this \"Windows Form Application\" used to exists in Visual C++ 2010 ? Where are these drag and drop controls? I installed this because I got details telling this supports GUI intellisense (Visual C++: Unable to invoke method from another class)

Please help!

回答1:

It is an unsubtle hint that they want you to stop creating C++/CLI Winforms applications. The plumbing is still in place however, at least for VS2012 and VS2013. This might not be the case in a future one.

You can turn a CLR console application into a Winforms application with these steps:

  • Start with File + New + Project, CLR node, CLR Console Application
  • Project + Add New Item, UI node, Windows Form
  • Project + Properties, Linker, System, SubSystem = Windows
  • Project + Properties, Linker, Advanced, Entry Point = main

Change the pre-generated .cpp file to look like this:

#include \"stdafx.h\"
#include \"MyForm.h\"

namespace ConsoleApplication45 {    // Change this!!
    using namespace System;
    using namespace System::Windows::Forms;

    [STAThread]
    int main(array<System::String ^> ^args)
    {
        Application::EnableVisualStyles();
        Application::SetCompatibleTextRenderingDefault(false); 
        Application::Run(gcnew MyForm());
        return 0;
    }
}

Note that you\'ll need to change the namespace name to your project name. Press F5 to test. You can design the form as normal once everything checks out.


NOTE, Visual Studio 2015 has a nasty static initialization order bug in the CRT that can cause the app to crash instantly with an AVE at startup if the project contains any native C++ code. As yet an unfixed bug, the somewhat inevitable hazard of having these project templates removed. A possible workaround is to change the Entry Point (4th bullet).

For a project that targets x86, copy paste this string:

?mainCRTStartupStrArray@@$$FYMHP$01AP$AAVString@System@@@Z

For a project that targets x64, copy paste:

?mainCRTStartupStrArray@@$$FYMHP$01EAPE$AAVString@System@@@Z